Example #1
0
func DoTestChunkdb_WLwBlWLc() {
	const (
		testName  = "verifying"   // This is a special name allocated in the avatar DB for testing.
		testCoord = math.MaxInt32 // Too far away for anyone to reach
	)
	pl := &player{}
	begin := time.Now()
	uid, ok := pl.Load_WLwBlWLc(testName)
	DoTestCheck("DoTestChunkdb: pl.Load() success", ok)
	delta := time.Now().Sub(begin)
	if *verboseFlag > 0 {
		fmt.Printf("DoTestChunkdb: pl.Load() %d ms\n", delta/1e6)
		fmt.Printf("DoTestChunkdb: Player %#v\n", pl.String())
	}
	terr := pl.territory
	DoTestCheck("DoTestChunkdb: Initial empty territory list", terr == nil)
	chunk1 := chunkdb.CC{testCoord, 0, 0}
	chunk2 := chunkdb.CC{0, testCoord, 0}
	chunk3 := chunkdb.CC{0, 0, testCoord}
	chunkList := []chunkdb.CC{chunk1, chunk2, chunk3}
	ok = chunkdb.SaveAvatar_Bl(uid, chunkList)
	DoTestCheck("DoTestChunkdb: chunkdb.SaveAvatar() success", ok)

	// Load the player again, and verify that the chunk list is updated.
	_, ok = pl.Load_WLwBlWLc(testName)
	DoTestCheck("DoTestChunkdb: second pl.Load() success", ok)
	terr = pl.territory
	DoTestCheck("DoTestChunkdb: Territory list now exists", len(terr) == 3)
	// fmt.Printf("DoTestChunkdb: Returned terr list: %v\n", terr)
	// The returned list is in the opposite order. It is probably incorrect to assume that.
	// TODO: This test usually fail, and then succeed next time!
	DoTestCheck("DoTestChunkdb: Same territory list", terr[0].Equal(chunk3) && terr[1].Equal(chunk2) && terr[2].Equal(chunk1))

	// Clean up and clear the territory list
	ok = chunkdb.SaveAvatar_Bl(uid, nil)
	DoTestCheck("DoTestChunkdb: chunkdb.SaveAvatar(nil) success", ok)
	_, ok = pl.Load_WLwBlWLc(testName)
	DoTestCheck("DoTestChunkdb: third pl.Load() success", ok)
	DoTestCheck("DoTestChunkdb: Final empty territory list", pl.territory == nil)
}
func (up *user) TerritoryClaim_WLwWLc(arg []string) {
	const usage = "Usage: /territory claim [up/down]"
	if up.pl.adminLevel < 1 && len(up.pl.territory) >= up.pl.maxchunks {
		up.Printf_Bl("#FAIL !You are not allowed more chunks than %d", up.pl.maxchunks)
		return
	}
	if *allowTestUser && NameIsTestPlayer(up.pl.name) {
		up.Printf_Bl("#FAIL !Test players can't claim territory")
		return
	}
	if MonsterDifficulty(&up.pl.coord) > up.pl.level && up.pl.adminLevel == 0 {
		up.Printf_Bl("#FAIL !You are too low level for this area")
		return
	}
	if len(arg) > 1 {
		up.Printf_Bl(usage)
		return
	}
	cc := up.pl.coord.GetChunkCoord()
	if len(arg) > 0 {
		switch arg[0] {
		case "up":
			cc.Z++
		case "down":
			cc.Z--
		case "west":
			cc.X--
		case "east":
			cc.X++
		case "south":
			cc.Y--
		case "north":
			cc.Y++
		default:
			up.Printf_Bl(usage)
			return
		}
	}
	cp := ChunkFind_WLwWLc(cc)
	cp.Lock()
	if cp.owner != OWNER_NONE {
		cp.Unlock()
		up.Printf_Bl("#FAIL !Chunk %v is already allocated to ID %d", cc, cp.owner)
		return
	}

	// Make sure either it is the first chunk, or an adjacent chunk is already allocated, or the request will be denied.
	approved := len(up.pl.territory) == 0 || up.pl.adminLevel > 0
	adjacent := dBGetAdjacentChunks(&cc)
	for _, cp := range adjacent {
		if cp.owner == up.uid {
			approved = true
			break
		}
	}
	if !approved {
		up.Printf_Bl("#FAIL !You must allocate adjacent to another of your chunks")
		return
	}

	// All tests are approved, allocate the chunk
	ChunkFind_WLwWLc(chunkdb.CC{cc.X, cc.Y, cc.Z})
	cp.owner = up.uid
	cp.flag |= CHF_MODIFIED
	cp.Write()
	cp.Unlock()
	up.Printf_Bl("!Congratulations, you now own chunk %v", cc)
	if up.pl.territory == nil {
		up.pl.territory = []chunkdb.CC{cc}
	} else {
		for _, chunk := range up.pl.territory {
			if chunk.X == cc.X && chunk.Y == cc.Y && chunk.Z == cc.Z {
				log.Printf("Chunk %v allocated to user %d (%s), but was already in DB list\n", cc, up.uid, up.pl.name)
				return
			}
		}
		up.pl.territory = append(up.pl.territory, cc)
	}
	chunkdb.SaveAvatar_Bl(up.uid, up.pl.territory)
}