diff --git a/apps/server/src/domains/game/GameLoop.ts b/apps/server/src/domains/game/GameLoop.ts index 0e3ffa7..1838556 100644 --- a/apps/server/src/domains/game/GameLoop.ts +++ b/apps/server/src/domains/game/GameLoop.ts @@ -1,6 +1,7 @@ import { Player } from "./entities/player/Player.js"; import { MapStore } from "./entities/map/MapStore"; -import { gridMapLogic, config } from "@repo/shared"; +import { getPlayerGridIndex } from "./entities/player/playerPosition.js"; +import { config } from "@repo/shared"; import type { gridMapTypes } from "@repo/shared"; import { logEvent } from "@server/logging/logEvent"; @@ -52,7 +53,7 @@ const player = this.players.get(id); if (!player) return; - const gridIndex = gridMapLogic.getGridIndexFromPosition(player.x, player.y); + const gridIndex = getPlayerGridIndex(player); if (gridIndex !== null) { this.mapStore.paintCell(gridIndex, player.teamId); } diff --git a/apps/server/src/domains/game/application/services/GameRoomSession.ts b/apps/server/src/domains/game/application/services/GameRoomSession.ts index 845836d..302fda2 100644 --- a/apps/server/src/domains/game/application/services/GameRoomSession.ts +++ b/apps/server/src/domains/game/application/services/GameRoomSession.ts @@ -1,8 +1,12 @@ -import { config } from "@repo/shared"; import { logEvent } from "@server/logging/logEvent"; import { GameLoop, type TickData } from "../../GameLoop"; import { Player } from "../../entities/player/Player.js"; import { MapStore } from "../../entities/map/MapStore"; +import { createSpawnedPlayer } from "../../entities/player/playerSpawn.js"; +import { + isValidPosition, + setPlayerPosition, +} from "../../entities/player/playerMovement.js"; export class GameRoomSession { private players: Map; @@ -15,9 +19,7 @@ this.mapStore = new MapStore(); playerIds.forEach((playerId) => { - const player = new Player(playerId); - player.x = config.GAME_CONFIG.GRID_COLS / 2; - player.y = config.GAME_CONFIG.GRID_ROWS / 2; + const player = createSpawnedPlayer(playerId); this.players.set(playerId, player); }); } @@ -60,7 +62,7 @@ return; } - if (typeof x !== "number" || typeof y !== "number" || isNaN(x) || isNaN(y)) { + if (!isValidPosition(x, y)) { logEvent("GameRoomSession", { event: "MOVE", result: "ignored_invalid_payload", @@ -70,8 +72,7 @@ return; } - player.x = x; - player.y = y; + setPlayerPosition(player, x, y); } public removePlayer(id: string): boolean { diff --git a/apps/server/src/domains/game/entities/player/playerMovement.ts b/apps/server/src/domains/game/entities/player/playerMovement.ts new file mode 100644 index 0000000..b7cd590 --- /dev/null +++ b/apps/server/src/domains/game/entities/player/playerMovement.ts @@ -0,0 +1,14 @@ +import { Player } from "./Player.js"; + +export const isValidPosition = (x: number, y: number): boolean => { + return Number.isFinite(x) && Number.isFinite(y); +}; + +export const setPlayerPosition = ( + player: Player, + x: number, + y: number +): void => { + player.x = x; + player.y = y; +}; diff --git a/apps/server/src/domains/game/entities/player/playerPosition.ts b/apps/server/src/domains/game/entities/player/playerPosition.ts new file mode 100644 index 0000000..f0c825a --- /dev/null +++ b/apps/server/src/domains/game/entities/player/playerPosition.ts @@ -0,0 +1,6 @@ +import { gridMapLogic } from "@repo/shared"; +import { Player } from "./Player.js"; + +export const getPlayerGridIndex = (player: Player): number | null => { + return gridMapLogic.getGridIndexFromPosition(player.x, player.y); +}; diff --git a/apps/server/src/domains/game/entities/player/playerSpawn.ts b/apps/server/src/domains/game/entities/player/playerSpawn.ts new file mode 100644 index 0000000..064a690 --- /dev/null +++ b/apps/server/src/domains/game/entities/player/playerSpawn.ts @@ -0,0 +1,9 @@ +import { config } from "@repo/shared"; +import { Player } from "./Player.js"; + +export const createSpawnedPlayer = (id: string): Player => { + const player = new Player(id); + player.x = config.GAME_CONFIG.GRID_COLS / 2; + player.y = config.GAME_CONFIG.GRID_ROWS / 2; + return player; +};