aorecomp

Anarchy Online classic in the browser

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


server/mission_progress_test.go (22304B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestMissionEncounterProgressRetainsEachSelectedLevel(t *testing.T) {
     12 	world, _ := newMissionEncounterOfferWorld(t)
     13 	client := world.clients[1]
     14 	client.missionOffer.definition.Encounters = []gamedata.MissionEncounter{
     15 		{Definition: "950/district/2/group/0/authored_point", Level: 1, Positions: [][3]float32{{15, .011, 46}}},
     16 		{Definition: "950/district/2/group/0/authored_point", Level: 2, Positions: [][3]float32{{15, .011, 43}}},
     17 	}
     18 
     19 	if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)); err != nil {
     20 		t.Fatal(err)
     21 	}
     22 	cold := reloadMissionEntranceWorld(t)
     23 
     24 	for _, expected := range []struct {
     25 		id    uint32
     26 		level uint16
     27 	}{{2000000, 1}, {2000001, 2}} {
     28 		var level uint16
     29 		if err := cold.db.QueryRow("SELECT level FROM mission_encounters WHERE npc_id=?", expected.id).Scan(&level); err != nil {
     30 			t.Fatal(err)
     31 		}
     32 		if level != expected.level || cold.characters[expected.id].level != expected.level {
     33 			t.Fatal("population progress lost the accepted encounter level", expected.id, level)
     34 		}
     35 	}
     36 }
     37 
     38 func newMissionProgressCombatWorld(t *testing.T, attack string) (*server, *worldClient, *character, *combatTestConn) {
     39 	t.Helper()
     40 	world, client, output := enteredMissionEncounterWorld(t)
     41 	player, npc := world.characters[1], world.characters[2000003]
     42 	player.position, npc.health = [3]float32{15, .011, 27}, 1
     43 	// Keep allocation rollback assertions focused on one ordinary reward.
     44 	profile := *npc.combatProfile
     45 	profile.Loot = profile.Loot[:1]
     46 	npc.combatProfile = &profile
     47 	player.stats = testCharacterStats()
     48 	world.combatRoll = func(uint32) uint32 { return 0 }
     49 	if attack == "axe" || attack == "fast" || attack == "pistol" || attack == "fling" {
     50 		weapon := ownedItem{ID: world.nextItem + 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1}
     51 		if attack == "pistol" || attack == "fling" {
     52 			weapon.Template, weapon.Ammo = 123742, 3
     53 		}
     54 		player.inventory.Items = append(player.inventory.Items, weapon)
     55 		player.inventory.LastID = weapon.ID
     56 	}
     57 	if attack == "nano" || attack == "dot" {
     58 		player.profession, player.nano = 8, 30
     59 		player.stats.NanoAiding.PsychologicalModification, player.stats.NanoAiding.MatterCreation = 7, 6
     60 		player.inventory.LearnedNanos = []uint32{29618}
     61 		if attack == "dot" {
     62 			player.profession = 10
     63 			player.stats.NanoAiding.BiologicalMetamorphosis, player.stats.NanoAiding.MatterCreation = 9, 8
     64 			player.inventory.LearnedNanos = []uint32{43376}
     65 		}
     66 	}
     67 	if err := world.saveUser(player); err != nil {
     68 		t.Fatal(err)
     69 	}
     70 	if err := world.saveInventory(player, player.inventory); err != nil {
     71 		t.Fatal(err)
     72 	}
     73 	if err := world.flushWorld(); err != nil {
     74 		t.Fatal(err)
     75 	}
     76 	output.packets = nil
     77 	return world, client, npc, output
     78 }
     79 
     80 func missionProgressAttack(world *server, attack string) error {
     81 	client, npc := world.clients[1], world.characters[2000003]
     82 	switch attack {
     83 	case "fast":
     84 		return world.handleWeaponSpecial(client, fastAttackRequest(1, npc.id)[20:])
     85 	case "fling":
     86 		return world.handleWeaponSpecial(client, flingShotRequest(1, npc.id)[20:])
     87 	case "nano", "dot":
     88 		program := uint32(29618)
     89 		if attack == "dot" {
     90 			program = 43376
     91 		}
     92 		if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, program})[20:]); err != nil {
     93 			return err
     94 		}
     95 		if client.nanoCast == nil {
     96 			return fmt.Errorf("mission progress fixture's cast was rejected")
     97 		}
     98 		world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     99 		if attack == "dot" && len(npc.activeNanos) != 0 {
    100 			world.advanceNanos(npc.activeNanos[0].nextTick)
    101 		}
    102 	default:
    103 		if err := world.handleCombat(client, attackPacket(1, npc.id)[20:]); err != nil {
    104 			return err
    105 		}
    106 		world.advanceCombat(world.characters[1].nextAttack)
    107 	}
    108 	return nil
    109 }
    110 
    111 func TestMissionEncounterProgressSurvivesDeathAndCorpseExpiry(t *testing.T) {
    112 	world, client, output := enteredMissionEncounterWorld(t)
    113 	player, npc := world.characters[1], world.characters[2000003]
    114 	player.position = [3]float32{15, .011, 27}
    115 	npc.health = 1
    116 	world.combatRoll = func(uint32) uint32 { return 0 }
    117 	var initial, dead int
    118 	if err := world.db.QueryRow("SELECT count(*),sum(killed) FROM mission_encounters WHERE mission_id=?", player.missions[0].id).Scan(&initial, &dead); err != nil {
    119 		t.Fatal(err)
    120 	}
    121 	if initial != 5 || dead != 0 {
    122 		t.Fatal("acceptance did not record the concrete initial population", initial, dead)
    123 	}
    124 
    125 	if err := world.handleCombat(client, attackPacket(player.id, npc.id)[20:]); err != nil {
    126 		t.Fatal(err)
    127 	}
    128 	world.advanceCombat(player.nextAttack)
    129 	if npc.health != 0 || npc.corpse == nil {
    130 		t.Fatal("fixture did not complete its killing swing")
    131 	}
    132 	world.advanceCorpseExpiry(npc.corpse.expiresAt)
    133 	if err := world.flushWorld(); err != nil {
    134 		t.Fatal(err)
    135 	}
    136 	cold := reloadMissionEntranceWorld(t)
    137 
    138 	if err := cold.db.QueryRow("SELECT count(*),sum(killed) FROM mission_encounters WHERE mission_id=?", player.missions[0].id).Scan(&initial, &dead); err != nil {
    139 		t.Fatal(err)
    140 	}
    141 	var remaining int
    142 	if err := cold.db.QueryRow("SELECT count(*) FROM characters WHERE id=?", npc.id).Scan(&remaining); err != nil {
    143 		t.Fatal(err)
    144 	}
    145 	if initial != 5 || dead != 1 || remaining != 0 || cold.characters[npc.id] != nil || cold.characters[player.id].experience != 100 {
    146 		t.Fatal("corpse cleanup lost the mission kill or recreated its enemy", initial, dead, remaining)
    147 	}
    148 	for _, packet := range output.packets {
    149 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5f2e1b19 {
    150 			t.Fatal("recording a kill completed the objective")
    151 		}
    152 	}
    153 }
    154 
    155 func TestMissionEncounterProgressCountsADeathOnce(t *testing.T) {
    156 	world, client, _ := enteredMissionEncounterWorld(t)
    157 	player, npc := world.characters[1], world.characters[2000003]
    158 	player.position = [3]float32{15, .011, 27}
    159 	npc.health = 1
    160 	world.combatRoll = func(uint32) uint32 { return 0 }
    161 	if _, err := world.db.Exec("CREATE TABLE progress_writes (count INTEGER); INSERT INTO progress_writes VALUES(0); CREATE TRIGGER count_progress AFTER UPDATE ON mission_encounters BEGIN UPDATE progress_writes SET count=count+1; END"); err != nil {
    162 		t.Fatal(err)
    163 	}
    164 
    165 	request := attackPacket(player.id, npc.id)[20:]
    166 	if err := world.handleCombat(client, request); err != nil {
    167 		t.Fatal(err)
    168 	}
    169 	at := player.nextAttack
    170 	world.advanceCombat(at)
    171 	if err := world.handleCombat(client, request); err != nil {
    172 		t.Fatal(err)
    173 	}
    174 	world.advanceCombat(at.Add(time.Minute))
    175 	if err := world.flushWorld(); err != nil {
    176 		t.Fatal(err)
    177 	}
    178 
    179 	var killed int
    180 	if err := world.db.QueryRow("SELECT sum(killed) FROM mission_encounters WHERE mission_id=?", player.missions[0].id).Scan(&killed); err != nil {
    181 		t.Fatal(err)
    182 	}
    183 	var writes int
    184 	if err := world.db.QueryRow("SELECT count FROM progress_writes").Scan(&writes); err != nil {
    185 		t.Fatal(err)
    186 	}
    187 	if killed != 1 || writes != 1 || player.experience != 100 || npc.health != 0 {
    188 		t.Fatal("repeat attack or checkpoint counted a second mission kill", killed, player.experience)
    189 	}
    190 }
    191 
    192 func TestMissionEncounterProgressAcceptanceFailureRollsBackAndRetries(t *testing.T) {
    193 	for _, fault := range []struct{ name, sql string }{
    194 		{"abort", "CREATE TRIGGER fail_population BEFORE INSERT ON mission_encounters BEGIN SELECT RAISE(FAIL,'population'); END"},
    195 		{"ignore", "CREATE TRIGGER fail_population BEFORE INSERT ON mission_encounters BEGIN SELECT RAISE(IGNORE); END"},
    196 		{"delete", "CREATE TRIGGER fail_population AFTER INSERT ON mission_encounters BEGIN DELETE FROM mission_encounters WHERE npc_id=NEW.npc_id; END"},
    197 		{"rewrite", "CREATE TRIGGER fail_population AFTER INSERT ON mission_encounters BEGIN UPDATE mission_encounters SET level=level+1 WHERE npc_id=NEW.npc_id; END"},
    198 		{"commit", "CREATE TABLE pending_population (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_population AFTER INSERT ON mission_encounters BEGIN INSERT INTO pending_population VALUES(999999); END"},
    199 	} {
    200 		t.Run(fault.name, func(t *testing.T) {
    201 			world, output := newMissionEncounterOfferWorld(t)
    202 			client := world.clients[1]
    203 			request := missionAcceptanceRequest(*client.missionOffer)
    204 			beforeItem, beforeNPC := world.nextItem, world.nextNPC
    205 			if _, err := world.db.Exec(fault.sql); err != nil {
    206 				t.Fatal(err)
    207 			}
    208 
    209 			err := world.handleMissionAcceptance(client, request)
    210 			cold := reloadMissionEntranceWorld(t)
    211 
    212 			var count int
    213 			if failure := cold.db.QueryRow("SELECT count(*) FROM mission_encounters").Scan(&count); failure != nil {
    214 				t.Fatal(failure)
    215 			}
    216 			if err == nil || count != 0 || client.missionOffer == nil || len(world.characters[1].missions) != 0 || len(cold.missionExpiry) != 0 ||
    217 				world.nextItem != beforeItem || world.nextNPC != beforeNPC || len(world.characters) != 2 || len(cold.characters) != 2 || len(output.packets) != 0 {
    218 				t.Fatal("failed population write leaked an assignment, actors, identities or success", count, err)
    219 			}
    220 
    221 			// The same offer can commit once after the fault is removed.
    222 			if _, err := world.db.Exec("DROP TRIGGER fail_population"); err != nil {
    223 				t.Fatal(err)
    224 			}
    225 			if err := world.handleMissionAcceptance(client, request); err != nil {
    226 				t.Fatal(err)
    227 			}
    228 			if err := world.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE killed=0 AND level=1").Scan(&count); err != nil {
    229 				t.Fatal(err)
    230 			}
    231 			if count != 5 || world.nextItem != beforeItem+2 || world.nextNPC != beforeNPC+10 || len(world.characters) != 7 {
    232 				t.Fatal("acceptance retry lost or repeated population identities", count, world.nextItem, world.nextNPC)
    233 			}
    234 		})
    235 	}
    236 }
    237 
    238 func TestMissionEncounterProgressIsolatesWorldsAndRetiresWithItsAssignment(t *testing.T) {
    239 	for _, reason := range []string{"abandon", "expire"} {
    240 		t.Run(reason, func(t *testing.T) {
    241 			world, _ := newMissionBoundaryWorld(t)
    242 			npc := world.characters[2000001]
    243 			npc.health = 1
    244 			world.combatRoll = func(uint32) uint32 { return 0 }
    245 			first, second := world.characters[1].missions[0], world.characters[2].missions[0]
    246 
    247 			if err := world.handleCombat(world.clients[3], attackPacket(3, npc.id)[20:]); err != nil {
    248 				t.Fatal(err)
    249 			}
    250 			world.advanceCombat(world.characters[3].nextAttack)
    251 
    252 			for _, expected := range []struct {
    253 				mission uint32
    254 				kills   int
    255 			}{{first.id, 1}, {second.id, 0}} {
    256 				var killed int
    257 				if err := world.db.QueryRow("SELECT sum(killed) FROM mission_encounters WHERE mission_id=?", expected.mission).Scan(&killed); err != nil {
    258 					t.Fatal(err)
    259 				}
    260 				if killed != expected.kills {
    261 					t.Fatal("guest kill counted toward another private world", expected.mission, killed)
    262 				}
    263 			}
    264 			if reason == "abandon" {
    265 				if err := world.handleMissionRemoval(world.clients[1], missionRemovalPacket(1, first.id)[20:]); err != nil {
    266 					t.Fatal(err)
    267 				}
    268 			} else {
    269 				world.advanceMissions(first.expiresAt)
    270 			}
    271 			var firstRows, secondRows int
    272 			if err := world.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE mission_id=?", first.id).Scan(&firstRows); err != nil {
    273 				t.Fatal(err)
    274 			}
    275 			if err := world.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE mission_id=?", second.id).Scan(&secondRows); err != nil {
    276 				t.Fatal(err)
    277 			}
    278 			if firstRows != 0 || secondRows != 5 {
    279 				t.Fatal("retirement retained old progress or erased another world's population", firstRows, secondRows)
    280 			}
    281 		})
    282 	}
    283 }
    284 
    285 func TestMissionEncounterProgressSharesAllKillingActions(t *testing.T) {
    286 	for _, attack := range []string{"unarmed", "axe", "pistol", "fast", "fling", "nano", "dot"} {
    287 		t.Run(attack, func(t *testing.T) {
    288 			world, _, npc, _ := newMissionProgressCombatWorld(t, attack)
    289 
    290 			if err := missionProgressAttack(world, attack); err != nil {
    291 				t.Fatal(err)
    292 			}
    293 			cold := reloadMissionEntranceWorld(t)
    294 
    295 			var count, killed int
    296 			if err := cold.db.QueryRow("SELECT count(*),sum(killed) FROM mission_encounters").Scan(&count, &killed); err != nil {
    297 				t.Fatal(err)
    298 			}
    299 			if count != 5 || killed != 1 || npc.health != 0 || npc.corpse == nil || cold.characters[1].experience != 100 {
    300 				t.Fatal("killing action lost population, kill progress or its ordinary reward", count, killed, npc.health)
    301 			}
    302 		})
    303 	}
    304 }
    305 
    306 func TestMissionEncounterProgressFailureRollsBackTheDeathAndRetries(t *testing.T) {
    307 	for _, attack := range []string{"unarmed", "pistol", "fast", "fling", "nano"} {
    308 		for _, fault := range []struct{ name, sql string }{
    309 			{"abort", "CREATE TRIGGER fail_progress BEFORE UPDATE ON mission_encounters BEGIN SELECT RAISE(FAIL,'progress'); END"},
    310 			{"ignore", "CREATE TRIGGER fail_progress BEFORE UPDATE ON mission_encounters BEGIN SELECT RAISE(IGNORE); END"},
    311 			{"undo", "CREATE TRIGGER fail_progress AFTER UPDATE ON mission_encounters BEGIN UPDATE mission_encounters SET killed=0 WHERE npc_id=NEW.npc_id; END"},
    312 			{"rewrite", "CREATE TRIGGER fail_progress AFTER UPDATE ON mission_encounters BEGIN UPDATE mission_encounters SET level=level+1 WHERE npc_id=NEW.npc_id; END"},
    313 			{"loot", "CREATE TRIGGER fail_progress BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'loot'); END"},
    314 			{"XP", "CREATE TRIGGER fail_progress BEFORE UPDATE OF experience ON characters WHEN NEW.id=1 AND NEW.experience>OLD.experience BEGIN SELECT RAISE(FAIL,'XP'); END"},
    315 			{"commit", "CREATE TABLE pending_progress (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_progress AFTER UPDATE ON mission_encounters BEGIN INSERT INTO pending_progress VALUES(999999); END"},
    316 		} {
    317 			t.Run(attack+"/"+fault.name, func(t *testing.T) {
    318 				world, _, npc, output := newMissionProgressCombatWorld(t, attack)
    319 				before := world.nextItem
    320 				if _, err := world.db.Exec(fault.sql); err != nil {
    321 					t.Fatal(err)
    322 				}
    323 
    324 				err := missionProgressAttack(world, attack)
    325 				cold := reloadMissionEntranceWorld(t)
    326 
    327 				if (attack == "fast" || attack == "fling") && err == nil {
    328 					t.Fatal("failed special returned success")
    329 				}
    330 				var killed int
    331 				if err := cold.db.QueryRow("SELECT sum(killed) FROM mission_encounters").Scan(&killed); err != nil {
    332 					t.Fatal(err)
    333 				}
    334 				if killed != 0 || npc.health != 1 || npc.corpse != nil || cold.characters[npc.id].health != 1 || world.characters[1].experience != 0 ||
    335 					cold.characters[1].experience != 0 || world.nextItem != before || cold.nextItem != before {
    336 					t.Fatal("failed outcome leaked kill progress, death, XP or an item allocation", killed, npc.health)
    337 				}
    338 				if attack == "pistol" || attack == "fling" {
    339 					for _, player := range []*character{world.characters[1], cold.characters[1]} {
    340 						if player.inventory.Items[len(player.inventory.Items)-1].Ammo != 3 {
    341 							t.Fatal("failed progress consumed a round")
    342 						}
    343 					}
    344 				}
    345 				if attack == "nano" && (world.characters[1].nano != 30 || world.characters[1].nanoReserved != 0 || cold.characters[1].nano != 30 || cold.characters[1].nanoReserved != 0) {
    346 					t.Fatal("failed progress retained a nano cost or reservation")
    347 				}
    348 				for _, packet := range output.packets {
    349 					switch binary.BigEndian.Uint32(packet[16:20]) {
    350 					case 0x46002f16, 0x754f1115, 0x4727213e, 0x4f474e05, 0x7f405a16:
    351 						t.Fatalf("failed outcome published damage, death or XP: %x", packet)
    352 					case 0x5e477770:
    353 						if binary.BigEndian.Uint32(packet[29:33]) == 99 {
    354 							t.Fatal("failed outcome published a death action")
    355 						}
    356 					}
    357 				}
    358 
    359 				// Retry the public action after removing the transactional fault.
    360 				if _, err := world.db.Exec("DROP TRIGGER fail_progress"); err != nil {
    361 					t.Fatal(err)
    362 				}
    363 				if err := missionProgressAttack(world, attack); err != nil {
    364 					t.Fatal(err)
    365 				}
    366 				if err := world.db.QueryRow("SELECT sum(killed) FROM mission_encounters").Scan(&killed); err != nil {
    367 					t.Fatal(err)
    368 				}
    369 				if killed != 1 || npc.health != 0 || world.characters[1].experience != 100 || world.nextItem != before+2 {
    370 					t.Fatal("retry failed to commit one kill and its ordinary reward", killed, npc.health, world.characters[1].experience)
    371 				}
    372 				cold = reloadMissionEntranceWorld(t)
    373 				if attack == "pistol" || attack == "fling" {
    374 					for _, player := range []*character{world.characters[1], cold.characters[1]} {
    375 						if attack == "pistol" && player.inventory.Items[len(player.inventory.Items)-1].Ammo != 2 || attack == "fling" && player.inventory.Items[len(player.inventory.Items)-1].Ammo != 3 {
    376 							t.Fatal("progress retry changed ammunition beyond the killing action's cost")
    377 						}
    378 					}
    379 				}
    380 				if attack == "nano" && (world.characters[1].nano != 21 || world.characters[1].nanoReserved != 0 || cold.characters[1].nano != 21 || cold.characters[1].nanoReserved != 0) {
    381 					t.Fatal("progress retry did not consume exactly one nano cost")
    382 				}
    383 			})
    384 		}
    385 	}
    386 }
    387 
    388 func TestMissionEncounterProgressFailedPeriodicDeathRetriesTheSameTick(t *testing.T) {
    389 	world, client, npc, _ := newMissionProgressCombatWorld(t, "dot")
    390 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 43376})[20:]); err != nil {
    391 		t.Fatal(err)
    392 	}
    393 	if client.nanoCast == nil {
    394 		t.Fatal("fixture did not start the periodic program")
    395 	}
    396 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    397 	if len(npc.activeNanos) != 1 {
    398 		t.Fatal("fixture did not install the periodic effect")
    399 	}
    400 	tick := npc.activeNanos[0].nextTick
    401 	if _, err := world.db.Exec("CREATE TRIGGER fail_tick BEFORE UPDATE ON mission_encounters BEGIN SELECT RAISE(FAIL,'tick'); END"); err != nil {
    402 		t.Fatal(err)
    403 	}
    404 
    405 	world.advanceNanos(tick)
    406 
    407 	var killed int
    408 	if err := world.db.QueryRow("SELECT sum(killed) FROM mission_encounters").Scan(&killed); err != nil {
    409 		t.Fatal(err)
    410 	}
    411 	if killed != 0 || npc.health != 1 || npc.corpse != nil || len(npc.activeNanos) != 1 || npc.activeNanos[0].ticksLeft != 8 || !npc.activeNanos[0].nextTick.Equal(tick) || world.characters[1].experience != 0 {
    412 		t.Fatal("failed periodic death consumed the tick or published its kill", killed, npc.activeNanos)
    413 	}
    414 	if _, err := world.db.Exec("DROP TRIGGER fail_tick"); err != nil {
    415 		t.Fatal(err)
    416 	}
    417 	world.advanceNanos(tick.Add(time.Nanosecond))
    418 	if err := world.db.QueryRow("SELECT sum(killed) FROM mission_encounters").Scan(&killed); err != nil {
    419 		t.Fatal(err)
    420 	}
    421 	if killed != 1 || npc.health != 0 || len(npc.activeNanos) != 0 || npc.corpse == nil || world.characters[1].experience != 100 {
    422 		t.Fatal("retry failed to commit the original lethal tick exactly once", killed, npc.health)
    423 	}
    424 }
    425 
    426 func TestMissionEncounterProgressRetirementFailureKeepsThePopulationForRetry(t *testing.T) {
    427 	for _, operation := range []string{"FAIL,'retirement'", "IGNORE"} {
    428 		t.Run(operation, func(t *testing.T) {
    429 			world, client, _ := enteredMissionEncounterWorld(t)
    430 			mission := world.characters[1].missions[0]
    431 			if _, err := world.db.Exec("CREATE TRIGGER fail_retirement BEFORE DELETE ON mission_encounters BEGIN SELECT RAISE(" + operation + "); END"); err != nil {
    432 				t.Fatal(err)
    433 			}
    434 
    435 			err := world.handleMissionRemoval(client, missionRemovalPacket(1, mission.id)[20:])
    436 			cold := reloadMissionEntranceWorld(t)
    437 
    438 			var count int
    439 			if failure := cold.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE mission_id=?", mission.id).Scan(&count); failure != nil {
    440 				t.Fatal(failure)
    441 			}
    442 			if err == nil || count != 5 || len(world.characters[1].missions) != 1 || len(cold.characters[1].missions) != 1 || len(world.mobileNPCs[mission.world]) != 5 {
    443 				t.Fatal("failed retirement erased progress or the active assignment", count, err)
    444 			}
    445 			if _, err := world.db.Exec("DROP TRIGGER fail_retirement"); err != nil {
    446 				t.Fatal(err)
    447 			}
    448 			if err := world.handleMissionRemoval(client, missionRemovalPacket(1, mission.id)[20:]); err != nil {
    449 				t.Fatal(err)
    450 			}
    451 			if err := world.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE mission_id=?", mission.id).Scan(&count); err != nil {
    452 				t.Fatal(err)
    453 			}
    454 			if count != 0 || len(world.characters[1].missions) != 0 || len(world.mobileNPCs[mission.world]) != 0 {
    455 				t.Fatal("retirement retry retained the population", count)
    456 			}
    457 		})
    458 	}
    459 }
    460 
    461 func TestMissionEncounterProgressDoesNotDependOnReceivingKillXP(t *testing.T) {
    462 	world, _, npc, _ := newMissionProgressCombatWorld(t, "unarmed")
    463 	player := world.characters[1]
    464 	player.level = 200
    465 	if err := world.saveUser(player); err != nil {
    466 		t.Fatal(err)
    467 	}
    468 
    469 	if err := missionProgressAttack(world, "unarmed"); err != nil {
    470 		t.Fatal(err)
    471 	}
    472 	cold := reloadMissionEntranceWorld(t)
    473 
    474 	var killed int
    475 	if err := cold.db.QueryRow("SELECT sum(killed) FROM mission_encounters").Scan(&killed); err != nil {
    476 		t.Fatal(err)
    477 	}
    478 	if killed != 1 || npc.health != 0 || player.experience != 0 || cold.characters[1].experience != 0 || cold.characters[1].level != 200 {
    479 		t.Fatal("raw kill history depended on XP award eligibility", killed, npc.health, player.experience)
    480 	}
    481 }
    482 
    483 func TestMissionEncounterProgressCompletionRetainsHistoryUntilExpiry(t *testing.T) {
    484 	world, client, _, _ := killedMissionEncounterWorld(t)
    485 	player := world.characters[1]
    486 	mission := player.missions[0]
    487 	player.position = world.worldItems[mission.objective].position
    488 	if err := world.handleInventory(client, worldPickupRequest(1, mission.objective)); err != nil {
    489 		t.Fatal(err)
    490 	}
    491 	destination := player.userData()
    492 	destination.Playfield, destination.Position = 3000, gamedata.MissionBooths[3221228472].Position
    493 	if err := world.updateUser(destination); err != nil {
    494 		t.Fatal(err)
    495 	}
    496 	world.clients[1], client.ready, client.loading = client, true, false
    497 
    498 	if err := world.handleMissionReturn(client, missionReturnRequest(1)); err != nil {
    499 		t.Fatal(err)
    500 	}
    501 	cold := reloadMissionEntranceWorld(t)
    502 
    503 	var count, killed int
    504 	if err := cold.db.QueryRow("SELECT count(*),sum(killed) FROM mission_encounters WHERE mission_id=?", mission.id).Scan(&count, &killed); err != nil {
    505 		t.Fatal(err)
    506 	}
    507 	if count != 5 || killed != 1 || !cold.missionExpiry[0].completed || cold.characters[1].experience != 100 || cold.characters[1].inventory.Credits != 24 {
    508 		t.Fatal("objective completion lost kill history or changed its reward policy", count, killed)
    509 	}
    510 	world.advanceMissions(mission.expiresAt)
    511 	if err := world.db.QueryRow("SELECT count(*) FROM mission_encounters WHERE mission_id=?", mission.id).Scan(&count); err != nil {
    512 		t.Fatal(err)
    513 	}
    514 	if count != 0 {
    515 		t.Fatal("completed instance expiry retained its population history", count)
    516 	}
    517 }