aorecomp

Anarchy Online classic in the browser

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


server/shop_ammunition_test.go (8079B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 )
     10 
     11 func TestShopAmmunitionProductionPackages(t *testing.T) {
     12 	for _, scenario := range []struct {
     13 		machine uint32
     14 		side    uint16
     15 		credits uint32
     16 	}{
     17 		{3221554335, 2, 765}, {3223717020, 1, 754},
     18 		{3222406306, 0, 754}, {3221226898, 1, 765},
     19 		{3222012404, 0, 754}, {3221422645, 0, 754},
     20 	} {
     21 		t.Run(fmt.Sprint(scenario.machine), func(t *testing.T) {
     22 			world, conn := newPurchaseWorld(t)
     23 			client, player := world.clients[1], world.characters[1]
     24 			machine := gamedata.ShopMachines[scenario.machine]
     25 			player.playfield, player.position = machine.Playfield, machine.Position
     26 			player.appearance = 0x115 | scenario.side<<9
     27 			player.inventory.Items[0] = ownedItem{ID: 1, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: 7}
     28 			if err := world.saveUser(player); err != nil {
     29 				t.Fatal(err)
     30 			}
     31 			if err := world.saveInventory(player, player.inventory); err != nil {
     32 				t.Fatal(err)
     33 			}
     34 			request := shopUseRequest(2)
     35 			binary.BigEndian.PutUint32(request[44:48], scenario.machine)
     36 
     37 			if err := world.handleShopUse(client, request); err != nil {
     38 				t.Fatal(err)
     39 			}
     40 			if client.shop == nil || len(client.shop.stock) < 4 || !slices.Equal(client.shop.stock[:4], []gamedata.ShopItem{
     41 				{Template: 21605, Quality: 1}, {Template: 21609, Quality: 1},
     42 				{Template: 21613, Quality: 1}, {Template: 21601, Quality: 1},
     43 			}) {
     44 				t.Fatal("ammunition catalog unavailable", client.shop)
     45 			}
     46 			for index := range uint32(4) {
     47 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, scenario.machine}, [2]uint32{111, index})[20:]); err != nil {
     48 					t.Fatal(err)
     49 				}
     50 			}
     51 			conn.packets = nil
     52 			accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
     53 			if err := world.handleTrade(client, accept); err != nil {
     54 				t.Fatal(err)
     55 			}
     56 			if err := world.handleTrade(client, accept); err != nil {
     57 				t.Fatal(err)
     58 			}
     59 			cold := newTestInventoryServer(t).characters[1].inventory
     60 
     61 			expected := []ownedItem{
     62 				{ID: 1, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: 7},
     63 				{ID: 2, Type: 51005, Template: 21605, Quality: 1, Slot: 65, Count: 100},
     64 				{ID: 3, Type: 51005, Template: 21609, Quality: 1, Slot: 66, Count: 100},
     65 				{ID: 4, Type: 51005, Template: 21613, Quality: 1, Slot: 67, Count: 100},
     66 				{ID: 5, Type: 51005, Template: 21601, Quality: 1, Slot: 68, Count: 100},
     67 			}
     68 			if cold.Credits != scenario.credits || player.inventory.Credits != scenario.credits || !slices.Equal(cold.Items, expected) || !slices.Equal(player.inventory.Items, expected) || world.nextItem != 5 {
     69 				t.Fatal("ammunition purchase lost package quantities, identity or price", cold)
     70 			}
     71 			if len(conn.packets) != 6 {
     72 				t.Fatal("repeated Accept duplicated delivery", len(conn.packets))
     73 			}
     74 			for index, template := range []uint32{21605, 21609, 21613, 21601} {
     75 				packet := conn.packets[index+1]
     76 				if binary.BigEndian.Uint32(packet[16:20]) != 0x052e2f0c || binary.BigEndian.Uint32(packet[29:33]) != template || binary.BigEndian.Uint32(packet[41:45]) != 100 {
     77 					t.Fatal("native grant lost its ammunition package", template)
     78 				}
     79 			}
     80 		})
     81 	}
     82 }
     83 
     84 func TestShopPurchasedAmmunitionReloadRejectsOtherTypes(t *testing.T) {
     85 	world, _ := newPurchaseWorld(t)
     86 	client, player := world.clients[1], world.characters[1]
     87 	player.inventory.Items[0] = ownedItem{ID: 1, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1}
     88 	if err := world.saveInventory(player, player.inventory); err != nil {
     89 		t.Fatal(err)
     90 	}
     91 	player.position = gamedata.ShopMachines[3221554335].Position
     92 	request := shopUseRequest(2)
     93 	binary.BigEndian.PutUint32(request[44:48], 3221554335)
     94 	if err := world.handleShopUse(client, request); err != nil {
     95 		t.Fatal(err)
     96 	}
     97 	for index := range uint32(4) {
     98 		if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221554335}, [2]uint32{111, index})[20:]); err != nil {
     99 			t.Fatal(err)
    100 		}
    101 	}
    102 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    103 		t.Fatal(err)
    104 	}
    105 	before := slices.Clone(player.inventory.Items)
    106 
    107 	for index, slot := range []uint32{65, 66, 67} {
    108 		if err := world.handleReload(client, explicitReloadRequest(uint32(index+3), slot, 1)); err != nil {
    109 			t.Fatal(err)
    110 		}
    111 		if !slices.Equal(player.inventory.Items, before) || !client.reloadUntil.IsZero() {
    112 			t.Fatal("wrong purchased ammo changed rounds or recharge", slot)
    113 		}
    114 	}
    115 	reload := explicitReloadRequest(6, 64, 1)
    116 	if err := world.handleReload(client, reload); err != nil {
    117 		t.Fatal(err)
    118 	}
    119 	if err := world.handleReload(client, reload); err != nil {
    120 		t.Fatal(err)
    121 	}
    122 	cold := newTestInventoryServer(t).characters[1].inventory
    123 
    124 	expected := []ownedItem{
    125 		{ID: 1, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1, Ammo: 20},
    126 		{ID: 2, Type: 51005, Template: 21605, Quality: 1, Slot: 64, Count: 80},
    127 		{ID: 3, Type: 51005, Template: 21609, Quality: 1, Slot: 65, Count: 100},
    128 		{ID: 4, Type: 51005, Template: 21613, Quality: 1, Slot: 66, Count: 100},
    129 		{ID: 5, Type: 51005, Template: 21601, Quality: 1, Slot: 67, Count: 100},
    130 	}
    131 	if cold.Credits != 765 || !slices.Equal(cold.Items, expected) || !slices.Equal(player.inventory.Items, expected) || world.nextItem != 5 {
    132 		t.Fatal("reload did not conserve purchased ammunition after restart", cold)
    133 	}
    134 }
    135 
    136 func TestShopAmmunitionPurchaseFailureRetry(t *testing.T) {
    137 	for _, failure := range []string{"abort", "ignore", "commit"} {
    138 		t.Run(failure, func(t *testing.T) {
    139 			world, conn := newPurchaseWorld(t)
    140 			client, player := world.clients[1], world.characters[1]
    141 			player.position = gamedata.ShopMachines[3221554335].Position
    142 			request := shopUseRequest(2)
    143 			binary.BigEndian.PutUint32(request[44:48], 3221554335)
    144 			if err := world.handleShopUse(client, request); err != nil {
    145 				t.Fatal(err)
    146 			}
    147 			add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221554335}, [2]uint32{111, 0})[20:]
    148 			for range 2 {
    149 				if err := world.handleTrade(client, add); err != nil {
    150 					t.Fatal(err)
    151 				}
    152 			}
    153 			trigger := "CREATE TRIGGER reject_ammo BEFORE INSERT ON items WHEN NEW.id=3 BEGIN SELECT RAISE(ABORT, 'ammo'); END"
    154 			if failure == "ignore" {
    155 				trigger = "CREATE TRIGGER reject_ammo BEFORE INSERT ON items WHEN NEW.id=3 BEGIN SELECT RAISE(IGNORE); END"
    156 			}
    157 			if failure == "commit" {
    158 				if _, err := world.db.Exec("CREATE TABLE broken_ammo (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED)"); err != nil {
    159 					t.Fatal(err)
    160 				}
    161 				trigger = "CREATE TRIGGER reject_ammo AFTER INSERT ON items WHEN NEW.id=3 BEGIN INSERT INTO broken_ammo VALUES(9999999); END"
    162 			}
    163 			if _, err := world.db.Exec(trigger); err != nil {
    164 				t.Fatal(err)
    165 			}
    166 			conn.packets = nil
    167 			accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
    168 
    169 			failed := world.handleTrade(client, accept)
    170 			rejected := newTestInventoryServer(t).characters[1].inventory
    171 			if failed == nil || rejected.Credits != 1000 || len(rejected.Items) != 1 || player.inventory.Credits != 1000 || len(player.inventory.Items) != 1 || world.nextItem != 1 || client.shop == nil || len(conn.packets) != 0 {
    172 				t.Fatal("failed ammunition purchase published a partial basket", failed, rejected)
    173 			}
    174 			if _, err := world.db.Exec("DROP TRIGGER reject_ammo"); err != nil {
    175 				t.Fatal(err)
    176 			}
    177 			if err := world.handleTrade(client, accept); err != nil {
    178 				t.Fatal(err)
    179 			}
    180 			if err := world.handleTrade(client, accept); err != nil {
    181 				t.Fatal(err)
    182 			}
    183 			cold := newTestInventoryServer(t).characters[1].inventory
    184 
    185 			if cold.Credits != 950 || len(cold.Items) != 3 || cold.Items[1] != (ownedItem{ID: 4, Type: 51005, Template: 21605, Quality: 1, Slot: 65, Count: 100}) || cold.Items[2] != (ownedItem{ID: 5, Type: 51005, Template: 21605, Quality: 1, Slot: 66, Count: 100}) || world.nextItem != 5 || !slices.Equal(cold.Items, player.inventory.Items) {
    186 				t.Fatal("ammunition retry lost or duplicated packages", cold)
    187 			}
    188 		})
    189 	}
    190 }