server/mission_cash_test.go (6305B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "slices" 9 "testing" 10 ) 11 12 var missionCashShops = []struct { 13 booth, machine, supplyCost uint32 14 appearance uint16 15 }{ 16 {3221228472, 0xc00003b6, 470, 0x515}, 17 {3221228572, 0xc00003b8, 492, 0x315}, 18 {3221228532, 0xc00003ba, 492, 0x115}, 19 } 20 21 func newMissionCashWorld(t *testing.T, boothID uint32, level uint16, difficulty byte) (*server, *combatTestConn) { 22 t.Helper() 23 world, output := newBackyardMissionWorld(t, boothID) 24 player, client := world.characters[1], world.clients[1] 25 player.level = level 26 // Only seed the generation fee; the completed contract must fund the purchases. 27 player.inventory.Credits = uint32(level) 28 player.stats.Abilities.Intelligence, player.stats.TradeRepair.ComputerLiteracy = 5, 0 29 if err := world.saveUser(player); err != nil { 30 t.Fatal(err) 31 } 32 if err := world.saveInventory(player, player.inventory); err != nil { 33 t.Fatal(err) 34 } 35 request := missionOffersRequest() 36 request[17] = difficulty 37 originator, accepted := world.missionBoothEligibility(client, gamedata.MissionBooths[boothID]) 38 if !accepted { 39 t.Fatal("fixture cannot use its starter booth") 40 } 41 request[28] = byte(originator) 42 binary.BigEndian.PutUint32(request[33:37], boothID) 43 if err := world.handleMissionOffers(client, request); err != nil { 44 t.Fatal(err) 45 } 46 if client.missionOffer == nil { 47 t.Fatal("production starter mission was unavailable") 48 } 49 return world, output 50 } 51 52 func collectMissionCashObjective(t *testing.T, world *server) { 53 t.Helper() 54 player, client := world.characters[1], world.clients[1] 55 if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)); err != nil { 56 t.Fatal(err) 57 } 58 mission := player.missions[0] 59 // Controlled placement isolates settlement from already-covered combat/travel. 60 player.position = mission.offer.definition.Position 61 if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil { 62 t.Fatal(err) 63 } 64 world.clients[1], client.ready, client.loading = client, true, false 65 player.position, player.movement = world.worldItems[player.missions[0].objective].position, playerMovement{} 66 if err := world.handleInventory(client, worldPickupRequest(1, player.missions[0].objective)); err != nil { 67 t.Fatal(err) 68 } 69 if len(player.inventory.Items) != 3 { 70 t.Fatal("fixture did not pick up its generated objective", player.inventory) 71 } 72 destination := player.userData() 73 destination.Playfield, destination.Position = mission.offer.definition.Playfield, gamedata.MissionBooths[mission.offer.booth].Position 74 if err := world.updateUser(destination); err != nil { 75 t.Fatal(err) 76 } 77 world.clients[1], client.ready, client.loading = client, true, false 78 } 79 80 func TestStarterMissionCashFundsRecoveryAndSurvivesSettlement(t *testing.T) { 81 for _, shop := range missionCashShops { 82 for _, scenario := range []struct { 83 level uint16 84 difficulty byte 85 credits uint32 86 }{{1, 6, 500}, {5, 6, 2500}, {10, 6, 5000}, {5, 11, 4000}} { 87 t.Run(fmt.Sprintf("%d/level%d/difficulty%d", shop.booth, scenario.level, scenario.difficulty), func(t *testing.T) { 88 world, output := newMissionCashWorld(t, shop.booth, scenario.level, scenario.difficulty) 89 player, client := world.characters[1], world.clients[1] 90 offer := *client.missionOffer 91 var advertised []byte 92 for _, word := range []uint32{56001, shop.booth, 5, scenario.credits, 0, 0} { 93 advertised = binary.BigEndian.AppendUint32(advertised, word) 94 } 95 if offer.definition.Credits != scenario.credits || !bytes.Contains(output.packets[1], advertised) || player.inventory.Credits != 0 { 96 t.Fatal("mission did not advertise only payable rewards after charging its fee", offer.definition.Credits) 97 } 98 collectMissionCashObjective(t, world) 99 cold := reloadMissionEntranceWorld(t) 100 if cold.characters[1].missions[0].offer.definition.Credits != scenario.credits || 101 !bytes.Contains(missionJournalPacket(cold.characters[1].missions[0], cold.missionExpiry[0].expiresAt), advertised) { 102 t.Fatal("accepted cash reward changed during cold recovery") 103 } 104 105 // Pay the saved promise, even after the player's level changes. 106 player.level++ 107 request := missionReturnRequest(1) 108 binary.BigEndian.PutUint32(request[52:56], shop.booth) 109 for range 2 { 110 if err := world.handleMissionReturn(client, request); err != nil { 111 t.Fatal(err) 112 } 113 } 114 cold = reloadMissionEntranceWorld(t) 115 if player.inventory.Credits != scenario.credits || cold.characters[1].inventory.Credits != scenario.credits || 116 len(player.missions) != 0 || !cold.missionExpiry[0].completed || player.experience != 0 { 117 t.Fatal("hand-in changed the promise, paid twice or granted completion XP", player.inventory) 118 } 119 120 // Spend only completion cash, retaining the reward and original equipment. 121 machine := gamedata.ShopMachines[shop.machine] 122 player.playfield, player.position, player.appearance = machine.Playfield, machine.Position, shop.appearance 123 request = shopUseRequest(2) 124 binary.BigEndian.PutUint32(request[44:48], shop.machine) 125 if err := world.handleShopUse(client, request); err != nil { 126 t.Fatal(err) 127 } 128 for _, row := range []uint32{0, 1, 2, 4} { 129 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, shop.machine}, [2]uint32{111, row})[20:]); err != nil { 130 t.Fatal(err) 131 } 132 } 133 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil { 134 t.Fatal(err) 135 } 136 cold = reloadMissionEntranceWorld(t) 137 if cold.characters[1].inventory.Credits != scenario.credits-shop.supplyCost || len(cold.characters[1].inventory.Items) != 6 || 138 !slices.Equal(cold.characters[1].inventory.Items[2:], []ownedItem{ 139 {ID: 5, Type: 51005, Template: 25812, Quality: 1, Slot: 66, Count: 50}, 140 {ID: 6, Type: 51005, Template: 85366, Quality: 1, Slot: 67, Count: 25}, 141 {ID: 7, Type: 51005, Template: 25820, Quality: 1, Slot: 68, Count: 50}, 142 {ID: 8, Type: 51005, Template: 21609, Quality: 1, Slot: 69, Count: 100}, 143 }) { 144 t.Fatal("mission cash did not buy the exact recovery/ammunition packages", cold.characters[1].inventory) 145 } 146 }) 147 } 148 } 149 }