server/player_trading_test.go (9232B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "reflect" 7 "slices" 8 "testing" 9 ) 10 11 func newPlayerTradeWorld(t *testing.T) (*server, [2]*combatTestConn) { 12 t.Helper() 13 world, first := newEquipmentWorld(t) 14 peer := &character{id: 2, name: "Other Owner", level: 1, profession: 1, appearance: 277, playfield: 950, position: [3]float32{2, 0, 0}, health: 100, stats: testCharacterStats()} 15 peer.rotation[3] = 1 16 world.characters[2] = peer 17 second := &combatTestConn{} 18 world.clients[2] = &worldClient{id: 2, conn: second, target: true, ready: true} 19 if err := world.saveUser(peer); err != nil { 20 t.Fatal(err) 21 } 22 if granted, err := world.grantItem(world.characters[1], 85366); err != nil || !granted { 23 t.Fatalf("first stack: %v %v", granted, err) 24 } 25 if granted, err := world.grantItem(peer, 25820); err != nil || !granted { 26 t.Fatalf("second stack: %v %v", granted, err) 27 } 28 for id, cash := range map[uint32]uint32{1: 20000, 2: 10000} { 29 inventory := world.characters[id].inventory 30 inventory.Credits = cash 31 if err := world.saveInventory(world.characters[id], inventory); err != nil { 32 t.Fatal(err) 33 } 34 } 35 first.packets, second.packets = nil, nil 36 return world, [2]*combatTestConn{first, second} 37 } 38 39 func playerTradeRequest(t *testing.T, world *server, player, action uint32, first, second [2]uint32) { 40 t.Helper() 41 if err := world.handleTrade(world.clients[player], shopTradePacket([2]uint32{50000, player}, action, first, second)[20:]); err != nil { 42 t.Fatal(err) 43 } 44 } 45 46 func TestPlayerTradeAdmissionAndNativeParticipants(t *testing.T) { 47 for _, scenario := range []string{"eligible", "self", "missing", "too far", "different playfield", "dead", "loading", "replaced", "equipment", "shop", "busy", "wrong type", "wrong tag"} { 48 t.Run(scenario, func(t *testing.T) { 49 world, conns := newPlayerTradeWorld(t) 50 first, second := [2]uint32{50000, 2}, [2]uint32{} 51 switch scenario { 52 case "self": 53 first[1] = 1 54 case "missing": 55 first[1] = 99 56 case "too far": 57 world.characters[2].position[0] = 5.01 58 case "different playfield": 59 world.characters[2].playfield = 705 60 case "dead": 61 world.characters[2].health = 0 62 case "loading": 63 world.clients[2].loading = true 64 case "replaced": 65 world.clients[2].ready = false 66 case "equipment": 67 world.clients[2].equipment = &equipmentMove{} 68 case "shop": 69 world.clients[2].shop = &shopTrade{} 70 case "busy": 71 world.clients[2].trade = &playerTrade{} 72 case "wrong type": 73 first[0] = 51017 74 case "wrong tag": 75 second[0] = 111 76 } 77 78 playerTradeRequest(t, world, 1, 0, first, second) 79 80 if scenario != "eligible" { 81 if world.clients[1].trade != nil || len(conns[0].packets) != 0 || len(conns[1].packets) != 0 { 82 t.Fatal("ineligible start opened or disturbed a trade") 83 } 84 return 85 } 86 if world.clients[1].trade == nil || world.clients[1].trade != world.clients[2].trade { 87 t.Fatal("participants did not share the same draft") 88 } 89 for _, conn := range conns { 90 if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][24:28]) != 1 || 91 binary.BigEndian.Uint32(conn.packets[0][33:37]) != 0 || binary.BigEndian.Uint32(conn.packets[0][41:45]) != 2 { 92 t.Fatal("start did not initialize the same native participant pair") 93 } 94 } 95 }) 96 } 97 } 98 99 func TestPlayerTradePeerPartialStackDeliveryAndRemovalPreserveQuantity(t *testing.T) { 100 world, conns := newPlayerTradeWorld(t) 101 inventory := world.characters[1].inventory 102 inventory.Items[1].Count = 7 103 if err := world.saveInventory(world.characters[1], inventory); err != nil { 104 t.Fatal(err) 105 } 106 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 107 conns[0].packets, conns[1].packets = nil, nil 108 before, err := readTestItemStore(world) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 65}) 114 staged, err := readTestItemStore(world) 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 if !bytes.Equal(before, staged) || len(conns[1].packets) != 2 { 120 t.Fatal("staging changed durable ownership or omitted the counted peer row") 121 } 122 seed, move := conns[1].packets[0], conns[1].packets[1] 123 if binary.BigEndian.Uint32(seed[16:20]) != 0x052e2f0c || binary.BigEndian.Uint32(seed[24:28]) != 1 || 124 binary.BigEndian.Uint32(seed[29:33]) != 85366 || binary.BigEndian.Uint32(seed[41:45]) != 7 || 125 binary.BigEndian.Uint32(move[24:28]) != 1 || binary.BigEndian.Uint32(move[33:37]) != 4 || binary.BigEndian.Uint32(move[49:53]) != 48 { 126 t.Fatal("peer delivery did not seed/move an intact seven-charge remote row") 127 } 128 conns[1].packets = nil 129 playerTradeRequest(t, world, 1, 5, [2]uint32{50000, 1}, [2]uint32{108, 0}) 130 if len(conns[1].packets) != 2 || binary.BigEndian.Uint32(conns[1].packets[0][24:28]) != 1 || 131 binary.BigEndian.Uint32(conns[1].packets[0][33:37]) != 5 || binary.BigEndian.Uint32(conns[1].packets[1][29:33]) != 112 || 132 binary.BigEndian.Uint32(conns[1].packets[1][37:41]) != 48 || len(world.clients[1].trade.items[0]) != 0 { 133 t.Fatal("peer removal did not clear its restored scratch row") 134 } 135 } 136 137 func TestPlayerTradeItemsCashAndReplayAreAtomic(t *testing.T) { 138 world, conns := newPlayerTradeWorld(t) 139 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 140 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 65}) 141 playerTradeRequest(t, world, 2, 4, [2]uint32{50000, 2}, [2]uint32{104, 64}) 142 playerTradeRequest(t, world, 1, 6, [2]uint32{0, 500}, [2]uint32{}) 143 playerTradeRequest(t, world, 2, 6, [2]uint32{0, 200}, [2]uint32{}) 144 conns[0].packets, conns[1].packets = nil, nil 145 146 playerTradeRequest(t, world, 1, 1, [2]uint32{}, [2]uint32{}) 147 if len(conns[0].packets) != 0 || len(conns[1].packets) != 1 || binary.BigEndian.Uint32(conns[1].packets[0][24:28]) != 2 || 148 world.characters[1].inventory.Credits != 20000 { 149 t.Fatal("first consent lit the wrong lamp or transferred value early") 150 } 151 playerTradeRequest(t, world, 2, 6, [2]uint32{0, 200}, [2]uint32{}) 152 playerTradeRequest(t, world, 2, 1, [2]uint32{}, [2]uint32{}) 153 playerTradeRequest(t, world, 2, 1, [2]uint32{}, [2]uint32{}) 154 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 65}) 155 restored := newTestInventoryServer(t) 156 157 first, second := restored.characters[1].inventory, restored.characters[2].inventory 158 if first.Credits != 19700 || second.Credits != 10300 || world.nextItem != 3 || 159 !reflect.DeepEqual(first, world.characters[1].inventory) || !reflect.DeepEqual(second, world.characters[2].inventory) || 160 !reflect.DeepEqual(first.Items, []ownedItem{ 161 {ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}, 162 {ID: 3, Type: 51005, Template: 25820, Quality: 1, Slot: 65, Count: 50}, 163 }) || !reflect.DeepEqual(second.Items, []ownedItem{{ID: 2, Type: 51005, Template: 85366, Quality: 1, Slot: 64, Count: 25}}) || 164 world.clients[1].trade != nil || world.clients[2].trade != nil { 165 t.Fatalf("wrong durable exchange: first=%+v second=%+v", first, second) 166 } 167 for _, conn := range conns { 168 var subjects []uint32 169 for _, packet := range conn.packets { 170 if binary.BigEndian.Uint32(packet[16:20]) == 0x36284f6e && binary.BigEndian.Uint32(packet[33:37]) == 3 { 171 subjects = append(subjects, binary.BigEndian.Uint32(packet[24:28])) 172 } 173 } 174 if !slices.Equal(subjects, []uint32{1, 2}) { 175 t.Fatal("native completion directions missing or replayed") 176 } 177 } 178 } 179 180 func TestPlayerTradeDatabaseFailureDoesNotPublishEitherSide(t *testing.T) { 181 for _, trigger := range []string{ 182 "CREATE TRIGGER fail_trade BEFORE UPDATE OF credits ON players WHEN NEW.id=2 BEGIN SELECT RAISE(ABORT, 'second balance'); END", 183 "CREATE TRIGGER fail_trade BEFORE INSERT ON item_locations WHEN NEW.item_id=3 BEGIN SELECT RAISE(ABORT, 'placement'); END", 184 } { 185 t.Run(trigger, func(t *testing.T) { 186 world, conns := newPlayerTradeWorld(t) 187 playerTradeRequest(t, world, 1, 0, [2]uint32{50000, 2}, [2]uint32{}) 188 playerTradeRequest(t, world, 1, 4, [2]uint32{50000, 1}, [2]uint32{104, 65}) 189 playerTradeRequest(t, world, 2, 4, [2]uint32{50000, 2}, [2]uint32{104, 64}) 190 playerTradeRequest(t, world, 1, 6, [2]uint32{0, 500}, [2]uint32{}) 191 playerTradeRequest(t, world, 1, 1, [2]uint32{}, [2]uint32{}) 192 before, err := readTestItemStore(world) 193 if err != nil { 194 t.Fatal(err) 195 } 196 if _, err := world.db.Exec(trigger); err != nil { 197 t.Fatal(err) 198 } 199 conns[0].packets, conns[1].packets = nil, nil 200 201 failed := world.handleTrade(world.clients[2], shopTradePacket([2]uint32{50000, 2}, 1, [2]uint32{}, [2]uint32{})[20:]) 202 after, err := readTestItemStore(world) 203 if err != nil { 204 t.Fatal(err) 205 } 206 207 if failed == nil || !bytes.Equal(before, after) || world.clients[1].trade == nil || world.clients[2].trade == nil || 208 world.characters[1].inventory.Credits != 20000 || world.characters[2].inventory.Credits != 10000 || 209 len(conns[0].packets) != 0 || len(conns[1].packets) != 0 { 210 t.Fatal("failed transaction published one side or sent a completion") 211 } 212 if _, err := world.db.Exec("DROP TRIGGER fail_trade"); err != nil { 213 t.Fatal(err) 214 } 215 playerTradeRequest(t, world, 2, 1, [2]uint32{}, [2]uint32{}) 216 if newTestInventoryServer(t).characters[2].inventory.Credits != 10500 { 217 t.Fatal("retry did not commit both sides") 218 } 219 }) 220 } 221 }