aorecomp

Anarchy Online classic in the browser

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


server/world_admin.go (2037B)


      1 package main
      2 
      3 import (
      4 	"cmp"
      5 	"net"
      6 	"slices"
      7 )
      8 
      9 // Copy owner handles on the directory, then collect detached observations on
     10 // the caller. A busy zone cannot hold up routing or another zone's commands.
     11 type worldSnapshot struct {
     12 	arrivals int
     13 	zones    map[uint32]*zoneRuntime
     14 	seconds  float64
     15 	weather  int
     16 }
     17 
     18 func (world *worldRuntime) snapshot() (worldSnapshot, error) {
     19 	reply := make(chan worldSnapshot, 1)
     20 	select {
     21 	case world.snapshots <- reply:
     22 		return <-reply, nil
     23 	case <-world.done:
     24 		return worldSnapshot{}, net.ErrClosed
     25 	}
     26 }
     27 
     28 type zoneAdminPlayers struct{ players []mapPlayer }
     29 
     30 func (query *zoneAdminPlayers) execute(state *server) error {
     31 	query.players = state.mapPlayerSnapshots()
     32 	return nil
     33 }
     34 
     35 func (server *server) adminPlayers() ([]mapPlayer, error) {
     36 	if server.world == nil {
     37 		return server.mapPlayerSnapshots(), nil
     38 	}
     39 	snapshot, err := server.world.snapshot()
     40 	if err != nil {
     41 		return nil, err
     42 	}
     43 	// Admin views are live samples, not a transaction spanning every zone.
     44 	// Deduplicate players who transfer between the sampled owners.
     45 	players := make(map[uint32]mapPlayer)
     46 	for _, owner := range snapshot.zones {
     47 		query := &zoneAdminPlayers{}
     48 		if err := owner.observe(query); err != nil {
     49 			return nil, err
     50 		}
     51 		for _, player := range query.players {
     52 			players[player.ID] = player
     53 		}
     54 	}
     55 	result := make([]mapPlayer, 0, len(players))
     56 	for _, player := range players {
     57 		result = append(result, player)
     58 	}
     59 	slices.SortFunc(result, func(a, b mapPlayer) int { return cmp.Compare(a.ID, b.ID) })
     60 	return result, nil
     61 }
     62 
     63 type zoneAdminCounts struct{ players, connected, npcs int }
     64 
     65 func (counts *zoneAdminCounts) execute(state *server) error {
     66 	for _, character := range state.characters {
     67 		if character.npc {
     68 			counts.npcs++
     69 		} else {
     70 			counts.players++
     71 		}
     72 	}
     73 	for _, client := range state.clients {
     74 		if client.ready {
     75 			counts.connected++
     76 		}
     77 	}
     78 	if state.populationDormant() {
     79 		return state.visitDormantNPCs(func(_ uint32, _ string, _ [3]float32, _ bool) { counts.npcs++ })
     80 	}
     81 	return nil
     82 }