mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-11-17 20:43:56 +01:00
Begin payment logic and tests
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* MovementManager
|
||||
* Handles rate limiting and applying player movement to games.
|
||||
*/
|
||||
class MovementManager {
|
||||
constructor({ activeGames, io, debugManager, moveCooldown = 100, postMoveHook = null }) {
|
||||
this.activeGames = activeGames; // Map socketId -> Game
|
||||
this.io = io;
|
||||
this.debugManager = debugManager;
|
||||
this.moveCooldown = moveCooldown;
|
||||
this._lastMove = new Map(); // socketId -> timestamp
|
||||
this.postMoveHook = typeof postMoveHook === 'function' ? postMoveHook : null;
|
||||
}
|
||||
|
||||
handleMove(socketId, moveData) {
|
||||
if (!moveData || typeof moveData.direction !== 'string') return;
|
||||
const now = Date.now();
|
||||
const last = this._lastMove.get(socketId) || 0;
|
||||
if (now - last < this.moveCooldown) return; // rate limit
|
||||
this._lastMove.set(socketId, now);
|
||||
|
||||
const game = this.activeGames.get(socketId);
|
||||
if (!game) return; // not in a game
|
||||
|
||||
const dir = moveData.direction;
|
||||
let dx = 0, dy = 0;
|
||||
switch (dir) {
|
||||
case 'up': dy = -1; break;
|
||||
case 'down': dy = 1; break;
|
||||
case 'left': dx = -1; break;
|
||||
case 'right': dx = 1; break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
let moveResult = null;
|
||||
if (typeof game.movePlayer === 'function') {
|
||||
moveResult = game.movePlayer(dx, dy);
|
||||
}
|
||||
|
||||
// Allow hook to perform monster movement or other side-effects BEFORE we snapshot state
|
||||
if (moveResult && moveResult.status === 'moved' && this.postMoveHook) {
|
||||
try {
|
||||
this.postMoveHook({ socketId, game, moveResult });
|
||||
} catch (e) {
|
||||
console.error('postMoveHook error:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Build state after player + potential monster move
|
||||
let state;
|
||||
if (typeof game.getState === 'function') {
|
||||
state = game.getState();
|
||||
} else {
|
||||
state = { player: game.player };
|
||||
}
|
||||
if (this.debugManager && typeof this.debugManager.getCurrentBlockHeight === 'function') {
|
||||
state.blockHeight = this.debugManager.getCurrentBlockHeight();
|
||||
}
|
||||
|
||||
this.io.to(socketId).emit('game_update', state);
|
||||
|
||||
// Handle special events from moveResult (escape / treasure)
|
||||
if (moveResult && moveResult.event) {
|
||||
this.io.to(socketId).emit('game_event', { event: moveResult.event });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MovementManager;
|
||||
Reference in New Issue
Block a user