server/remote_combat_stats_test.go (5550B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 func TestRemotePlayerSpawnCarriesUnmodifiedCombatFoundations(t *testing.T) { 13 for _, scenario := range []string{"joining loaded player", "joining loading player", "new player", "replacing player"} { 14 t.Run(scenario, func(t *testing.T) { 15 world, _ := newAggDefEquipmentWorld(t, 124210) 16 player := world.characters[1] 17 world.handleInventory(world.clients[1], inventoryMovePacket(1, 65, 6)[20:]) 18 world.advanceEquipment(world.clients[1].equipment.readyAt) 19 player.stats.Abilities = characterAbilitiesStats{7, 11, 13, 17, 19, 23} 20 player.stats.Speed.MeleeInitiative, player.stats.Speed.RangedInitiative = 123, 0 21 player.stats.Speed.PhysicalInitiative, player.stats.Speed.NanoInitiative = 567, 1490 22 player.aggDef = -100 23 player.activeNanos = []activeNano{{program: 27169, caster: 1, expiresAt: time.Now().Add(time.Minute)}} 24 world.characters[2] = &character{id: 2, name: "Observer", playfield: 950, appearance: 277, level: 1, health: 100} 25 keyOffset, valueOffset := 20, 36 26 input := systemPacket(0x1b, []byte{0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}) 27 incoming := &combatInterestConn{} 28 observer := incoming 29 if scenario == "joining loading player" { 30 world.clients[1].loading = true 31 } else if scenario == "new player" || scenario == "replacing player" { 32 observer = &combatInterestConn{} 33 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, target: true} 34 if scenario == "new player" { 35 delete(world.clients, 1) 36 } else { 37 world.clients[1].conn = &combatInterestConn{} 38 } 39 keyOffset, valueOffset = 16, 29 40 input = systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}) 41 input = append(input, n3Packet(0x570c2039, 50000, 1, nil)...) 42 } 43 incoming.input = bytes.NewReader(input) 44 45 shutdown := startTestService(t, world) 46 err := world.serveWorldClient(&worldClient{conn: incoming, loading: true}) 47 if scenario == "joining loading player" { 48 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 49 for _, packet := range observer.packets { 50 if binary.BigEndian.Uint32(packet[20:]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[28:]) == 1 { 51 return fmt.Errorf("loading actor received foundations before its ready notification") 52 } 53 } 54 state.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true} 55 return state.handleCharacterReady(state.clients[1]) 56 })) 57 } 58 if err := shutdown(); err != nil { 59 t.Fatal(err) 60 } 61 62 var values [][2]uint32 63 spawned, ready := false, false 64 for _, packet := range observer.packets { 65 if binary.BigEndian.Uint32(packet[keyOffset+8:]) != 1 { 66 continue 67 } 68 switch binary.BigEndian.Uint32(packet[keyOffset:]) { 69 case 0x271b3a6b: 70 spawned = true 71 case 0x570c2039: 72 ready = true 73 case 0x2b333d6e: 74 if !spawned || scenario != "joining loaded player" && !ready { 75 t.Fatal("foundation preceded its actor spawn or readiness") 76 } 77 values = append(values, [2]uint32{binary.BigEndian.Uint32(packet[valueOffset:]), binary.BigEndian.Uint32(packet[valueOffset+4:])}) 78 case 0x3b1d2268, 0x35505644, 0x5e477770: 79 if len(values) != 16 { 80 t.Fatal("equipment or nano replay preceded raw combat foundations", values) 81 } 82 case 0x29304349: 83 t.Fatal("observer received the owner's private FullCharacter") 84 } 85 } 86 87 expected := [][2]uint32{{16, 7}, {17, 11}, {18, 13}, {19, 17}, {20, 19}, {21, 23}, 88 {138, 5}, {118, 123}, {119, 0}, {120, 567}, {149, 1490}, {51, 0xffffff9c}, {425, 100}, {430, 0}, {78, 1200}, {156, 0}} 89 if err != nil || !spawned || !reflect.DeepEqual(values, expected) || player.equipmentBonus(51) != 5 || player.nanoBonus(16) != 12 { 90 t.Fatal("observer omitted, duplicated or derived a raw combat foundation", err, values) 91 } 92 }) 93 } 94 } 95 96 func TestRemoteAggDefChangesReachOnlyReadySamePlayfieldObservers(t *testing.T) { 97 for _, value := range []int32{-100, -1, 0, 1, 100, -101, 101} { 98 t.Run(fmt.Sprint(value), func(t *testing.T) { 99 world, owner := newCombatWorld(t) 100 connections := []*combatTestConn{{}, {}, {}, {}} 101 for index, conn := range connections { 102 id := uint32(index + 2) 103 world.characters[id] = &character{id: id, playfield: 950, health: 100} 104 world.clients[id] = &worldClient{id: id, conn: conn, target: true, ready: true, players: map[uint32]bool{1: true}} 105 } 106 world.clients[3].ready = false 107 world.characters[4].playfield = 705 108 world.clients[5].loading = true 109 request := n3Packet(0x76793f0d, 50000, 1, binary.BigEndian.AppendUint32(nil, uint32(value))) 110 111 err := world.handleCombat(world.clients[1], request[20:]) 112 113 valid := value >= -100 && value <= 100 114 for index, conn := range append([]*combatTestConn{owner}, connections...) { 115 expected := 0 116 if valid && (index == 0 || index == 1 || index == 4) { 117 expected = 1 118 } 119 if err != nil || len(conn.packets) != expected { 120 t.Fatal("slider replication reached the wrong session or zone", index, err, len(conn.packets), expected) 121 } 122 if expected == 1 && (binary.BigEndian.Uint32(conn.packets[0][16:]) != 0x2b333d6e || 123 binary.BigEndian.Uint32(conn.packets[0][24:]) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:]) != 51 || 124 int32(binary.BigEndian.Uint32(conn.packets[0][33:])) != value) { 125 t.Fatal("slider replication changed the actor, stat or signed base value") 126 } 127 } 128 }) 129 } 130 }