import { playerData } from "../Classes/playerData"; import { dungeonData } from "./dungeonData"; import { levelState } from "./levelState"; import { userData } from "./userData"; export class lobbyState{ users : userData[] = []; players : playerData[] = []; rooms : levelState[] = [] dungeons : dungeonData[] = [] copyLight(){ let lobby = Object.assign({}, this); lobby.players = [] lobby.users = [] for (let i = 0; i < this.players.length; i++) { lobby.players.push(this.players[i].copyUpdate()); } lobby.rooms = this.rooms lobby.dungeons = this.dungeons return lobby } addUser(userData, playerData){ let uIdx = this.users.findIndex(u=>u.id == userData.id) let pIdx = this.players.findIndex(u=>u.id == playerData.id) if(uIdx != -1 || pIdx != -1){ console.log("User " + userData.login + " is already in the game!") return false; } this.users.push(userData) this.players.push(playerData) return true } removeUser(id){ let idx = this.users.findIndex(u=>u.id == id) if(idx != -1){ this.users.splice(idx, 1) } idx = this.players.findIndex(u=>u.id == id) if(idx != -1){ let player = this.players[idx] this.players.splice(idx, 1) this.roomExit(player.room, player) } } findPlayer(id: number){ return this.players.find(p=>p.id == id) } getDungeonRoot(name){ let split = name.split("_") return split[0] + "_" + split[1] + "_0_0" } getDungeon(id : string, create : boolean){ let dungeon = this.dungeons.find(r=>r.entranceId == id) if(dungeon == null && create){ dungeon = new dungeonData(id) this.dungeons.push(dungeon) } return dungeon } getRoom(id: string, create : boolean){ let room = this.rooms.find(r=>r.id == id) if(room == null && create){ room = new levelState(id, -1) this.rooms.push(room) } return room } createRoom(id : string, host : number){ let room = new levelState(id, host) this.rooms.push(room) return room } roomEnter(targetRoom : levelState, player: playerData){ if(!targetRoom.isDungeon){ let room = this.getRoom(targetRoom.id, true) room.addPlayer(player) return room } else{ let dungeon = this.getDungeon(this.getDungeonRoot(targetRoom.id), true); let room = dungeon.getRoom(targetRoom.id, true) if(room.addPlayer(player)){ dungeon.playerCount += 1; } return room } } roomComplete(roomId: string){ let room = this.getRoom(roomId, false) if(room != null){ room.completed = true return room } return null } roomExit(roomId : string, player: playerData){ let room = this.getRoom(roomId, false) if(room != null){ if(room.removePlayer(player.id)){ if(room.isDungeon){ let dungeon = this.getDungeon(this.getDungeonRoot(roomId), false) dungeon.playerCount -= 1; this.tryRemoveDungeon(room.id) } else if(room.hostId == -1){ let roomIdx = this.rooms.indexOf(room) this.rooms.splice(roomIdx, 1) } } return room } return null } tryRemoveDungeon(roomId : string){ let dungeonIdx = this.dungeons.findIndex(r=>r.entranceId == this.getDungeonRoot(roomId)) if(dungeonIdx == -1) return false let dungeon = this.dungeons[dungeonIdx] if(dungeon.playerCount == 0){ this.dungeons.splice(dungeonIdx, 1) dungeon.rooms.forEach(room=>{ let roomIdx = this.rooms.findIndex(r=>r.id == room.id) this.rooms.splice(roomIdx, 1) }) } return true } }