218 lines
7.4 KiB
Go
218 lines
7.4 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/internal/game"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
)
|
|
|
|
var ErrTravel = errors.New("travel unavailable")
|
|
|
|
func travelError(message string) error { return fmt.Errorf("%w: %s", ErrTravel, message) }
|
|
|
|
type Transfer struct {
|
|
ID string `json:"id"`
|
|
RequestID string `json:"requestId"`
|
|
Account string `json:"-"`
|
|
Source string `json:"source"`
|
|
SourceKind string `json:"sourceKind"`
|
|
Destination string `json:"destination"`
|
|
CharacterID string `json:"characterId"`
|
|
Route string `json:"route"`
|
|
Status string `json:"status"`
|
|
AcceptedWorld string `json:"acceptedWorld"`
|
|
Payload string `json:"-"`
|
|
}
|
|
|
|
const transferColumns = `id,request_id,account_id,source_world,source_kind,destination,character_id,route,status,accepted_world,character`
|
|
|
|
func scanTransfer(row interface{ Scan(...any) error }) (*Transfer, error) {
|
|
t := &Transfer{}
|
|
err := row.Scan(&t.ID, &t.RequestID, &t.Account, &t.Source, &t.SourceKind, &t.Destination, &t.CharacterID, &t.Route, &t.Status, &t.AcceptedWorld, &t.Payload)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return t, err
|
|
}
|
|
func (d *DB) PendingTransfer(ctx context.Context, account string) (*Transfer, error) {
|
|
return scanTransfer(d.sql.QueryRowContext(ctx, d.q(`SELECT `+transferColumns+` FROM character_transfers WHERE account_id=? AND status='prepared'`), account))
|
|
}
|
|
func (d *DB) Transfer(ctx context.Context, account, id string) (*Transfer, error) {
|
|
return scanTransfer(d.sql.QueryRowContext(ctx, d.q(`SELECT `+transferColumns+` FROM character_transfers WHERE account_id=? AND id=?`), account, id))
|
|
}
|
|
func (d *DB) RequestedTransfer(ctx context.Context, account, request string) (*Transfer, error) {
|
|
return scanTransfer(d.sql.QueryRowContext(ctx, d.q(`SELECT `+transferColumns+` FROM character_transfers WHERE account_id=? AND request_id=?`), account, request))
|
|
}
|
|
func (l *Lease) fenceTravel(ctx context.Context, tx *sql.Tx, w *game.World) error {
|
|
raw, err := json.Marshal(w)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r, err := tx.ExecContext(ctx, l.DB.q(`UPDATE worlds SET snapshot=?,revision=revision+1,lease_until=`+l.DB.now()+`+30 WHERE id=? AND owner=? AND revision=? AND lease_until>=`+l.DB.now()), string(raw), l.WorldID, l.Owner, l.Revision)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, _ := r.RowsAffected()
|
|
if n != 1 {
|
|
return ErrOwnership
|
|
}
|
|
return nil
|
|
}
|
|
func (l *Lease) PrepareTravel(ctx context.Context, live *game.World, account, request, destination, route string) (*Transfer, error) {
|
|
d := l.DB
|
|
previous, err := d.RequestedTransfer(ctx, account, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if previous != nil {
|
|
if previous.Source != live.ID || previous.Destination != destination || previous.Route != route {
|
|
return nil, travelError("request already used")
|
|
}
|
|
return previous, nil
|
|
}
|
|
c := live.ForAccount(account)
|
|
if err = live.Departure(c, route); err != nil {
|
|
return nil, travelError(err.Error())
|
|
}
|
|
if destination == live.ID {
|
|
return nil, travelError("choose another world")
|
|
}
|
|
worlds, err := d.Worlds(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
valid := false
|
|
for _, w := range worlds {
|
|
if w.ID == destination && w.Available && !w.Closed && w.Living < w.Capacity && (w.Phase == "hub" || w.Phase == "gathering" || w.Phase == "cataclysm") && (live.IsHub() || w.Kind == "hub") {
|
|
valid = true
|
|
}
|
|
}
|
|
if !valid {
|
|
return nil, travelError("destination unavailable or full")
|
|
}
|
|
// Only the departing copy is changed before commit. Other online players retain
|
|
// their connections, inputs and acknowledgement counters.
|
|
staged := *live
|
|
staged.Characters = maps.Clone(live.Characters)
|
|
raw, err := json.Marshal(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var departing game.Character
|
|
if err = json.Unmarshal(raw, &departing); err != nil {
|
|
return nil, err
|
|
}
|
|
departing.PendingCast = nil
|
|
departing.View.Casting = nil
|
|
if route == "escape" {
|
|
game.TakeStack(&departing, "escape_stone", 1)
|
|
}
|
|
raw, err = json.Marshal(&departing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t := &Transfer{ID: game.ID(), RequestID: request, Account: account, Source: live.ID, SourceKind: live.Kind, Destination: destination, CharacterID: c.View.Entity.ID, Route: route, Status: "prepared", Payload: string(raw)}
|
|
delete(staged.Characters, t.CharacterID)
|
|
tx, err := d.sql.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = l.fenceTravel(ctx, tx, &staged); err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = tx.ExecContext(ctx, d.q(`INSERT INTO character_transfers(`+transferColumns+`) VALUES(?,?,?,?,?,?,?,?,?,?,?)`), t.ID, t.RequestID, t.Account, t.Source, t.SourceKind, t.Destination, t.CharacterID, t.Route, t.Status, "", t.Payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
marker := "travel:" + t.ID
|
|
if _, err = tx.ExecContext(ctx, d.q(`UPDATE passages SET claimed_world=? WHERE character_id=? AND account_id=? AND source_world=? AND claimed_world IS NULL`), marker, t.CharacterID, account, live.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
r, err := tx.ExecContext(ctx, d.q(`UPDATE character_records SET world_id=?,state='transit',character=? WHERE character_id=? AND account_id=? AND world_id=? AND state IN ('alive','eligible')`), marker, t.Payload, t.CharacterID, account, live.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
n, _ := r.RowsAffected()
|
|
if n != 1 {
|
|
return nil, travelError("character no longer belongs to source")
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
l.Revision++
|
|
*live = staged
|
|
live.Saved()
|
|
return t, nil
|
|
}
|
|
func (l *Lease) AcceptTravel(ctx context.Context, live *game.World, account, id string) (*Transfer, error) {
|
|
d := l.DB
|
|
t, err := d.Transfer(ctx, account, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if t == nil {
|
|
return nil, travelError("transfer not found")
|
|
}
|
|
if t.Status == "accepted" {
|
|
return t, nil
|
|
}
|
|
if t.SourceKind != "hub" && !live.IsHub() {
|
|
return nil, travelError("escape and victory lead to a hub")
|
|
}
|
|
staged := *live
|
|
staged.Characters = maps.Clone(live.Characters)
|
|
var c game.Character
|
|
if err = json.Unmarshal([]byte(t.Payload), &c); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = staged.Arrive(&c); err != nil {
|
|
return nil, travelError(err.Error())
|
|
}
|
|
raw, err := json.Marshal(&c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tx, err := d.sql.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = l.fenceTravel(ctx, tx, &staged); err != nil {
|
|
return nil, err
|
|
}
|
|
r, err := tx.ExecContext(ctx, d.q(`UPDATE character_transfers SET status='accepted',accepted_world=? WHERE id=? AND account_id=? AND status='prepared'`), live.ID, id, account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
n, _ := r.RowsAffected()
|
|
if n != 1 {
|
|
return nil, travelError("transfer already accepted; refresh travel status")
|
|
}
|
|
marker := "travel:" + id
|
|
if _, err = tx.ExecContext(ctx, d.q(`UPDATE passages SET claimed_world=? WHERE character_id=? AND account_id=? AND claimed_world=?`), live.ID, t.CharacterID, account, marker); err != nil {
|
|
return nil, err
|
|
}
|
|
r, err = tx.ExecContext(ctx, d.q(`UPDATE character_records SET world_id=?,state='alive',character=? WHERE character_id=? AND account_id=? AND world_id=? AND state='transit'`), live.ID, string(raw), t.CharacterID, account, marker)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
n, _ = r.RowsAffected()
|
|
if n != 1 {
|
|
return nil, travelError("transfer no longer owns character")
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
l.Revision++
|
|
*live = staged
|
|
live.Saved()
|
|
t.Status = "accepted"
|
|
t.AcceptedWorld = live.ID
|
|
return t, nil
|
|
}
|