aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 5412f5092c78db35968830972e446aedcdbcb31b
parent 5042ea09cb6fbb6612feef09d2e894dcdfdb448d
Author: anon <anon@example.com>
Date:   Fri, 23 Oct 2026 15:16:42 +0000

Show named item slots and separate equipment in control editor

Diffstat:
MTODO.md | 13+++++++++++--
Mdocs/world-state.md | 22++++++++++++++++++++--
Mserver/character_editor.go | 4+++-
Mserver/control.go | 5++++-
Mserver/control_data_test.go | 7++++---
Aserver/control_inventory.go | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/control_inventory_test.go | 194+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/item_database.go | 4++--
Mserver/item_pages.go | 28+++++++++++++++++++++-------
Mserver/item_store.go | 22+++++++++++++---------
10 files changed, 338 insertions(+), 27 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -501,16 +501,25 @@ and unresolved indoor lifecycle are explicit in [flight](docs/vehicles.md#yalmah ## Complete coverage beyond first examples -- [ ] Show readable item tables below the control panel's character YAML editor. +- [x] Show readable item tables below the control panel's character YAML editor. Check: inventory and equipment rows show slot, item name and a link to the exact item instance; empty slots are clear, and the YAML remains editable. Include backpack contents with links to their container and item instances, so every assigned item can be found without looking up numeric IDs manually. -- [ ] Clarify inventory, equipment and backpack structure in the control panel. + Inventory, equipment, reclaim and backpack tables now show native slot order, + names, exact instance links and empty slots. Backpack pages show readable + contents, and contained-item pages link to their parent bag and owner. +- [x] Clarify inventory, equipment and backpack structure in the control panel. Check: the character YAML has top-level equipment separate from the ordinary inventory slots. Backpack instances expose their own contents on their item pages, with clear navigation from the owning character and the readable tables. Update validation and editor round-trip tests; no legacy YAML reader is needed. + Top-level equipment round-trips through the existing atomic placement save. + Positive, invalid-slot, duplicate, missing-item, incompatible-slot, ownership, + reclaim and SQL rollback checks pass, alongside the full Go suite and focused + race checks. Browser form/link testing and a connected WASM editor round-trip + preserved all 28 fixture items; all 16 native inventory entries matched SQLite + before and after reconnect. Evidence: `local/control-inventory-smoke/verification.json`. - [ ] Audit the complete 14.9.1 server-facing action set. Check: native requests, catalog action/effect types and player-facing systems have an evidence-based coverage record; each missing handler, silent no-op or provisional rule has diff --git a/docs/world-state.md b/docs/world-state.md @@ -49,11 +49,16 @@ world clock continues advancing across downtime. Reconnecting chat clients receive their latest 500 history entries, in chronological order. The character control page presents one YAML editing document for profile, -position, vitals and inventory. Appearance has separate race, fatness, breed, -sex, side and head fields, with comments explaining native codes. Every skill +position, vitals, inventory and top-level equipment. Appearance has separate +race, fatness, breed, sex, side and head fields, with comments explaining native codes. Every skill and empty slot is visible. Equipment uses names such as `clothes_left_arm` and `implants_left_arm`; ordinary inventory uses `inventory_slot_1` through `inventory_slot_30`. Backpack item pages use `slot_1` through `slot_21`. +Below the editor, inventory and equipment tables show every slot, including +empty positions, with item names and links to exact instances. Reclaim items +have their own table. Owned backpacks have contents tables, even in reclaim. +Backpack item pages retain their editable YAML and show the same readable +contents. Contained-item pages link to the parent backpack and owning character. Saving the character document commits all sections and item placements in one transaction. YAML is only an editing format; the server does not read or write mutable YAML files. There is no migration path for previous server databases. @@ -61,3 +66,16 @@ To reset the world, stop the server and delete `data/world.sqlite` and any matching `-wal`/`-shm` files before restarting. Server builds require CGO and a C compiler for the [SQLite driver](https://github.com/mattn/go-sqlite3). + +Editor checks cover separate inventory/equipment slot validation, missing and +duplicate instances, incompatible equipment, ownership, explicit unassignment, +reclaim contents and transaction rollback across all document sections. The +full Go suite and focused control/item/database/position/backpack race checks +pass. Browser form and link checks exercised a 28-instance inventory, including +a backpack containing twelve items; a separate full armor suit showed all seven +worn pieces. Saving a helmet to equipment appeared in the WASM client, and +restoring the document while connected reloaded it into ordinary inventory. +All 28 records and locations were unchanged after that round-trip, and all +sixteen native top-level entries matched SQLite before and after reconnect. +No page or adapter errors occurred. The checks and screenshots are retained in +`local/control-inventory-smoke/verification.json` and its sibling files. diff --git a/server/character_editor.go b/server/character_editor.go @@ -6,11 +6,13 @@ type characterEditor struct { playerPosition `yaml:",inline"` Vitals playerVitals `yaml:"vitals"` Inventory inventoryReferences `yaml:"inventory"` + Equipment map[string]*uint32 `yaml:"equipment"` } func (character *character) editor() characterEditor { record := character.record() - return characterEditor{record.Profile, record.Position.editor(), record.Vitals, character.inventory.references()} + items := character.inventory.references() + return characterEditor{record.Profile, record.Position.editor(), record.Vitals, items.inventoryReferences, items.Equipment} } func (data characterEditor) character() *character { diff --git a/server/control.go b/server/control.go @@ -65,6 +65,8 @@ func (server *server) userPage(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + var itemTables strings.Builder + server.inventoryTables_locked(&itemTables, player.inventory) server.mutex.Unlock() w.Header().Set("Content-Type", "text/html; charset=utf-8") @@ -74,6 +76,7 @@ func (server *server) userPage(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "<h2>Character data</h2><p>This YAML document edits the character in the world database. Slots contain item IDs or null. Saving reconnects an online character. ID is fixed; position is XYZ and rotation is XYZW.</p>") fmt.Fprintf(w, "<form method='post' action='control/users/%d'><p><label>Character:<br><textarea name='data' rows='40' cols='100' required>%s</textarea></label></p><button>Save character</button></form>", data.ID, html.EscapeString(string(contents))) fmt.Fprint(w, "<p>Inventory uses inventory_slot_1 through inventory_slot_30; equipment uses named weapons, clothes and implants positions. Slots contain item IDs or null. Clearing a slot unassigns its item. <a href='control/items'>Browse and create item instances</a>. Backpack slots are edited on the backpack's item page. improvement_points is the available IP balance; native IP spending is not implemented.</p>") + fmt.Fprint(w, itemTables.String()) fmt.Fprint(w, "</body></html>") } @@ -136,7 +139,7 @@ func (server *server) editUser(w http.ResponseWriter, r *http.Request) { err = fmt.Errorf("unknown resurrection terminal") } if err == nil { - updated.inventory, err = data.Inventory.inventory(server.items, server.nextItem) + updated.inventory, err = (characterItemReferences{data.Inventory, data.Equipment}).inventory(server.items, server.nextItem) } var store itemStore if err == nil { diff --git a/server/control_data_test.go b/server/control_data_test.go @@ -218,7 +218,7 @@ func TestControlEditorShowsAllSkillCategoriesAndDefaultFields(t *testing.T) { if err := decodeUsers(strings.NewReader(html.UnescapeString(editors[0][1])), &data); err != nil { t.Fatal(err) } - if data.Inventory.Credits != 0 || len(data.Inventory.Slots) != 30 || len(data.Inventory.Equipment) != 43 || data.Inventory.Slots["inventory_slot_1"] != nil || data.Inventory.Slots["inventory_slot_30"] != nil || !strings.Contains(editors[0][1], "return_door: null\n") { + if data.Inventory.Credits != 0 || len(data.Inventory.Slots) != 30 || len(data.Equipment) != 43 || data.Inventory.Slots["inventory_slot_1"] != nil || data.Inventory.Slots["inventory_slot_30"] != nil || !strings.Contains(editors[0][1], "return_door: null\n") { t.Fatal("empty inventory slots or optional fields are hidden") } } @@ -279,7 +279,7 @@ func TestControlCharacterDocumentSavesNativeFieldsAndNamedSlots(t *testing.T) { data.Inventory.Credits = 913 pistol, armor, implant := uint32(1), uint32(2), uint32(3) data.Inventory.Slots["inventory_slot_7"] = &pistol - data.Inventory.Equipment["clothes_chest"], data.Inventory.Equipment["implants_chest"] = &armor, &implant + data.Equipment["clothes_chest"], data.Equipment["implants_chest"] = &armor, &implant contents, err := yaml.Marshal(data) if err != nil { t.Fatal(err) @@ -315,10 +315,11 @@ func TestControlCharacterDocumentRollsBackAllSections(t *testing.T) { world := newTestServer(t) postControlForm(world, "/control/users", url.Values{"name": {"Explorer"}}) postControlForm(world, "/control/items", url.Values{"template": {"121567"}}) + postControlForm(world, "/control/items", url.Values{"template": {"85557"}}) if _, err := world.db.Exec("CREATE TRIGGER reject_editor BEFORE UPDATE ON world BEGIN SELECT RAISE(ABORT, 'write failed'); END"); err != nil { t.Fatal(err) } - contents := "name: Changed\nposition: [53, 14, 60]\ninventory:\n credits: 913\n slots: {inventory_slot_7: 1}\n" + contents := "name: Changed\nposition: [53, 14, 60]\ninventory:\n credits: 913\n slots: {inventory_slot_7: 1}\nequipment: {clothes_head: 2}\n" response := postControlForm(world, "/control/users/1", url.Values{"data": {contents}}) restarted := newServer() diff --git a/server/control_inventory.go b/server/control_inventory.go @@ -0,0 +1,66 @@ +package main + +import ( + "ao/server/gamedata" + "fmt" + "html" + "io" + "maps" + "slices" +) + +func (server *server) itemTableRow_locked(w io.Writer, slot string, id *uint32) { + fmt.Fprintf(w, "<tr><td>%s</td><td>", html.EscapeString(slot)) + if id == nil { + fmt.Fprint(w, "Empty") + } else { + name := "Unknown template" + if definition := gamedata.ItemTemplates[server.items[*id].Template]; definition != nil { + name = definition.Name + } + fmt.Fprintf(w, "<a href='control/items/%d'>%s (ID %d)</a>", *id, html.EscapeString(name), *id) + } + fmt.Fprint(w, "</td></tr>") +} + +func (server *server) backpackTable_locked(w io.Writer, record itemRecord) { + fmt.Fprint(w, "<table><tr><th>Slot</th><th>Item</th></tr>") + for number := 1; number <= len(record.Slots); number++ { + slot := fmt.Sprintf("slot_%d", number) + server.itemTableRow_locked(w, slot, record.Slots[slot]) + } + fmt.Fprint(w, "</table>") +} + +func (server *server) inventoryTables_locked(w io.Writer, inventory playerInventory) { + // Use native slot order so numbered positions do not sort lexically. + references := inventory.references() + fmt.Fprint(w, "<h2>Inventory</h2><table><tr><th>Slot</th><th>Item</th></tr>") + for number := 1; number <= 30; number++ { + slot := fmt.Sprintf("inventory_slot_%d", number) + server.itemTableRow_locked(w, slot, references.Slots[slot]) + } + fmt.Fprint(w, "</table><h2>Equipment</h2><table><tr><th>Slot</th><th>Item</th></tr>") + for _, number := range slices.Sorted(maps.Keys(equipmentSlots)) { + server.itemTableRow_locked(w, equipmentSlots[number], references.Equipment[equipmentSlots[number]]) + } + fmt.Fprint(w, "</table>") + + if len(inventory.Reclaim) != 0 { + fmt.Fprint(w, "<h2>Reclaim</h2><table><tr><th>Original slot</th><th>Item</th></tr>") + for _, entry := range inventory.Reclaim { + server.itemTableRow_locked(w, fmt.Sprintf("inventory_slot_%d", entry.Item.Slot-63), &entry.Item.ID) + } + fmt.Fprint(w, "</table>") + } + + // Include every owned backpack, including bags waiting in reclaim. + for _, item := range inventory.allItems() { + if len(server.items[item.ID].Slots) == 0 { + continue + } + fmt.Fprintf(w, "<h2>Backpack <a href='control/items/%d'>%s (ID %d)</a></h2>", item.ID, + html.EscapeString(gamedata.ItemTemplates[item.Template].Name), item.ID) + server.backpackTable_locked(w, server.items[item.ID]) + } +} diff --git a/server/control_inventory_test.go b/server/control_inventory_test.go @@ -0,0 +1,194 @@ +package main + +import ( + "html" + "net/http" + "net/http/httptest" + "net/url" + "regexp" + "strings" + "testing" + + "go.yaml.in/yaml/v3" +) + +func newControlInventoryWorld(t *testing.T) *server { + t.Helper() + useNPCData(t, testNPCData()) + world := newTestServer(t) + if response := postControlForm(world, "/control/users", url.Values{"name": {"Explorer"}}); response.Code != http.StatusSeeOther { + t.Fatal(response.Body.String()) + } + for _, template := range []string{"121567", "85557", "99228", "121567"} { + if response := postControlForm(world, "/control/items", url.Values{"template": {template}}); response.Code != http.StatusSeeOther { + t.Fatal(response.Body.String()) + } + } + for _, placement := range [][2]string{{"1", "0:64"}, {"2", "0:18"}, {"3", "0:65"}, {"4", "3:0"}} { + response := postControlForm(world, "/control/items/"+placement[0], url.Values{"action": {"assign"}, "owner": {"1"}, "destination": {placement[1]}}) + if response.Code != http.StatusSeeOther { + t.Fatal(response.Body.String()) + } + } + return world +} + +func TestControlCharacterDocumentSeparatesEquipment(t *testing.T) { + useNPCData(t, testNPCData()) + world := newTestServer(t) + postControlForm(world, "/control/users", url.Values{"name": {"Explorer"}}) + postControlForm(world, "/control/items", url.Values{"template": {"85557"}}) + + response := postControlForm(world, "/control/users/1", url.Values{"data": {"equipment: {clothes_head: 1}\n"}}) + restarted := newServer() + defer restarted.db.Close() + page := httptest.NewRecorder() + restarted.httpHandler().ServeHTTP(page, httptest.NewRequest("GET", "/control/users/1", nil)) + + if response.Code != http.StatusSeeOther { + t.Fatalf("top-level equipment save: %d %s", response.Code, response.Body.String()) + } + document := html.UnescapeString(regexp.MustCompile(`(?s)<textarea[^>]*>(.*?)</textarea>`).FindStringSubmatch(page.Body.String())[1]) + var data map[string]any + if err := yaml.Unmarshal([]byte(document), &data); err != nil { + t.Fatal(err) + } + if len(data["equipment"].(map[string]any)) != 43 || data["equipment"].(map[string]any)["clothes_head"] != 1 { + t.Fatal("equipment is not a complete top-level section") + } + if _, nested := data["inventory"].(map[string]any)["equipment"]; nested { + t.Fatal("equipment is nested under ordinary inventory") + } + if len(restarted.characters[1].inventory.Items) != 1 || restarted.characters[1].inventory.Items[0].Slot != 18 { + t.Fatal("the equipped instance did not survive restart") + } +} + +func TestControlInventoryTablesShowInstancesContainersAndEmptySlots(t *testing.T) { + world := newControlInventoryWorld(t) + world.baseURL = "/share/test/" + + page := httptest.NewRecorder() + world.httpHandler().ServeHTTP(page, httptest.NewRequest("GET", "/share/test/control/users/1", nil)) + + body := page.Body.String() + for _, expected := range []string{ + "<base href='/share/test/'>", "<textarea name='data'", "<h2>Inventory</h2>", "<h2>Equipment</h2>", + "<tr><td>inventory_slot_1</td><td><a href='control/items/1'>Solar-Powered Pistol (ID 1)</a></td></tr>", + "<tr><td>clothes_head</td><td><a href='control/items/2'>Worn Metaplast Armor Helmet (ID 2)</a></td></tr>", + "<h2>Backpack <a href='control/items/3'>Small Backpack (ID 3)</a></h2>", + "<tr><td>slot_1</td><td><a href='control/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>", + "<tr><td>slot_21</td><td>Empty</td></tr>", + } { + if !strings.Contains(body, expected) { + t.Errorf("missing %s", expected) + } + } + if strings.Count(body, "<td>Empty</td>") != 90 || strings.Index(body, "<td>inventory_slot_2</td>") > strings.Index(body, "<td>inventory_slot_10</td>") { + t.Fatal("slot tables omit empty slots or use lexical slot order") + } +} + +func TestControlBackpackPagesLinkContentsAndParentContainer(t *testing.T) { + world := newControlInventoryWorld(t) + + bag, child := httptest.NewRecorder(), httptest.NewRecorder() + world.httpHandler().ServeHTTP(bag, httptest.NewRequest("GET", "/control/items/3", nil)) + world.httpHandler().ServeHTTP(child, httptest.NewRequest("GET", "/control/items/4", nil)) + + if !strings.Contains(bag.Body.String(), "<h2>Contents</h2>") || + !strings.Contains(bag.Body.String(), "<tr><td>slot_1</td><td><a href='control/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>") || + !strings.Contains(bag.Body.String(), "<tr><td>slot_21</td><td>Empty</td></tr>") || + !strings.Contains(bag.Body.String(), "<a href='control/users/1'>Explorer (1)</a>") { + t.Fatal("backpack page lost its owner or readable contents") + } + if !strings.Contains(child.Body.String(), "<a href='control/items/3'>Backpack 3, slot_1</a>") { + t.Fatal("contained item has no link to its parent backpack") + } +} + +func TestControlEquipmentClearUnassignsWithoutLosingItems(t *testing.T) { + world := newControlInventoryWorld(t) + + response := postControlForm(world, "/control/users/1", url.Values{"data": {"equipment: {clothes_head: null}\n"}}) + restarted := newServer() + defer restarted.db.Close() + var placements int + err := restarted.db.QueryRow("SELECT count(*) FROM item_locations").Scan(&placements) + + if response.Code != http.StatusSeeOther || err != nil || placements != 3 || len(restarted.items) != 4 || len(restarted.characters[1].inventory.Items) != 2 { + t.Fatalf("clear changed unrelated items: %d, placements %d, %v", response.Code, placements, err) + } + page := httptest.NewRecorder() + restarted.httpHandler().ServeHTTP(page, httptest.NewRequest("GET", "/control/items/2", nil)) + if !strings.Contains(page.Body.String(), "<p>Unassigned.") || len(restarted.characters[1].inventory.Containers[3]) != 1 { + t.Fatal("cleared armor remained assigned or backpack contents changed") + } +} + +func TestControlEquipmentRejectsInvalidPlacementsAtomically(t *testing.T) { + for name, document := range map[string]string{ + "ordinary slot in equipment": "equipment: {inventory_slot_1: 2}\n", + "equipment slot in inventory": "inventory: {slots: {clothes_head: 2}}\n", + "duplicate instance": "inventory: {slots: {inventory_slot_3: 2}}\nequipment: {clothes_head: 2}\n", + "missing instance": "equipment: {clothes_head: 999}\n", + "incompatible slot": "equipment: {clothes_feet: 2}\n", + "contained instance": "equipment: {weapons_right_hand: 4}\n", + } { + t.Run(name, func(t *testing.T) { + world := newControlInventoryWorld(t) + + response := postControlForm(world, "/control/users/1", url.Values{"data": {"name: Changed\n" + document}}) + restarted := newServer() + defer restarted.db.Close() + + if response.Code != http.StatusBadRequest || world.characters[1].name != "Explorer" || restarted.characters[1].name != "Explorer" { + t.Fatalf("invalid placement changed profile: %d %s", response.Code, response.Body.String()) + } + for _, expected := range [][4]uint32{{1, 1, 0, 64}, {2, 1, 0, 18}, {3, 1, 0, 65}, {4, 0, 3, 0}} { + var player, bag, slot uint32 + err := restarted.db.QueryRow("SELECT coalesce(player_id, 0), coalesce(backpack_id, 0), slot FROM item_locations WHERE item_id=?", expected[0]).Scan(&player, &bag, &slot) + if err != nil || player != expected[1] || bag != expected[2] || slot != expected[3] { + t.Fatalf("invalid placement changed item %d: %d %d %d, %v", expected[0], player, bag, slot, err) + } + } + }) + } +} + +func TestControlEquipmentCannotTakeAnotherPlayersItem(t *testing.T) { + world := newControlInventoryWorld(t) + postControlForm(world, "/control/users", url.Values{"name": {"Other"}}) + postControlForm(world, "/control/items", url.Values{"template": {"85557"}}) + assigned := postControlForm(world, "/control/items/5", url.Values{"action": {"assign"}, "owner": {"2"}, "destination": {"0:18"}}) + if assigned.Code != http.StatusSeeOther { + t.Fatal(assigned.Body.String()) + } + + response := postControlForm(world, "/control/users/1", url.Values{"data": {"equipment: {clothes_head: 5}\n"}}) + var owner, slot uint32 + err := world.db.QueryRow("SELECT player_id, slot FROM item_locations WHERE item_id=5").Scan(&owner, &slot) + + if response.Code != http.StatusBadRequest || err != nil || owner != 2 || slot != 18 { + t.Fatalf("edit moved another player's armor: %d, owner %d, slot %d, %v", response.Code, owner, slot, err) + } +} + +func TestControlInventoryTablesIncludeReclaimAndItsBackpackContents(t *testing.T) { + world := newControlInventoryWorld(t) + document := "inventory:\n slots: {inventory_slot_2: null}\n reclaim: [{id: 3, original_slot: inventory_slot_2, available_at: 2026-10-23T00:00:00Z}]\n" + prepared := postControlForm(world, "/control/users/1", url.Values{"data": {document}}) + if prepared.Code != http.StatusSeeOther { + t.Fatal(prepared.Body.String()) + } + + page := httptest.NewRecorder() + world.httpHandler().ServeHTTP(page, httptest.NewRequest("GET", "/control/users/1", nil)) + + if !strings.Contains(page.Body.String(), "<h2>Reclaim</h2>") || + !strings.Contains(page.Body.String(), "<tr><td>inventory_slot_2</td><td><a href='control/items/3'>Small Backpack (ID 3)</a></td></tr>") || + !strings.Contains(page.Body.String(), "<h2>Backpack <a href='control/items/3'>Small Backpack (ID 3)</a></h2>") || + !strings.Contains(page.Body.String(), "<tr><td>slot_1</td><td><a href='control/items/4'>Solar-Powered Pistol (ID 4)</a></td></tr>") { + t.Fatal("reclaim hides an assigned item or its backpack contents") + } +} diff --git a/server/item_database.go b/server/item_database.go @@ -161,7 +161,7 @@ func (server *server) persistItems_locked(store itemStore) error { } func (server *server) readItems() (itemStore, error) { - store := itemStore{Items: make(map[uint32]itemRecord), Players: make(map[uint32]inventoryReferences), Corpses: make(map[uint32][]ownedItem)} + store := itemStore{Items: make(map[uint32]itemRecord), Players: make(map[uint32]characterItemReferences), Corpses: make(map[uint32][]ownedItem)} db, err := server.database() if err != nil { return store, err @@ -196,7 +196,7 @@ func (server *server) readItems() (itemStore, error) { } for rows.Next() { var id uint32 - references := inventoryReferences{Slots: emptyInventorySlots(), Equipment: emptyEquipmentSlots()} + references := characterItemReferences{inventoryReferences: inventoryReferences{Slots: emptyInventorySlots()}, Equipment: emptyEquipmentSlots()} if err := rows.Scan(&id, &references.Credits, &references.FirstAidLockedUntil); err != nil { rows.Close() return store, err diff --git a/server/item_pages.go b/server/item_pages.go @@ -15,8 +15,9 @@ import ( ) type itemLocation struct { - owner uint32 - label string + owner uint32 + container uint32 + label string } func itemPagination(w http.ResponseWriter, r *http.Request, total int) (int, int) { @@ -54,16 +55,16 @@ func (server *server) itemLocations_locked() map[uint32]itemLocation { if item.Slot >= 64 { label = fmt.Sprintf("inventory_slot_%d", item.Slot-63) } - locations[item.ID] = itemLocation{player.id, label} + locations[item.ID] = itemLocation{owner: player.id, label: label} } for _, entry := range player.inventory.Reclaim { - locations[entry.Item.ID] = itemLocation{player.id, "Reclaim"} + locations[entry.Item.ID] = itemLocation{owner: player.id, label: "Reclaim"} } } for id, record := range server.items { for slot, contained := range record.Slots { if contained != nil { - locations[*contained] = itemLocation{locations[id].owner, fmt.Sprintf("Backpack %d, %s", id, slot)} + locations[*contained] = itemLocation{locations[id].owner, id, fmt.Sprintf("Backpack %d, %s", id, slot)} } } } @@ -197,6 +198,10 @@ func (server *server) itemPage(w http.ResponseWriter, r *http.Request) { return } location := server.itemLocations_locked()[uint32(id)] + var contentsTable strings.Builder + if len(record.Slots) != 0 { + server.backpackTable_locked(&contentsTable, record) + } users := server.users_locked() ownerName := "" if location.owner != 0 { @@ -222,10 +227,16 @@ func (server *server) itemPage(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "<h2>%s</h2><p>%s</p>", html.EscapeString(definition.Name), html.EscapeString(definition.Description)) } if location.owner != 0 { - fmt.Fprintf(w, "<p>Owner: <a href='control/users/%d'>%s (%d)</a>. %s.</p>", location.owner, html.EscapeString(ownerName), location.owner, html.EscapeString(location.label)) + fmt.Fprintf(w, "<p>Owner: <a href='control/users/%d'>%s (%d)</a>. ", location.owner, html.EscapeString(ownerName), location.owner) + } else { + fmt.Fprint(w, "<p>Unassigned. ") + } + if location.container != 0 { + fmt.Fprintf(w, "<a href='control/items/%d'>%s</a>", location.container, html.EscapeString(location.label)) } else { - fmt.Fprintf(w, "<p>Unassigned. %s</p>", html.EscapeString(location.label)) + fmt.Fprint(w, html.EscapeString(location.label)) } + fmt.Fprint(w, "</p>") if location.label == "" { fmt.Fprintf(w, "<h2>Assign item</h2><form method='get' action='control/items/%d'><label>Player <select name='owner' required>", id) for _, user := range users { @@ -248,6 +259,9 @@ func (server *server) itemPage(w http.ResponseWriter, r *http.Request) { } } fmt.Fprintf(w, "<h2>Instance properties</h2><p>ID %d is fixed. Slots contain instance IDs or null; backpacks have 21 slots. Saving reconnects the owner.</p><form method='post' action='control/items/%d'><textarea name='data' rows='25' cols='80' required>%s</textarea><p><button>Save item</button></p></form>", id, id, html.EscapeString(string(contents))) + if contentsTable.Len() != 0 { + fmt.Fprint(w, "<h2>Contents</h2>", contentsTable.String()) + } if definition != nil { catalog, err := yaml.Marshal(definition) if err != nil { diff --git a/server/item_store.go b/server/item_store.go @@ -28,17 +28,21 @@ type reclaimReference struct { type inventoryReferences struct { Credits uint32 `yaml:"credits"` Slots map[string]*uint32 `yaml:"slots"` - Equipment map[string]*uint32 `yaml:"equipment"` Reclaim []reclaimReference `yaml:"reclaim"` FirstAidLockedUntil time.Time `yaml:"first_aid_locked_until"` TreatmentLocks map[uint32]time.Time `yaml:"treatment_locks"` LearnedNanos []uint32 `yaml:"learned_nanos"` } +type characterItemReferences struct { + inventoryReferences `yaml:",inline"` + Equipment map[string]*uint32 `yaml:"equipment"` +} + type itemStore struct { - LastID uint32 `yaml:"last_id"` - Items map[uint32]itemRecord `yaml:"items"` - Players map[uint32]inventoryReferences `yaml:"players"` + LastID uint32 `yaml:"last_id"` + Items map[uint32]itemRecord `yaml:"items"` + Players map[uint32]characterItemReferences `yaml:"players"` Corpses map[uint32][]ownedItem edited *character npcSaveAt time.Time @@ -53,9 +57,9 @@ func emptyItemSlots(count uint32) map[string]*uint32 { return slots } -func (inventory playerInventory) references() inventoryReferences { - result := inventoryReferences{Credits: inventory.Credits, Slots: emptyInventorySlots(), Equipment: emptyEquipmentSlots(), - FirstAidLockedUntil: inventory.FirstAidLockedUntil, TreatmentLocks: maps.Clone(inventory.TreatmentLocks), LearnedNanos: slices.Clone(inventory.LearnedNanos)} +func (inventory playerInventory) references() characterItemReferences { + result := characterItemReferences{inventoryReferences: inventoryReferences{Credits: inventory.Credits, Slots: emptyInventorySlots(), + FirstAidLockedUntil: inventory.FirstAidLockedUntil, TreatmentLocks: maps.Clone(inventory.TreatmentLocks), LearnedNanos: slices.Clone(inventory.LearnedNanos)}, Equipment: emptyEquipmentSlots()} for _, item := range inventory.Items { if item.Slot >= 64 { result.Slots[fmt.Sprintf("inventory_slot_%d", item.Slot-63)] = &item.ID @@ -102,7 +106,7 @@ func referencedItems(slots map[string]*uint32, first, count uint32, records map[ return items, nil } -func (references inventoryReferences) inventory(records map[uint32]itemRecord, lastID uint32) (playerInventory, error) { +func (references characterItemReferences) inventory(records map[uint32]itemRecord, lastID uint32) (playerInventory, error) { inventory := playerInventory{LastID: lastID, Credits: references.Credits, FirstAidLockedUntil: references.FirstAidLockedUntil, TreatmentLocks: references.TreatmentLocks, LearnedNanos: references.LearnedNanos} // Named character slots map directly to the native placement numbers. @@ -168,7 +172,7 @@ func reclaimItems(entries []reclaimItem) []ownedItem { } func (server *server) itemStore_locked(character *character, inventory playerInventory) (itemStore, error) { - store := itemStore{LastID: max(server.nextItem, inventory.LastID), Items: maps.Clone(server.items), Players: make(map[uint32]inventoryReferences), Corpses: make(map[uint32][]ownedItem)} + store := itemStore{LastID: max(server.nextItem, inventory.LastID), Items: maps.Clone(server.items), Players: make(map[uint32]characterItemReferences), Corpses: make(map[uint32][]ownedItem)} if store.Items == nil { store.Items = make(map[uint32]itemRecord) }