131 lines
3.5 KiB
TypeScript
131 lines
3.5 KiB
TypeScript
import { Socket } from "socket.io";
|
|
import { characterData } from "./characterData.js";
|
|
import { dungeonData } from "./dungeonData.js";
|
|
import { levelState } from "./levelState.js";
|
|
import { lobbyStateLight } from "./Outgoing/lobbyStatePartial.js";
|
|
|
|
export class lobbyState{
|
|
activeUsers : Socket[] = []
|
|
rooms : levelState[] = []
|
|
dungeons : dungeonData[] = []
|
|
|
|
copyLight(){
|
|
return new lobbyStateLight(this)
|
|
}
|
|
|
|
addUser(socket : Socket){
|
|
let uIdx = this.activeUsers.findIndex(u=>u === socket)
|
|
if(uIdx != -1){
|
|
console.log("User " + socket.user.login + " is already in the game!")
|
|
return false;
|
|
}
|
|
|
|
this.activeUsers.push(socket)
|
|
return true
|
|
}
|
|
|
|
removeUser(socket : Socket){
|
|
this.roomExit(socket.character.room, socket.character)
|
|
this.activeUsers = this.activeUsers.filter(u=>u !== socket)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
removeRoom(room : levelState){
|
|
this.rooms = this.rooms.filter(r => r !== room)
|
|
}
|
|
|
|
createRoom(id : string, host : number){
|
|
let room = new levelState(id, host)
|
|
this.rooms.push(room)
|
|
return room
|
|
}
|
|
|
|
roomEnter(targetRoom : levelState, player: characterData){
|
|
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: characterData){
|
|
let room = this.getRoom(roomId, false)
|
|
if(room != null){
|
|
if(room.removePlayer(player)){
|
|
if(room.isDungeon){
|
|
let dungeon = this.getDungeon(this.getDungeonRoot(roomId), false)
|
|
dungeon.playerCount -= 1;
|
|
this.tryRemoveDungeon(room.id)
|
|
}
|
|
else if(room.hostId == -1){
|
|
this.removeRoom(room)
|
|
}
|
|
}
|
|
|
|
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=>{
|
|
this.removeRoom(room)
|
|
})
|
|
}
|
|
|
|
return true
|
|
}
|
|
} |