aorecomp

Anarchy Online classic in the browser

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


server/mission_reward_pool_test.go (7565B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"reflect"
      9 	"testing"
     10 )
     11 
     12 func TestStarterMissionRewardPoolRetainsEveryFamilyAtTheSelectedMissionQuality(t *testing.T) {
     13 	for choice, endpoints := range [][2]uint32{
     14 		{85596, 85595}, {85742, 85741}, {85638, 85637}, {85512, 85511},
     15 		{85557, 85556}, {85686, 85685}, {122691, 122692}, {160216, 160217},
     16 	} {
     17 		t.Run(fmt.Sprint(endpoints[0]), func(t *testing.T) {
     18 			definition := gamedata.MissionTemplates[3221228472]
     19 			world, output := newMissionOfferWorld(t)
     20 			gamedata.MissionTemplates[3221228472] = definition
     21 			world.nextNPC = 2000000
     22 			player, client := world.characters[1], world.clients[1]
     23 			player.level = 5
     24 			if err := world.saveUser(player); err != nil {
     25 				t.Fatal(err)
     26 			}
     27 			draws := 0
     28 			world.combatRoll = func(bound uint32) uint32 {
     29 				if bound != 8 {
     30 					t.Fatal("production reward pool lost a reviewed family", bound)
     31 				}
     32 				draws++
     33 				return uint32(choice)
     34 			}
     35 			request := missionOffersRequest()
     36 			request[17] = 11
     37 
     38 			if err := world.handleMissionOffers(client, request); err != nil {
     39 				t.Fatal(err)
     40 			}
     41 			if client.missionOffer == nil {
     42 				t.Fatal("supported production contract returned no offer")
     43 			}
     44 			offer := *client.missionOffer
     45 			definition.RewardItems = []uint32{121567}
     46 			gamedata.MissionTemplates[3221228472] = definition
     47 			if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(offer)); err != nil {
     48 				t.Fatal(err)
     49 			}
     50 			cold := reloadMissionEntranceWorld(t)
     51 
     52 			var advertised []byte
     53 			for _, word := range []uint32{2018, endpoints[0], endpoints[1], 8, 0} {
     54 				advertised = binary.BigEndian.AppendUint32(advertised, word)
     55 			}
     56 			mission := cold.characters[1].missions[0]
     57 			if draws != 1 || mission.offer.definition.RewardItem != endpoints[0] || mission.offer.definition.RewardQuality != 8 ||
     58 				!bytes.Contains(output.packets[1], advertised) || !bytes.Contains(missionJournalPacket(mission, mission.expiresAt), advertised) ||
     59 				len(mission.offer.definition.RewardItems) != 0 || cold.characters[1].inventory.Credits != 0 ||
     60 				len(cold.items) != 2 || cold.nextItem != 2 {
     61 				t.Fatal("production reward changed after advertisement or was allocated before completion", mission.offer.definition)
     62 			}
     63 			if len(offer.definition.Encounters) != 1 || offer.definition.Encounters[0].Level != 8 || len(cold.mobileNPCs[mission.world]) != 5 {
     64 				t.Fatal("reward and encounter quality did not follow the same selected mission level")
     65 			}
     66 			for _, npc := range cold.mobileNPCs[mission.world] {
     67 				if npc.level != 8 {
     68 					t.Fatal("cold encounter lost its selected mission level", npc.id, npc.level)
     69 				}
     70 			}
     71 		})
     72 	}
     73 }
     74 
     75 func TestMissionRewardPoolRetainsItsAdvertisedChoiceThroughAcceptanceAndColdLoad(t *testing.T) {
     76 	for _, choice := range []uint32{0, 1} {
     77 		t.Run(fmt.Sprint(choice), func(t *testing.T) {
     78 			world, output := newMissionOfferWorld(t)
     79 			definition := gamedata.MissionTemplates[3221228472]
     80 			definition.RewardItems, definition.RewardQuality = []uint32{85596, 122691}, 2
     81 			gamedata.MissionTemplates[3221228472] = definition
     82 			draws := 0
     83 			world.combatRoll = func(bound uint32) uint32 {
     84 				if bound != 2 {
     85 					t.Fatal("reward pool used an unexpected choice bound", bound)
     86 				}
     87 				draws++
     88 				return choice
     89 			}
     90 
     91 			if err := world.handleMissionOffers(world.clients[1], missionOffersRequest()); err != nil {
     92 				t.Fatal(err)
     93 			}
     94 			offer := *world.clients[1].missionOffer
     95 			// Editing the generation pool cannot change the already advertised reward.
     96 			definition.RewardItems[choice] = 123742
     97 			definition.RewardQuality = 11
     98 			gamedata.MissionTemplates[3221228472] = definition
     99 			if err := world.handleMissionAcceptance(world.clients[1], missionAcceptanceRequest(offer)); err != nil {
    100 				t.Fatal(err)
    101 			}
    102 			cold := newTestInventoryServer(t)
    103 
    104 			low, high := uint32(85596), uint32(85595)
    105 			if choice == 1 {
    106 				low, high = 122691, 122692
    107 			}
    108 			var item []byte
    109 			for _, word := range []uint32{2018, low, high, 2, 0} {
    110 				item = binary.BigEndian.AppendUint32(item, word)
    111 			}
    112 			mission := cold.characters[1].missions[0]
    113 			if draws != 1 || offer.definition.RewardItem != low || mission.offer.definition.RewardItem != low || mission.offer.definition.RewardQuality != 2 ||
    114 				len(mission.offer.definition.RewardItems) != 0 || !bytes.Contains(output.packets[1], item) ||
    115 				!bytes.Contains(missionJournalPacket(mission, mission.expiresAt), item) || len(cold.items) != 2 || cold.nextItem != 2 {
    116 				t.Fatal("advertised reward was rerolled, lost its endpoints or allocated before completion", draws, mission.offer.definition)
    117 			}
    118 		})
    119 	}
    120 }
    121 
    122 func TestMissionRewardPoolFailureRetainsTheWalletAndPreviousOfferForRetry(t *testing.T) {
    123 	for _, statement := range []string{
    124 		"CREATE TRIGGER fail_reward_offer BEFORE UPDATE OF credits ON players BEGIN SELECT RAISE(FAIL,'offer'); END",
    125 		"CREATE TRIGGER fail_reward_offer BEFORE UPDATE OF credits ON players BEGIN SELECT RAISE(IGNORE); END",
    126 		"CREATE TABLE pending_offer (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_reward_offer AFTER UPDATE OF credits ON players BEGIN INSERT INTO pending_offer VALUES(999999); END",
    127 	} {
    128 		t.Run(statement, func(t *testing.T) {
    129 			world, output := newMissionOfferWorld(t)
    130 			definition := gamedata.MissionTemplates[3221228472]
    131 			definition.RewardItems, definition.RewardQuality = []uint32{85596}, 2
    132 			gamedata.MissionTemplates[3221228472] = definition
    133 			if err := world.handleMissionOffers(world.clients[1], missionOffersRequest()); err != nil {
    134 				t.Fatal(err)
    135 			}
    136 			before := *world.clients[1].missionOffer
    137 			definition.RewardItems = []uint32{85596, 122691}
    138 			gamedata.MissionTemplates[3221228472] = definition
    139 			world.combatRoll = func(uint32) uint32 { return 1 }
    140 			if _, err := world.db.Exec(statement); err != nil {
    141 				t.Fatal(err)
    142 			}
    143 			output.packets = nil
    144 
    145 			failure := world.handleMissionOffers(world.clients[1], missionOffersRequest())
    146 			cold := newTestInventoryServer(t)
    147 
    148 			if failure == nil || !reflect.DeepEqual(before, *world.clients[1].missionOffer) || len(output.packets) != 0 ||
    149 				world.characters[1].inventory.Credits != 4 || cold.characters[1].inventory.Credits != 4 || world.nextItem != 1 || cold.nextItem != 1 {
    150 				t.Fatal("failed generation charged, replaced the advertised choice or allocated an item", failure)
    151 			}
    152 			if _, err := world.db.Exec("DROP TRIGGER fail_reward_offer"); err != nil {
    153 				t.Fatal(err)
    154 			}
    155 			if err := world.handleMissionOffers(world.clients[1], missionOffersRequest()); err != nil {
    156 				t.Fatal(err)
    157 			}
    158 			cold = newTestInventoryServer(t)
    159 			if world.clients[1].missionOffer.definition.RewardItem != 122691 || world.characters[1].inventory.Credits != 3 ||
    160 				cold.characters[1].inventory.Credits != 3 || len(output.packets) != 2 || world.nextItem != 1 {
    161 				t.Fatal("retry failed to advertise the new choice with exactly one fee")
    162 			}
    163 		})
    164 	}
    165 }
    166 
    167 func TestMissionRewardPoolCashOnlyContractDoesNotDrawAnItem(t *testing.T) {
    168 	world, _ := newMissionOfferWorld(t)
    169 	world.combatRoll = func(uint32) uint32 {
    170 		t.Fatal("cash-only generation requested an item choice")
    171 		return 0
    172 	}
    173 
    174 	if err := world.handleMissionOffers(world.clients[1], missionOffersRequest()); err != nil {
    175 		t.Fatal(err)
    176 	}
    177 
    178 	if world.clients[1].missionOffer.definition.RewardItem != 0 || world.clients[1].missionOffer.definition.RewardQuality != 0 ||
    179 		world.characters[1].inventory.Credits != 4 || world.nextItem != 1 {
    180 		t.Fatal("cash-only generation promised an item or changed its ordinary fee")
    181 	}
    182 }