From 1ade2006754cee5fe1c4043871c2a134fa7b381d Mon Sep 17 00:00:00 2001 From: sli97 <775303361@qq.com> Date: Wed, 14 Dec 2022 20:25:40 +0800 Subject: [PATCH] update --- .../client/assets/Scripts/UI/PlayerManager.ts | 20 ++--- apps/server/src/biz/Player.ts | 18 ++-- apps/server/src/biz/Room.ts | 86 +++++++++---------- apps/server/src/index.ts | 1 + 4 files changed, 62 insertions(+), 63 deletions(-) diff --git a/apps/client/assets/Scripts/UI/PlayerManager.ts b/apps/client/assets/Scripts/UI/PlayerManager.ts index 3a8f6af..4caac51 100644 --- a/apps/client/assets/Scripts/UI/PlayerManager.ts +++ b/apps/client/assets/Scripts/UI/PlayerManager.ts @@ -1,19 +1,19 @@ -import { _decorator, Component, Node, Label } from "cc"; -import DataManager from "../Global/DataManager"; -const { ccclass, property } = _decorator; +import { _decorator, Component, Node, Label } from "cc" +import DataManager from "../Global/DataManager" +const { ccclass, property } = _decorator @ccclass("PlayerManager") export class PlayerManager extends Component { init({ id, nickname, rid }: { id: number; nickname: string; rid: number }) { - const label = this.getComponent(Label); - let str = nickname; + const label = this.getComponent(Label) + let str = nickname if (DataManager.Instance.myPlayerId === id) { - str += `(我)`; + str += `(我)` } - if (rid !== -1) { - str += `(房间${rid})`; + if (rid) { + str += `(房间${rid})` } - label.string = str; - this.node.active = true; + label.string = str + this.node.active = true } } diff --git a/apps/server/src/biz/Player.ts b/apps/server/src/biz/Player.ts index f375c38..e10eca6 100644 --- a/apps/server/src/biz/Player.ts +++ b/apps/server/src/biz/Player.ts @@ -1,16 +1,14 @@ -import { Connection } from "../Core"; +import { Connection } from "../Core" export default class Player { - id: number; - nickname: string; - connection: Connection; - rid: number; + id: number + nickname: string + connection: Connection + rid: number constructor({ id, nickname, connection }: Pick) { - this.id = id; - this.nickname = nickname; - this.connection = connection; - this.connection.playerId = this.id; - this.rid = -1; + this.id = id + this.nickname = nickname + this.connection = connection } } diff --git a/apps/server/src/biz/Room.ts b/apps/server/src/biz/Room.ts index 32d9bd3..d341dcb 100644 --- a/apps/server/src/biz/Room.ts +++ b/apps/server/src/biz/Room.ts @@ -1,57 +1,57 @@ -import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common"; -import { Connection } from "../Core"; -import type Player from "./Player"; -import PlayerManager from "./PlayerManager"; -import RoomManager from "./RoomManager"; +import { ApiMsgEnum, EntityTypeEnum, IClientInput, IMsgClientSync, InputTypeEnum, IState, toFixed } from "../Common" +import { Connection } from "../Core" +import type Player from "./Player" +import PlayerManager from "./PlayerManager" +import RoomManager from "./RoomManager" export default class Room { - id: number; - players: Set = new Set(); + id: number + players: Set = new Set() - private lastTime?: number; - private timers: NodeJS.Timer[] = []; - private pendingInput: Array = []; - private lastPlayerFrameIdMap: Map = new Map(); + private lastTime?: number + private timers: NodeJS.Timer[] = [] + private pendingInput: Array = [] + private lastPlayerFrameIdMap: Map = new Map() constructor(rid: number) { - this.id = rid; + this.id = rid } join(uid: number) { - const player = PlayerManager.Instance.getPlayerById(uid); + const player = PlayerManager.Instance.getPlayerById(uid) if (player) { - player.rid = this.id; - this.players.add(player); + player.rid = this.id + this.players.add(player) } } leave(uid: number) { - const player = PlayerManager.Instance.getPlayerById(uid); + const player = PlayerManager.Instance.getPlayerById(uid) if (player) { - player.rid = -1; - player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this); - this.players.delete(player); + player.rid = undefined + player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this) + this.players.delete(player) if (!this.players.size) { - RoomManager.Instance.closeRoom(this.id); + RoomManager.Instance.closeRoom(this.id) } } } close() { - this.timers.forEach((t) => clearInterval(t)); + this.timers.forEach((t) => clearInterval(t)) for (const player of this.players) { - player.rid = -1; - player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {}); - player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this); + player.rid = undefined + player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {}) + player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this) } - this.players.clear(); + this.players.clear() } sync() { for (const player of this.players) { player.connection.sendMsg(ApiMsgEnum.MsgRoom, { room: RoomManager.Instance.getRoomView(this), - }); + }) } } @@ -75,47 +75,47 @@ export default class Room { })), bullets: [], nextBulletId: 1, - }; + } for (const player of this.players) { player.connection.sendMsg(ApiMsgEnum.MsgGameStart, { state, - }); - player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this); + }) + player.connection.listenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this) } let t1 = setInterval(() => { - this.sendServerMsg(); - }, 100); + this.sendServerMsg() + }, 100) let t2 = setInterval(() => { - this.timePast(); - }, 16); - this.timers = [t1, t2]; + this.timePast() + }, 16) + this.timers = [t1, t2] } getClientMsg(connection: Connection, { frameId, input }: IMsgClientSync) { - this.lastPlayerFrameIdMap.set(connection.playerId, frameId); - this.pendingInput.push(input); + this.lastPlayerFrameIdMap.set(connection.playerId, frameId) + this.pendingInput.push(input) } sendServerMsg() { - const pendingInput = this.pendingInput; - this.pendingInput = []; + const pendingInput = this.pendingInput + this.pendingInput = [] for (const player of this.players) { player.connection.sendMsg(ApiMsgEnum.MsgServerSync, { lastFrameId: this.lastPlayerFrameIdMap.get(player.id) ?? 0, inputs: pendingInput, - }); + }) } } timePast() { - let now = process.uptime(); - const dt = now - (this.lastTime ?? now); + let now = process.uptime() + const dt = now - (this.lastTime ?? now) this.pendingInput.push({ type: InputTypeEnum.TimePast, dt: toFixed(dt), - }); - this.lastTime = now; + }) + this.lastTime = now } } diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 3e504b1..23cf2c1 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -50,6 +50,7 @@ server.setApi(ApiMsgEnum.ApiPlayerList, (connection: Connection, data: IApiPlaye server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, { nickname }: IApiPlayerJoinReq): IApiPlayerJoinRes => { const player = PlayerManager.Instance.createPlayer({ connection, nickname }) + connection.playerId = player.id PlayerManager.Instance.syncPlayers() return { player: PlayerManager.Instance.getPlayerView(player),