87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
const DatapackLoader = require("./DatapackLoader");
|
|
|
|
class DungeonManager {
|
|
constructor() {
|
|
this.activeSessions = new Map();
|
|
}
|
|
|
|
startDungeon(playerId, dungeonId) {
|
|
const dungeon = DatapackLoader.getDungeon(dungeonId);
|
|
if (!dungeon || !dungeon.rooms?.length) return null;
|
|
|
|
const session = {
|
|
dungeonId,
|
|
currentRoomIndex: 0,
|
|
isFinished: false,
|
|
currentEnemyHp: undefined,
|
|
rewards: { xp: 0, credits: 0, items: [] },
|
|
};
|
|
|
|
this.activeSessions.set(playerId, session);
|
|
return this.getCurrentRoomData(playerId);
|
|
}
|
|
|
|
getCurrentRoomData(playerId) {
|
|
const session = this.activeSessions.get(playerId);
|
|
if (!session) return null;
|
|
|
|
const dungeon = DatapackLoader.getDungeon(session.dungeonId);
|
|
const roomRef = dungeon.rooms[session.currentRoomIndex];
|
|
const roomData = DatapackLoader.getRoom(roomRef.id);
|
|
if (!roomData) return null;
|
|
|
|
const hostiles = (roomData.hostiles || [])
|
|
.map((hId) => {
|
|
const hostile = DatapackLoader.getEnemy(hId);
|
|
return hostile ? { ...hostile } : null;
|
|
})
|
|
.filter(Boolean);
|
|
|
|
return {
|
|
roomIndex: session.currentRoomIndex,
|
|
totalRooms: dungeon.rooms.length,
|
|
config: roomData,
|
|
hostiles,
|
|
};
|
|
}
|
|
|
|
moveToNextRoom(playerId) {
|
|
const session = this.activeSessions.get(playerId);
|
|
if (!session || session.isFinished) return null;
|
|
|
|
const dungeon = DatapackLoader.getDungeon(session.dungeonId);
|
|
const roomRef = dungeon.rooms[session.currentRoomIndex];
|
|
const currentRoom = DatapackLoader.getRoom(roomRef.id);
|
|
|
|
if (currentRoom) {
|
|
session.rewards.xp += currentRoom.gainXp || 0;
|
|
session.rewards.credits += currentRoom.credits || 0;
|
|
|
|
if (currentRoom.loot && Array.isArray(currentRoom.loot)) {
|
|
currentRoom.loot.forEach((item) => {
|
|
session.rewards.items.push({ ...item });
|
|
});
|
|
}
|
|
}
|
|
|
|
session.currentEnemyHp = undefined;
|
|
|
|
if (session.currentRoomIndex < dungeon.rooms.length - 1) {
|
|
session.currentRoomIndex++;
|
|
return this.getCurrentRoomData(playerId);
|
|
}
|
|
|
|
session.isFinished = true;
|
|
return {
|
|
status: "completed",
|
|
rewards: session.rewards,
|
|
};
|
|
}
|
|
|
|
leaveDungeon(playerId) {
|
|
this.activeSessions.delete(playerId);
|
|
}
|
|
}
|
|
|
|
module.exports = new DungeonManager();
|