Compare commits
No commits in common. "dev" and "master" have entirely different histories.
@ -1,20 +1,19 @@
|
||||
import { _decorator, Component, Node, Label } from "cc"
|
||||
import { IPlayer } from "../Common"
|
||||
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 }: IPlayer) {
|
||||
const label = this.getComponent(Label)
|
||||
let str = nickname
|
||||
init({ id, nickname, rid }: { id: number; nickname: string; rid: number }) {
|
||||
const label = this.getComponent(Label);
|
||||
let str = nickname;
|
||||
if (DataManager.Instance.myPlayerId === id) {
|
||||
str += `(我)`
|
||||
str += `(我)`;
|
||||
}
|
||||
if (rid) {
|
||||
str += `(房间${rid})`
|
||||
if (rid !== -1) {
|
||||
str += `(房间${rid})`;
|
||||
}
|
||||
label.string = str
|
||||
this.node.active = true
|
||||
label.string = str;
|
||||
this.node.active = true;
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
import { _decorator, Component, Node, Label } from "cc"
|
||||
import { IRoom } from "../Common"
|
||||
import { EventEnum } from "../Enum"
|
||||
import EventManager from "../Global/EventManager"
|
||||
const { ccclass, property } = _decorator
|
||||
import { _decorator, Component, Node, Label } from "cc";
|
||||
import { EventEnum } from "../Enum";
|
||||
import EventManager from "../Global/EventManager";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("RoomManager")
|
||||
export class RoomManager extends Component {
|
||||
id: number
|
||||
init({ id, players }: IRoom) {
|
||||
this.id = id
|
||||
const label = this.getComponent(Label)
|
||||
label.string = `房间id:${id},当前人数:${players.length}`
|
||||
this.node.active = true
|
||||
id: number;
|
||||
init({ id, players }: { id: number; players: Array<{ id: number; nickname: string }> }) {
|
||||
this.id = id;
|
||||
const label = this.getComponent(Label);
|
||||
label.string = `房间id:${id},当前人数:${players.length}`;
|
||||
this.node.active = true;
|
||||
}
|
||||
|
||||
handleClick() {
|
||||
EventManager.Instance.emit(EventEnum.RoomJoin, this.id)
|
||||
EventManager.Instance.emit(EventEnum.RoomJoin, this.id);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { EventEmitter } from "stream"
|
||||
import WebSocket, { WebSocketServer } from "ws"
|
||||
import { ApiMsgEnum, IModel } from "../Common"
|
||||
import { Connection, ConnectionEventEnum } from "./Connection"
|
||||
import { EventEmitter } from "stream";
|
||||
import WebSocket, { WebSocketServer } from "ws";
|
||||
import { ApiMsgEnum } from "../Common";
|
||||
import { Connection, ConnectionEventEnum } from "./Connection";
|
||||
|
||||
export interface IMyServerOptions {
|
||||
port: number
|
||||
port: number;
|
||||
}
|
||||
|
||||
export enum MyServerEventEnum {
|
||||
@ -13,51 +13,51 @@ export enum MyServerEventEnum {
|
||||
}
|
||||
|
||||
export class MyServer extends EventEmitter {
|
||||
wss?: WebSocketServer
|
||||
port: number
|
||||
connections: Set<Connection> = new Set()
|
||||
apiMap: Map<ApiMsgEnum, Function> = new Map()
|
||||
wss?: WebSocketServer;
|
||||
port: number;
|
||||
connections: Set<Connection> = new Set();
|
||||
apiMap: Map<ApiMsgEnum, Function> = new Map();
|
||||
|
||||
constructor({ port = 8080 }: Partial<IMyServerOptions>) {
|
||||
super()
|
||||
this.port = port
|
||||
super();
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
start() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.wss = new WebSocketServer({ port: this.port })
|
||||
this.wss.on("connection", this.handleConnect.bind(this))
|
||||
this.wss = new WebSocketServer({ port: this.port });
|
||||
this.wss.on("connection", this.handleConnect.bind(this));
|
||||
|
||||
this.wss.on("error", (e) => {
|
||||
reject(e)
|
||||
})
|
||||
reject(e);
|
||||
});
|
||||
|
||||
this.wss.on("close", () => {
|
||||
console.log("MyServer 服务关闭")
|
||||
})
|
||||
console.log("MyServer 服务关闭");
|
||||
});
|
||||
|
||||
this.wss.on("listening", () => {
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
handleConnect(ws: WebSocket) {
|
||||
//初始化
|
||||
const connection = new Connection(this, ws)
|
||||
const connection = new Connection(this, ws);
|
||||
|
||||
//向外告知有人来了
|
||||
this.connections.add(connection)
|
||||
this.emit(MyServerEventEnum.Connect, connection)
|
||||
this.connections.add(connection);
|
||||
this.emit(MyServerEventEnum.Connect, connection);
|
||||
|
||||
//向外告知有人走了
|
||||
connection.on(ConnectionEventEnum.Close, (code: number, reason: string) => {
|
||||
this.connections.delete(connection)
|
||||
this.emit(MyServerEventEnum.DisConnect, connection, code, reason)
|
||||
})
|
||||
this.connections.delete(connection);
|
||||
this.emit(MyServerEventEnum.DisConnect, connection, code, reason);
|
||||
});
|
||||
}
|
||||
|
||||
setApi<T extends keyof IModel["api"]>(name: T, cb: (connection: Connection, args: IModel["api"][T]["req"]) => IModel["api"][T]["res"]) {
|
||||
this.apiMap.set(name, cb)
|
||||
setApi(apiName: ApiMsgEnum, cb: Function) {
|
||||
this.apiMap.set(apiName, cb);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,16 @@
|
||||
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<Player, "id" | "nickname" | "connection">) {
|
||||
this.id = id
|
||||
this.nickname = nickname
|
||||
this.connection = connection
|
||||
this.id = id;
|
||||
this.nickname = nickname;
|
||||
this.connection = connection;
|
||||
this.connection.playerId = this.id;
|
||||
this.rid = -1;
|
||||
}
|
||||
}
|
||||
|
@ -1,54 +1,55 @@
|
||||
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.players.delete(player);
|
||||
this.idMapPlayer.delete(uid);
|
||||
this.syncPlayers();
|
||||
}
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
}
|
||||
|
@ -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<Player> = new Set()
|
||||
id: number;
|
||||
players: Set<Player> = new Set();
|
||||
|
||||
private lastTime?: number
|
||||
private timers: NodeJS.Timer[] = []
|
||||
private pendingInput: Array<IClientInput> = []
|
||||
private lastPlayerFrameIdMap: Map<number, number> = new Map()
|
||||
private lastTime?: number;
|
||||
private timers: NodeJS.Timer[] = [];
|
||||
private pendingInput: Array<IClientInput> = [];
|
||||
private lastPlayerFrameIdMap: Map<number, number> = 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 = undefined
|
||||
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
|
||||
this.players.delete(player)
|
||||
player.rid = -1;
|
||||
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 = undefined
|
||||
player.connection.sendMsg(ApiMsgEnum.MsgGameEnd, {})
|
||||
player.connection.unlistenMsg(ApiMsgEnum.MsgClientSync, this.getClientMsg, this)
|
||||
player.rid = -1;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,6 @@ server.on(MyServerEventEnum.DisConnect, (connection: Connection) => {
|
||||
console.log(`${getTime()}走人|人数|${server.connections.size}`)
|
||||
if (connection.playerId) {
|
||||
PlayerManager.Instance.removePlayer(connection.playerId)
|
||||
PlayerManager.Instance.syncPlayers()
|
||||
}
|
||||
})
|
||||
|
||||
@ -51,7 +50,6 @@ 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),
|
||||
|
Loading…
Reference in New Issue
Block a user