aorecomp

Anarchy Online classic in the browser

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


server/zone_checkpoint.go (2001B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"log"
      6 	"time"
      7 )
      8 
      9 // One in-flight batch per zone bounds memory and keeps periodic submissions
     10 // from queuing repeatedly behind a slow disk. Full writer queues retry next tick.
     11 func (zone *zoneRuntime) startCheckpoint(state *server, now time.Time) {
     12 	if zone.checkpoint != nil {
     13 		return
     14 	}
     15 	checkpoint := state.prepareSimulationCheckpoint(now)
     16 	if !checkpoint.changed() {
     17 		state.applySimulationCheckpoint(checkpoint)
     18 		return
     19 	}
     20 	done := state.databaseWriter.trySubmit(checkpoint)
     21 	if done != nil {
     22 		zone.checkpoint, zone.checkpointDone = &checkpoint, done
     23 	}
     24 }
     25 
     26 func (zone *zoneRuntime) finishCheckpoint(state *server, err error) {
     27 	if err != nil {
     28 		log.Printf("Zone %d checkpoint: %v", state.playfield, err)
     29 	} else {
     30 		state.applySimulationCheckpoint(*zone.checkpoint)
     31 	}
     32 	zone.checkpoint, zone.checkpointDone = nil, nil
     33 }
     34 
     35 // Shutdown has drained departures, arrivals and cross-zone operations. Capture
     36 // every remaining actor at the current time, regardless of its periodic deadline.
     37 // Inventory and rewards are already durable; do not rewrite them on shutdown.
     38 func (zone *zoneRuntime) finalCheckpoint(state *server, now time.Time) error {
     39 	if state.populationDormant() {
     40 		return nil
     41 	}
     42 	checkpoint := state.prepareFinalCheckpoint(now)
     43 	if checkpoint.changed() {
     44 		if err := state.databaseWriter.commit(checkpoint); err != nil {
     45 			return fmt.Errorf("zone %d final checkpoint: %w", state.playfield, err)
     46 		}
     47 	}
     48 	state.applySimulationCheckpoint(checkpoint)
     49 	return nil
     50 }
     51 
     52 func (state *server) prepareFinalCheckpoint(now time.Time) simulationCheckpoint {
     53 	checkpoint := simulationCheckpoint{active: make(map[uint32]bool), now: now}
     54 	for _, player := range state.characters {
     55 		if !player.npc && player.savedProfile.ID != 0 {
     56 			checkpoint.players.include(player, now)
     57 		}
     58 	}
     59 	for id := range state.simulationNPCs(now) {
     60 		checkpoint.active[id] = true
     61 	}
     62 	checkpoint.npcs = state.prepareNPCWrite(itemStore{flush: true}, checkpoint.active)
     63 	return checkpoint
     64 }