server/combat_interest_test.go (5152B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "testing" 7 "time" 8 ) 9 10 type combatInterestConn struct { 11 combatTestConn 12 input *bytes.Reader 13 onWrite func([]byte) 14 } 15 16 func (conn *combatInterestConn) Read(buffer []byte) (int, error) { 17 return conn.input.Read(buffer) 18 } 19 20 func (conn *combatInterestConn) Write(packet []byte) (int, error) { 21 previous := len(conn.packets) 22 count, err := conn.combatTestConn.Write(packet) 23 if conn.onWrite != nil { 24 for _, complete := range conn.packets[previous:] { 25 conn.onWrite(complete) 26 } 27 } 28 return count, err 29 } 30 31 func (conn *combatInterestConn) Close() error { 32 return nil 33 } 34 35 func TestCombatLateLoginReceivesTargetsAfterParticipantSpawns(t *testing.T) { 36 world, _ := newCombatWorld(t) 37 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 38 world.advanceCombat(world.characters[1].nextAttack) 39 world.characters[2] = &character{id: 2, name: "Observer", playfield: 950, health: 100} 40 conn := &combatInterestConn{input: bytes.NewReader(systemPacket(0x1b, []byte{0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}))} 41 42 if err := serveWorldClient(world, &worldClient{conn: conn}); err != nil { 43 t.Fatal(err) 44 } 45 46 seen := make(map[uint32]bool) 47 attacks := 0 48 for _, packet := range conn.packets { 49 switch binary.BigEndian.Uint32(packet[20:24]) { 50 case 0x271b3a6b: 51 seen[binary.BigEndian.Uint32(packet[28:32])] = true 52 case 0x28494070: 53 attacker := binary.BigEndian.Uint32(packet[28:32]) 54 target := binary.BigEndian.Uint32(packet[40:44]) 55 if !seen[attacker] || !seen[target] { 56 t.Fatalf("attack %d -> %d preceded a participant spawn", attacker, target) 57 } 58 attacks++ 59 } 60 } 61 if attacks != 2 { 62 t.Fatalf("ongoing attacks replayed: %d", attacks) 63 } 64 } 65 66 func TestCombatNPCInterestReplaysTargetsOnceAfterSpawning(t *testing.T) { 67 world, _ := newCombatWorld(t) 68 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 69 world.advanceCombat(world.characters[1].nextAttack) 70 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 71 conn := &combatTestConn{} 72 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: conn, ready: true, npcs: make(map[uint32]bool)} 73 now := time.Now() 74 75 world.advanceNPCs(now) 76 world.advanceNPCs(now.Add(time.Second)) 77 78 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[0][20:24]) != 0x271b3a6b { 79 t.Fatalf("expected one spawn before two attack targets: %x", conn.packets) 80 } 81 seen := make(map[uint32]uint32) 82 for _, packet := range conn.packets[1:] { 83 if binary.BigEndian.Uint32(packet[20:24]) != 0x28494070 { 84 t.Fatalf("missing attack target: %x", packet) 85 } 86 seen[binary.BigEndian.Uint32(packet[28:32])] = binary.BigEndian.Uint32(packet[40:44]) 87 } 88 if seen[1] != 1000000 || seen[1000000] != 1 { 89 t.Fatalf("replayed combat targets: %v", seen) 90 } 91 } 92 93 func TestCombatInterestWaitsForBothNPCParticipants(t *testing.T) { 94 world, conn := newCombatWorld(t) 95 world.clients[1].npcs = make(map[uint32]bool) 96 world.characters[1000000].fighting = 1000001 97 world.fighting[1000000] = world.characters[1000000] 98 world.characters[1000001] = &character{id: 1000001, npc: true, playfield: 950, position: [3]float32{1000, 0, 0}, health: 100} 99 now := time.Now() 100 101 world.advanceNPCs(now) 102 world.characters[1000001].position[0] = 2 103 world.indexNPC(world.characters[1000001]) 104 world.advanceNPCs(now.Add(time.Second)) 105 106 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x271b3a6b || 107 binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x271b3a6b || binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x28494070 { 108 t.Fatalf("attack preceded both NPC spawns: %x", conn.packets) 109 } 110 } 111 112 func TestCombatSessionReplacementCancelsBeforeInitialization(t *testing.T) { 113 world, _ := newCombatWorld(t) 114 world.clients[1].conn = &combatInterestConn{} 115 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 116 world.advanceCombat(world.characters[1].nextAttack) 117 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 118 observer := &combatTestConn{} 119 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, npcs: map[uint32]bool{1000000: true}} 120 activeAtInitialization := -1 121 conn := &combatInterestConn{ 122 input: bytes.NewReader(systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})), 123 onWrite: func(packet []byte) { 124 if binary.BigEndian.Uint32(packet[20:24]) == 0x5f52412e { 125 activeAtInitialization = len(world.fighting) 126 } 127 }, 128 } 129 130 if err := serveWorldClient(world, &worldClient{conn: conn}); err != nil { 131 t.Fatal(err) 132 } 133 134 if activeAtInitialization != 0 || world.characters[1].fighting != 0 || world.characters[1000000].fighting != 0 { 135 t.Fatal("replaced connection retained an active fight") 136 } 137 if len(observer.packets) < 2 || binary.BigEndian.Uint32(observer.packets[0][20:24]) != 0x4a41203e || 138 binary.BigEndian.Uint32(observer.packets[1][20:24]) != 0x4a41203e { 139 t.Fatalf("observer did not receive both stop messages: %x", observer.packets) 140 } 141 for _, packet := range conn.packets { 142 if binary.BigEndian.Uint32(packet[20:24]) == 0x28494070 { 143 t.Fatal("replacement restored a cancelled attack") 144 } 145 } 146 }