summaryrefslogtreecommitdiff
path: root/server/lib/net/game_server.dart
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/net/game_server.dart')
-rw-r--r--server/lib/net/game_server.dart308
1 files changed, 294 insertions, 14 deletions
diff --git a/server/lib/net/game_server.dart b/server/lib/net/game_server.dart
index aa9aee3..5a95194 100644
--- a/server/lib/net/game_server.dart
+++ b/server/lib/net/game_server.dart
@@ -17,8 +17,11 @@ import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:x_haven_server/core/commands/add_character_command.dart';
import 'package:x_haven_server/core/commands/command_registry.dart';
+import 'package:x_haven_server/core/commands/track_standees_command.dart';
import 'package:x_haven_server/core/enums.dart';
import 'package:x_haven_server/core/game_methods.dart';
+import 'package:x_haven_server/core/service_locator.dart';
+import 'package:x_haven_server/core/settings.dart';
import 'package:x_haven_server/core/state/game_state.dart';
import 'protocol.dart';
@@ -205,6 +208,20 @@ class GameServer {
_handleUndoRedo(connection, undo: true);
case Protocol.redo:
_handleUndoRedo(connection, undo: false);
+ case Protocol.setSetting:
+ _handleSetSetting(connection, message);
+ case Protocol.save:
+ _handleSave(connection, message);
+ case Protocol.loadSave:
+ _handleLoadSave(connection, message);
+ case Protocol.deleteSave:
+ _handleDeleteSave(connection, message);
+ case Protocol.saveCharacter:
+ _handleSaveCharacter(connection, message);
+ case Protocol.loadCharacterSave:
+ _handleLoadCharacterSave(connection, message);
+ case Protocol.deleteCharacterSave:
+ _handleDeleteCharacterSave(connection, message);
default:
connection.send(
Protocol.errorMessage(
@@ -258,23 +275,44 @@ class GameServer {
Map<String, dynamic> _welcomeFor(_Connection connection) {
final session = connection.session;
- final index = _gameState.commandIndex;
- final descriptions = _gameState.commandDescriptions;
- final String? description =
- index >= 0 && index < descriptions.length
- ? descriptions[index]
- : null;
return Protocol.welcomeMessage(
session: session.id,
role: session.role,
- index: index,
- description: description,
+ index: _gameState.commandIndex,
+ description: _currentDescription(),
event: null,
state: _serializeFor(connection),
seats: _sessions.seatsForState(_gameState),
+ settings: _settings().forClient(),
+ historyLength: _gameState.commandDescriptions.length,
+ redoDescription: _redoDescription(),
+ undoDescription: _undoDescription(),
);
}
+ /// The description of the action at the current index (null at the
+ /// baseline), for the client's undo label.
+ String? _currentDescription() {
+ final index = _gameState.commandIndex;
+ final descriptions = _gameState.commandDescriptions;
+ if (index < 0 || index >= descriptions.length) return null;
+ return descriptions[index];
+ }
+
+ /// The description of the next action (null when nothing can be redone),
+ /// for the client's `Redo: …` label.
+ String? _redoDescription() {
+ final index = _gameState.commandIndex + 1;
+ final descriptions = _gameState.commandDescriptions;
+ if (index < 0 || index >= descriptions.length) return null;
+ return descriptions[index];
+ }
+
+ /// The description of the action at the current index — what an undo would
+ /// undo (null at the baseline), for the client's `Undo: …` label. The
+ /// reference app's `MainMenuViewModel.undoDescription` getter, ported.
+ String? _undoDescription() => _currentDescription();
+
// --- claims ---------------------------------------------------------------
/// `{t: claim, characterId}` — any session may claim an unclaimed
@@ -442,23 +480,262 @@ class GameServer {
}
void _handleUndoRedo(_Connection connection, {required bool undo}) {
+ if (!_requirePlayer(connection, 'undo or redo')) return;
+ if (undo) {
+ _gameState.undo();
+ } else {
+ _gameState.redo();
+ }
+ }
+
+ // --- Phase 6: settings and multi-save --------------------------------------
+
+ /// The server-side settings (the game-relevant half of the reference
+ /// app's `Settings`; Phase 6 split, PLAN.md section 4).
+ Settings _settings() => getIt<Settings>();
+
+ /// The locales the client may select (the reference app's `supportedLocales`
+ /// plus `zh_Hant`, which the gen-l10n arb set covers).
+ static const List<String> _kSettableLocales = [
+ 'en', 'de', 'es', 'fr', 'ko', 'pl', 'ru', 'th', 'zh', 'zh_Hant',
+ ];
+
+ /// Gate for player-only messages (undo/redo, settings, saves): answers the
+ /// sender with a clear `error` when the session has no seat.
+ bool _requirePlayer(_Connection connection, String what) {
if (!connection.handshaken) {
_notHandshaken(connection);
- return;
+ return false;
}
if (!connection.session.isPlayer) {
connection.send(
Protocol.errorMessage(
Protocol.errForbidden,
- 'spectators cannot undo or redo — claim a character first',
+ 'spectators cannot $what — claim a character first',
+ ),
+ );
+ return false;
+ }
+ return true;
+ }
+
+ /// `{t: setSetting, name, value}` — one server-side (game-relevant) setting
+ /// (Phase 6). Persisted immediately and broadcast to every screen, since
+ /// the setting changes game behaviour for everyone (e.g. standee tracking,
+ /// the loot deck, reminders).
+ ///
+ /// `noStandees` additionally runs `TrackStandeesCommand` (the reference
+ /// app's settings menu does the same — the command itself writes the
+ /// setting and adjusts the already-added monsters' standees).
+ void _handleSetSetting(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'change settings')) return;
+ final name = message['name'];
+ final value = message['value'];
+ if (name is! String || name.isEmpty) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errBadRequest,
+ 'setSetting requires a non-empty "name" string',
),
);
return;
}
- if (undo) {
- _gameState.undo();
- } else {
- _gameState.redo();
+ final s = _settings();
+ var ok = true;
+ switch (name) {
+ case 'noInit':
+ ok = value is bool && (s.noInit = value);
+ case 'noStandees':
+ ok = value is bool && (s.noStandees = value);
+ case 'randomStandees':
+ ok = value is bool && (s.randomStandees = value);
+ case 'expireConditions':
+ ok = value is bool && (s.expireConditions = value);
+ case 'autoAddStandees':
+ ok = value is bool && (s.autoAddStandees = value);
+ case 'autoAddSpawns':
+ ok = value is bool && (s.autoAddSpawns = value);
+ case 'hideLootDeck':
+ ok = value is bool && (s.hideLootDeck = value);
+ case 'showBattleGoalReminder':
+ ok = value is bool && (s.showBattleGoalReminder = value);
+ case 'showReminders':
+ ok = value is bool && (s.showReminders = value);
+ case 'showAmdDeck':
+ ok = value is bool && (s.showAmdDeck = value);
+ case 'fhHazTerrainCalcInOGGloom':
+ ok = value is bool && (s.fhHazTerrainCalcInOGGloom = value);
+ case 'style':
+ ok = value is int && value >= 0 && value < Style.values.length;
+ if (ok) s.style = Style.values[value];
+ case 'locale':
+ ok = value is String && _kSettableLocales.contains(value);
+ if (ok) s.locale = value;
+ default:
+ ok = false;
+ }
+ if (!ok) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errRejected,
+ 'invalid setting "$name" (value $value)',
+ ),
+ );
+ return;
+ }
+ if (name == 'noStandees') {
+ // The reference toggles this through the command so the standing
+ // monsters' standees are adjusted; the command writes the setting
+ // itself (track = !noStandees), so the value survives the execute.
+ _gameState.action(
+ TrackStandeesCommand(!s.noStandees, gameState: _gameState, settings: s),
+ );
+ }
+ s.saveToDisk();
+ _broadcastSettings();
+ }
+
+ /// `{t: save, name}` — snapshot the current game state under [name]
+ /// (multi-save; the content lives only on the server).
+ void _handleSave(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'save game states')) return;
+ final name = _nonEmptyName(connection, message, 'save');
+ if (name == null) return;
+ _settings().saveState(name);
+ _broadcastSettings();
+ }
+
+ /// `{t: loadSave, name}` — runs `LoadSaveCommand` (an undoable action, so
+ /// the index advances and every screen receives the loaded state).
+ void _handleLoadSave(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'load save states')) return;
+ final name = _nonEmptyName(connection, message, 'loadSave');
+ if (name == null) return;
+ final s = _settings();
+ if (!s.saves.containsKey(name)) {
+ connection.send(
+ Protocol.errorMessage(Protocol.errRejected, 'no save named "$name"'),
+ );
+ return;
+ }
+ s.loadSave(name);
+ // The loaded state may contain different characters — prune stale seats.
+ _syncSeats();
+ }
+
+ /// `{t: deleteSave, name}`.
+ void _handleDeleteSave(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'delete save states')) return;
+ final name = _nonEmptyName(connection, message, 'deleteSave');
+ if (name == null) return;
+ _settings().deleteSave(name);
+ _broadcastSettings();
+ }
+
+ /// `{t: saveCharacter, name, characterId}` — snapshot one character's
+ /// state (the reference `saveCharacterState`).
+ void _handleSaveCharacter(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'save characters')) return;
+ final name = _nonEmptyName(connection, message, 'saveCharacter');
+ if (name == null) return;
+ final characterId = message['characterId'];
+ if (characterId is! String || characterId.isEmpty) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errBadRequest,
+ 'saveCharacter requires a non-empty "characterId" string',
+ ),
+ );
+ return;
+ }
+ final item = _gameState.currentList.firstWhereOrNull(
+ (item) => item is Character && item.id == characterId,
+ );
+ final character = item is Character ? item : null;
+ if (character == null) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errRejected,
+ 'no character with id "$characterId" in the current list',
+ ),
+ );
+ return;
+ }
+ _settings().saveCharacterState(name, character);
+ _broadcastSettings();
+ }
+
+ /// `{t: loadCharacterSave, saveId}` — runs `LoadCharacterSaveCommand`
+ /// (undoable). [saveId] is the `"$name\n${characterId}"` key.
+ void _handleLoadCharacterSave(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'load character saves')) return;
+ final saveId = message['saveId'];
+ if (saveId is! String || saveId.isEmpty) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errBadRequest,
+ 'loadCharacterSave requires a non-empty "saveId" string',
+ ),
+ );
+ return;
+ }
+ final s = _settings();
+ if (!s.characterSaves.containsKey(saveId)) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errRejected,
+ 'no character save with id "$saveId"',
+ ),
+ );
+ return;
+ }
+ s.loadCharacterSave(saveId);
+ _syncSeats();
+ }
+
+ /// `{t: deleteCharacterSave, saveId}`.
+ void _handleDeleteCharacterSave(_Connection connection, Map<String, dynamic> message) {
+ if (!_requirePlayer(connection, 'delete character saves')) return;
+ final saveId = message['saveId'];
+ if (saveId is! String || saveId.isEmpty) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errBadRequest,
+ 'deleteCharacterSave requires a non-empty "saveId" string',
+ ),
+ );
+ return;
+ }
+ _settings().deleteCharacterSave(saveId);
+ _broadcastSettings();
+ }
+
+ /// The `"name"` field of a save message, with the bad-request answer sent
+ /// when it is missing/wrong-typed (returns null in that case).
+ String? _nonEmptyName(
+ _Connection connection,
+ Map<String, dynamic> message,
+ String verb,
+ ) {
+ final name = message['name'];
+ if (name is! String || name.isEmpty) {
+ connection.send(
+ Protocol.errorMessage(
+ Protocol.errBadRequest,
+ '$verb requires a non-empty "name" string',
+ ),
+ );
+ return null;
+ }
+ return name;
+ }
+
+ /// Broadcasts the current settings payload to every connection (after a
+ /// setting change or a save-list change).
+ void _broadcastSettings() {
+ final payload = _settings().forClient();
+ for (final connection in _activeConnections()) {
+ connection.send(Protocol.settingsMessage(payload));
}
}
@@ -476,6 +753,9 @@ class GameServer {
description: description,
event: event,
state: _serializeFor(connection),
+ historyLength: _gameState.commandDescriptions.length,
+ redoDescription: _redoDescription(),
+ undoDescription: _undoDescription(),
),
);
}