server/basic_shop_nanos_test.go (8961B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "slices" 9 "testing" 10 "time" 11 ) 12 13 type basicShopNanoFixture struct { 14 prices [2]uint32 15 profession uint32 16 machines [3]uint32 17 crystal, program, quality uint32 18 stat uint32 19 bonus int32 20 minimum, maximum uint32 21 hostile bool 22 } 23 24 func basicShopNanoFixtures() []basicShopNanoFixture { 25 return []basicShopNanoFixture{ 26 {[2]uint32{831, 872}, 1, [3]uint32{0xc015049f, 0xc016049c, 0xc00b04a2}, 75415, 75413, 4, 90, 21, 0, 0, false}, 27 {[2]uint32{201, 211}, 2, [3]uint32{0xc012049f, 0xc013049c, 0xc00804a2}, 75354, 75350, 1, 91, 16, 0, 0, false}, 28 {[2]uint32{833, 874}, 3, [3]uint32{0xc00f049f, 0xc010049c, 0xc00504a2}, 70577, 70548, 4, 90, 23, 0, 0, false}, 29 {[2]uint32{820, 861}, 4, [3]uint32{0xc010049f, 0xc011049c, 0xc00604a2}, 85273, 85215, 4, 0, 0, 2, 6, false}, 30 {[2]uint32{830, 871}, 5, [3]uint32{0xc00b049f, 0xc00c049c, 0xc00104a2}, 81952, 81942, 4, 0, 0, 2, 2, true}, 31 {[2]uint32{1536, 1612}, 6, [3]uint32{0xc00a049f, 0xc00b049c, 0xc00004a2}, 82082, 82064, 7, 0, 0, 41, 72, false}, 32 {[2]uint32{1536, 1612}, 7, [3]uint32{0xc016049f, 0xc017049c, 0xc00c04a2}, 30764, 30713, 7, 119, 15, 0, 0, false}, 33 {[2]uint32{824, 865}, 8, [3]uint32{0xc00c049f, 0xc00d049c, 0xc00204a2}, 56003, 55992, 4, 0, 0, 0, 0, true}, 34 {[2]uint32{1777, 1865}, 9, [3]uint32{0xc00e049f, 0xc00f049c, 0xc00404a2}, 202849, 202848, 8, 107, 14, 0, 0, false}, 35 {[2]uint32{827, 868}, 10, [3]uint32{0xc00d049f, 0xc00e049c, 0xc00304a2}, 43957, 43831, 4, 0, 0, 36, 59, false}, 36 {[2]uint32{1510, 1585}, 11, [3]uint32{0xc014049f, 0xc015049c, 0xc00a04a2}, 28811, 28608, 7, 0, 0, 26, 46, true}, 37 {[2]uint32{201, 211}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 26681, 26465, 1, 0, 0, 5, 10, false}, 38 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27265, 27080, 4, 112, 10, 0, 0, false}, 39 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27285, 27149, 4, 113, 10, 0, 0, false}, 40 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27228, 27093, 4, 114, 10, 0, 0, false}, 41 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27298, 27162, 4, 115, 10, 0, 0, false}, 42 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27205, 27075, 4, 107, 10, 0, 0, false}, 43 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 27232, 27097, 4, 100, 10, 0, 0, false}, 44 {[2]uint32{828, 869}, 1, [3]uint32{0xc011049f, 0xc012049c, 0xc00704a2}, 26481, 26354, 4, 116, 10, 0, 0, false}, 45 } 46 } 47 48 func trainBasicShopNanoFixture(player *character, profession uint32) { 49 // A funded level-ten checkpoint; no claim of earned IP or affordability. 50 player.level, player.profession = 10, uint16(profession) 51 player.stats.Abilities = characterAbilitiesStats{36, 36, 36, 36, 36, 36} 52 for _, field := range player.stats.trainingStats() { 53 if field.stat == 122 || field.stat >= 127 && field.stat <= 132 { 54 *field.value = uint16(player.trainingMaximum(field.stat)) 55 } 56 } 57 player.stats.TradeRepair.ComputerLiteracy = 0 58 player.health, player.nano = 20, player.maximumNano() 59 } 60 61 func mpShopNanoFixtures() []basicShopNanoFixture { 62 return []basicShopNanoFixture{ 63 {[2]uint32{201, 211}, 12, [3]uint32{0xc013049f, 0xc014049c, 0xc00904a2}, 43784, 43725, 1, 0, 0, 0, 0, false}, 64 {[2]uint32{1490, 1564}, 12, [3]uint32{0xc013049f, 0xc014049c, 0xc00904a2}, 43789, 43719, 7, 0, 0, 0, 0, false}, 65 {[2]uint32{1518, 1593}, 12, [3]uint32{0xc013049f, 0xc014049c, 0xc00904a2}, 43769, 43745, 7, 0, 0, 0, 0, false}, 66 } 67 } 68 69 func TestBasicShopNanosPurchaseLearnCastAndRecover(t *testing.T) { 70 for _, fixture := range append(basicShopNanoFixtures(), mpShopNanoFixtures()...) { 71 for _, machineID := range fixture.machines { 72 t.Run(fmt.Sprintf("%x/%d", machineID, fixture.crystal), func(t *testing.T) { 73 world, _ := newPurchaseWorld(t) 74 world.nextNPC = 1000001 75 player, client, target := world.characters[1], world.clients[1], world.characters[1000000] 76 machine := gamedata.ShopMachines[machineID] 77 player.playfield, player.position = machine.Playfield, machine.Position 78 player.appearance = map[uint32]uint16{1183: 0x515, 1180: 0x315, 1186: 0x115}[machine.Playfield] 79 trainBasicShopNanoFixture(player, fixture.profession) 80 player.inventory.Credits = 10000 81 target.playfield, target.position, target.health = player.playfield, player.position, 100 82 if err := world.saveUser(player); err != nil { 83 t.Fatal(err) 84 } 85 if err := world.saveInventory(player, player.inventory); err != nil { 86 t.Fatal(err) 87 } 88 request := shopUseRequest(2) 89 binary.BigEndian.PutUint32(request[44:48], machineID) 90 91 // Buy the exact authored row; repeated Accept must not duplicate it. 92 if err := world.handleShopUse(client, request); err != nil { 93 t.Fatal(err) 94 } 95 row := gamedata.ShopItem{Template: fixture.crystal, Quality: fixture.quality} 96 index := slices.Index(client.shop.stock, row) 97 if index < 0 || nanoCrystalProgram(fixture.crystal) != fixture.program { 98 t.Fatal("missing complete supported crystal", row) 99 } 100 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, machineID}, [2]uint32{111, uint32(index)})[20:]); err != nil { 101 t.Fatal(err) 102 } 103 accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:] 104 for range 2 { 105 if err := world.handleTrade(client, accept); err != nil { 106 t.Fatal(err) 107 } 108 } 109 if len(player.inventory.Items) != 2 || player.inventory.Items[1] != (ownedItem{ID: 2, Type: 51005, Template: fixture.crystal, Quality: fixture.quality, Slot: 65, Count: 1}) { 110 t.Fatal("crystal purchase changed identity or quality", player.inventory) 111 } 112 credits := player.inventory.Credits 113 price := fixture.prices[0] 114 if machine.Playfield != 1183 { 115 price = fixture.prices[1] 116 } 117 if credits != 10000-price { 118 t.Fatal("crystal purchase charged wrong price", credits, price) 119 } 120 121 // Upload the purchase and cast it through the ordinary public handlers. 122 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 65, 3)[20:]); err != nil { 123 t.Fatal(err) 124 } 125 if !slices.Equal(player.inventory.LearnedNanos, []uint32{fixture.program}) || len(player.inventory.Items) != 1 { 126 t.Fatal("purchased crystal cannot be learned at level ten", player.inventory, player.stats.NanoAiding) 127 } 128 recipient := player 129 if fixture.hostile { 130 recipient = target 131 } 132 beforeHealth, beforeNano := recipient.health, player.nano 133 at := castTestBuff(t, world, 1, recipient.id, fixture.program) 134 if fixture.program == 85215 || fixture.program == 81942 { 135 world.advanceNanos(recipient.activeNanos[0].nextTick.Add(time.Nanosecond)) 136 } 137 138 if player.nano >= beforeNano || player.nanoReserved != 0 { 139 t.Fatal("cast did not spend nano once") 140 } 141 if fixture.stat != 0 && player.nanoBonus(fixture.stat) != fixture.bonus { 142 t.Fatal("purchased buff did not apply", player.activeNanos) 143 } 144 if fixture.minimum != 0 { 145 amount := recipient.health - beforeHealth 146 if fixture.hostile { 147 amount = beforeHealth - recipient.health 148 } 149 if amount < fixture.minimum || amount > fixture.maximum { 150 t.Fatal("purchased nano has wrong health effect", amount, at) 151 } 152 } 153 if fixture.program == 55992 && (len(target.activeNanos) != 1 || !target.movementRestricted()) { 154 t.Fatal("purchased root did not stop target") 155 } 156 157 // Credits, consumed crystal, learned program and effects survive restart. 158 var coldWorld *server 159 if fixture.profession == 12 { 160 coldWorld = newServerForTest(t) 161 } else { 162 coldWorld = newTestInventoryServer(t) 163 } 164 cold := coldWorld.characters[1] 165 if fixture.profession == 12 { 166 level := map[uint32]uint16{43725: 2, 43719: 6, 43745: 8}[fixture.program] 167 for _, current := range []*server{world, coldWorld} { 168 if len(current.characters[1].pets) != 1 { 169 t.Fatal("purchased summon lost pet ownership") 170 } 171 for id := range current.characters[1].pets { 172 pet := current.characters[id] 173 if pet.level != level || pet.health != 17*uint32(level) || pet.pet.program != fixture.program || pet.attackWeapon().Quality == nil || *pet.attackWeapon().Quality != uint32(level) { 174 t.Fatal("purchased pet lost its level, resources or weapon", pet) 175 } 176 } 177 } 178 } 179 if !reflect.DeepEqual(cold.inventory, player.inventory) || cold.inventory.Credits != credits || world.nextItem != 2 || cold.nano != player.nano { 180 t.Fatal("purchase/upload/cast changed during cold recovery") 181 } 182 if fixture.stat != 0 && cold.nanoBonus(fixture.stat) != fixture.bonus { 183 t.Fatal("buff did not recover") 184 } 185 t.Logf("price=%d", 10000-credits) 186 }) 187 } 188 } 189 }