aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/team_resources_test.go (18472B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"net"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func teamResourceUpdates(packets [][]byte) [][]byte {
     12 	var resources [][]byte
     13 	for _, packet := range packets {
     14 		if binary.BigEndian.Uint32(packet[16:20]) == 0x28784248 {
     15 			resources = append(resources, packet)
     16 		}
     17 	}
     18 	return resources
     19 }
     20 
     21 func newResourceBackpackTeam(t *testing.T) (*server, []*combatTestConn) {
     22 	t.Helper()
     23 	world, connections := newTeamWorld(t)
     24 	player := world.characters[1]
     25 	player.level = 3
     26 	player.stats.Abilities = characterAbilitiesStats{12, 12, 12, 12, 12, 12}
     27 	player.stats.Body.Adventuring, player.stats.TradeRepair.ComputerLiteracy = 98, 848
     28 	if err := world.saveUser(player); err != nil {
     29 		t.Fatal(err)
     30 	}
     31 	if granted, err := world.grantItem(player, 200461); err != nil || !granted {
     32 		t.Fatal("resource backpack grant failed", err)
     33 	}
     34 	if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 19)[20:]); err != nil {
     35 		t.Fatal(err)
     36 	}
     37 	world.advanceEquipment(world.clients[1].equipment.readyAt)
     38 	formTestTeam(t, world, 1, 2)
     39 	return world, connections
     40 }
     41 
     42 func TestTeamResourcesInitializeTheOtherMemberAfterItsRoster(t *testing.T) {
     43 	world, connections := newTeamWorld(t)
     44 	world.characters[1].health, world.characters[1].nano = 20, 10
     45 	world.characters[2].health, world.characters[2].nano = 15, 7
     46 
     47 	formTestTeam(t, world, 1, 2)
     48 
     49 	for index, conn := range connections[:2] {
     50 		packets := teamResourceUpdates(conn.packets)
     51 		if len(packets) != 1 {
     52 			t.Fatalf("recipient %d got %d resource snapshots", index+1, len(packets))
     53 		}
     54 		packet := packets[0]
     55 		if len(packet) != 72 || binary.BigEndian.Uint32(packet[24:28]) != uint32(index+1) ||
     56 			binary.BigEndian.Uint32(packet[29:33]) != 50000 || binary.BigEndian.Uint32(packet[33:37]) != uint32(2-index) {
     57 			t.Fatalf("resource identity/envelope: %x", packet)
     58 		}
     59 		want := [2][8]uint32{{3, 29, 3, 29, 7, 35, 37, 15}, {3, 29, 3, 29, 10, 35, 37, 20}}
     60 		for field, value := range want[index] {
     61 			if got := binary.BigEndian.Uint32(packet[37+field*4:]); got != value {
     62 				t.Fatalf("recipient %d resource %d=%d, want %d", index+1, field, got, value)
     63 			}
     64 		}
     65 		if binary.BigEndian.Uint32(conn.packets[len(conn.packets)-1][16:20]) != 0x28784248 {
     66 			t.Fatal("resources must follow the native roster")
     67 		}
     68 	}
     69 	if len(connections[2].packets) != 0 {
     70 		t.Fatal("resource snapshot leaked to an unrelated player")
     71 	}
     72 }
     73 
     74 func TestTeamResourcesCombatUpdatesPreserveQueuedHP(t *testing.T) {
     75 	for _, incoming := range []bool{false, true} {
     76 		t.Run(map[bool]string{false: "attacker", true: "victim"}[incoming], func(t *testing.T) {
     77 			world, connections := newTeamWorld(t)
     78 			formTestTeam(t, world, 1, 2)
     79 			start := time.Unix(100, 0)
     80 			for _, client := range world.clients {
     81 				client.regenerationAt = start
     82 			}
     83 			for _, conn := range connections {
     84 				conn.packets = nil
     85 			}
     86 			world.characters[1].health, world.characters[1].nano = 11, 6
     87 			world.characters[1].stats.Body.BodyDevelopment = 100
     88 			world.characters[1].stats.Body.NanoPool = 100
     89 			if incoming {
     90 				world.characters[3].fighting = 1
     91 				world.fighting[3] = world.characters[3]
     92 			} else {
     93 				world.characters[1].fighting = 3
     94 				world.fighting[1] = world.characters[1]
     95 			}
     96 
     97 			world.advanceRegeneration(start.Add(2 * time.Second))
     98 
     99 			want := map[uint32]uint32{343: 3, 342: 59, 364: 3, 363: 59, 214: 6, 221: 320, 1: 322}
    100 			if incoming {
    101 				want[342], want[363] = 29, 29
    102 			}
    103 			found := make(map[uint32]uint32)
    104 			for _, packet := range connections[1].packets {
    105 				if binary.BigEndian.Uint32(packet[24:28]) != 1 {
    106 					continue
    107 				}
    108 				if binary.BigEndian.Uint32(packet[16:20]) != 0x2b333d6e {
    109 					t.Fatalf("absolute snapshot interrupted queued damage: %x", packet)
    110 				}
    111 				stat, value := binary.BigEndian.Uint32(packet[29:33]), binary.BigEndian.Uint32(packet[33:37])
    112 				if value != want[stat] || stat == 27 {
    113 					t.Fatalf("combat resource %d=%d, expected %v", stat, value, want)
    114 				}
    115 				found[stat]++
    116 			}
    117 			if len(found) != 7 {
    118 				t.Fatalf("missing combat resources: %v", found)
    119 			}
    120 			for _, count := range found {
    121 				if count != 1 {
    122 					t.Fatal("duplicated combat resource")
    123 				}
    124 			}
    125 			for _, packet := range connections[2].packets {
    126 				if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 {
    127 					t.Fatal("private resource fields reached an unrelated observer")
    128 				}
    129 			}
    130 		})
    131 	}
    132 }
    133 
    134 func TestTeamResourcesReadyInitializesAnAlreadyFightingPeer(t *testing.T) {
    135 	world, connections := newTeamWorld(t)
    136 	formTestTeam(t, world, 1, 2)
    137 	world.characters[1].health = 11
    138 	world.characters[1].fighting = 3
    139 	world.fighting[1] = world.characters[1]
    140 	world.clients[2].loading = true
    141 	connections[1].packets = nil
    142 
    143 	err := world.handleCharacterReady(world.clients[2])
    144 
    145 	packets := teamResourceUpdates(connections[1].packets)
    146 	if err != nil || len(packets) != 1 || binary.BigEndian.Uint32(packets[0][65:69]) != 11 || world.clients[2].loading {
    147 		t.Fatalf("ready missed the peer's initial HP before combat delivery: %v %x", err, packets)
    148 	}
    149 }
    150 
    151 func TestTeamResourcesReadyRefreshesTheReturningMemberForItsPeersOnce(t *testing.T) {
    152 	world, connections := newTeamWorld(t)
    153 	formTestTeam(t, world, 1, 2)
    154 	world.clients[1].loading = true
    155 	world.characters[1].nano = 7
    156 	for _, conn := range connections {
    157 		conn.packets = nil
    158 	}
    159 
    160 	first := world.handleCharacterReady(world.clients[1])
    161 	second := world.handleCharacterReady(world.clients[1])
    162 
    163 	packets := teamResourceUpdates(connections[1].packets)
    164 	if first != nil || second != nil || len(packets) != 1 || binary.BigEndian.Uint32(packets[0][33:37]) != 1 ||
    165 		binary.BigEndian.Uint32(packets[0][53:57]) != 7 {
    166 		t.Fatalf("returning member was not initialized exactly once: %v %v %x", first, second, packets)
    167 	}
    168 	if len(teamResourceUpdates(connections[2].packets)) != 0 {
    169 		t.Fatal("returning member's resources leaked outside its team")
    170 	}
    171 }
    172 
    173 func TestTeamResourcesUseEquippedMaximumsInAndOutOfCombat(t *testing.T) {
    174 	for _, scenario := range []string{"quiet", "combat before snapshot", "combat after snapshot"} {
    175 		t.Run(scenario, func(t *testing.T) {
    176 			world, connections := newTeamWorld(t)
    177 			formTestTeam(t, world, 1, 2)
    178 			player := world.characters[1]
    179 			player.stats.Body.Adventuring, player.stats.TradeRepair.ComputerLiteracy = 98, 848
    180 			if err := world.saveUser(player); err != nil {
    181 				t.Fatal(err)
    182 			}
    183 			if granted, err := world.grantItem(player, 200461); err != nil || !granted {
    184 				t.Fatal("resource backpack grant failed", err)
    185 			}
    186 			if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 19)[20:]); err != nil {
    187 				t.Fatal(err)
    188 			}
    189 			if world.clients[1].equipment == nil {
    190 				t.Fatal("resource backpack wear was rejected")
    191 			}
    192 			world.advanceEquipment(world.clients[1].equipment.readyAt)
    193 			start := time.Unix(100, 0)
    194 			for _, client := range world.clients {
    195 				client.regenerationAt = start
    196 			}
    197 			if scenario == "combat after snapshot" {
    198 				world.advanceRegeneration(start.Add(2 * time.Second))
    199 				start = start.Add(2 * time.Second)
    200 			}
    201 			if scenario != "quiet" {
    202 				player.fighting = 3
    203 				world.fighting[1] = player
    204 			}
    205 			connections[1].packets = nil
    206 
    207 			world.advanceRegeneration(start.Add(2 * time.Second))
    208 			restored := newTestInventoryServer(t).characters[1]
    209 
    210 			if restored.maximumHealth() != 127 || restored.maximumNano() != 125 {
    211 				t.Fatal("equipped maxima were not durable")
    212 			}
    213 			if scenario != "quiet" {
    214 				found := make(map[uint32]uint32)
    215 				for _, packet := range connections[1].packets {
    216 					if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 {
    217 						found[binary.BigEndian.Uint32(packet[29:33])] = binary.BigEndian.Uint32(packet[33:37])
    218 					}
    219 				}
    220 				wantHealth, wantNano := uint32(127), uint32(125)
    221 				if scenario == "combat before snapshot" {
    222 					wantHealth, wantNano = 37, 35
    223 				}
    224 				if found[1] != wantHealth || found[221] != wantNano {
    225 					t.Fatalf("combat correction used the wrong native modifier basis: %v", found)
    226 				}
    227 			} else {
    228 				packets := teamResourceUpdates(connections[1].packets)
    229 				if len(packets) != 1 || binary.BigEndian.Uint32(packets[0][57:61]) != 125 || binary.BigEndian.Uint32(packets[0][61:65]) != 127 {
    230 					t.Fatalf("quiet equipped maxima: %x", packets)
    231 				}
    232 			}
    233 		})
    234 	}
    235 }
    236 
    237 func TestTeamResourcesPublishDeathWithoutRegeneratingTheMember(t *testing.T) {
    238 	world, connections := newTeamWorld(t)
    239 	formTestTeam(t, world, 1, 2)
    240 	start := time.Unix(100, 0)
    241 	for _, client := range world.clients {
    242 		client.regenerationAt = start
    243 	}
    244 	for _, conn := range connections {
    245 		conn.packets = nil
    246 	}
    247 	world.characters[1].health, world.characters[1].nano = 0, 0
    248 
    249 	world.advanceRegeneration(start.Add(2 * time.Second))
    250 
    251 	packets := teamResourceUpdates(connections[1].packets)
    252 	if len(packets) != 1 || binary.BigEndian.Uint32(packets[0][65:69]) != 0 || binary.BigEndian.Uint32(packets[0][53:57]) != 0 ||
    253 		world.characters[1].health != 0 || world.characters[1].nano != 0 {
    254 		t.Fatalf("dead team member changed resources: %x", packets)
    255 	}
    256 }
    257 
    258 func TestTeamResourceMaximumsKeepTheirNativeBasisAfterLeaving(t *testing.T) {
    259 	world, connections := newResourceBackpackTeam(t)
    260 	player := world.characters[1]
    261 	if err := world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0)); err != nil {
    262 		t.Fatal(err)
    263 	}
    264 	for _, conn := range connections {
    265 		conn.packets = nil
    266 	}
    267 	stats := player.baseStats()
    268 	stats.Body.BodyDevelopment = 6
    269 
    270 	err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:])
    271 
    272 	if err != nil || player.stats.Body.BodyDevelopment != 6 || player.maximumHealth() != 142 {
    273 		t.Fatalf("training was rejected or changed the equipped total: %v %d", err, player.maximumHealth())
    274 	}
    275 	for index, want := range []uint32{52, 142, 52} {
    276 		var found []uint32
    277 		for _, packet := range connections[index].packets {
    278 			if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    279 				found = append(found, binary.BigEndian.Uint32(packet[33:37]))
    280 			}
    281 		}
    282 		if len(found) != 1 || found[0] != want {
    283 			t.Fatalf("observer %d raw maximum=%v, want %d for its native modifier basis", index+1, found, want)
    284 		}
    285 	}
    286 }
    287 
    288 func TestTeamResourceMaximumsTrainingUpdatesBothCurrentMembers(t *testing.T) {
    289 	world, connections := newResourceBackpackTeam(t)
    290 	for _, conn := range connections {
    291 		conn.packets = nil
    292 	}
    293 	stats := world.characters[1].baseStats()
    294 	stats.Body.BodyDevelopment, stats.Body.NanoPool = 6, 6
    295 
    296 	err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:])
    297 
    298 	if err != nil || world.characters[1].stats.Body.NanoPool != 6 {
    299 		t.Fatal("training failed", err)
    300 	}
    301 	for index, want := range [][2]uint32{{52, 46}, {142, 136}, {52, 0}} {
    302 		found := make(map[uint32]uint32)
    303 		for _, packet := range connections[index].packets {
    304 			if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 {
    305 				found[binary.BigEndian.Uint32(packet[29:33])] = binary.BigEndian.Uint32(packet[33:37])
    306 			}
    307 		}
    308 		if found[1] != want[0] || found[221] != want[1] {
    309 			t.Fatalf("observer %d maxima=%v, want %v", index+1, found, want)
    310 		}
    311 	}
    312 }
    313 
    314 func TestTeamResourceMaximumsFollowEquipmentAfterAnAbsorbedSnapshot(t *testing.T) {
    315 	world, connections := newResourceBackpackTeam(t)
    316 	player, client := world.characters[1], world.clients[1]
    317 	if granted, err := world.grantItem(player, 152039); err != nil || !granted {
    318 		t.Fatal("Pioneer grant failed", err)
    319 	}
    320 	for _, conn := range connections {
    321 		conn.packets = nil
    322 	}
    323 
    324 	remove := world.handleInventory(client, itemMovePacket(1, 19, 111)[20:])
    325 	world.advanceEquipment(client.equipment.readyAt)
    326 	wear := world.handleInventory(client, inventoryMovePacket(1, 64, 19)[20:])
    327 	world.advanceEquipment(client.equipment.readyAt)
    328 	restored := newTestInventoryServer(t).characters[1]
    329 
    330 	if remove != nil || wear != nil || player.maximumHealth() != 52 || restored.maximumHealth() != 52 ||
    331 		len(restored.inventory.Items) != 2 || restored.inventory.Items[0].ID != 1 || restored.inventory.Items[0].Slot != 65 ||
    332 		restored.inventory.Items[1].ID != 2 || restored.inventory.Items[1].Slot != 19 {
    333 		t.Fatalf("equipment or durable identity changed: %v %v health=%d/%d items=%+v", remove, wear, player.maximumHealth(), restored.maximumHealth(), restored.inventory.Items)
    334 	}
    335 	for index, want := range []uint32{52, 142, 52} {
    336 		var found []uint32
    337 		for _, packet := range connections[index].packets {
    338 			if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    339 				found = append(found, binary.BigEndian.Uint32(packet[33:37]))
    340 			}
    341 		}
    342 		if len(found) != 1 || found[0] != want {
    343 			t.Fatalf("observer %d equipment raw maximum=%v, want %d", index+1, found, want)
    344 		}
    345 	}
    346 }
    347 
    348 func TestTeamResourceMaximumBasisResetsWithTheActorOnReconnect(t *testing.T) {
    349 	world, connections := newResourceBackpackTeam(t)
    350 	if err := world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0)); err != nil {
    351 		t.Fatal(err)
    352 	}
    353 	world.clients[1].conn = &combatInterestConn{}
    354 	connections[1].packets = nil
    355 	stats := world.characters[1].baseStats()
    356 	stats.Body.BodyDevelopment = 6
    357 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
    358 	binary.BigEndian.PutUint16(cookie, 0xdfdf)
    359 	input := append(cookie, targetPacket(n3Packet(0x570c2039, 50000, 1, nil))...)
    360 	input = append(input, targetPacket(skillTrainingRequest(stats))...)
    361 	addressConn, peer := net.Pipe()
    362 	t.Cleanup(func() {
    363 		addressConn.Close()
    364 		peer.Close()
    365 	})
    366 	replacement := &combatInterestConn{combatTestConn: combatTestConn{Conn: addressConn}, input: bytes.NewReader(input)}
    367 
    368 	err := serveWorldClient(world, &worldClient{conn: replacement, loading: true})
    369 
    370 	var found []uint32
    371 	for _, packet := range connections[1].packets {
    372 		if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    373 			found = append(found, binary.BigEndian.Uint32(packet[33:37]))
    374 		}
    375 	}
    376 	if err != nil || len(found) != 1 || found[0] != 52 {
    377 		t.Fatalf("new actor retained the previous absorbed bonus: %v %v", err, found)
    378 	}
    379 }
    380 
    381 func TestTeamResourcesRefreshOnTheRegenerationCadence(t *testing.T) {
    382 	world, connections := newTeamWorld(t)
    383 	formTestTeam(t, world, 1, 2)
    384 	start := time.Unix(100, 0)
    385 	for _, client := range world.clients {
    386 		client.regenerationAt = start
    387 	}
    388 	for _, conn := range connections {
    389 		conn.packets = nil
    390 	}
    391 	world.characters[1].health, world.characters[1].nano = 11, 6
    392 
    393 	world.advanceRegeneration(start.Add(2*time.Second - time.Nanosecond))
    394 	before := len(teamResourceUpdates(connections[1].packets))
    395 	world.advanceRegeneration(start.Add(2 * time.Second))
    396 	world.advanceRegeneration(start.Add(2 * time.Second))
    397 
    398 	packets := teamResourceUpdates(connections[1].packets)
    399 	if before != 0 || len(packets) != 1 || binary.BigEndian.Uint32(packets[0][53:57]) != 6 || binary.BigEndian.Uint32(packets[0][65:69]) != 11 {
    400 		t.Fatalf("team resource cadence/current values: %x", packets)
    401 	}
    402 	if len(teamResourceUpdates(connections[0].packets)) != 1 || len(teamResourceUpdates(connections[2].packets)) != 0 {
    403 		t.Fatal("resources reached the wrong recipients")
    404 	}
    405 }
    406 
    407 func TestTeamResourcesSkipUnavailableOrUnrelatedRecipients(t *testing.T) {
    408 	for _, scenario := range []string{"different playfield", "loading recipient", "unready recipient", "offline recipient", "left team", "loading member", "unready member", "offline member"} {
    409 		t.Run(scenario, func(t *testing.T) {
    410 			world, connections := newTeamWorld(t)
    411 			formTestTeam(t, world, 1, 2)
    412 			start := time.Unix(100, 0)
    413 			for _, client := range world.clients {
    414 				client.regenerationAt = start
    415 			}
    416 			switch scenario {
    417 			case "different playfield":
    418 				world.characters[2].playfield = 705
    419 			case "loading recipient":
    420 				world.clients[2].loading = true
    421 			case "unready recipient":
    422 				world.clients[2].ready = false
    423 			case "offline recipient":
    424 				delete(world.clients, 2)
    425 			case "left team":
    426 				if err := world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0)); err != nil {
    427 					t.Fatal(err)
    428 				}
    429 			case "loading member":
    430 				world.clients[1].loading = true
    431 			case "unready member":
    432 				world.clients[1].ready = false
    433 			case "offline member":
    434 				delete(world.clients, 1)
    435 			}
    436 			for _, conn := range connections {
    437 				conn.packets = nil
    438 			}
    439 
    440 			world.advanceRegeneration(start.Add(2 * time.Second))
    441 
    442 			for _, conn := range connections {
    443 				if len(teamResourceUpdates(conn.packets)) != 0 {
    444 					t.Fatalf("%s published a team snapshot", scenario)
    445 				}
    446 			}
    447 		})
    448 	}
    449 }
    450 
    451 func TestTeamResourcesOutsidePlayerInterestDoNotRequireAnActor(t *testing.T) {
    452 	world, connections := newTeamWorld(t)
    453 	formTestTeam(t, world, 1, 2)
    454 	delete(world.clients[2].players, 1)
    455 	world.characters[1].health, world.characters[1].fighting = 20, 1000000
    456 	now := time.Now()
    457 	world.clients[1].regenerationAt = now.Add(-2 * time.Second)
    458 	connections[1].packets = nil
    459 
    460 	world.advanceRegeneration(now)
    461 
    462 	if len(teamResourceUpdates(connections[1].packets)) != 1 {
    463 		t.Fatal("hidden teammate did not receive its roster resource update", connections[1].packets)
    464 	}
    465 	for _, packet := range connections[1].packets {
    466 		if binary.BigEndian.Uint32(packet[24:28]) == 1 {
    467 			t.Fatal("hidden teammate received a world-actor update", packet)
    468 		}
    469 	}
    470 }
    471 
    472 func TestTeamResourceMaximumBasisResetsOnPlayerInterestReentry(t *testing.T) {
    473 	world, connections := newResourceBackpackTeam(t)
    474 	player, client := world.characters[1], world.clients[1]
    475 	now := time.Now()
    476 	player.position[0] = 400
    477 	world.advanceActions(now)
    478 	client.regenerationAt = now.Add(-2 * time.Second)
    479 	world.advanceRegeneration(now)
    480 	if world.clients[2].absorbedBonuses[1].health != 90 {
    481 		t.Fatal("hidden team snapshot did not establish its resource basis")
    482 	}
    483 	player.position[0] = 0
    484 	world.advanceActions(now)
    485 	connections[1].packets = nil
    486 	stats := player.baseStats()
    487 	stats.Body.BodyDevelopment = 6
    488 
    489 	if err := world.handleSkillTraining(client, skillTrainingRequest(stats)[20:]); err != nil {
    490 		t.Fatal(err)
    491 	}
    492 
    493 	var maximum uint32
    494 	for _, packet := range connections[1].packets {
    495 		if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    496 			maximum = binary.BigEndian.Uint32(packet[33:37])
    497 		}
    498 	}
    499 	if maximum != 52 {
    500 		t.Fatal("fresh actor retained the hidden team snapshot's modifier basis", maximum)
    501 	}
    502 }