aorecomp

Anarchy Online classic in the browser

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


server/player_write.go (2028B)


      1 package main
      2 
      3 import (
      4 	"database/sql"
      5 	"fmt"
      6 	"strings"
      7 	"time"
      8 )
      9 
     10 type playerProfileWrite struct {
     11 	record characterRecord
     12 }
     13 
     14 func (write playerProfileWrite) persist(tx *sql.Tx) error {
     15 	if err := insertPlayerRecord(tx, write.record); err != nil {
     16 		return err
     17 	}
     18 	profile, vitals := write.record.Profile, write.record.Vitals
     19 	columns := profileColumns + ", " + vitalColumns
     20 	fields := append(profile.fields(), vitals.fields()...)
     21 	if err := updateRecord(tx, "characters", write.record.Profile.ID, columns, fields); err != nil {
     22 		return err
     23 	}
     24 	if _, err := tx.Exec("UPDATE world SET last_character_id = max(last_character_id, ?) WHERE id = 1", write.record.Profile.ID); err != nil {
     25 		return err
     26 	}
     27 
     28 	// Verify the final profile and vitals before committing their transaction.
     29 	var savedID uint32
     30 	query := "SELECT id FROM characters WHERE id=? AND (" + columns + ") IS (?" + strings.Repeat(", ?", len(fields)-1) + ")"
     31 	if err := tx.QueryRow(query, append([]any{write.record.Profile.ID}, fields...)...).Scan(&savedID); err != nil {
     32 		return fmt.Errorf("character %d profile and resources were not saved: %w", write.record.Profile.ID, err)
     33 	}
     34 	return nil
     35 }
     36 
     37 type playerPositionWrite struct {
     38 	record              characterRecord
     39 	effects             []activeNano
     40 	now                 time.Time
     41 	updatePosition      bool
     42 	connectedSeconds    int64
     43 	updateConnectedTime bool
     44 }
     45 
     46 func (write playerPositionWrite) persist(tx *sql.Tx) error {
     47 	if write.updateConnectedTime {
     48 		if err := updateRecord(tx, "players", write.record.Profile.ID, "connected_seconds", []any{write.connectedSeconds}); err != nil {
     49 			return err
     50 		}
     51 	}
     52 	if write.updatePosition {
     53 		if err := insertPlayerRecord(tx, write.record); err != nil {
     54 			return err
     55 		}
     56 		if err := updateRecord(tx, "characters", write.record.Profile.ID, positionColumns+", "+vitalColumns,
     57 			append(write.record.Position.fields(), write.record.Vitals.fields()...)); err != nil {
     58 			return err
     59 		}
     60 	}
     61 	return writeActiveNanos(tx, write.record.Profile.ID, write.effects, write.now)
     62 }