server/dialogue_test.go (10249B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "encoding/hex" 6 "net" 7 "strings" 8 "testing" 9 "time" 10 ) 11 12 func newDialogueWorld(t *testing.T) *server { 13 t.Helper() 14 data := testNPCData() 15 data.Playfields[0].Districts[0].Groups[0].ID = "710/district/0/fixed/23" 16 data.Playfields[0].Districts[0].Groups[0].Name = "Ida Schuller" 17 useNPCData(t, data) 18 world := newTestServer(t) 19 world.newCharacter() 20 return world 21 } 22 23 func sendDialogueRequest(t *testing.T, client net.Conn, key uint32, payload string) { 24 t.Helper() 25 body, err := hex.DecodeString(payload) 26 if err != nil { 27 t.Fatal(err) 28 } 29 if _, err := client.Write(targetPacket(n3Packet(key, 50000, 1, body))); err != nil { 30 t.Fatal(err) 31 } 32 } 33 34 func readDialogueUpdate(t *testing.T, client net.Conn, key uint32) []byte { 35 t.Helper() 36 packet, err := readPacket(client) 37 if err != nil { 38 t.Fatal(err) 39 } 40 if len(packet) < 39 || binary.BigEndian.Uint32(packet[16:20]) != key || hex.EncodeToString(packet[29:39]) != "00020000c350000f4240" { 41 t.Fatalf("unexpected native dialogue packet: %x", packet) 42 } 43 return packet[39:] 44 } 45 46 func openIdaConversation(t *testing.T, client net.Conn) { 47 t.Helper() 48 // Native default action sends version 2, NPC identity and two false booleans. 49 sendDialogueRequest(t, client, 0x3b132d64, "00020000c350000f42400000000000000000") 50 if payload := readDialogueUpdate(t, client, 0x3b132d64); hex.EncodeToString(payload) != "0000000100000000" { 51 t.Fatalf("unexpected open flags: %x", payload) 52 } 53 if payload := readDialogueUpdate(t, client, 0x5d70532a); string(payload[8:]) != "Hi.\n" || binary.BigEndian.Uint32(payload[4:8]) != 4 { 54 t.Fatalf("unexpected Ida greeting: %x", payload) 55 } 56 if payload := readDialogueUpdate(t, client, 0x55704d31); binary.BigEndian.Uint32(payload) != 21 || string(payload[8:31]) != "Tell me about yourself." { 57 t.Fatalf("unexpected Ida topic menu: %x", payload) 58 } 59 } 60 61 func TestNPCConversationUsesNativeWorldMessages(t *testing.T) { 62 world := newDialogueWorld(t) 63 startTestService(t, world) 64 client := connectZoningClient(t, world, true) 65 openIdaConversation(t, client) 66 67 sendDialogueRequest(t, client, 0x2103247d, "00020000c350000f424000000000") 68 answer := readDialogueUpdate(t, client, 0x5d70532a) 69 choices := readDialogueUpdate(t, client, 0x55704d31) 70 sendDialogueRequest(t, client, 0x270a4c62, "00020000c350000f42400000000000000000") 71 // An answer after closing must be ignored. A ping acts as an ordered barrier. 72 sendDialogueRequest(t, client, 0x2103247d, "00020000c350000f424000000000") 73 ping := systemPacket(1, nil) 74 binary.BigEndian.PutUint16(ping[2:4], 11) 75 client.Write(ping) 76 response, err := readPacket(client) 77 78 if len(answer) != int(binary.BigEndian.Uint32(answer[4:8]))+8 || !strings.HasPrefix(string(answer[8:]), "\nI'm Ida Sculler. I'm a nano-technician.") { 79 t.Fatalf("recorded answer or native string length was lost: %x", answer) 80 } 81 if binary.BigEndian.Uint32(choices) != 21 || err != nil || binary.BigEndian.Uint16(response[2:4]) != 11 { 82 t.Fatalf("menu or conversation close failed: %x %x %v", choices, response, err) 83 } 84 } 85 86 func TestNPCConversationIgnoresRepeatedOpen(t *testing.T) { 87 world := newDialogueWorld(t) 88 startTestService(t, world) 89 client := connectZoningClient(t, world, true) 90 openIdaConversation(t, client) 91 92 sendDialogueRequest(t, client, 0x3b132d64, "00020000c350000f42400000000000000000") 93 sendDialogueRequest(t, client, 0x2103247d, "00020000c350000f424000000000") 94 answer := readDialogueUpdate(t, client, 0x5d70532a) 95 choices := readDialogueUpdate(t, client, 0x55704d31) 96 97 if !strings.HasPrefix(string(answer[8:]), "\nI'm Ida Sculler. I'm a nano-technician.") || binary.BigEndian.Uint32(choices) != 21 { 98 t.Fatalf("repeated open interrupted the conversation: %x %x", answer, choices) 99 } 100 } 101 102 func TestScriptedNPCAdvertisesNativeDialogue(t *testing.T) { 103 for _, spawn := range []struct { 104 name string 105 npc character 106 flagged bool 107 }{ 108 {"scripted NPC", character{npc: true, npcDefinitionID: "710/district/0/fixed/23"}, true}, 109 {"Arrival Hall representative", character{npc: true, npcDefinitionID: "4604/district/0/fixed/1"}, true}, 110 {"ordinary NPC", character{npc: true}, false}, 111 {"player", character{npcDefinitionID: "710/district/0/fixed/23"}, false}, 112 } { 113 t.Run(spawn.name, func(t *testing.T) { 114 spawn.npc.rotation[3] = 1 115 116 packet := targetPacket(characterPacket(&spawn.npc)) 117 118 if (binary.BigEndian.Uint32(packet[30:34])&0x08000000 != 0) != spawn.flagged { 119 t.Fatalf("native HasKnuBotData flag: %x", packet[30:34]) 120 } 121 }) 122 } 123 } 124 125 func TestNPCRecordedFarewellEndsConversation(t *testing.T) { 126 world := newDialogueWorld(t) 127 world.characters[1000000].npcDefinitionID = "600/district/7/fixed/8" 128 startTestService(t, world) 129 client := connectZoningClient(t, world, true) 130 sendDialogueRequest(t, client, 0x3b132d64, "00020000c350000f42400000000000000000") 131 readDialogueUpdate(t, client, 0x3b132d64) 132 readDialogueUpdate(t, client, 0x5d70532a) 133 readDialogueUpdate(t, client, 0x55704d31) 134 135 sendDialogueRequest(t, client, 0x2103247d, "00020000c350000f424000000006") 136 farewell := readDialogueUpdate(t, client, 0x5d70532a) 137 closed := readDialogueUpdate(t, client, 0x270a4c62) 138 139 if string(farewell[8:]) != "\nGoodbye.\n" || hex.EncodeToString(closed) != "0000000500000000" { 140 t.Fatalf("recorded farewell did not end the dialogue: %x %x", farewell, closed) 141 } 142 } 143 144 func TestNPCWithoutRecordedGreetingOpensWithChoices(t *testing.T) { 145 world := newDialogueWorld(t) 146 world.characters[1000000].npcDefinitionID = "550/district/2/fixed/9" 147 startTestService(t, world) 148 client := connectZoningClient(t, world, true) 149 150 sendDialogueRequest(t, client, 0x3b132d64, "00020000c350000f42400000000000000000") 151 readDialogueUpdate(t, client, 0x3b132d64) 152 choices := readDialogueUpdate(t, client, 0x55704d31) 153 154 if binary.BigEndian.Uint32(choices) != 5 { 155 t.Fatalf("Sun Tzu lost his topics or received invented greeting text: %x", choices) 156 } 157 } 158 159 func TestNPCConversationBelongsToItsWorldConnection(t *testing.T) { 160 world := newDialogueWorld(t) 161 world.newCharacter() 162 startTestService(t, world) 163 first := connectZoningClient(t, world, true) 164 second, connection := net.Pipe() 165 t.Cleanup(func() { second.Close() }) 166 second.SetDeadline(time.Now().Add(5 * time.Second)) 167 go func() { defer connection.Close(); world.serveWorldConnection(connection) }() 168 cookie := systemPacket(0x1b, []byte{0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}) 169 binary.BigEndian.PutUint16(cookie, 0xdfdf) 170 second.Write(cookie) 171 for { 172 packet, err := readPacket(second) 173 if err != nil { 174 t.Fatal(err) 175 } 176 if binary.BigEndian.Uint32(packet[16:20]) == 0x5f52412e { 177 break 178 } 179 } 180 readPacket(first) // The second player's spawn is broadcast to the first. 181 title, err := readPacket(first) 182 if err != nil || binary.BigEndian.Uint32(title[16:20]) != 0x734e5a7b { 183 t.Fatalf("observer did not receive the second player's title: %x %v", title, err) 184 } 185 second.Write(targetPacket(n3Packet(0x570c2039, 50000, 2, nil))) 186 readPacket(first) 187 for _, stat := range []uint32{16, 17, 18, 19, 20, 21, 138, 118, 119, 120, 149, 51, 425, 430, 78, 156} { 188 packet, err := readPacket(first) 189 if err != nil || binary.BigEndian.Uint32(packet[16:20]) != 0x2b333d6e || binary.BigEndian.Uint32(packet[24:28]) != 2 || binary.BigEndian.Uint32(packet[29:33]) != stat { 190 t.Fatalf("observer did not receive player combat foundations: %x %v", packet, err) 191 } 192 } 193 readPacket(second) 194 openIdaConversation(t, first) 195 196 // The forged player-1 header cannot borrow player 1's open conversation. 197 sendDialogueRequest(t, second, 0x2103247d, "00020000c350000f424000000000") 198 sendDialogueRequest(t, second, 0x270a4c62, "00020000c350000f42400000000000000000") 199 ping := systemPacket(1, nil) 200 binary.BigEndian.PutUint16(ping[2:4], 11) 201 second.Write(ping) 202 ignored, err := readPacket(second) 203 sendDialogueRequest(t, first, 0x2103247d, "00020000c350000f424000000000") 204 answer := readDialogueUpdate(t, first, 0x5d70532a) 205 readDialogueUpdate(t, first, 0x55704d31) 206 207 if err != nil || binary.BigEndian.Uint16(ignored[2:4]) != 11 || !strings.HasPrefix(string(answer[8:]), "\nI'm Ida Sculler.") { 208 t.Fatalf("another connection selected or closed the first conversation: %x %x %v", ignored, answer, err) 209 } 210 } 211 212 func TestNPCConversationRevalidatesAnswers(t *testing.T) { 213 for _, change := range []string{"distance", "playfield", "loading", "invalid choice"} { 214 t.Run(change, func(t *testing.T) { 215 world := newDialogueWorld(t) 216 startTestService(t, world) 217 client := connectZoningClient(t, world, true) 218 openIdaConversation(t, client) 219 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 220 switch change { 221 case "distance": 222 state.characters[1].position[0] += 11 223 case "playfield": 224 state.characters[1].playfield = 800 225 case "loading": 226 state.clients[1].loading = true 227 } 228 return nil 229 })) 230 index := "00000000" 231 if change == "invalid choice" { 232 index = "ffffffff" 233 } 234 235 sendDialogueRequest(t, client, 0x2103247d, "00020000c350000f4240"+index) 236 closed := readDialogueUpdate(t, client, 0x270a4c62) 237 238 if hex.EncodeToString(closed) != "0000000500000000" { 239 t.Fatalf("invalid conversation did not close: %x", closed) 240 } 241 }) 242 } 243 } 244 245 func TestNPCConversationIgnoresUnofferedAnswersAndRemoteStarts(t *testing.T) { 246 for _, request := range []struct { 247 name string 248 key uint32 249 payload string 250 }{ 251 {"answer before opening", 0x2103247d, "00020000c350000f424000000000"}, 252 {"unknown NPC", 0x3b132d64, "00020000c350000f42410000000000000000"}, 253 {"distant NPC", 0x3b132d64, "00020000c350000f42400000000000000000"}, 254 } { 255 t.Run(request.name, func(t *testing.T) { 256 world := newDialogueWorld(t) 257 startTestService(t, world) 258 client := connectZoningClient(t, world, true) 259 if request.name == "distant NPC" { 260 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 261 state.characters[1].position[0] += 100 262 return nil 263 })) 264 } 265 266 sendDialogueRequest(t, client, request.key, request.payload) 267 ping := systemPacket(1, nil) 268 binary.BigEndian.PutUint16(ping[2:4], 11) 269 client.Write(ping) 270 response, err := readPacket(client) 271 272 if err != nil || binary.BigEndian.Uint16(response[2:4]) != 11 { 273 t.Fatalf("unavailable dialogue request produced a response: %x %v", response, err) 274 } 275 }) 276 } 277 }