KF-MMO-Server/Classes/lobbyState.ts

153 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-09-16 05:42:42 +08:00
import { playerData } from "../Classes/playerData";
2023-11-01 10:01:57 +08:00
import { dungeonData } from "./dungeonData";
2023-09-21 02:17:18 +08:00
import { levelState } from "./levelState";
import { userData } from "./userData";
2023-09-16 05:42:42 +08:00
export class lobbyState{
users : userData[] = [];
2023-09-16 05:42:42 +08:00
players : playerData[] = [];
2023-09-21 02:17:18 +08:00
rooms : levelState[] = []
2023-11-01 10:01:57 +08:00
dungeons : dungeonData[] = []
2023-09-16 05:42:42 +08:00
copyLight(){
let lobby = Object.assign({}, this);
lobby.players = []
lobby.users = []
2023-09-16 05:42:42 +08:00
for (let i = 0; i < this.players.length; i++) {
2023-09-18 18:33:06 +08:00
lobby.players.push(this.players[i].copyLight());
2023-09-16 05:42:42 +08:00
}
2023-09-21 02:17:18 +08:00
lobby.rooms = this.rooms
2023-11-01 10:01:57 +08:00
lobby.dungeons = this.dungeons
2023-09-16 05:42:42 +08:00
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){
2023-11-01 10:01:57 +08:00
let player = this.players[idx]
this.players.splice(idx, 1)
2023-11-01 10:01:57 +08:00
this.roomExit(player.room, player)
}
}
findPlayer(id: number){
return this.players.find(p=>p.id == id)
}
2023-11-01 10:01:57 +08:00
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
}
2023-09-16 05:42:42 +08:00
}