update
This commit is contained in:
@@ -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>
|
||||
}
|
@@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user