aorecomp

Anarchy Online classic in the browser

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


server/shop_machines_test.go (8150B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"net"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func TestShopUseEvaluatesAuthoredAccessContracts(t *testing.T) {
     14 	for _, scenario := range []struct {
     15 		name                    string
     16 		machine                 uint32
     17 		side, profession, level uint16
     18 		breaking                uint16
     19 		accepted                bool
     20 	}{
     21 		{"Omni", 3221226655, 2, 1, 1, 0, true},
     22 		{"Clan denied Omni", 3221226655, 1, 1, 1, 0, false},
     23 		{"Neutral denied Omni", 3221226655, 0, 1, 1, 0, false},
     24 		{"unrestricted", 3221750943, 0, 1, 1, 0, true},
     25 		{"action0 does not restrict Use", 3221685407, 1, 1, 1, 0, true},
     26 		{"Trader", 3224895988, 2, 7, 25, 0, true},
     27 		{"Trader level boundary", 3224895988, 2, 7, 24, 0, false},
     28 		{"Trader profession", 3224895988, 2, 4, 25, 0, false},
     29 		{"Trader side", 3224895988, 1, 7, 25, 0, false},
     30 		{"Clan Trader", 3224109556, 1, 7, 25, 0, true},
     31 		{"neutral Trader", 3221488809, 0, 7, 25, 0, true},
     32 		{"Shop of Fortune level", 3221815399, 0, 1, 40, 0, true},
     33 		{"Shop of Fortune boundary", 3221815399, 0, 1, 39, 0, false},
     34 		{"Fixer", 3221226272, 0, 4, 1, 180, true},
     35 		{"Fixer effective skill boundary", 3221226272, 0, 4, 1, 177, false},
     36 		{"Fixer effective skill above boundary", 3221226272, 0, 4, 1, 178, true},
     37 		{"Fixer skill denied", 3221226272, 0, 4, 1, 0, false},
     38 		{"Fixer profession denied", 3221226272, 0, 7, 1, 180, false},
     39 		{"unknown entitlement", 3221426784, 2, 7, 200, 1000, false},
     40 		{"missing Can Use", 3221225572, 0, 1, 1, 0, false},
     41 	} {
     42 		t.Run(scenario.name, func(t *testing.T) {
     43 			world, conn := newCombatWorld(t)
     44 			player, client := world.characters[1], world.clients[1]
     45 			machine := gamedata.ShopMachines[scenario.machine]
     46 			player.playfield, player.position = machine.Playfield, machine.Position
     47 			player.appearance, player.profession, player.level = 0x115|scenario.side<<9, scenario.profession, scenario.level
     48 			player.stats.Spying.BreakingAndEntering = scenario.breaking
     49 			request := shopUseRequest(1)
     50 			binary.BigEndian.PutUint32(request[44:48], scenario.machine)
     51 
     52 			err := world.handleShopUse(client, request)
     53 
     54 			if err != nil {
     55 				t.Fatal(err)
     56 			}
     57 			if (client.shop != nil) != scenario.accepted {
     58 				t.Fatalf("shop admission = %v, want %v", client.shop != nil, scenario.accepted)
     59 			}
     60 			if scenario.accepted {
     61 				start := conn.packets[len(conn.packets)-1]
     62 				if len(conn.packets) != 5 || client.shop.machine != scenario.machine || binary.BigEndian.Uint32(start[41:45]) != scenario.machine ||
     63 					binary.BigEndian.Uint32(start[45:49]) != 111 || binary.BigEndian.Uint32(start[49:53]) != 0 {
     64 					t.Fatal("opened the wrong shop contract")
     65 				}
     66 			} else if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 2 {
     67 				t.Fatal("rejection initialized a trade")
     68 			}
     69 		})
     70 	}
     71 }
     72 
     73 func TestShopFullUpdatePreservesRotationAndHighLowMesh(t *testing.T) {
     74 	world, conn := newCombatWorld(t)
     75 	player, client := world.characters[1], world.clients[1]
     76 	player.playfield, player.position = 1180, [3]float32{190.88559, 4.61, 166.79538}
     77 	player.appearance = 0x315
     78 	request := shopUseRequest(1)
     79 	binary.BigEndian.PutUint32(request[44:48], 3223913628)
     80 
     81 	err := world.handleShopUse(client, request)
     82 
     83 	if err != nil || len(conn.packets) != 5 {
     84 		t.Fatalf("shop failed to open: %v", err)
     85 	}
     86 	packet := conn.packets[1]
     87 	if binary.BigEndian.Uint32(packet[16:20]) != 0x7f544905 || binary.BigEndian.Uint32(packet[24:28]) != 3223913628 {
     88 		t.Fatal("wrong vending object")
     89 	}
     90 	body := packet[29:]
     91 	for index, expected := range []float32{190.88559, 4.61, 166.79538, 0, 0.7071080803871155, 0, 0.7071054577827454} {
     92 		if math.Float32frombits(binary.BigEndian.Uint32(body[12+index*4:])) != expected {
     93 			t.Fatalf("transform field %d changed", index)
     94 		}
     95 	}
     96 	if binary.BigEndian.Uint32(body[40:44]) != 1180 || binary.BigEndian.Uint32(body[54:58]) != 4036 {
     97 		t.Fatal("world or stat count changed")
     98 	}
     99 	for index, expected := range []uint32{23, 155602, 701, 1, 12, 90937, 0, 2, 0, 1009, 3} {
    100 		if binary.BigEndian.Uint32(body[58+index*4:]) != expected {
    101 			t.Fatalf("native stat/inheritance field %d changed", index)
    102 		}
    103 	}
    104 }
    105 
    106 func TestShopRoomEntryReplaysOnlyItsAuthoredMachines(t *testing.T) {
    107 	for _, playfield := range []uint32{1180, 1183, 1186, 705} {
    108 		t.Run(fmt.Sprint(playfield), func(t *testing.T) {
    109 			world := newTestServer(t)
    110 			world.characters = make(map[uint32]*character)
    111 			player := world.newCharacter()
    112 			player.playfield = playfield
    113 			startTestService(t, world)
    114 			connection, peer := net.Pipe()
    115 			defer connection.Close()
    116 			connection.SetDeadline(time.Now().Add(5 * time.Second))
    117 			go func() { defer peer.Close(); world.serveWorldConnection(peer) }()
    118 			cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
    119 			binary.BigEndian.PutUint16(cookie, 0xdfdf)
    120 			found := map[uint32]bool{}
    121 
    122 			connection.Write(cookie)
    123 			for {
    124 				packet, err := readPacket(connection)
    125 				if err != nil {
    126 					t.Fatal(err)
    127 				}
    128 				key := binary.BigEndian.Uint32(packet[16:20])
    129 				if key == 0x7f544905 {
    130 					id := binary.BigEndian.Uint32(packet[24:28])
    131 					machine, exists := gamedata.ShopMachines[id]
    132 					if !exists || machine.Playfield != playfield || found[id] {
    133 						t.Fatalf("unexpected/duplicate shop %d", id)
    134 					}
    135 					found[id] = true
    136 				}
    137 				if key == 0x5f52412e {
    138 					break
    139 				}
    140 			}
    141 
    142 			expected := map[uint32]int{1180: 55, 1183: 76, 1186: 55, 705: 0}[playfield]
    143 			if len(found) != expected {
    144 				t.Fatalf("entered world %d with %d machines, want %d", playfield, len(found), expected)
    145 			}
    146 		})
    147 	}
    148 }
    149 
    150 func TestShopRoomSaleCommitsItsOwnModifier(t *testing.T) {
    151 	world, _ := newShopWorld(t)
    152 	player, client := world.characters[1], world.clients[1]
    153 	player.playfield, player.position = 1183, [3]float32{192, 5.75, 139.5}
    154 	if err := world.savePosition(player); err != nil {
    155 		t.Fatal(err)
    156 	}
    157 	request := shopUseRequest(2)
    158 	binary.BigEndian.PutUint32(request[44:48], 3221226655)
    159 
    160 	if err := world.handleShopUse(client, request); err != nil {
    161 		t.Fatal(err)
    162 	}
    163 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
    164 		t.Fatal(err)
    165 	}
    166 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    167 		t.Fatal(err)
    168 	}
    169 	restored := newTestInventoryServer(t).characters[1].inventory
    170 
    171 	if restored.Credits != 827 || len(restored.Items) != 0 {
    172 		t.Fatalf("room sale changed cash/ownership: %+v", restored)
    173 	}
    174 }
    175 
    176 func TestShopSpecialistSaleRevalidatesChangedRequirements(t *testing.T) {
    177 	for _, scenario := range []string{"profession", "level", "skill"} {
    178 		t.Run(scenario, func(t *testing.T) {
    179 			world, _ := newShopWorld(t)
    180 			player, client := world.characters[1], world.clients[1]
    181 			machineID := uint32(3221488809)
    182 			player.profession, player.level = 7, 25
    183 			if scenario == "skill" {
    184 				machineID, player.profession = 3221226272, 4
    185 				player.stats.Spying.BreakingAndEntering = 178
    186 			}
    187 			machine := gamedata.ShopMachines[machineID]
    188 			player.playfield, player.position = machine.Playfield, machine.Position
    189 			request := shopUseRequest(2)
    190 			binary.BigEndian.PutUint32(request[44:48], machineID)
    191 			if err := world.handleShopUse(client, request); err != nil || client.shop == nil {
    192 				t.Fatalf("specialist shop failed to open: %v", err)
    193 			}
    194 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
    195 				t.Fatal(err)
    196 			}
    197 			switch scenario {
    198 			case "profession":
    199 				player.profession = 4
    200 			case "level":
    201 				player.level = 24
    202 			case "skill":
    203 				player.stats.Spying.BreakingAndEntering = 177
    204 			}
    205 
    206 			err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:])
    207 			restored := newTestInventoryServer(t).characters[1].inventory
    208 
    209 			if err != nil || client.shop != nil || player.inventory.Credits != 100 || len(player.inventory.Items) != 1 || restored.Credits != 100 || len(restored.Items) != 1 || restored.Items[0].ID != 1 {
    210 				t.Fatalf("changed specialist eligibility consumed the offer: %v %+v", err, restored)
    211 			}
    212 		})
    213 	}
    214 }