update
This commit is contained in:
parent
dc8ce750ff
commit
c7a859b9bb
@ -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<PlayerManager>();
|
||||
return super.GetInstance<PlayerManager>()
|
||||
}
|
||||
|
||||
players: Set<Player> = new Set();
|
||||
players: Set<Player> = new Set()
|
||||
|
||||
private nextPlayerId = 1;
|
||||
private idMapPlayer: Map<number, Player> = new Map();
|
||||
private nextPlayerId = 1
|
||||
private idMapPlayer: Map<number, Player> = 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<Player> = 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 }
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user