aorecomp

Anarchy Online classic in the browser

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


server/token_board_wear_test.go (6685B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	_ "embed"
      7 	"encoding/json"
      8 	"fmt"
      9 	"slices"
     10 	"testing"
     11 )
     12 
     13 //go:embed testdata/token-board-wear.json
     14 var tokenBoardWearData []byte
     15 
     16 type tokenBoardWearFixture struct {
     17 	Template, Quality uint32
     18 	Side              uint16
     19 	Name              string
     20 	Requirements      []gamedata.ItemRequirement
     21 	Modifiers         [][2]int32
     22 }
     23 
     24 func tokenBoardWearFixtures(t *testing.T) []tokenBoardWearFixture {
     25 	t.Helper()
     26 	var fixtures []tokenBoardWearFixture
     27 	if err := json.Unmarshal(tokenBoardWearData, &fixtures); err != nil {
     28 		t.Fatal(err)
     29 	}
     30 	if len(fixtures) != 30 {
     31 		t.Fatal("original token-board census changed", len(fixtures))
     32 	}
     33 	return fixtures
     34 }
     35 
     36 func newTokenBoardWearWorld(t *testing.T, fixture tokenBoardWearFixture) (*server, *combatTestConn) {
     37 	t.Helper()
     38 	world, conn := newEquipmentWorld(t)
     39 	player := world.characters[1]
     40 	player.level = 190
     41 	appearance := unpackAppearance(player.appearance)
     42 	appearance.Side = fixture.Side
     43 	player.appearance = appearance.packed()
     44 	for _, requirement := range fixture.Requirements {
     45 		if requirement.Stat == 60 {
     46 			player.profession = uint16(requirement.Value)
     47 			break
     48 		}
     49 	}
     50 	if err := world.saveUser(player); err != nil {
     51 		t.Fatal(err)
     52 	}
     53 	inventory := playerInventory{LastID: 1, ClanTokens: 1750, OmniTokens: 1750, Items: []ownedItem{
     54 		{ID: 1, Type: 51022, Template: fixture.Template, Quality: fixture.Quality, Slot: 64, Count: 1}}}
     55 	if err := world.saveInventory(player, inventory); err != nil {
     56 		t.Fatal(err)
     57 	}
     58 	return world, conn
     59 }
     60 
     61 func TestTokenBoardsWearRemoveAndRestoreEveryOriginalCompanion(t *testing.T) {
     62 	for _, fixture := range tokenBoardWearFixtures(t) {
     63 		t.Run(fmt.Sprint(fixture.Template), func(t *testing.T) {
     64 			world, _ := newTokenBoardWearWorld(t, fixture)
     65 			player, client := world.characters[1], world.clients[1]
     66 			before := player.stats
     67 			definition := gamedata.ItemTemplates[fixture.Template]
     68 			var requirements []gamedata.ItemRequirement
     69 			var modifiers [][2]int32
     70 			for _, section := range definition.Sections {
     71 				if section.Actions != nil {
     72 					for _, action := range *section.Actions {
     73 						if action.Type == 6 {
     74 							requirements = action.Requirements
     75 						}
     76 					}
     77 				}
     78 				if section.Type == 2 && section.Subtype == 14 {
     79 					for _, function := range *section.Functions {
     80 						if function.ID == 53045 {
     81 							modifiers = append(modifiers, [2]int32{*function.Arguments[0].Integer, *function.Arguments[1].Integer})
     82 						}
     83 					}
     84 				}
     85 			}
     86 			if !slices.Equal(requirements, fixture.Requirements) || !slices.Equal(modifiers, fixture.Modifiers) {
     87 				t.Fatal("board wear differs from the independently reparsed original contract")
     88 			}
     89 
     90 			for _, move := range [][2]uint32{{64, 17}, {17, 64}, {64, 17}} {
     91 				if err := world.handleInventory(client, itemMovePacket(1, move[0], move[1])[20:]); err != nil || client.equipment == nil {
     92 					t.Fatal("qualified board wear/removal was refused", move, err)
     93 				}
     94 				world.advanceEquipment(client.equipment.readyAt)
     95 				cold := newTestInventoryServer(t).characters[1]
     96 
     97 				if player.stats != before || cold.stats != before || !slices.Equal(player.inventory.Items, cold.inventory.Items) ||
     98 					!slices.Equal(player.inventory.Items, []ownedItem{{ID: 1, Type: 51022, Template: fixture.Template, Quality: fixture.Quality, Slot: move[1], Count: 1}}) ||
     99 					player.inventory.ClanTokens != 1750 || player.inventory.OmniTokens != 1750 || cold.inventory.ClanTokens != 1750 || cold.inventory.OmniTokens != 1750 ||
    100 					world.nextItem != 1 || cold.inventory.LastID != 1 {
    101 					t.Fatal("board wear changed raw skills, tokens, identity or cold ownership")
    102 				}
    103 				for _, modifier := range fixture.Modifiers {
    104 					expected := modifier[1]
    105 					if move[1] == 64 {
    106 						expected = 0
    107 					}
    108 					if player.equipmentBonus(uint32(modifier[0])) != expected || cold.equipmentBonus(uint32(modifier[0])) != expected {
    109 						t.Fatal("board companion did not apply/remove exactly once", modifier, move)
    110 					}
    111 				}
    112 			}
    113 		})
    114 	}
    115 }
    116 
    117 func TestTokenBoardWearPreservesEveryOriginalRequirement(t *testing.T) {
    118 	type wearCase struct {
    119 		stat, value int32
    120 		accepted    bool
    121 	}
    122 	for _, fixture := range tokenBoardWearFixtures(t) {
    123 		scenarios := []wearCase{{33, 0, len(fixture.Requirements) == 0}, {33, int32(3 - fixture.Side), len(fixture.Requirements) == 0}}
    124 		profession := int32(0)
    125 		for _, requirement := range fixture.Requirements {
    126 			switch requirement.Stat {
    127 			case 62, 75:
    128 				scenarios = append(scenarios, wearCase{requirement.Stat, requirement.Value, false}, wearCase{requirement.Stat, requirement.Value + 1, true})
    129 			case 37:
    130 				level := map[int32]int32{1: 14, 2: 49, 3: 99, 5: 189}[requirement.Value]
    131 				scenarios = append(scenarios, wearCase{37, level, false}, wearCase{37, level + 1, true})
    132 			case 60:
    133 				scenarios = append(scenarios, wearCase{60, requirement.Value, true})
    134 				profession = 9
    135 				if requirement.Value == 9 {
    136 					profession = 1
    137 				}
    138 			}
    139 		}
    140 		if profession != 0 {
    141 			scenarios = append(scenarios, wearCase{60, profession, false})
    142 		}
    143 		for _, scenario := range scenarios {
    144 			t.Run(fmt.Sprintf("%d/stat%d=%d", fixture.Template, scenario.stat, scenario.value), func(t *testing.T) {
    145 				world, conn := newTokenBoardWearWorld(t, fixture)
    146 				player, client := world.characters[1], world.clients[1]
    147 				switch scenario.stat {
    148 				case 33:
    149 					appearance := unpackAppearance(player.appearance)
    150 					appearance.Side = uint16(scenario.value)
    151 					player.appearance = appearance.packed()
    152 				case 62:
    153 					player.inventory.ClanTokens = uint32(scenario.value)
    154 				case 75:
    155 					player.inventory.OmniTokens = uint32(scenario.value)
    156 				case 37:
    157 					player.level = uint16(scenario.value)
    158 				case 60:
    159 					player.profession = uint16(scenario.value)
    160 				}
    161 				if err := world.saveUser(player); err != nil {
    162 					t.Fatal(err)
    163 				}
    164 				if err := world.saveInventory(player, player.inventory); err != nil {
    165 					t.Fatal(err)
    166 				}
    167 				before, err := readTestItemStore(world)
    168 				if err != nil {
    169 					t.Fatal(err)
    170 				}
    171 
    172 				if err := world.handleInventory(client, inventoryMovePacket(1, 64, 17)[20:]); err != nil {
    173 					t.Fatal(err)
    174 				}
    175 				if scenario.accepted {
    176 					if client.equipment == nil {
    177 						t.Fatal("authored faction/profession alternative was rejected")
    178 					}
    179 					world.advanceEquipment(client.equipment.readyAt)
    180 				}
    181 				after, err := readTestItemStore(world)
    182 
    183 				if err != nil || !scenario.accepted && (client.equipment != nil || !bytes.Equal(before, after) || len(conn.packets) != 2) ||
    184 					(player.inventory.Items[0].Slot == 17) != scenario.accepted {
    185 					t.Fatal("original wear requirement or refusal ownership changed", scenario.accepted, err)
    186 				}
    187 			})
    188 		}
    189 	}
    190 }