@rpgjs/server 5.0.0-alpha.9 → 5.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/dist/Gui/DialogGui.d.ts +5 -0
  2. package/dist/Gui/GameoverGui.d.ts +23 -0
  3. package/dist/Gui/Gui.d.ts +6 -0
  4. package/dist/Gui/MenuGui.d.ts +22 -3
  5. package/dist/Gui/NotificationGui.d.ts +1 -2
  6. package/dist/Gui/SaveLoadGui.d.ts +13 -0
  7. package/dist/Gui/ShopGui.d.ts +28 -3
  8. package/dist/Gui/TitleGui.d.ts +23 -0
  9. package/dist/Gui/index.d.ts +10 -1
  10. package/dist/Player/BattleManager.d.ts +44 -32
  11. package/dist/Player/ClassManager.d.ts +24 -4
  12. package/dist/Player/ComponentManager.d.ts +95 -32
  13. package/dist/Player/Components.d.ts +345 -0
  14. package/dist/Player/EffectManager.d.ts +50 -4
  15. package/dist/Player/ElementManager.d.ts +77 -4
  16. package/dist/Player/GoldManager.d.ts +1 -1
  17. package/dist/Player/GuiManager.d.ts +87 -4
  18. package/dist/Player/ItemFixture.d.ts +1 -1
  19. package/dist/Player/ItemManager.d.ts +431 -4
  20. package/dist/Player/MoveManager.d.ts +301 -34
  21. package/dist/Player/ParameterManager.d.ts +364 -28
  22. package/dist/Player/Player.d.ts +558 -14
  23. package/dist/Player/SkillManager.d.ts +187 -13
  24. package/dist/Player/StateManager.d.ts +75 -4
  25. package/dist/Player/VariableManager.d.ts +62 -4
  26. package/dist/RpgServer.d.ts +278 -63
  27. package/dist/RpgServerEngine.d.ts +2 -1
  28. package/dist/decorators/event.d.ts +46 -0
  29. package/dist/decorators/map.d.ts +299 -0
  30. package/dist/index.d.ts +10 -0
  31. package/dist/index.js +17920 -29711
  32. package/dist/index.js.map +1 -1
  33. package/dist/logs/log.d.ts +2 -3
  34. package/dist/module-CaCW1SDh.js +11018 -0
  35. package/dist/module-CaCW1SDh.js.map +1 -0
  36. package/dist/module.d.ts +43 -1
  37. package/dist/node/connection.d.ts +51 -0
  38. package/dist/node/index.d.ts +5 -0
  39. package/dist/node/index.js +551 -0
  40. package/dist/node/index.js.map +1 -0
  41. package/dist/node/map.d.ts +16 -0
  42. package/dist/node/room.d.ts +21 -0
  43. package/dist/node/transport.d.ts +28 -0
  44. package/dist/node/types.d.ts +47 -0
  45. package/dist/presets/index.d.ts +0 -9
  46. package/dist/rooms/BaseRoom.d.ts +132 -0
  47. package/dist/rooms/lobby.d.ts +10 -2
  48. package/dist/rooms/map.d.ts +1359 -32
  49. package/dist/services/save.d.ts +43 -0
  50. package/dist/storage/index.d.ts +1 -0
  51. package/dist/storage/localStorage.d.ts +23 -0
  52. package/package.json +25 -10
  53. package/src/Gui/DialogGui.ts +19 -4
  54. package/src/Gui/GameoverGui.ts +39 -0
  55. package/src/Gui/Gui.ts +23 -1
  56. package/src/Gui/MenuGui.ts +155 -6
  57. package/src/Gui/NotificationGui.ts +1 -2
  58. package/src/Gui/SaveLoadGui.ts +60 -0
  59. package/src/Gui/ShopGui.ts +146 -16
  60. package/src/Gui/TitleGui.ts +39 -0
  61. package/src/Gui/index.ts +15 -2
  62. package/src/Player/BattleManager.ts +39 -56
  63. package/src/Player/ClassManager.ts +82 -74
  64. package/src/Player/ComponentManager.ts +394 -32
  65. package/src/Player/Components.ts +380 -0
  66. package/src/Player/EffectManager.ts +50 -96
  67. package/src/Player/ElementManager.ts +74 -152
  68. package/src/Player/GuiManager.ts +125 -14
  69. package/src/Player/ItemManager.ts +747 -341
  70. package/src/Player/MoveManager.ts +1532 -750
  71. package/src/Player/ParameterManager.ts +636 -106
  72. package/src/Player/Player.ts +1273 -79
  73. package/src/Player/SkillManager.ts +558 -197
  74. package/src/Player/StateManager.ts +131 -258
  75. package/src/Player/VariableManager.ts +85 -157
  76. package/src/RpgServer.ts +293 -62
  77. package/src/decorators/event.ts +61 -0
  78. package/src/decorators/map.ts +343 -0
  79. package/src/index.ts +11 -1
  80. package/src/logs/log.ts +10 -3
  81. package/src/module.ts +126 -3
  82. package/src/node/connection.ts +254 -0
  83. package/src/node/index.ts +22 -0
  84. package/src/node/map.ts +328 -0
  85. package/src/node/room.ts +63 -0
  86. package/src/node/transport.ts +532 -0
  87. package/src/node/types.ts +61 -0
  88. package/src/presets/index.ts +1 -10
  89. package/src/rooms/BaseRoom.ts +232 -0
  90. package/src/rooms/lobby.ts +25 -7
  91. package/src/rooms/map.ts +2682 -206
  92. package/src/services/save.ts +147 -0
  93. package/src/storage/index.ts +1 -0
  94. package/src/storage/localStorage.ts +76 -0
  95. package/tests/battle.spec.ts +375 -0
  96. package/tests/change-map.spec.ts +72 -0
  97. package/tests/class.spec.ts +274 -0
  98. package/tests/custom-websocket.spec.ts +127 -0
  99. package/tests/effect.spec.ts +219 -0
  100. package/tests/element.spec.ts +221 -0
  101. package/tests/event.spec.ts +80 -0
  102. package/tests/gold.spec.ts +99 -0
  103. package/tests/item.spec.ts +609 -0
  104. package/tests/module.spec.ts +38 -0
  105. package/tests/move.spec.ts +601 -0
  106. package/tests/node-transport.spec.ts +223 -0
  107. package/tests/player-param.spec.ts +45 -0
  108. package/tests/prediction-reconciliation.spec.ts +182 -0
  109. package/tests/random-move.spec.ts +65 -0
  110. package/tests/skill.spec.ts +658 -0
  111. package/tests/state.spec.ts +467 -0
  112. package/tests/variable.spec.ts +185 -0
  113. package/tests/world-maps.spec.ts +896 -0
  114. package/vite.config.ts +36 -3
@@ -1,4 +1,6 @@
1
1
  import { Constructor, PlayerCtor } from "@rpgjs/common";
2
+ import { signal } from "@signe/reactive";
3
+ import { type } from "@signe/sync";
2
4
 
3
5
  /**
4
6
  * Variable Manager Mixin
@@ -6,6 +8,11 @@ import { Constructor, PlayerCtor } from "@rpgjs/common";
6
8
  * Provides variable management capabilities to any class. Variables are key-value
7
9
  * pairs that can store any type of data associated with the player, such as
8
10
  * quest progress, game flags, inventory state, and custom game data.
11
+ *
12
+ * Player variables have two main roles:
13
+ *
14
+ * 1. Persist player-specific state so it can be restored from saves.
15
+ * 2. Carry that state across maps and map servers through the player snapshot.
9
16
  *
10
17
  * @param Base - The base class to extend with variable management
11
18
  * @returns Extended class with variable management methods
@@ -25,183 +32,104 @@ import { Constructor, PlayerCtor } from "@rpgjs/common";
25
32
  */
26
33
  export function WithVariableManager<TBase extends PlayerCtor>(Base: TBase) {
27
34
  return class extends Base {
28
- variables: Map<string, any> = new Map();
35
+ variables = type(signal<Record<string, any>>({}) as any, 'variables', { persist: true }, this as any);
29
36
 
30
- /**
31
- * Assign a variable to the player
32
- *
33
- * Stores a key-value pair in the player's variable map. This is useful for
34
- * tracking game state, quest progress, flags, and other player-specific data.
35
- * The variable system provides a flexible way to store any type of data
36
- * associated with the player that persists throughout the game session.
37
- *
38
- * @param key - The variable identifier (string key to reference the variable)
39
- * @param val - The value to store (can be any type: boolean, number, string, object, array)
40
- * @returns void
41
- *
42
- * @example
43
- * ```ts
44
- * // Set different types of variables
45
- * player.setVariable('CHEST_OPENED', true);
46
- * player.setVariable('playerLevel', 5);
47
- * player.setVariable('questProgress', { step: 1, completed: false });
48
- * player.setVariable('inventory', ['sword', 'potion', 'key']);
49
- * player.setVariable('lastSaveTime', new Date().toISOString());
50
- * ```
51
- */
52
37
  setVariable(key: string, val: any): void {
53
- this.variables.set(key, val);
38
+ this.variables.mutate((variables) => {
39
+ variables[key] = val;
40
+ });
54
41
  }
55
42
 
56
- /**
57
- * Get a variable value
58
- *
59
- * Retrieves the value associated with the given key from the player's variables.
60
- * Returns undefined if the variable doesn't exist. This method is type-safe
61
- * and can be used with generic types for better TypeScript support.
62
- *
63
- * @param key - The variable identifier to retrieve
64
- * @returns The stored value or undefined if not found
65
- *
66
- * @example
67
- * ```ts
68
- * // Get variables with type inference
69
- * const hasKey = player.getVariable('CHEST_OPENED'); // boolean | undefined
70
- * const level = player.getVariable('playerLevel'); // number | undefined
71
- * const quest = player.getVariable('questProgress'); // object | undefined
72
- * const missing = player.getVariable('nonexistent'); // undefined
73
- *
74
- * // Use with default values
75
- * const level = player.getVariable('playerLevel') ?? 1;
76
- * const isChestOpened = player.getVariable('CHEST_OPENED') ?? false;
77
- * ```
78
- */
79
43
  getVariable<U = any>(key: string): U | undefined {
80
- return this.variables.get(key);
44
+ return this.variables()[key];
81
45
  }
82
46
 
83
- /**
84
- * Remove a variable
85
- *
86
- * Deletes a variable from the player's variable map. This is useful for
87
- * cleaning up temporary flags, resetting certain game states, or managing
88
- * memory by removing unused variables. The method returns a boolean indicating
89
- * whether the variable existed and was successfully removed.
90
- *
91
- * @param key - The variable identifier to remove
92
- * @returns true if a variable existed and has been removed, false if the variable does not exist
93
- *
94
- * @example
95
- * ```ts
96
- * // Remove variables and check if they existed
97
- * const removed = player.removeVariable('CHEST_OPENED'); // true if existed
98
- * const notFound = player.removeVariable('nonexistent'); // false
99
- *
100
- * // Clean up temporary variables
101
- * player.removeVariable('tempQuestFlag');
102
- * player.removeVariable('battleTempData');
103
- *
104
- * // Conditional removal
105
- * if (player.getVariable('questCompleted')) {
106
- * player.removeVariable('questProgress');
107
- * }
108
- * ```
109
- */
110
47
  removeVariable(key: string): boolean {
111
- return this.variables.delete(key);
48
+ const variables = this.variables();
49
+ if (!(key in variables)) {
50
+ return false;
51
+ }
52
+ this.variables.mutate((draft) => {
53
+ delete draft[key];
54
+ });
55
+ return true;
112
56
  }
113
57
 
114
- /**
115
- * Check if a variable exists
116
- *
117
- * Determines whether a variable with the given key exists in the player's
118
- * variable map, regardless of its value (including falsy values like false, 0, '').
119
- * This is useful when you need to distinguish between a variable that doesn't
120
- * exist and one that has a falsy value.
121
- *
122
- * @param key - The variable identifier to check
123
- * @returns true if the variable exists, false otherwise
124
- *
125
- * @example
126
- * ```ts
127
- * // Check variable existence
128
- * player.setVariable('flag', false);
129
- * player.hasVariable('flag'); // true (even though value is false)
130
- * player.hasVariable('missing'); // false
131
- *
132
- * // Use in conditional logic
133
- * if (player.hasVariable('questStarted')) {
134
- * // Quest has been started, check progress
135
- * const progress = player.getVariable('questProgress');
136
- * } else {
137
- * // Quest not started yet
138
- * player.setVariable('questStarted', true);
139
- * }
140
- * ```
141
- */
142
58
  hasVariable(key: string): boolean {
143
- return this.variables.has(key);
59
+ return key in this.variables();
144
60
  }
145
61
 
146
- /**
147
- * Get all variable keys
148
- *
149
- * Returns an array of all variable keys currently stored for this player.
150
- * This is useful for debugging, serialization, or iterating over all variables.
151
- * The keys are returned in insertion order.
152
- *
153
- * @returns Array of all variable keys
154
- *
155
- * @example
156
- * ```ts
157
- * // Get all variable keys
158
- * const keys = player.getVariableKeys();
159
- * console.log('Player has variables:', keys);
160
- *
161
- * // Iterate over all variables
162
- * keys.forEach(key => {
163
- * const value = player.getVariable(key);
164
- * console.log(`${key}: ${value}`);
165
- * });
166
- *
167
- * // Filter specific variable types
168
- * const questKeys = keys.filter(key => key.startsWith('quest_'));
169
- * ```
170
- */
171
62
  getVariableKeys(): string[] {
172
- return Array.from(this.variables.keys());
63
+ return Object.keys(this.variables());
173
64
  }
174
65
 
175
- /**
176
- * Clear all variables
177
- *
178
- * Removes all variables from the player's variable map. This is useful for
179
- * resetting the player state, cleaning up before saving, or starting fresh.
180
- * Use with caution as this operation cannot be undone.
181
- *
182
- * @returns void
183
- *
184
- * @example
185
- * ```ts
186
- * // Clear all variables (use with caution)
187
- * player.clearVariables();
188
- *
189
- * // Clear variables conditionally
190
- * if (gameReset) {
191
- * player.clearVariables();
192
- * // Re-initialize essential variables
193
- * player.setVariable('gameStarted', true);
194
- * }
195
- * ```
196
- */
197
66
  clearVariables(): void {
198
- this.variables.clear();
67
+ this.variables.set({});
199
68
  }
200
69
  } as unknown as TBase;
201
70
  }
202
71
 
203
72
  /**
204
- * Type helper to extract the interface from the WithVariableManager mixin
205
- * This provides the type without duplicating method signatures
73
+ * Interface for Variable Manager functionality
74
+ *
75
+ * Provides variable management capabilities including storing, retrieving, and managing
76
+ * key-value pairs for player-specific data. This interface defines the public API
77
+ * of the VariableManager mixin.
206
78
  */
207
- export type IVariableManager = InstanceType<ReturnType<typeof WithVariableManager>>;
79
+ export interface IVariableManager {
80
+ /**
81
+ * Map storing all player variables.
82
+ *
83
+ * These values belong to the player, are persisted, and travel with the
84
+ * player snapshot when switching maps or servers.
85
+ */
86
+ variables: Map<string, any>;
87
+
88
+ /**
89
+ * Assign a variable to the player.
90
+ *
91
+ * Use player variables for quest flags, per-player event state, and any value
92
+ * that must survive saves and map transitions.
93
+ *
94
+ * @param key - The variable identifier
95
+ * @param val - The value to store
96
+ * @memberof VariableManager
97
+ */
98
+ setVariable(key: string, val: any): void;
99
+
100
+ /**
101
+ * Get a variable value
102
+ *
103
+ * @param key - The variable identifier to retrieve
104
+ * @returns The stored value or undefined if not found
105
+ */
106
+ getVariable<U = any>(key: string): U | undefined;
107
+
108
+ /**
109
+ * Remove a variable
110
+ *
111
+ * @param key - The variable identifier to remove
112
+ * @returns true if a variable existed and has been removed, false otherwise
113
+ */
114
+ removeVariable(key: string): boolean;
115
+
116
+ /**
117
+ * Check if a variable exists
118
+ *
119
+ * @param key - The variable identifier to check
120
+ * @returns true if the variable exists, false otherwise
121
+ */
122
+ hasVariable(key: string): boolean;
123
+
124
+ /**
125
+ * Get all variable keys
126
+ *
127
+ * @returns Array of all variable keys
128
+ */
129
+ getVariableKeys(): string[];
130
+
131
+ /**
132
+ * Clear all variables
133
+ */
134
+ clearVariables(): void;
135
+ }