server/team_side_test.go (5241B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "testing" 7 ) 8 9 func assertTeamSide(t *testing.T, packets [][]byte, owner, want uint32) { 10 t.Helper() 11 var found int 12 for _, packet := range packets { 13 if binary.BigEndian.Uint32(packet[16:20]) != 0x2b333d6e || binary.BigEndian.Uint32(packet[29:33]) != 213 { 14 continue 15 } 16 found++ 17 if len(packet) != 40 || binary.BigEndian.Uint32(packet[20:24]) != 50000 || 18 binary.BigEndian.Uint32(packet[24:28]) != owner || binary.BigEndian.Uint32(packet[33:37]) != want { 19 t.Fatalf("team side owner=%d want=%d: %x", owner, want, packet) 20 } 21 } 22 if found != 1 { 23 t.Fatalf("owner %d received %d team-side updates, want one", owner, found) 24 } 25 } 26 27 func TestTeamSideFormationUsesCommonAffiliationOrNeutral(t *testing.T) { 28 for _, scenario := range []struct { 29 name string 30 sides []uint16 31 want uint32 32 }{ 33 {"neutral", []uint16{0, 0}, 0}, 34 {"clan", []uint16{1, 1}, 1}, 35 {"omni", []uint16{2, 2}, 2}, 36 {"clan neutral", []uint16{1, 0}, 0}, 37 {"neutral clan", []uint16{0, 1}, 0}, 38 {"omni neutral", []uint16{2, 0}, 0}, 39 {"neutral omni", []uint16{0, 2}, 0}, 40 {"clan omni", []uint16{1, 2}, 0}, 41 {"omni clan", []uint16{2, 1}, 0}, 42 {"clan majority", []uint16{1, 1, 2}, 0}, 43 {"all affiliations", []uint16{0, 1, 2}, 0}, 44 } { 45 t.Run(scenario.name, func(t *testing.T) { 46 world, connections := newTeamWorld(t) 47 for index, side := range scenario.sides { 48 world.characters[uint32(index+1)].appearance = 16 | side<<9 49 } 50 if len(scenario.sides) == 3 { 51 formTestTeam(t, world, 1, 2) 52 } 53 for _, conn := range connections { 54 conn.packets = nil 55 } 56 57 formTestTeam(t, world, 1, uint32(len(scenario.sides))) 58 59 for index := range scenario.sides { 60 assertTeamSide(t, connections[index].packets, uint32(index+1), scenario.want) 61 } 62 for _, conn := range connections[len(scenario.sides):] { 63 if len(conn.packets) != 0 { 64 t.Fatal("unrelated player received team metadata") 65 } 66 } 67 }) 68 } 69 } 70 71 func TestTeamSideDepartureReclassifiesRemainingMembersAndClearsLeaver(t *testing.T) { 72 for _, action := range []uint32{22, 24} { 73 t.Run(fmt.Sprint(action), func(t *testing.T) { 74 world, connections := newTeamWorld(t) 75 world.characters[1].appearance, world.characters[2].appearance = 16|1<<9, 16|1<<9 76 world.characters[3].appearance = 16 | 2<<9 77 formTestTeam(t, world, 1, 2) 78 formTestTeam(t, world, 1, 3) 79 for _, conn := range connections { 80 conn.packets = nil 81 } 82 client, target := world.clients[1], uint32(3) 83 if action == 24 { 84 client, target = world.clients[3], 0 85 } 86 87 err := world.handleTeamAction(client, teamRequest(client.id, action, target, 0)) 88 89 if err != nil { 90 t.Fatal(err) 91 } 92 assertTeamSide(t, connections[0].packets, 1, 1) 93 assertTeamSide(t, connections[1].packets, 2, 1) 94 assertTeamSide(t, connections[2].packets, 3, 0) 95 }) 96 } 97 } 98 99 func TestTeamSideProfileEditPublishesDurableAffiliationToCurrentMembers(t *testing.T) { 100 for _, offline := range []bool{false, true} { 101 t.Run(fmt.Sprint(offline), func(t *testing.T) { 102 world, connections := newTeamWorld(t) 103 world.characters[1].appearance, world.characters[2].appearance = 16|1<<9, 16|1<<9 104 formTestTeam(t, world, 1, 2) 105 for _, conn := range connections { 106 conn.packets = nil 107 } 108 if offline { 109 delete(world.clients, 1) 110 } 111 updated := world.characters[1].userData() 112 updated.Appearance = 16 | 2<<9 113 114 err := world.updateUser(updated) 115 restored := newTestInventoryServer(t) 116 conn := &combatTestConn{} 117 restored.clients[1] = &worldClient{id: 1, conn: conn, target: true, ready: true, loading: true} 118 ready := restored.handleCharacterReady(restored.clients[1]) 119 120 if err != nil || ready != nil || restored.characters[1].appearance != 16|2<<9 || world.characters[1].team != world.characters[2].team { 121 t.Fatalf("profile edit lost durable affiliation or membership: %v %v", err, ready) 122 } 123 assertTeamSide(t, connections[1].packets, 2, 0) 124 assertTeamSide(t, conn.packets, 1, 0) 125 if offline { 126 if len(connections[0].packets) != 0 { 127 t.Fatal("offline member received metadata") 128 } 129 } else { 130 assertTeamSide(t, connections[0].packets, 1, 0) 131 } 132 for _, packet := range connections[2].packets { 133 if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e { 134 t.Fatal("unrelated player received private team stats") 135 } 136 } 137 }) 138 } 139 } 140 141 func TestTeamSideFailedProfileEditPublishesNoAffiliation(t *testing.T) { 142 world, connections := newTeamWorld(t) 143 formTestTeam(t, world, 1, 2) 144 for _, conn := range connections { 145 conn.packets = nil 146 } 147 updated := world.characters[1].userData() 148 updated.Appearance = 16 | 2<<9 149 if _, err := world.db.Exec("CREATE TRIGGER fail_side BEFORE UPDATE ON characters WHEN OLD.id=1 BEGIN SELECT RAISE(FAIL, 'test profile failure'); END"); err != nil { 150 t.Fatal(err) 151 } 152 before := world.characters[1].appearance 153 154 err := world.updateUser(updated) 155 restored := newTestInventoryServer(t) 156 157 if err == nil || world.characters[1].appearance != before || restored.characters[1].appearance != before { 158 t.Fatal("failed profile transaction changed affiliation") 159 } 160 for _, conn := range connections { 161 if len(conn.packets) != 0 { 162 t.Fatal("failed profile transaction published affiliation") 163 } 164 } 165 }