增加等待玩家发送准备消息的计时器

This commit is contained in:
zhl 2021-03-16 16:40:12 +08:00
parent 747259977a
commit e466c29524
6 changed files with 99 additions and 35 deletions

View File

@ -78,6 +78,8 @@ export class GameEnv {
// 变态机器人的胜率值
public robotLvlExtra: number
public robotRateExtra: number
// 等待玩家确认的最长时间
public maxWaitingTime: number
public init(data: Map<number, BaseCfg>) {
this.initCardNum = data.get(BaseConst.INIT_CARD_NUM).value
@ -123,6 +125,7 @@ export class GameEnv {
this.robotRateMid = data.get(BaseConst.ROBOT_RATE_MID).value
this.robotRateHigh = data.get(BaseConst.ROBOT_RATE_HIGHT).value
this.robotRateExtra = data.get(BaseConst.ROBOT_RATE_EXTRA).value
this.maxWaitingTime = 60
}
public getCheatRate(val: number) {
if (val < this.robotRateMid) {

View File

@ -29,4 +29,9 @@ export class ClockNameConst {
* @type {string}
*/
public static readonly EAT_ROUND = 'eat_round'
/**
*
* @type {string}
*/
public static readonly WAITING_PREPARE = 'waiting_prepare'
}

7
src/global.d.ts vendored
View File

@ -45,6 +45,13 @@ declare module 'colyseus' {
*/
getClient(player: string | Player): Client;
/**
*
* @param {string | Player} player
* @return {boolean}
*/
checkClient(player: string | Player): boolean;
// >>>>>>>>> Begin of extend send message <<<<<<<<<<<<<
/**

View File

@ -204,7 +204,7 @@ export class GeneralRoom extends Room {
player.online = true
}
} catch (e) {
debugRoom(`player realy level :${ client.sessionId }, try add robot`)
debugRoom(`player realy level :${ client.sessionId }`)
// this.state.players.delete(client.sessionId);
}
}
@ -242,6 +242,16 @@ export class GeneralRoom extends Room {
return result
}
checkClient(player: string | Player): boolean {
let result: Client
if (typeof player == 'string') {
result = this.clients.find(client => client.sessionId == player)
} else {
result = this.clients.find(client => client.sessionId == player.id)
}
return !!result
}
/**
* client数量
* @return {number}

View File

@ -191,8 +191,9 @@ export class GameResultCommand extends Command<CardGameState, {}> {
error(`所有人都点击了重新开始, 为啥还没开始游戏???`)
}
}
let time = new GameEnv().gameResultTime * 1000
this.room.beginSchedule(time, resultTimeOver, ClockNameConst.RESTART_SCHEDULE)
// 20200316 确认, 自动重开功能已经没用了
// let time = new GameEnv().gameResultTime * 1000
// this.room.beginSchedule(time, resultTimeOver, ClockNameConst.RESTART_SCHEDULE)
let saveData: any
try {
saveData = (await self.reportGameResult(winner, mvp.id, results.get(mvp).mvpScore, results, this.state.mode)).data.data

View File

@ -1,6 +1,6 @@
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
import { Client } from 'colyseus'
import { Client, Room } from 'colyseus'
import { PlayerStateConst } from '../../constants/PlayerStateConst'
import { GameStateConst } from '../../constants/GameStateConst'
import { GameEnv } from '../../cfg/GameEnv'
@ -8,6 +8,8 @@ import { SelectHeroCommand } from './SelectHeroCommand'
import { HeroCfg } from '../../cfg/parsers/HeroCfg'
import { BaseConst } from '../../constants/BaseConst'
import { ClockNameConst } from '../../constants/ClockNameConst'
import { debugRoom } from '../../common/Debug'
import { Player } from '../schema/Player'
/**
*
@ -18,17 +20,47 @@ export class PlayReadyCommand extends Command<CardGameState, {
async execute({ client }: { client: Client }) {
this.state.players.get(client.sessionId).state = PlayerStateConst.PLAYER_READY
this.room.broadcast('player_ready_s2c', { player: client.sessionId }, { except: client })
let readyCount = 0
for (let [, player] of this.state.players) {
if (player.state === PlayerStateConst.PLAYER_READY) {
readyCount++
}
}
let self = this
if (readyCount == 1) {
let timeOutWaitingPrepare = async function() {
debugRoom(`waiting player prepare time out`)
// 等待玩家确认超时
// 检查剩余玩家, 如果所有剩余玩家都是机器人, 那么直接解散房间
let robotCount = 0;
const humans: Player[] = []
for (let client of self.room.clients) {
let player = self.room.state.players.get(client.sessionId)
if (player.robot) {
robotCount ++
} else {
humans.push(player)
}
}
if (robotCount === self.room.clientCount) {
await self.room.disconnect()
} else {
for (let [, player] of self.state.players) {
if (player.state !== PlayerStateConst.PLAYER_READY) {
player.state = PlayerStateConst.PLAYER_READY
let playerClient = self.room.getClient(player)
self.room.broadcast('player_ready_s2c', { player: playerClient.sessionId }, { except: playerClient })
}
}
await beginChangeHero(self.room)
}
}
self.room.beginSchedule(new GameEnv().maxWaitingTime * 1000, timeOutWaitingPrepare, ClockNameConst.WAITING_PREPARE)
}
this.room.broadcast('player_ready_s2c', { player: client.sessionId }, { except: client })
// 如果所有人的状态都为已准备状态, 则开始发牌
if (readyCount >= this.room.maxClients) {
this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE)
this.room.stopSchedule(ClockNameConst.WAITING_PLAYER)
// 比大小, 确定先手
// return [new PrepareCommand()];
// let i = 0;
@ -36,36 +68,42 @@ export class PlayReadyCommand extends Command<CardGameState, {
// player.team = (i == 1 || i == 2) ? 1 : 0;
// i += 1;
// }
await this.room.setPrivate(true)
this.room.state.updateGameState(GameStateConst.CHANGE_HERO)
let self = this
/**
*
*/
let pickHeroTimeOut = function () {
for (let [, curPlayer] of self.state.players) {
if (curPlayer.state == PlayerStateConst.PLAYER_READY) {
let heroId: number
if (curPlayer.heros) {
heroId = curPlayer.heros.randomOne()
} else {
let heroMap: Map<number, HeroCfg> = global.$cfg.get(BaseConst.HERO)
let heroArr: HeroCfg[] = [...heroMap.values()]
let hero: HeroCfg = heroArr.randomOne()
heroId = hero.id
}
let targetClient = self.room.getClient(curPlayer)
self.room.dispatcher.dispatch(new SelectHeroCommand(), {
client: targetClient,
heroId
})
}
}
}
let time = new GameEnv().pickHeroTime * 1000
this.room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO)
self.room.stopSchedule(ClockNameConst.WAITING_PREPARE)
await beginChangeHero(self.room)
}
}
}
const beginChangeHero = async function (room: Room) {
/**
*
*/
let pickHeroTimeOut = function () {
for (let [, curPlayer] of room.state.players) {
if (curPlayer.state == PlayerStateConst.PLAYER_READY) {
let heroId: number
if (curPlayer.heros) {
heroId = curPlayer.heros.randomOne()
} else {
let heroMap: Map<number, HeroCfg> = global.$cfg.get(BaseConst.HERO)
let heroArr: HeroCfg[] = [...heroMap.values()]
let hero: HeroCfg = heroArr.randomOne()
heroId = hero.id
}
let targetClient = room.getClient(curPlayer)
room.dispatcher.dispatch(new SelectHeroCommand(), {
client: targetClient,
heroId
})
}
}
}
await room.setPrivate(true)
room.stopSchedule(ClockNameConst.RESTART_SCHEDULE)
room.stopSchedule(ClockNameConst.WAITING_PLAYER)
room.state.updateGameState(GameStateConst.CHANGE_HERO)
let time = new GameEnv().pickHeroTime * 1000
room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO)
}