server/inventory_move.go (3169B)
1 package main 2 3 import ( 4 "database/sql" 5 "fmt" 6 "maps" 7 ) 8 9 type itemMoveWrite struct { 10 player, item uint32 11 source, destination itemPlacement 12 } 13 14 // An unworn item move changes placement only; the transaction owns no live state. 15 func moveStoredItem(db *sql.DB, player, item uint32, source, destination itemPlacement) error { 16 return commitDatabaseWrite(db, itemMoveWrite{player, item, source, destination}) 17 } 18 19 func (write itemMoveWrite) persist(tx *sql.Tx) error { 20 player, item, source, destination := write.player, write.item, write.source, write.destination 21 // A backpack remains the player's carried container throughout the transfer. 22 var savedID uint32 23 for _, placement := range []itemPlacement{source, destination} { 24 if placement.Backpack != 0 { 25 if err := tx.QueryRow(`SELECT item_id FROM item_locations 26 WHERE item_id=? AND player_id=? AND reclaim_at IS NULL`, placement.Backpack, player).Scan(&savedID); err != nil { 27 return fmt.Errorf("player %d no longer carries backpack %d: %w", player, placement.Backpack, err) 28 } 29 } 30 } 31 32 // Check the expected owner and source inside the same transaction as the move. 33 if err := tx.QueryRow(`DELETE FROM item_locations 34 WHERE item_id=? AND (player_id, bank_player_id, backpack_id, slot) 35 IS (NULLIF(?,0), NULLIF(?,0), NULLIF(?,0), ?) AND reclaim_at IS NULL 36 RETURNING item_id`, item, source.Player, source.Bank, source.Backpack, source.Slot).Scan(&savedID); err != nil { 37 return fmt.Errorf("remove item %d move source: %w", item, err) 38 } 39 if err := tx.QueryRow(`INSERT INTO item_locations (item_id, player_id, bank_player_id, backpack_id, slot) 40 VALUES (?, NULLIF(?,0), NULLIF(?,0), NULLIF(?,0), ?) RETURNING item_id`, 41 item, destination.Player, destination.Bank, destination.Backpack, destination.Slot).Scan(&savedID); err != nil { 42 return fmt.Errorf("save item %d move destination: %w", item, err) 43 } 44 if err := tx.QueryRow(`SELECT item_id FROM item_locations 45 WHERE item_id=? AND (player_id, bank_player_id, backpack_id, slot) 46 IS (NULLIF(?,0), NULLIF(?,0), NULLIF(?,0), ?) AND reclaim_at IS NULL`, 47 item, destination.Player, destination.Bank, destination.Backpack, destination.Slot).Scan(&savedID); err != nil { 48 return fmt.Errorf("item %d move was not saved: %w", item, err) 49 } 50 return nil 51 } 52 53 func (server *server) moveInventoryItem(player *character, inventory playerInventory, item uint32, source, destination itemPlacement) error { 54 if err := inventory.validate(); err != nil { 55 return err 56 } 57 if err := server.commitWrite(itemMoveWrite{player.id, item, source, destination}); err != nil { 58 return err 59 } 60 61 // Publish just the committed placement and the affected backpack references. 62 for _, placement := range []itemPlacement{source, destination} { 63 if placement.Backpack != 0 { 64 record := server.items[placement.Backpack] 65 record.Slots = maps.Clone(record.Slots) 66 record.Slots[fmt.Sprintf("slot_%d", placement.Slot+1)] = nil 67 if placement == destination { 68 record.Slots[fmt.Sprintf("slot_%d", placement.Slot+1)] = &item 69 } 70 server.items[placement.Backpack] = record 71 } 72 } 73 server.itemPlacements[item] = destination 74 player.inventory = inventory 75 return nil 76 }