diff --git a/src/global.d.ts b/src/global.d.ts index be797b9..e608bd3 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -34,6 +34,10 @@ declare module 'colyseus' { robotCount: number; match: string | undefined; score: number; + /** + * 当前room的client数量 + */ + clientCount: number; /** * 根据sessionId获取client @@ -41,10 +45,6 @@ declare module 'colyseus' { */ getClient(player: string | Player): Client; - /** - * 获取当前room的client数量 - */ - clientCount(): number; // >>>>>>>>> Begin of extend send message <<<<<<<<<<<<< /** @@ -167,7 +167,7 @@ declare module 'colyseus' { * @param data * @param options */ - bPlayerDead(data: any, options?: any) : void; + bPlayerDead(data: any, options?: any): void; /** * 广播的消息列表 @@ -245,14 +245,23 @@ declare module 'colyseus' { */ beginSchedule(millisecond: number, handler: Function, name: string): void; + /** + * 判断某个定时器是否active + * @param {string} name + * @return {boolean} + */ + scheduleActive(name: string): boolean; + /** * 取消某个计时器 */ stopSchedule(name: string): number; + /** * 暂停某个计时器 */ pauseSchedule(name: string): number; + /** * 恢复某个计时器 */ @@ -312,7 +321,7 @@ declare module 'colyseus' { * @param exPlayer * @return {Player[]} */ - getOtherTeamPlayers(srcPlayer: string, exPlayer?: string ): Player[]; + getOtherTeamPlayers(srcPlayer: string, exPlayer?: string): Player[]; /** * 根据index获取玩家 diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 7acb2fe..16ed0f6 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -246,7 +246,7 @@ export class GeneralRoom extends Room { * 加入当前房间的client数量 * @return {number} */ - clientCount(): number { + get clientCount(): number { return this.clients.length } @@ -288,7 +288,9 @@ export class GeneralRoom extends Room { this.gameClock.delete(name) return time } - + scheduleActive(name: string): boolean { + return this.gameClock.has(name) && this.gameClock.get(name).active + } /** * 暂停某个计时器, 返回这个机器器的剩余时间 * @param {string} name diff --git a/src/rooms/commands/GMCommand.ts b/src/rooms/commands/GMCommand.ts index b0797a7..67bd8aa 100644 --- a/src/rooms/commands/GMCommand.ts +++ b/src/rooms/commands/GMCommand.ts @@ -65,7 +65,7 @@ export class GMCommand extends Command { await self.room.setPrivate(false) //开启匹配定时, 长时间没匹配到人的话, 添加机器人 let timeOutWaitingPlayer = async function () { - let count = self.room.maxClients - self.room.clientCount() + let count = self.room.maxClients - self.room.clientCount if (count > 0) { for (let i = 0; i < count; i++) { await self.room.addRobot() diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index 1946729..ab221dd 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -1,12 +1,13 @@ import { Command } from '@colyseus/command' import { CardGameState } from '../schema/CardGameState' import { Player } from '../schema/Player' -import { Client } from 'colyseus' +import { Client, Room } from 'colyseus' import { GameStateConst } from '../../constants/GameStateConst' import { GameEnv } from '../../cfg/GameEnv' import { BaseConst } from '../../constants/BaseConst' import { getUserInfo, randomUserInfo } from '../../common/WebApi' import { ClockNameConst } from '../../constants/ClockNameConst' +import { debugRoom } from '../../common/Debug' /** * 玩家成功加入房间 @@ -18,12 +19,12 @@ export class OnJoinCommand extends Command { async execute({ client, accountId, seat, score } = this.payload) { - let count = this.state.players.size + const count = this.room.clientCount if (count >= this.room.maxClients) { return } // begin of set seat and team - let idx = count + let idx: number let seatSet = new Set([0, 1, 2, 3]) let accounts: string[] = [] for (let [, p] of this.state.players) { @@ -70,16 +71,19 @@ export class OnJoinCommand extends Command= this.room.maxClients) { - this.room.lock().then(() => { - }) - this.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE) + /** + * 对于非匹配, 并且创建时不带机器人数量的房间(这种情况一般是好友对战), + * 只判断人数是否达到maxClients, 不添加机器人 + */ + if (this.room.clientCount >= this.room.maxClients) { + beginWaitingPrepare(this.room) } } else { - if (this.room.clientCount() == 1) { + if (this.room.clientCount == 1) { // 正常的匹配逻辑进入的第一个玩家, 开启定时, 超过设定时间人没齐的话, 添加机器人 let timeOutWaitingPlayer = async function () { - let count = self.room.maxClients - self.room.clientCount() + let count = self.room.maxClients - self.room.clientCount + debugRoom(`waiting_player time over, add ${ count } robot`) if (count > 0) { for (let i = 0; i < count; i++) { await self.room.addRobot() @@ -88,17 +92,14 @@ export class OnJoinCommand extends Command 1 && this.room.clientCount() < this.room.maxClients) { + } else if (this.room.clientCount > 1 && this.room.clientCount < this.room.maxClients) { let moreTime = new GameEnv().waitingPlayerOnePlus * 1000 self.room.addScheduleTime(moreTime, 'play_join', ClockNameConst.WAITING_PLAYER) } - if (this.state.players.size >= this.room.maxClients) { - this.room.stopSchedule(ClockNameConst.WAITING_PLAYER) - this.room.lock().then(() => { - }) - this.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE) - } else if (this.state.players.size < this.room.maxClients - && this.state.players.size >= this.room.maxClients - this.room.robotCount) { + if (this.room.clientCount >= this.room.maxClients) { + beginWaitingPrepare(this.room) + } else if (this.room.clientCount < this.room.maxClients + && this.room.clientCount >= this.room.maxClients - this.room.robotCount) { for (let i = 0; i < this.room.robotCount; i++) { this.room.robotCount-- await self.room.addRobot() @@ -106,8 +107,14 @@ export class OnJoinCommand extends Command { + }) + room.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE) +} diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index 948b53e..c992299 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -1,68 +1,71 @@ -import {Command} from "@colyseus/command"; -import {CardGameState} from "../schema/CardGameState"; -import {Client} from "colyseus"; -import {PlayerStateConst} from "../../constants/PlayerStateConst"; -import {GameStateConst} from "../../constants/GameStateConst"; -import {GameEnv} from "../../cfg/GameEnv"; -import {SelectHeroCommand} from "./SelectHeroCommand"; -import {HeroCfg} from "../../cfg/parsers/HeroCfg"; -import {BaseConst} from "../../constants/BaseConst"; +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { Client } from 'colyseus' +import { PlayerStateConst } from '../../constants/PlayerStateConst' +import { GameStateConst } from '../../constants/GameStateConst' +import { GameEnv } from '../../cfg/GameEnv' +import { SelectHeroCommand } from './SelectHeroCommand' +import { HeroCfg } from '../../cfg/parsers/HeroCfg' +import { BaseConst } from '../../constants/BaseConst' import { ClockNameConst } from '../../constants/ClockNameConst' /** * 玩家已准备 */ export class PlayReadyCommand extends Command { - 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++; + 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++ + } + } + // 如果所有人的状态都为已准备状态, 则开始发牌 + if (readyCount >= this.room.maxClients) { + this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE) + this.room.stopSchedule(ClockNameConst.WAITING_PLAYER) + // 比大小, 确定先手 + // return [new PrepareCommand()]; + // let i = 0; + // for (let [,player] of this.state.players) { + // 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 = 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 + }) + } } - // 如果所有人的状态都为已准备状态, 则开始发牌 - if (readyCount >= this.room.maxClients) { - this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE); - this.room.stopSchedule(ClockNameConst.WAITING_PLAYER); - // 比大小, 确定先手 - // return [new PrepareCommand()]; - // let i = 0; - // for (let [,player] of this.state.players) { - // 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 = 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); - } - + } + let time = new GameEnv().pickHeroTime * 1000 + this.room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO) } + } + } diff --git a/src/rooms/schema/Player.ts b/src/rooms/schema/Player.ts index 9101766..992a55b 100644 --- a/src/rooms/schema/Player.ts +++ b/src/rooms/schema/Player.ts @@ -5,58 +5,58 @@ import { Schema, SetSchema, type -} from "@colyseus/schema"; -import {Pet} from "./Pet"; -import {Card} from "./Card"; -import {GameEnv} from "../../cfg/GameEnv"; -import {PlayerStateConst} from "../../constants/PlayerStateConst"; -import {StateTypeEnum} from "../enums/StateTypeEnum"; +} from '@colyseus/schema' +import { Pet } from './Pet' +import { Card } from './Card' +import { GameEnv } from '../../cfg/GameEnv' +import { PlayerStateConst } from '../../constants/PlayerStateConst' +import { StateTypeEnum } from '../enums/StateTypeEnum' export class Player extends Schema { - @type("string") - id: string; + @type('string') + id: string - accountId: string; + accountId: string /** * 用于显示的分 * @type {number} */ - @type("number") - score: number; + @type('number') + score: number /** * 用于组队匹配时候队伍的标记 * @type {string} */ - group?: string; + group?: string - @type("number") - heroId: number; + @type('number') + heroId: number /** * 手牌 */ @filter(function (this: Player, client, value, root) { - return (client.sessionId == this.id); + return (client.sessionId == this.id) }) - @type({map: Card}) - cards = new MapSchema(); + @type({ map: Card }) + cards = new MapSchema() /** * 公开的手牌数据 * @type {SetSchema} */ - @type({set: "string"}) - cardSet = new SetSchema(); + @type({ set: 'string' }) + cardSet = new SetSchema() /** * 玩家出的牌 */ @type([Card]) - cardQueue = new ArraySchema(); + cardQueue = new ArraySchema() /** * 当前hp */ - @type("number") - hp: number; + @type('number') + hp: number /** * 状态 @@ -66,81 +66,81 @@ export class Player extends Schema { * 3: 已换完牌 * 4: 玩家已选择英雄 */ - @type("number") - state: number; + @type('number') + state: number - online: boolean = true; + online: boolean = true /** * 随从 */ - @type({map: Pet}) - pets = new MapSchema(); + @type({ map: Pet }) + pets = new MapSchema() /** * 队伍 */ - @type("number") - team: number; + @type('number') + team: number - @type("number") - idx: number; + @type('number') + idx: number /** * 玩家灵活时限, 客户端每次显示倒计时的时候, 需读取该时间 */ - @type("number") - extraTime: number; + @type('number') + extraTime: number - @type("string") - nickname: string; + @type('string') + nickname: string - @type("string") - avatar: string; + @type('string') + avatar: string /** * 当前游戏总抽卡数量 */ - countTotal: number; + countTotal: number /** * 当前累计抽卡 */ - countPresent: number; + countPresent: number /** * 记录当前局内统计数字 */ - statData: Map = new Map(); + statData: Map = new Map() /** * 用于记录pet值, 用于统计伤害 */ - petData: Map = new Map(); - heros: number[]; + petData: Map = new Map() + heros: number[] // 是否是机器人 - robot: boolean; + robot: boolean // 十场胜率 - winRate: number; + winRate: number /** * 英雄绑定的卡组, 选好英雄后, 从默认配置或玩家卡组(待实现)中获取 * key = 效果卡id * val = weight */ - @type({map: "number"}) - unitCfgs = new MapSchema(); + @type({ map: 'number' }) + unitCfgs = new MapSchema() constructor(id: string, idx: number, team: number) { - super(); - this.id = id; - this.state = PlayerStateConst.PLAYER_NORMAL; - this.hp = 0; - this.idx = idx; - this.heroId = 0; - this.team = team; - this.countTotal = 0; - this.countPresent = 0; - this.online = true; + super() + this.id = id + this.state = PlayerStateConst.PLAYER_NORMAL + this.hp = 0 + this.idx = idx + this.heroId = 0 + this.team = team + this.countTotal = 0 + this.countPresent = 0 + this.online = true for (let i = 0; i < new GameEnv().maxPlayerPetCount + 1; i++) { - let pet = new Pet(i); - pet.state = 0; - pet.isHero = i === 0; - this.pets.set(i + '', pet); + let pet = new Pet(i) + pet.state = 0 + pet.isHero = i === 0 + this.pets.set(i + '', pet) } }