update
This commit is contained in:
parent
dc8ce750ff
commit
c7a859b9bb
@ -1,55 +1,54 @@
|
|||||||
import Singleton from "../Base/Singleton";
|
import Singleton from "../Base/Singleton"
|
||||||
import { ApiMsgEnum, IApiPlayerJoinReq } from "../Common";
|
import { ApiMsgEnum, IApiPlayerJoinReq } from "../Common"
|
||||||
import { Connection } from "../Core";
|
import { Connection } from "../Core"
|
||||||
import Player from "./Player";
|
import Player from "./Player"
|
||||||
import RoomManager from "./RoomManager";
|
import RoomManager from "./RoomManager"
|
||||||
export default class PlayerManager extends Singleton {
|
export default class PlayerManager extends Singleton {
|
||||||
static get Instance() {
|
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 nextPlayerId = 1
|
||||||
private idMapPlayer: Map<number, Player> = new Map();
|
private idMapPlayer: Map<number, Player> = new Map()
|
||||||
|
|
||||||
createPlayer({ connection, nickname }: IApiPlayerJoinReq & { connection: Connection }) {
|
createPlayer({ connection, nickname }: IApiPlayerJoinReq & { connection: Connection }) {
|
||||||
const player = new Player({ id: this.nextPlayerId++, connection, nickname });
|
const player = new Player({ id: this.nextPlayerId++, connection, nickname })
|
||||||
this.players.add(player);
|
this.players.add(player)
|
||||||
this.idMapPlayer.set(player.id, player);
|
this.idMapPlayer.set(player.id, player)
|
||||||
return player;
|
return player
|
||||||
}
|
}
|
||||||
|
|
||||||
removePlayer(uid: number) {
|
removePlayer(uid: number) {
|
||||||
const player = this.idMapPlayer.get(uid);
|
const player = this.idMapPlayer.get(uid)
|
||||||
if (player) {
|
if (player) {
|
||||||
const rid = player.rid;
|
const rid = player.rid
|
||||||
if (rid !== undefined) {
|
if (rid !== undefined) {
|
||||||
RoomManager.Instance.leaveRoom(rid, uid);
|
RoomManager.Instance.leaveRoom(rid, uid)
|
||||||
RoomManager.Instance.syncRooms();
|
RoomManager.Instance.syncRooms()
|
||||||
RoomManager.Instance.syncRoom(rid);
|
RoomManager.Instance.syncRoom(rid)
|
||||||
}
|
}
|
||||||
this.players.delete(player);
|
this.players.delete(player)
|
||||||
this.idMapPlayer.delete(uid);
|
this.idMapPlayer.delete(uid)
|
||||||
this.syncPlayers();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlayerById(uid: number) {
|
getPlayerById(uid: number) {
|
||||||
return this.idMapPlayer.get(uid);
|
return this.idMapPlayer.get(uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
syncPlayers() {
|
syncPlayers() {
|
||||||
for (const player of this.players) {
|
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) {
|
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) {
|
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}`)
|
console.log(`${getTime()}走人|人数|${server.connections.size}`)
|
||||||
if (connection.playerId) {
|
if (connection.playerId) {
|
||||||
PlayerManager.Instance.removePlayer(connection.playerId)
|
PlayerManager.Instance.removePlayer(connection.playerId)
|
||||||
|
PlayerManager.Instance.syncPlayers()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user