Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-01-30 03:01:14 +05:30
parent 274c0b98e3
commit 7a55f6c376
9 changed files with 90 additions and 6 deletions
+17
View File
@@ -5,6 +5,18 @@ const timeoutManager = require('./timeoutManager.js');
const PawnSchema = require('./pawn');
const PlayerSchema = require('./player');
// Safe/colored box positions in Ludo (where pawns cannot be killed)
const SAFE_POSITIONS = [16, 29, 42, 55];
const HOME_ENTRY_POSITIONS = [66, 27, 40, 53];
const STAR_POSITIONS = [63, 24, 37, 50];
const isSafePosition = (position) => {
return SAFE_POSITIONS.includes(position)
|| HOME_ENTRY_POSITIONS.includes(position)
|| STAR_POSITIONS.includes(position)
|| position > 66; // Also safe in home stretch
};
const RoomSchema = new mongoose.Schema({
name: String,
private: { type: Boolean, default: false },
@@ -36,6 +48,11 @@ const RoomSchema = new mongoose.Schema({
});
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
// Do not beat pawns on safe/colored positions
if (isSafePosition(position)) {
return;
}
const pawnsOnPosition = this.pawns.filter(pawn => pawn.position === position);
pawnsOnPosition.forEach(pawn => {
if (pawn.color !== attackingPawnColor) {