aorecomp

Anarchy Online classic in the browser

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


server/world_operations.go (3387B)


      1 package main
      2 
      3 import "net"
      4 
      5 // Lifecycle operations own account drafts, but never borrow a zone's entities.
      6 type worldCoordinator struct {
      7 	worldServices
      8 	browserCreations map[string]*browserCreationCredentials
      9 }
     10 
     11 // Operations that hold multiple zone owners share one coordinator queue. The
     12 // directory continues routing while the active operation waits for zones or SQL.
     13 type worldOperation struct {
     14 	run    func(*worldCoordinator) error
     15 	finish func(error)
     16 }
     17 
     18 func (world *worldRuntime) operate(run func(*worldCoordinator) error) error {
     19 	done := make(chan error, 1)
     20 	operation := worldOperation{run: run, finish: func(err error) { done <- err }}
     21 	select {
     22 	case world.operations <- operation:
     23 		return <-done
     24 	case <-world.done:
     25 		return net.ErrClosed
     26 	}
     27 }
     28 
     29 // The coordinator owns account drafts and pending native creation credentials.
     30 // Its callbacks finish before the next account or mission lifecycle operation.
     31 func (state *server) accountOperation(run func(*worldCoordinator) error) error {
     32 	if state.world != nil {
     33 		return state.world.operate(run)
     34 	}
     35 	coordinator := &worldCoordinator{worldServices: state.services(), browserCreations: state.browserCreations}
     36 	err := run(coordinator)
     37 	state.browserCreations = coordinator.browserCreations
     38 	return err
     39 }
     40 
     41 type playerCreation struct {
     42 	player *character
     43 	reply  chan *worldRoute
     44 }
     45 
     46 type zoneNewPlayer struct{ player *character }
     47 
     48 func (command zoneNewPlayer) execute(state *server) error {
     49 	state.publishBrowserPlayer(command.player)
     50 	state.zone.players[command.player.id] = command.player
     51 	return nil
     52 }
     53 
     54 func (world *worldRuntime) createPlayer(player *character) error {
     55 	request := playerCreation{player: player, reply: make(chan *worldRoute, 1)}
     56 	select {
     57 	case world.playerCreations <- request:
     58 	case <-world.done:
     59 		return net.ErrClosed
     60 	}
     61 	route := <-request.reply
     62 	<-route.ready
     63 	return route.err
     64 }
     65 
     66 // The directory starts one lifecycle worker at a time while continuing to route
     67 // messages. Completion callbacks run here before the next operation can start.
     68 func (directory *worldDirectory) enqueue(operation worldOperation) {
     69 	directory.lifecycle = append(directory.lifecycle, operation)
     70 	if len(directory.lifecycle) == 1 {
     71 		go func() { directory.completed <- operation.run(directory.coordinator) }()
     72 	}
     73 }
     74 
     75 func (directory *worldDirectory) retire(operation missionOperation) {
     76 	directory.enqueue(worldOperation{
     77 		run: func(coordinator *worldCoordinator) error {
     78 			return directory.world.retireMission(coordinator, operation)
     79 		},
     80 		finish: func(err error) {
     81 			finishMissionOperation(operation, err)
     82 			if operation.expired != nil {
     83 				delete(directory.expiring, operation.expired.id)
     84 			}
     85 		},
     86 	})
     87 }
     88 
     89 func (directory *worldDirectory) finishOperation(err error) {
     90 	directory.lifecycle[0].finish(err)
     91 	directory.lifecycle[0] = worldOperation{}
     92 	directory.lifecycle = directory.lifecycle[1:]
     93 	// Authentication can observe a committed account just before its
     94 	// new player is published. Admissions join that publication, then
     95 	// any remaining unknown identities are rejected at operation end.
     96 	for id, waiting := range directory.admissions {
     97 		for _, query := range waiting {
     98 			query.reply <- directory.routes[id]
     99 		}
    100 	}
    101 	clear(directory.admissions)
    102 	if len(directory.lifecycle) != 0 {
    103 		operation := directory.lifecycle[0]
    104 		go func() { directory.completed <- operation.run(directory.coordinator) }()
    105 	}
    106 }