aorecomp

Anarchy Online classic in the browser

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


server/portable_container_loot_test.go (4684B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"reflect"
      6 	"testing"
      7 	"time"
      8 
      9 	"ao/server/gamedata"
     10 )
     11 
     12 func newFilledLootContainerWorld(t *testing.T, template uint32) *server {
     13 	t.Helper()
     14 	world, _ := newLootWorld(t)
     15 	bag := ownedItem{ID: 17, Type: 51017, Template: template, Quality: *gamedata.ItemTemplates[template].Quality, Count: 1}
     16 	contained := ownedItem{ID: 18, Type: 51005, Template: 152030, Quality: 42, Count: 3}
     17 	world.characters[1000000].corpse.items = []ownedItem{bag}
     18 	store := itemStore{
     19 		LastID:  18,
     20 		Items:   map[uint32]itemRecord{17: recordItem(bag, []ownedItem{contained}), 18: recordItem(contained, nil)},
     21 		Players: map[uint32]characterItemReferences{1: world.characters[1].inventory.references()},
     22 		Corpses: map[uint32][]ownedItem{1000000: {bag}},
     23 	}
     24 	if err := world.writeItemStore(store); err != nil {
     25 		t.Fatal(err)
     26 	}
     27 	return world
     28 }
     29 
     30 func TestFilledLootContainerContentsSurviveCheckpoint(t *testing.T) {
     31 	world := newFilledLootContainerWorld(t, 99241)
     32 
     33 	err := world.checkpointWorld(time.Now())
     34 	restarted := newTestInventoryServer(t)
     35 	var bag, slot uint32
     36 	locationErr := restarted.db.QueryRow("SELECT backpack_id, slot FROM item_locations WHERE item_id=18").Scan(&bag, &slot)
     37 
     38 	if err != nil || locationErr != nil || bag != 17 || slot != 0 || len(restarted.items) != 2 || restarted.items[18].Count != 3 {
     39 		t.Fatalf("checkpoint detached corpse-container contents: bag %d, slot %d, %v, %v", bag, slot, err, locationErr)
     40 	}
     41 }
     42 
     43 func TestPortableContainerLootRetainsContentsAndCounter(t *testing.T) {
     44 	for _, container := range portableContainerCatalog {
     45 		t.Run(container.name, func(t *testing.T) {
     46 			world := newFilledLootContainerWorld(t, container.template)
     47 			client := world.clients[1]
     48 
     49 			if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     50 				t.Fatal(err)
     51 			}
     52 			err := world.handleInventory(client, lootTakeRequest())
     53 			restarted := newTestInventoryServer(t)
     54 
     55 			if err != nil || len(restarted.items) != 2 || restarted.nextItem != 18 || len(world.characters[1000000].corpse.items) != 0 {
     56 				t.Fatalf("container pickup lost identities or source ownership: %v", err)
     57 			}
     58 			inventory := restarted.characters[1].inventory
     59 			if !reflect.DeepEqual(inventory.Items, []ownedItem{{ID: 17, Type: 51017, Template: container.template, Quality: container.quality, Slot: 64, Count: 1}}) ||
     60 				!reflect.DeepEqual(inventory.Containers[17], []ownedItem{{ID: 18, Type: 51005, Template: 152030, Quality: 42, Slot: 0, Count: 3}}) {
     61 				t.Fatalf("container pickup lost contents: %+v", inventory)
     62 			}
     63 		})
     64 	}
     65 }
     66 
     67 func TestFilledLootContainerPickupFailurePreservesBothLocations(t *testing.T) {
     68 	world := newFilledLootContainerWorld(t, 143832)
     69 	client := world.clients[1]
     70 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     71 		t.Fatal(err)
     72 	}
     73 	before, err := readTestItemStore(world)
     74 	if err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	if _, err := world.db.Exec("CREATE TRIGGER reject_container_loot BEFORE INSERT ON item_locations WHEN NEW.item_id=17 BEGIN SELECT RAISE(ABORT, 'write failed'); END"); err != nil {
     78 		t.Fatal(err)
     79 	}
     80 
     81 	err = world.handleInventory(client, lootTakeRequest())
     82 	after, readErr := readTestItemStore(world)
     83 
     84 	if err == nil || readErr != nil || !bytes.Equal(before, after) || len(world.characters[1].inventory.Items) != 0 ||
     85 		len(world.characters[1000000].corpse.items) != 1 || world.items[17].Slots["slot_1"] == nil || *world.items[17].Slots["slot_1"] != 18 {
     86 		t.Fatal("failed pickup moved the bag or its contents", err, readErr)
     87 	}
     88 }
     89 
     90 func TestFilledLootContainerExpiryRemovesContents(t *testing.T) {
     91 	world := newFilledLootContainerWorld(t, 143832)
     92 	corpse := world.characters[1000000]
     93 	world.corpseExpiry = []*character{corpse}
     94 	expires := corpse.corpse.expiresAt
     95 
     96 	world.advanceCorpseExpiry(expires)
     97 	restarted := newTestInventoryServer(t)
     98 
     99 	if len(restarted.items) != 0 || restarted.nextItem != 18 {
    100 		t.Fatal("expired container left recoverable orphan contents or reused IDs")
    101 	}
    102 }
    103 
    104 func TestLootedContainerContentsSurviveOldCorpseExpiry(t *testing.T) {
    105 	world := newFilledLootContainerWorld(t, 143832)
    106 	corpse := world.characters[1000000]
    107 	world.corpseExpiry = []*character{corpse}
    108 	expires := corpse.corpse.expiresAt
    109 	world.handleLootUse(world.clients[1], lootUseRequest(1))
    110 	if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
    111 		t.Fatal(err)
    112 	}
    113 
    114 	world.advanceCorpseExpiry(expires)
    115 	err := world.checkpointWorld(expires)
    116 	restarted := newTestInventoryServer(t)
    117 
    118 	if err != nil || len(restarted.items) != 2 || len(restarted.characters[1].inventory.Containers[17]) != 1 || restarted.items[18].Count != 3 {
    119 		t.Fatal("old corpse expiry removed a looted bag or its contents", err)
    120 	}
    121 }