aorecomp

Anarchy Online classic in the browser

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


server/world_missions_test.go (9633B)


      1 package main
      2 
      3 import (
      4 	"reflect"
      5 	"slices"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 type zoneMissionSnapshot struct {
     11 	npcs, items, physical []uint32
     12 	journal               []acceptedMission
     13 	inventory             []ownedItem
     14 	saved                 int
     15 	offer                 bool
     16 }
     17 
     18 type zoneMissionEntry struct{ client *worldClient }
     19 
     20 func (command zoneMissionEntry) execute(state *server) error {
     21 	player := state.characters[command.client.id]
     22 	player.geometry = state.geometry
     23 	player.position, player.rotation = [3]float32{89.30216, 115.75, 152.5}, [4]float32{0, 1, 0, 0}
     24 	return state.handleMovement(command.client, playerMovementRequest(player, 1))
     25 }
     26 
     27 func (query *zoneMissionSnapshot) execute(state *server) error {
     28 	query.npcs, query.items, query.physical = nil, nil, nil
     29 	for id, actor := range state.characters {
     30 		if actor.npc {
     31 			query.npcs = append(query.npcs, id)
     32 		}
     33 	}
     34 	for id := range state.items {
     35 		query.items = append(query.items, id)
     36 	}
     37 	for id := range state.worldItems {
     38 		query.physical = append(query.physical, id)
     39 	}
     40 	slices.Sort(query.npcs)
     41 	slices.Sort(query.items)
     42 	slices.Sort(query.physical)
     43 	query.saved = len(state.savedNPCs)
     44 	query.journal, query.inventory = nil, nil
     45 	if player := state.characters[1]; player != nil {
     46 		for _, mission := range player.missions {
     47 			query.journal = append(query.journal, *cloneStoredMission(&mission))
     48 		}
     49 		query.inventory = slices.Clone(player.inventory.Items)
     50 	}
     51 	query.offer = state.clients[1] != nil && state.clients[1].missionOffer != nil
     52 	return nil
     53 }
     54 
     55 func TestWorldRuntimeMissionAcceptanceCreatesAnIndependentCompleteZone(t *testing.T) {
     56 	fixture, _ := newMissionEncounterOfferWorld(t)
     57 	client := fixture.clients[1]
     58 	request := missionAcceptanceRequest(*client.missionOffer)
     59 	world := newRoutedTestWorld(t, fixture)
     60 	defer world.stop()
     61 	source, _ := world.owner(1)
     62 
     63 	if err := source.call(zonePacket{client, request}); err != nil {
     64 		t.Fatal(err)
     65 	}
     66 	missions := world.queryMissions(missionLookup{world: 65536})
     67 	if len(missions) != 1 || missions[0].id != 2 || missions[0].objective != 3 {
     68 		t.Fatal("acceptance did not publish its complete committed contract", missions)
     69 	}
     70 	destination, err := world.owner(2000000)
     71 	if err != nil || destination == source {
     72 		t.Fatal("mission population remained in the accepting zone", err)
     73 	}
     74 	for _, id := range []uint32{2000001, 2000002, 2000003, 2000004} {
     75 		owner, err := world.owner(id)
     76 		if err != nil || owner != destination {
     77 			t.Fatal("mission population has inconsistent owners", id, err)
     78 		}
     79 	}
     80 	before, mission := &zoneMissionSnapshot{}, &zoneMissionSnapshot{}
     81 	if err := source.call(before); err != nil {
     82 		t.Fatal(err)
     83 	}
     84 	if err := destination.call(mission); err != nil {
     85 		t.Fatal(err)
     86 	}
     87 	if len(before.npcs) != 0 || before.saved != 0 || !slices.Equal(before.items, []uint32{1, 2}) || len(before.physical) != 0 ||
     88 		!slices.Equal(mission.npcs, []uint32{2000000, 2000001, 2000002, 2000003, 2000004}) || mission.saved != 5 || !slices.Equal(mission.items, []uint32{3}) || !slices.Equal(mission.physical, []uint32{3}) {
     89 		t.Fatal("mission entities or checkpoint bases have overlapping ownership", before, mission)
     90 	}
     91 	var objective, placement uint32
     92 	if err := fixture.db.QueryRow("SELECT objective_id,item_id FROM missions JOIN item_locations ON mission_id=missions.id WHERE missions.id=2").Scan(&objective, &placement); err != nil || objective != 3 || placement != 3 {
     93 		t.Fatal("mission publication preceded durable objective placement", objective, placement, err)
     94 	}
     95 
     96 	// An arrival joins the existing mission owner; it cannot recreate the objective.
     97 	if err := source.call(zoneMissionEntry{client}); err != nil {
     98 		t.Fatal(err)
     99 	}
    100 	arrived, err := world.owner(1)
    101 	if err != nil || arrived != destination {
    102 		t.Fatal("player did not join the mission zone", err)
    103 	}
    104 	if err := destination.call(mission); err != nil {
    105 		t.Fatal(err)
    106 	}
    107 	if !slices.Equal(mission.items, []uint32{1, 2, 3}) || !slices.Equal(mission.physical, []uint32{3}) || len(mission.journal) != 1 || mission.journal[0].objective != 3 {
    108 		t.Fatal("arrival lost or duplicated mission state", mission)
    109 	}
    110 }
    111 
    112 func TestWorldRuntimeMissionFailurePublishesNothingAndRetries(t *testing.T) {
    113 	for _, statement := range []string{
    114 		"CREATE TRIGGER fail_mission BEFORE INSERT ON npcs WHEN NEW.id>=2000000 BEGIN SELECT RAISE(ABORT,'population'); END",
    115 		"CREATE TRIGGER fail_mission BEFORE INSERT ON item_locations WHEN NEW.mission_id IS NOT NULL BEGIN SELECT RAISE(ABORT,'objective'); END",
    116 		"CREATE TRIGGER fail_mission AFTER UPDATE OF last_item_id ON world BEGIN UPDATE missions SET objective_id=0; END",
    117 		"CREATE TABLE pending_mission (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_mission AFTER INSERT ON missions BEGIN INSERT INTO pending_mission VALUES(999999); END",
    118 	} {
    119 		t.Run(statement, func(t *testing.T) {
    120 			fixture, _ := newMissionEncounterOfferWorld(t)
    121 			client := fixture.clients[1]
    122 			request := missionAcceptanceRequest(*client.missionOffer)
    123 			if _, err := fixture.db.Exec(statement); err != nil {
    124 				t.Fatal(err)
    125 			}
    126 			world := newRoutedTestWorld(t, fixture)
    127 			defer world.stop()
    128 			source, _ := world.owner(1)
    129 
    130 			if err := source.call(zonePacket{client, request}); err == nil {
    131 				t.Fatal("failed acceptance succeeded")
    132 			}
    133 			query := &zoneMissionSnapshot{}
    134 			if err := source.call(query); err != nil {
    135 				t.Fatal(err)
    136 			}
    137 			if len(world.queryMissions(missionLookup{world: 65536})) != 0 || len(query.journal) != 0 || !query.offer || !slices.Equal(query.items, []uint32{1}) {
    138 				t.Fatal("failed acceptance published a mission or consumed its offer", query)
    139 			}
    140 			if _, err := world.owner(2000000); err == nil {
    141 				t.Fatal("failed acceptance installed a population owner")
    142 			}
    143 			var contracts, objectives, actors int
    144 			if err := fixture.db.QueryRow("SELECT (SELECT count(*) FROM missions), (SELECT count(*) FROM item_locations WHERE mission_id IS NOT NULL), (SELECT count(*) FROM characters WHERE id>=2000000)").Scan(&contracts, &objectives, &actors); err != nil || contracts != 0 || objectives != 0 || actors != 0 {
    145 				t.Fatal("failed transaction retained part of the mission", contracts, objectives, actors, err)
    146 			}
    147 
    148 			if _, err := fixture.db.Exec("DROP TRIGGER fail_mission"); err != nil {
    149 				t.Fatal(err)
    150 			}
    151 			if err := source.call(zonePacket{client, request}); err != nil {
    152 				t.Fatal(err)
    153 			}
    154 			missions := world.queryMissions(missionLookup{world: 65536})
    155 			if len(missions) != 1 || missions[0].id != 4 || missions[0].objective != 5 {
    156 				t.Fatal("retry did not publish exactly one complete mission", missions)
    157 			}
    158 			if _, err := world.owner(2000005); err != nil {
    159 				t.Fatal("retry population has no owner", err)
    160 			}
    161 		})
    162 	}
    163 }
    164 
    165 func TestWorldRuntimeMissionCatalogSupportsKeysFromAnotherZone(t *testing.T) {
    166 	fixture, _ := newMissionKeyCopyWorld(t)
    167 	fixture.characters[1].playfield = 540
    168 	if err := fixture.savePosition(fixture.characters[1]); err != nil {
    169 		t.Fatal(err)
    170 	}
    171 	client := fixture.clients[1]
    172 	world := newRoutedTestWorld(t, fixture)
    173 	defer world.stop()
    174 	zone, _ := world.owner(1)
    175 	other, _ := world.owner(1000000)
    176 	entered, release := make(chan struct{}), make(chan struct{})
    177 	defer close(release)
    178 	go other.call(zoneBarrier{entered: entered, release: release})
    179 	<-entered
    180 
    181 	// Directory queries and key copies cannot depend on another zone's loop.
    182 	completed := make(chan error, 1)
    183 	go func() { completed <- zone.call(zonePacket{client, combinationRequest(1, 66, 65)}) }()
    184 	select {
    185 	case err := <-completed:
    186 		if err != nil {
    187 			t.Fatal(err)
    188 		}
    189 	case <-time.After(5 * time.Second):
    190 		t.Fatal("key copy waited for another zone")
    191 	}
    192 	var bindings int
    193 	if err := fixture.db.QueryRow("SELECT count(*) FROM mission_keys WHERE mission_id=2").Scan(&bindings); err != nil || bindings != 2 {
    194 		t.Fatal("remote-zone key copy lost its contract binding", bindings, err)
    195 	}
    196 	first := world.queryMissions(missionLookup{id: 2})
    197 	if len(first) != 1 {
    198 		t.Fatal("remote zone lost its mission metadata")
    199 	}
    200 	before := *cloneStoredMission(&first[0])
    201 	first[0].layout[0] ^= 1
    202 	first[0].doors[0].id = 999
    203 	second := world.queryMissions(missionLookup{exterior: 3000})
    204 	if len(second) != 1 || !reflect.DeepEqual(second[0], before) {
    205 		t.Fatal("mission query exposed mutable catalog state")
    206 	}
    207 }
    208 
    209 func TestWorldRuntimeShutdownDrainsMissionCreation(t *testing.T) {
    210 	fixture, _ := newMissionEncounterOfferWorld(t)
    211 	client := fixture.clients[1]
    212 	request := missionAcceptanceRequest(*client.missionOffer)
    213 	world := newRoutedTestWorld(t, fixture)
    214 	source, _ := world.owner(1)
    215 	other, _ := world.owner(1000000)
    216 	entered, release := make(chan struct{}), make(chan struct{}, 1)
    217 	defer close(release)
    218 	finished := make(chan error, 1)
    219 	go func() { finished <- source.call(zoneBarrier{entered, release, zonePacket{client, request}}) }()
    220 	<-entered
    221 	stopped := make(chan struct{})
    222 	go func() { world.stop(); close(stopped) }()
    223 	<-other.quiesced
    224 
    225 	// The admitted transaction may create a new destination during quiescence.
    226 	release <- struct{}{}
    227 	if err := <-finished; err != nil {
    228 		t.Fatal(err)
    229 	}
    230 	select {
    231 	case <-stopped:
    232 	case <-time.After(5 * time.Second):
    233 		t.Fatal("shutdown did not drain the newly created mission zone")
    234 	}
    235 	var contracts, population, objectives int
    236 	if err := fixture.db.QueryRow("SELECT (SELECT count(*) FROM missions), (SELECT count(*) FROM mission_encounters), (SELECT count(*) FROM item_locations WHERE mission_id IS NOT NULL)").Scan(&contracts, &population, &objectives); err != nil || contracts != 1 || population != 5 || objectives != 1 {
    237 		t.Fatal("shutdown lost an admitted mission transaction", contracts, population, objectives, err)
    238 	}
    239 }