This commit is contained in:
sli97
2022-12-03 21:28:38 +08:00
parent f3dc3ef7ba
commit a678a5b3fc
10 changed files with 76 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
import { IPlayer, IRoom } from "./Model"
import { IState } from "./State"
import { IClientInput, IState } from "./State"
export interface IMsgPlayerList {
list: Array<IPlayer>
@@ -20,4 +20,12 @@ export interface IMsgGameStart {
export interface IMsgGameStart {
state: IState
}
export interface IMsgClientSync {
input: IClientInput
}
export interface IMsgServerSync {
inputs: Array<IClientInput>
}

View File

@@ -1,4 +1,4 @@
import { ApiMsgEnum, EntityTypeEnum, IState } from '../Common'
import { ApiMsgEnum, EntityTypeEnum, IClientInput, IState } from '../Common'
import type Player from './Player'
import PlayerManager from './PlayerManager'
import RoomManager from './RoomManager'
@@ -11,7 +11,7 @@ export default class Room {
this.id = rid
}
private inputs = []
private inputs: Array<IClientInput> = []
join(uid: number) {
const player = PlayerManager.Instance.getPlayerById(uid)
@@ -69,12 +69,26 @@ export default class Room {
})
}
this.listenPlayer()
setInterval(() => {
this.syncInput()
}, 300)
}
listenPlayer() {
for (const player of this.players) {
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, () => { }
)
player.connection.listenMsg(ApiMsgEnum.MsgClientSync, ({ input }) => {
this.inputs.push(input)
})
}
}
syncInput() {
const inputs = this.inputs
this.inputs = []
for (const player of this.players) {
player.connection.sendMsg(ApiMsgEnum.MsgServerSync, {
inputs
})
}
}
}

View File

@@ -1,6 +1,6 @@
import { IApiGameStartReq, IApiGameStartRes, IApiPlayerJoinReq, IApiPlayerJoinRes, IApiPlayerListReq, IApiPlayerListRes, IApiRoomCreateReq, IApiRoomCreateRes, IApiRoomJoinReq, IApiRoomJoinRes, IApiRoomLeaveReq, IApiRoomLeaveRes, IApiRoomListReq, IApiRoomListRes } from './Api'
import { ApiMsgEnum } from './Enum'
import { IMsgGameStart, IMsgPlayerList, IMsgRoom, IMsgRoomList } from './Msg'
import { IMsgClientSync, IMsgGameStart, IMsgPlayerList, IMsgRoom, IMsgRoomList, IMsgServerSync } from './Msg'
import { IClientInput } from './State'
export * from './Api'
export * from './Msg'
@@ -44,8 +44,8 @@ export interface IModel {
[ApiMsgEnum.MsgRoomList]: IMsgRoomList,
[ApiMsgEnum.MsgRoom]: IMsgRoom,
[ApiMsgEnum.MsgGameStart]: IMsgGameStart,
[ApiMsgEnum.MsgClientSync]: IClientInput,
[ApiMsgEnum.MsgServerSync]: IMsgGameStart,
[ApiMsgEnum.MsgClientSync]: IMsgClientSync,
[ApiMsgEnum.MsgServerSync]: IMsgServerSync,
}
}

View File

@@ -54,7 +54,7 @@ export default class Connection extends EventEmitter {
})
}
listenMsg(name: string, cb: Function) {
listenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void) {
if (this.msgMap.has(name)) {
this.msgMap.get(name)?.push(cb)
} else {
@@ -62,7 +62,7 @@ export default class Connection extends EventEmitter {
}
}
unlistenMsg(name: string, cb: Function) {
unlistenMsg<T extends keyof IModel['msg']>(name: T, cb: (args: IModel['msg'][T]) => void) {
if (this.msgMap.has(name)) {
const index = this.msgMap.get(name)?.indexOf(cb) || -1
index > -1 && this.msgMap.get(name)?.splice(index, 1)