RPGTheory/aeternalis-portal had diverged from this repo (the git/CI/CD copy) with an in-progress control portal feature that was never synced back here, so none of it had gone through CI or been deployed: - New site/game/control routing split: index.html becomes a world directory landing page (site.ts), game.html is the renamed former index.html (main.ts), control.html is a new admin panel (control.ts, portal-shared.ts, portal.css). - Server: new internal/store/portal.go (character/world registry, command queue, admin grants) and internal/transport/control.go (portal API routes), plus a new internal/gitops package that lets the portal provision new worlds via a GitOps repo commit. - game.World gains BornAt/DiedAt/DeathReason and an operator Control() path (close/open admissions, awaken boss, end world, per-character rename/heal/unstuck/retire); death no longer reuses the same character id on rejoin, matching the "new life" permadeath model in Plan.md rather than the old bypass-prevention test's expectations. - deploy/: new control-egress network policy and portal/control values (admin role, GitOps repo/branch/values file/CIDRs). - New e2e coverage: tests/portal.spec.ts, tools/capture-world.mjs. Verified this repo's own independent commits (800ms movement-staleness window, 20s boss-attack poll, SRS/TLS deploy values, ubuntu-22.04 runner pin, DOCKER_HOST fix) were already present in the portal copy before merging, so nothing here regresses them. `tsc --noEmit`, the vite client build, and the protocol contract check all pass; no go.mod changes. Could not run `go build`/`go test` locally (no Go toolchain on PATH, Docker daemon not running) — every new/changed Go symbol was manually cross-referenced and resolves, but this should still be built and tested in CI before merging to master. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
222 lines
5.3 KiB
Go
222 lines
5.3 KiB
Go
package transport
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"maps"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"aeternalis/server/internal/store"
|
|
)
|
|
|
|
type peer struct {
|
|
character string
|
|
out chan []byte
|
|
known map[string]bool
|
|
close func()
|
|
}
|
|
type Service struct {
|
|
mu sync.Mutex
|
|
world *game.World
|
|
lease *store.Lease
|
|
peers map[string]*peer
|
|
ready atomic.Bool
|
|
lastSave time.Time
|
|
lastControl time.Time
|
|
done chan struct{}
|
|
}
|
|
|
|
func NewService(w *game.World, l *store.Lease) *Service {
|
|
s := &Service{world: w, lease: l, peers: map[string]*peer{}, lastSave: time.Now(), done: make(chan struct{})}
|
|
s.ready.Store(true)
|
|
return s
|
|
}
|
|
func (s *Service) persist(ctx context.Context, claim string) error {
|
|
err := s.lease.Save(ctx, s.world, claim)
|
|
if err != nil {
|
|
s.ready.Store(false)
|
|
slog.Error("world frozen: persistence failed", "error", err)
|
|
for _, p := range s.peers {
|
|
p.close()
|
|
}
|
|
return err
|
|
}
|
|
s.lastSave = time.Now()
|
|
return nil
|
|
}
|
|
func (s *Service) Run(ctx context.Context) {
|
|
defer close(s.done)
|
|
ticker := time.NewTicker(time.Second / time.Duration(s.world.Balance.TickHz))
|
|
defer ticker.Stop()
|
|
last := time.Now()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-ticker.C:
|
|
s.mu.Lock()
|
|
if !s.ready.Load() {
|
|
s.mu.Unlock()
|
|
return
|
|
}
|
|
s.world.Advance(now, now.Sub(last).Seconds())
|
|
last = now
|
|
if now.Sub(s.lastControl) >= time.Second {
|
|
if err := s.controls(ctx); err != nil {
|
|
s.ready.Store(false)
|
|
s.mu.Unlock()
|
|
return
|
|
}
|
|
s.lastControl = now
|
|
}
|
|
if s.world.Dirty() || now.Sub(s.lastSave) >= time.Second {
|
|
saveCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
err := s.persist(saveCtx, "")
|
|
cancel()
|
|
if err != nil {
|
|
s.mu.Unlock()
|
|
return
|
|
}
|
|
}
|
|
if s.world.Tick%int64(s.world.Balance.TickHz/s.world.Balance.SnapshotHz) == 0 {
|
|
for id, p := range s.peers {
|
|
snapshot := s.world.Snapshot(id, p.known)
|
|
data, err := json.Marshal(snapshot)
|
|
if err != nil {
|
|
slog.Error("snapshot encoding failed", "error", err)
|
|
p.close()
|
|
continue
|
|
}
|
|
select {
|
|
case p.out <- data:
|
|
for key := range p.known {
|
|
delete(p.known, key)
|
|
}
|
|
for _, key := range snapshot.ChunkKeys {
|
|
p.known[key] = true
|
|
}
|
|
default:
|
|
p.close()
|
|
}
|
|
}
|
|
}
|
|
s.mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
func (s *Service) Stop(ctx context.Context) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
wasReady := s.ready.Swap(false)
|
|
for id, p := range s.peers {
|
|
s.world.Disconnect(id)
|
|
p.close()
|
|
}
|
|
if !wasReady {
|
|
return errors.New("world already frozen; lease will expire")
|
|
}
|
|
if err := s.lease.Save(ctx, s.world, ""); err != nil {
|
|
return err
|
|
}
|
|
return s.lease.Release(ctx)
|
|
}
|
|
func (s *Service) join(ctx context.Context, account, name, claim string) (p.Character, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if !s.ready.Load() {
|
|
return p.Character{}, errors.New("world unavailable")
|
|
}
|
|
var c *game.Character
|
|
var err error
|
|
active, err := s.lease.DB.ActiveCharacter(ctx, account)
|
|
if err != nil {
|
|
return p.Character{}, errors.New("character registry unavailable")
|
|
}
|
|
if active != nil {
|
|
transfer := claim != "" && active.ID == claim && active.State == "eligible" && active.WorldID != s.world.ID
|
|
resume := claim == "" && active.WorldID == s.world.ID
|
|
if !transfer && !resume {
|
|
return p.Character{}, store.ErrLivingCharacter
|
|
}
|
|
}
|
|
existing := s.world.ForAccount(account)
|
|
if claim != "" {
|
|
c, err = s.lease.DB.ReadPassage(ctx, claim, account)
|
|
if err != nil {
|
|
return p.Character{}, errors.New("passage unavailable")
|
|
}
|
|
if err = s.world.Import(c); err != nil {
|
|
return p.Character{}, err
|
|
}
|
|
} else {
|
|
c, err = s.world.Join(account, name)
|
|
if err != nil {
|
|
return p.Character{}, err
|
|
}
|
|
}
|
|
if err = s.lease.Save(ctx, s.world, claim); err != nil {
|
|
if errors.Is(err, store.ErrLivingCharacter) {
|
|
if existing == nil {
|
|
delete(s.world.Characters, c.View.Entity.ID)
|
|
}
|
|
return p.Character{}, err
|
|
}
|
|
s.ready.Store(false)
|
|
for _, peer := range s.peers {
|
|
peer.close()
|
|
}
|
|
return p.Character{}, errors.New("world persistence unavailable")
|
|
}
|
|
s.lastSave = time.Now()
|
|
view := c.View
|
|
view.Inventory = maps.Clone(view.Inventory)
|
|
view.Skills = maps.Clone(view.Skills)
|
|
return view, nil
|
|
}
|
|
|
|
func (s *Service) controls(ctx context.Context) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
commands, err := s.lease.DB.Commands(ctx, s.world.ID, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, command := range commands {
|
|
result := ""
|
|
if time.Now().Unix()-command.CreatedAt > 900 {
|
|
result = "command expired before execution"
|
|
} else if command.Op == "revoke-sessions" {
|
|
found := false
|
|
for id, c := range s.world.Characters {
|
|
if c.Account == command.Target {
|
|
found = true
|
|
s.world.Disconnect(id)
|
|
if p := s.peers[id]; p != nil {
|
|
p.close()
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
result = "account not found in world"
|
|
}
|
|
} else if err = s.world.Control(command.Op, command.Target, command.Value, command.Reason); err != nil {
|
|
result = err.Error()
|
|
}
|
|
if err = s.lease.SaveCommand(ctx, s.world, "", &command, result); err != nil {
|
|
s.ready.Store(false)
|
|
for _, peer := range s.peers {
|
|
peer.close()
|
|
}
|
|
return err
|
|
}
|
|
s.lastSave = time.Now()
|
|
}
|
|
return nil
|
|
}
|