aorecomp

Anarchy Online classic in the browser

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


server/shop_availability_test.go (2564B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"maps"
      9 	"reflect"
     10 	"slices"
     11 	"testing"
     12 )
     13 
     14 func TestEarlyShopCatalogAvailabilityFeedback(t *testing.T) {
     15 	world, conn := newEquipmentWorld(t)
     16 	player, client := world.characters[1], world.clients[1]
     17 	before := player.inventory
     18 	visited, empty := 0, 0
     19 	for _, id := range slices.Sorted(maps.Keys(gamedata.ShopMachines)) {
     20 		machine := gamedata.ShopMachines[id]
     21 		appearance, selected := map[uint32]uint16{950: 0x515, 952: 0x315, 954: 0x115, 1180: 0x315, 1183: 0x515, 1186: 0x115}[machine.Playfield]
     22 		if !selected {
     23 			continue
     24 		}
     25 		visited++
     26 		t.Run(fmt.Sprintf("%x", id), func(t *testing.T) {
     27 			player.playfield, player.position, player.appearance = machine.Playfield, machine.Position, appearance
     28 			request := shopUseRequest(uint32(visited))
     29 			binary.BigEndian.PutUint32(request[44:48], id)
     30 			conn.packets = nil
     31 
     32 			if err := world.handleShopUse(client, request); err != nil {
     33 				t.Fatal(err)
     34 			}
     35 
     36 			if client.shop == nil || client.shop.machine != id || !reflect.DeepEqual(player.inventory, before) {
     37 				t.Fatal("browsing failed or changed ownership/cash")
     38 			}
     39 			messages, catalogs := 0, 0
     40 			for _, packet := range conn.packets {
     41 				switch binary.BigEndian.Uint32(packet[16:20]) {
     42 				case 0x5f4b442a:
     43 					messages++
     44 					text := gamedata.ItemTemplates[machine.Template].Name + ": Purchases are not yet supported in this alpha. You can still sell items here."
     45 					if binary.BigEndian.Uint32(packet[29:33]) != uint32(len(text)) || !bytes.Equal(packet[33:35+len(text)], append([]byte(text), 0, 0)) {
     46 						t.Fatalf("wrong terminal limitation: %x", packet)
     47 					}
     48 				case 0x58362220:
     49 					catalogs++
     50 				}
     51 			}
     52 			if len(gamedata.ShopStock[machine.Template]) == 0 {
     53 				empty++
     54 				if messages != 1 || catalogs != 0 {
     55 					t.Fatal("empty terminal lacks one explicit limitation or publishes an empty catalog")
     56 				}
     57 			} else if messages != 0 || catalogs != 1 {
     58 				t.Fatal("supported catalog is missing or incorrectly labelled unsupported")
     59 			}
     60 
     61 			// Replayed Use must not repeat the notice or reopen the transaction.
     62 			conn.packets = nil
     63 			if err := world.handleShopUse(client, request); err != nil || len(conn.packets) != 0 {
     64 				t.Fatal("replayed Use repeated feedback", err)
     65 			}
     66 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
     67 				t.Fatal(err)
     68 			}
     69 		})
     70 	}
     71 	if visited != 190 || empty != 0 {
     72 		t.Fatalf("finite terminal inventory changed: visited %d, empty %d", visited, empty)
     73 	}
     74 }