diff --git a/apps/server/src/biz/PlayerManager.ts b/apps/server/src/biz/PlayerManager.ts index 137baff..7e716bd 100644 --- a/apps/server/src/biz/PlayerManager.ts +++ b/apps/server/src/biz/PlayerManager.ts @@ -1,55 +1,54 @@ -import Singleton from "../Base/Singleton"; -import { ApiMsgEnum, IApiPlayerJoinReq } from "../Common"; -import { Connection } from "../Core"; -import Player from "./Player"; -import RoomManager from "./RoomManager"; +import Singleton from "../Base/Singleton" +import { ApiMsgEnum, IApiPlayerJoinReq } from "../Common" +import { Connection } from "../Core" +import Player from "./Player" +import RoomManager from "./RoomManager" export default class PlayerManager extends Singleton { static get Instance() { - return super.GetInstance(); + return super.GetInstance() } - players: Set = new Set(); + players: Set = new Set() - private nextPlayerId = 1; - private idMapPlayer: Map = new Map(); + private nextPlayerId = 1 + private idMapPlayer: Map = new Map() createPlayer({ connection, nickname }: IApiPlayerJoinReq & { connection: Connection }) { - const player = new Player({ id: this.nextPlayerId++, connection, nickname }); - this.players.add(player); - this.idMapPlayer.set(player.id, player); - return player; + const player = new Player({ id: this.nextPlayerId++, connection, nickname }) + this.players.add(player) + this.idMapPlayer.set(player.id, player) + return player } removePlayer(uid: number) { - const player = this.idMapPlayer.get(uid); + const player = this.idMapPlayer.get(uid) if (player) { - const rid = player.rid; + const rid = player.rid if (rid !== undefined) { - RoomManager.Instance.leaveRoom(rid, uid); - RoomManager.Instance.syncRooms(); - RoomManager.Instance.syncRoom(rid); + RoomManager.Instance.leaveRoom(rid, uid) + RoomManager.Instance.syncRooms() + RoomManager.Instance.syncRoom(rid) } - this.players.delete(player); - this.idMapPlayer.delete(uid); - this.syncPlayers(); + this.players.delete(player) + this.idMapPlayer.delete(uid) } } getPlayerById(uid: number) { - return this.idMapPlayer.get(uid); + return this.idMapPlayer.get(uid) } syncPlayers() { for (const player of this.players) { - player.connection.sendMsg(ApiMsgEnum.MsgPlayerList, { list: this.getPlayersView() }); + player.connection.sendMsg(ApiMsgEnum.MsgPlayerList, { list: this.getPlayersView() }) } } getPlayersView(players: Set = this.players) { - return [...players].map((player) => this.getPlayerView(player)); + return [...players].map((player) => this.getPlayerView(player)) } getPlayerView({ id, nickname, rid }: Player) { - return { id, nickname, rid }; + return { id, nickname, rid } } } diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 23cf2c1..e9781f3 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -40,6 +40,7 @@ server.on(MyServerEventEnum.DisConnect, (connection: Connection) => { console.log(`${getTime()}走人|人数|${server.connections.size}`) if (connection.playerId) { PlayerManager.Instance.removePlayer(connection.playerId) + PlayerManager.Instance.syncPlayers() } })