aorecomp

Anarchy Online classic in the browser

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


commit 978d6dd5fb14ae079ca8699f1cf498cd504bd90e
parent 8a6512c8fb0ec6a0ac281a107603b9b8567a742e
Author: anon <anon@example.com>
Date:   Sat,  7 Nov 2026 03:46:02 +0000

Own routed mission populations and committed metadata independently of player zones

Diffstat:
MDEVELOPERS.md | 10++++++++++
Mserver/item_database.go | 17+++++++++--------
Mserver/item_store.go | 20++++++++++++++++----
Mserver/mission_entrance.go | 15+++++++++++++--
Mserver/mission_key_copy.go | 6+++++-
Mserver/mission_retirement.go | 3+++
Mserver/missions.go | 13++++++++++++-
Mserver/world_items.go | 6+++++-
Aserver/world_missions.go | 122+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/world_missions_test.go | 239+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/world_runtime.go | 62+++++++++++++++++++++++++++++++++++++++++++++++++++-----------
11 files changed, 485 insertions(+), 28 deletions(-)

diff --git a/DEVELOPERS.md b/DEVELOPERS.md @@ -123,6 +123,16 @@ zone workers. External producers must stop before that shutdown. These are component boundaries: production startup, shared social/mission ownership and final checkpoint coordination remain to be connected. +The directory also owns the committed mission catalog, with detached lookups by +contract, private world and exterior playfield. Routed acceptance commits the +key, objective, doors and NPCs together, then transfers the physical population +and its checkpoint records into a new zone before publishing success. The +player's inventory and journal stay with the player. Normal keyed entry joins +that existing zone; metadata queries and key copying do not borrow another +zone's state. Accepted mission creation drains during shutdown. Cross-zone +retirement, expiry and credential revocation still require lifecycle coordination +before the production cutover. + NPC lifetimes register and retire their directory entries from the owning zone. Cross-zone messages retain acceptance order for each entity and follow it through later transfers. The directory dispatches them without waiting for destination diff --git a/server/item_database.go b/server/item_database.go @@ -338,6 +338,12 @@ func (write itemWrite) persist(tx *sql.Tx) error { return fmt.Errorf("remove item %d: %w", id, err) } } + // Mission placements reference their contract in this same transaction. + if write.mission != nil { + if err := insertMission(tx, write.mission); err != nil { + return err + } + } for id, placement := range write.addedPlacements { var player, bank, backpack, corpse, mission, resource, reclaim any if placement.Player != 0 { @@ -394,11 +400,6 @@ func (write itemWrite) persist(tx *sql.Tx) error { } } } - if write.mission != nil { - if err := insertMission(tx, write.mission); err != nil { - return err - } - } // New entities commit with the inventory operation that creates them. for index := range write.spawned { npc := &write.spawned[index] @@ -492,9 +493,9 @@ func (write itemWrite) persist(tx *sql.Tx) error { if err := tx.QueryRow("SELECT id FROM world WHERE id=1 AND last_playfield_id=?", write.mission.world).Scan(&savedID); err != nil { return fmt.Errorf("mission world allocation was not saved: %w", err) } - if err := tx.QueryRow("SELECT id FROM missions WHERE id=? AND reward_item=? AND reward_quality=?", write.mission.id, - write.mission.offer.definition.RewardItem, write.mission.offer.definition.RewardQuality).Scan(&savedID); err != nil { - return fmt.Errorf("mission item reward selection was not saved: %w", err) + if err := tx.QueryRow("SELECT id FROM missions WHERE id=? AND objective_id=? AND reward_item=? AND reward_quality=?", write.mission.id, + write.mission.objective, write.mission.offer.definition.RewardItem, write.mission.offer.definition.RewardQuality).Scan(&savedID); err != nil { + return fmt.Errorf("mission objective or item reward selection was not saved: %w", err) } } if write.updatedMission != nil { diff --git a/server/item_store.go b/server/item_store.go @@ -275,6 +275,10 @@ func (server *server) writeItemStore_locked(store itemStore) error { server.depletedResources = store.DepletedResources server.nextItem = store.LastID for _, npc := range store.spawnedNPCs { + server.nextNPC = max(server.nextNPC, npc.id+1) + if store.mission != nil && server.world != nil { + continue + } server.addNPC_locked(npc) if npc.pet != nil { server.petExpiry = append(server.petExpiry, npc) @@ -284,7 +288,6 @@ func (server *server) writeItemStore_locked(store itemStore) error { } server.mobileNPCs[npc.playfield] = append(server.mobileNPCs[npc.playfield], npc) } - server.nextNPC = max(server.nextNPC, npc.id+1) } if store.updatedMission != nil { mission := store.updatedMission @@ -293,11 +296,17 @@ func (server *server) writeItemStore_locked(store itemStore) error { server.missionExpiry[index] = *mission } } - for index := range server.characters[mission.owner].missions { - if server.characters[mission.owner].missions[index].id == mission.id { - server.characters[mission.owner].missions[index] = *mission + if player := server.characters[mission.owner]; player != nil { + for index := range player.missions { + if player.missions[index].id == mission.id { + player.missions[index] = *mission + } } } + if server.world != nil { + server.world.publishMission(*mission, false) + server.world.send(mission.owner, missionJournalUpdate(*cloneStoredMission(mission))) + } } for id, items := range store.Corpses { contents := slices.Clone(items) @@ -307,6 +316,9 @@ func (server *server) writeItemStore_locked(store itemStore) error { } server.characters[id].corpse.items = contents } + if store.mission != nil && server.world != nil { + return server.installMission_locked(*store.mission, store.spawnedNPCs) + } return nil } diff --git a/server/mission_entrance.go b/server/mission_entrance.go @@ -47,6 +47,13 @@ func (database *gameDatabase) missionEntrance(playfield, identity uint32) (zoneA } func (server *server) missionWorld_locked(identity uint32) *acceptedMission { + if server.world != nil { + missions := server.world.queryMissions(missionLookup{world: identity}) + if len(missions) == 0 { + return nil + } + return &missions[0] + } for index := range server.missionExpiry { if server.missionExpiry[index].world == identity { return &server.missionExpiry[index] @@ -136,8 +143,12 @@ func (server *server) missionDestination_locked(player *character, client *world // Only query credentials when a live assignment's exterior is nearby. var candidates []*acceptedMission now := time.Now() - for index := range server.missionExpiry { - mission := &server.missionExpiry[index] + missions := server.missionExpiry + if server.world != nil { + missions = server.world.queryMissions(missionLookup{exterior: player.playfield}) + } + for index := range missions { + mission := &missions[index] if !mission.completed && mission.offer.definition.Playfield == player.playfield && now.Before(mission.expiresAt) && near(mission.offer.definition.Position) { candidates = append(candidates, mission) } diff --git a/server/mission_key_copy.go b/server/mission_key_copy.go @@ -28,7 +28,11 @@ func (server *server) copyMissionKey_locked(client *worldClient, tool, original } return err } - if !slices.ContainsFunc(server.missionExpiry, func(mission acceptedMission) bool { + missions := server.missionExpiry + if server.world != nil { + missions = server.world.queryMissions(missionLookup{id: missionID}) + } + if !slices.ContainsFunc(missions, func(mission acceptedMission) bool { return mission.id == missionID && !mission.completed && time.Now().Before(mission.expiresAt) }) { return refuse() diff --git a/server/mission_retirement.go b/server/mission_retirement.go @@ -322,6 +322,9 @@ func (server *server) endMission_locked(mission acceptedMission, completed bool, } if !completed { server.missionExpiry = slices.DeleteFunc(server.missionExpiry, func(other acceptedMission) bool { return other.id == mission.id }) + if server.world != nil { + server.world.publishMission(mission, true) + } } for client, packets := range changes { if client.ready && !client.loading { diff --git a/server/missions.go b/server/missions.go @@ -152,6 +152,15 @@ func (server *server) handleMissionAcceptance(client *worldClient, body []byte) if err != nil { return err } + // A routed mission starts with all physical objects in its destination zone. + if server.world != nil { + if err := server.stageMissionObjective_locked(&store, &mission); err != nil { + return err + } + mission = *store.updatedMission + store.updatedMission = nil + inventory.LastID = store.LastID + } // Retain selected rewards and concrete world objects with the accepted contract. mission.offer.definition.RewardItems = nil mission.offer.definition.Encounters = nil @@ -161,7 +170,9 @@ func (server *server) handleMissionAcceptance(client *worldClient, body []byte) } player.inventory = inventory player.missions = append(player.missions, mission) - server.missionExpiry = append(server.missionExpiry, mission) + if server.world == nil { + server.missionExpiry = append(server.missionExpiry, mission) + } client.missionOffer = nil // Add the native template proxy only after the key and journal are durable. diff --git a/server/world_items.go b/server/world_items.go @@ -87,7 +87,11 @@ func (server *server) stageMissionObjective_locked(store *itemStore, mission *ac if mission.objective != 0 { return nil } - scene, err := server.prepareMissionWorld_locked(mission) + layout, err := decodeMissionLayout(mission.layout) + if err != nil { + return err + } + scene, err := server.geometry.missionScene(layout) if err != nil { return err } diff --git a/server/world_missions.go b/server/world_missions.go @@ -0,0 +1,122 @@ +package main + +import "net" + +// The directory owns committed mission metadata. Queries return detached values; +// the mission zone owns its NPCs and physical items independently of the journal. +type missionCatalog struct { + byID map[uint32]acceptedMission + byWorld map[uint32]uint32 + byExterior map[uint32]map[uint32]bool +} + +func newMissionCatalog(missions []acceptedMission) *missionCatalog { + catalog := &missionCatalog{byID: make(map[uint32]acceptedMission), byWorld: make(map[uint32]uint32), byExterior: make(map[uint32]map[uint32]bool)} + for _, mission := range missions { + catalog.publish(*cloneStoredMission(&mission)) + } + return catalog +} + +func (catalog *missionCatalog) publish(mission acceptedMission) { + catalog.byID[mission.id] = mission + catalog.byWorld[mission.world] = mission.id + exterior := mission.offer.definition.Playfield + if catalog.byExterior[exterior] == nil { + catalog.byExterior[exterior] = make(map[uint32]bool) + } + catalog.byExterior[exterior][mission.id] = true +} + +type missionLookup struct { + id, world, exterior uint32 + reply chan []acceptedMission +} + +func (catalog *missionCatalog) lookup(query missionLookup) []acceptedMission { + var result []acceptedMission + if query.exterior != 0 { + for id := range catalog.byExterior[query.exterior] { + mission := catalog.byID[id] + result = append(result, *cloneStoredMission(&mission)) + } + } else { + id := query.id + if query.world != 0 { + id = catalog.byWorld[query.world] + } + if mission, exists := catalog.byID[id]; exists { + result = append(result, *cloneStoredMission(&mission)) + } + } + return result +} + +type missionChange struct { + mission acceptedMission + removed bool + done chan struct{} +} + +type missionCreation struct { + mission acceptedMission + state *server + reply chan error +} + +func (world *worldRuntime) queryMissions(query missionLookup) []acceptedMission { + query.reply = make(chan []acceptedMission, 1) + world.missionLookups <- query + return <-query.reply +} + +func (world *worldRuntime) publishMission(mission acceptedMission, removed bool) { + change := missionChange{mission: *cloneStoredMission(&mission), removed: removed, done: make(chan struct{})} + world.missionChanges <- change + <-change.done +} + +func (world *worldRuntime) createMission(mission acceptedMission, state *server) error { + request := missionCreation{mission: *cloneStoredMission(&mission), state: state, reply: make(chan error, 1)} + select { + case world.missionCreations <- request: + return <-request.reply + case <-world.done: + return net.ErrClosed + } +} + +func (server *server) installMission_locked(mission acceptedMission, population []*character) error { + if _, err := server.prepareMissionWorld_locked(&mission); err != nil { + return err + } + state := server.emptyZone() + state.playfield = mission.world + state.savedNPCs = make(map[uint32]savedNPC) + for _, npc := range population { + state.characters[npc.id] = npc + state.mobileNPCs[mission.world] = append(state.mobileNPCs[mission.world], npc) + state.savedNPCs[npc.id] = server.savedNPCs[npc.id] + delete(server.savedNPCs, npc.id) + delete(server.activeNPCs, npc.id) + } + id := mission.objective + state.worldItems[id] = server.worldItems[id] + state.items[id], state.itemPlacements[id] = server.items[id], server.itemPlacements[id] + delete(server.worldItems, id) + delete(server.items, id) + delete(server.itemPlacements, id) + return server.world.createMission(mission, state) +} + +type missionJournalUpdate acceptedMission + +func (command missionJournalUpdate) execute(state *server) error { + player := state.characters[command.owner] + for index := range player.missions { + if player.missions[index].id == command.id { + player.missions[index] = acceptedMission(command) + } + } + return nil +} diff --git a/server/world_missions_test.go b/server/world_missions_test.go @@ -0,0 +1,239 @@ +package main + +import ( + "reflect" + "slices" + "testing" + "time" +) + +type zoneMissionSnapshot struct { + npcs, items, physical []uint32 + journal []acceptedMission + inventory []ownedItem + saved int + offer bool +} + +type zoneMissionEntry struct{ client *worldClient } + +func (command zoneMissionEntry) execute(state *server) error { + player := state.characters[command.client.id] + player.geometry = state.geometry + player.position, player.rotation = [3]float32{89.30216, 115.75, 152.5}, [4]float32{0, 1, 0, 0} + return state.handleMovement(command.client, playerMovementRequest(player, 1)) +} + +func (query *zoneMissionSnapshot) execute(state *server) error { + query.npcs, query.items, query.physical = nil, nil, nil + for id, actor := range state.characters { + if actor.npc { + query.npcs = append(query.npcs, id) + } + } + for id := range state.items { + query.items = append(query.items, id) + } + for id := range state.worldItems { + query.physical = append(query.physical, id) + } + slices.Sort(query.npcs) + slices.Sort(query.items) + slices.Sort(query.physical) + query.saved = len(state.savedNPCs) + query.journal, query.inventory = nil, nil + if player := state.characters[1]; player != nil { + for _, mission := range player.missions { + query.journal = append(query.journal, *cloneStoredMission(&mission)) + } + query.inventory = slices.Clone(player.inventory.Items) + } + query.offer = state.clients[1] != nil && state.clients[1].missionOffer != nil + return nil +} + +func TestWorldRuntimeMissionAcceptanceCreatesAnIndependentCompleteZone(t *testing.T) { + fixture, _ := newMissionEncounterOfferWorld(t) + client := fixture.clients[1] + request := missionAcceptanceRequest(*client.missionOffer) + world := newRoutedTestWorld(t, fixture) + defer world.stop() + source, _ := world.owner(1) + + if err := source.call(zonePacket{client, request}); err != nil { + t.Fatal(err) + } + missions := world.queryMissions(missionLookup{world: 65536}) + if len(missions) != 1 || missions[0].id != 2 || missions[0].objective != 3 { + t.Fatal("acceptance did not publish its complete committed contract", missions) + } + destination, err := world.owner(2000000) + if err != nil || destination == source { + t.Fatal("mission population remained in the accepting zone", err) + } + for _, id := range []uint32{2000001, 2000002, 2000003, 2000004} { + owner, err := world.owner(id) + if err != nil || owner != destination { + t.Fatal("mission population has inconsistent owners", id, err) + } + } + before, mission := &zoneMissionSnapshot{}, &zoneMissionSnapshot{} + if err := source.call(before); err != nil { + t.Fatal(err) + } + if err := destination.call(mission); err != nil { + t.Fatal(err) + } + if len(before.npcs) != 0 || before.saved != 0 || !slices.Equal(before.items, []uint32{1, 2}) || len(before.physical) != 0 || + !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}) { + t.Fatal("mission entities or checkpoint bases have overlapping ownership", before, mission) + } + var objective, placement uint32 + 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 { + t.Fatal("mission publication preceded durable objective placement", objective, placement, err) + } + + // An arrival joins the existing mission owner; it cannot recreate the objective. + if err := source.call(zoneMissionEntry{client}); err != nil { + t.Fatal(err) + } + arrived, err := world.owner(1) + if err != nil || arrived != destination { + t.Fatal("player did not join the mission zone", err) + } + if err := destination.call(mission); err != nil { + t.Fatal(err) + } + if !slices.Equal(mission.items, []uint32{1, 2, 3}) || !slices.Equal(mission.physical, []uint32{3}) || len(mission.journal) != 1 || mission.journal[0].objective != 3 { + t.Fatal("arrival lost or duplicated mission state", mission) + } +} + +func TestWorldRuntimeMissionFailurePublishesNothingAndRetries(t *testing.T) { + for _, statement := range []string{ + "CREATE TRIGGER fail_mission BEFORE INSERT ON npcs WHEN NEW.id>=2000000 BEGIN SELECT RAISE(ABORT,'population'); END", + "CREATE TRIGGER fail_mission BEFORE INSERT ON item_locations WHEN NEW.mission_id IS NOT NULL BEGIN SELECT RAISE(ABORT,'objective'); END", + "CREATE TRIGGER fail_mission AFTER UPDATE OF last_item_id ON world BEGIN UPDATE missions SET objective_id=0; END", + "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", + } { + t.Run(statement, func(t *testing.T) { + fixture, _ := newMissionEncounterOfferWorld(t) + client := fixture.clients[1] + request := missionAcceptanceRequest(*client.missionOffer) + if _, err := fixture.db.Exec(statement); err != nil { + t.Fatal(err) + } + world := newRoutedTestWorld(t, fixture) + defer world.stop() + source, _ := world.owner(1) + + if err := source.call(zonePacket{client, request}); err == nil { + t.Fatal("failed acceptance succeeded") + } + query := &zoneMissionSnapshot{} + if err := source.call(query); err != nil { + t.Fatal(err) + } + if len(world.queryMissions(missionLookup{world: 65536})) != 0 || len(query.journal) != 0 || !query.offer || !slices.Equal(query.items, []uint32{1}) { + t.Fatal("failed acceptance published a mission or consumed its offer", query) + } + if _, err := world.owner(2000000); err == nil { + t.Fatal("failed acceptance installed a population owner") + } + var contracts, objectives, actors int + 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 { + t.Fatal("failed transaction retained part of the mission", contracts, objectives, actors, err) + } + + if _, err := fixture.db.Exec("DROP TRIGGER fail_mission"); err != nil { + t.Fatal(err) + } + if err := source.call(zonePacket{client, request}); err != nil { + t.Fatal(err) + } + missions := world.queryMissions(missionLookup{world: 65536}) + if len(missions) != 1 || missions[0].id != 4 || missions[0].objective != 5 { + t.Fatal("retry did not publish exactly one complete mission", missions) + } + if _, err := world.owner(2000005); err != nil { + t.Fatal("retry population has no owner", err) + } + }) + } +} + +func TestWorldRuntimeMissionCatalogSupportsKeysFromAnotherZone(t *testing.T) { + fixture, _ := newMissionKeyCopyWorld(t) + fixture.characters[1].playfield = 540 + if err := fixture.savePosition_locked(fixture.characters[1]); err != nil { + t.Fatal(err) + } + client := fixture.clients[1] + world := newRoutedTestWorld(t, fixture) + defer world.stop() + zone, _ := world.owner(1) + other, _ := world.owner(1000000) + entered, release := make(chan struct{}), make(chan struct{}) + defer close(release) + go other.call(zoneBarrier{entered: entered, release: release}) + <-entered + + // Directory queries and key copies cannot depend on another zone's loop. + completed := make(chan error, 1) + go func() { completed <- zone.call(zonePacket{client, combinationRequest(1, 66, 65)}) }() + select { + case err := <-completed: + if err != nil { + t.Fatal(err) + } + case <-time.After(5 * time.Second): + t.Fatal("key copy waited for another zone") + } + var bindings int + if err := fixture.db.QueryRow("SELECT count(*) FROM mission_keys WHERE mission_id=2").Scan(&bindings); err != nil || bindings != 2 { + t.Fatal("remote-zone key copy lost its contract binding", bindings, err) + } + first := world.queryMissions(missionLookup{id: 2}) + if len(first) != 1 { + t.Fatal("remote zone lost its mission metadata") + } + before := *cloneStoredMission(&first[0]) + first[0].layout[0] ^= 1 + first[0].doors[0].id = 999 + second := world.queryMissions(missionLookup{exterior: 3000}) + if len(second) != 1 || !reflect.DeepEqual(second[0], before) { + t.Fatal("mission query exposed mutable catalog state") + } +} + +func TestWorldRuntimeShutdownDrainsMissionCreation(t *testing.T) { + fixture, _ := newMissionEncounterOfferWorld(t) + client := fixture.clients[1] + request := missionAcceptanceRequest(*client.missionOffer) + world := newRoutedTestWorld(t, fixture) + source, _ := world.owner(1) + other, _ := world.owner(1000000) + entered, release := make(chan struct{}), make(chan struct{}, 1) + defer close(release) + finished := make(chan error, 1) + go func() { finished <- source.call(zoneBarrier{entered, release, zonePacket{client, request}}) }() + <-entered + stopped := make(chan struct{}) + go func() { world.stop(); close(stopped) }() + <-other.quiesced + + // The admitted transaction may create a new destination during quiescence. + release <- struct{}{} + if err := <-finished; err != nil { + t.Fatal(err) + } + select { + case <-stopped: + case <-time.After(5 * time.Second): + t.Fatal("shutdown did not drain the newly created mission zone") + } + var contracts, population, objectives int + 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 { + t.Fatal("shutdown lost an admitted mission transaction", contracts, population, objectives, err) + } +} diff --git a/server/world_runtime.go b/server/world_runtime.go @@ -10,14 +10,17 @@ import ( // The directory owns routing only. Entity state belongs to the zone named by a // route; an arrival barrier keeps reconnects behind an in-flight ownership move. type worldRuntime struct { - lookups chan worldLookup - transfers chan worldTransfer - ownership chan worldOwnership - messages chan worldMessage - presence chan presenceUpdate - people chan presenceQuery - stopping chan struct{} - done chan struct{} + lookups chan worldLookup + transfers chan worldTransfer + ownership chan worldOwnership + messages chan worldMessage + presence chan presenceUpdate + people chan presenceQuery + missionLookups chan missionLookup + missionChanges chan missionChange + missionCreations chan missionCreation + stopping chan struct{} + done chan struct{} } type worldRoute struct { @@ -69,7 +72,9 @@ func newWorldRuntime(configuration *server, states map[uint32]*server) (*worldRu world := &worldRuntime{lookups: make(chan worldLookup), transfers: make(chan worldTransfer), ownership: make(chan worldOwnership), messages: make(chan worldMessage), presence: make(chan presenceUpdate), people: make(chan presenceQuery), - stopping: make(chan struct{}), done: make(chan struct{})} + stopping: make(chan struct{}), done: make(chan struct{}), + missionLookups: make(chan missionLookup), missionChanges: make(chan missionChange), missionCreations: make(chan missionCreation)} + missions := newMissionCatalog(configuration.missionExpiry) if configuration.chatService == nil { var err error configuration.chatService, err = newChatRuntime(configuration.db, configuration.databaseWriter, chatPeople) @@ -93,11 +98,11 @@ func newWorldRuntime(configuration *server, states map[uint32]*server) (*worldRu for player, playfield := range owners { routes[player] = &worldRoute{zone: zones[playfield], ready: ready} } - go world.run(configuration, zones, routes, presence) + go world.run(configuration, zones, routes, presence, missions) return world, nil } -func (world *worldRuntime) run(configuration *server, zones map[uint32]*zoneRuntime, routes map[uint32]*worldRoute, presence map[uint32]playerPresence) { +func (world *worldRuntime) run(configuration *server, zones map[uint32]*zoneRuntime, routes map[uint32]*worldRoute, presence map[uint32]playerPresence, missions *missionCatalog) { defer close(world.done) arrivals, quiescing, messages := 0, 0, 0 arrived, quiesced := make(chan struct{}), make(chan struct{}) @@ -112,6 +117,41 @@ func (world *worldRuntime) run(configuration *server, zones map[uint32]*zoneRunt return } select { + case query := <-world.missionLookups: + query.reply <- missions.lookup(query) + case change := <-world.missionChanges: + if change.removed { + delete(missions.byID, change.mission.id) + delete(missions.byWorld, change.mission.world) + delete(missions.byExterior[change.mission.offer.definition.Playfield], change.mission.id) + } else if _, exists := missions.byID[change.mission.id]; exists { + missions.publish(change.mission) + } + close(change.done) + case creation := <-world.missionCreations: + if zones[creation.mission.world] != nil { + creation.reply <- fmt.Errorf("mission %d already has a zone owner", creation.mission.id) + continue + } + state := creation.state + state.world = world + var identities []uint32 + for id := range state.characters { + identities = append(identities, id) + } + zone := newZoneRuntime(state) + zones[creation.mission.world] = zone + missions.publish(creation.mission) + ready := make(chan struct{}) + close(ready) + for _, id := range identities { + routes[id] = &worldRoute{zone: zone, ready: ready} + } + if stopping == nil { + quiescing++ + go func() { zone.quiesce(); quiesced <- struct{}{} }() + } + creation.reply <- nil case lookup := <-world.lookups: lookup.reply <- routes[lookup.player] case query := <-world.people: