将各计时器的名字改为常量

This commit is contained in:
zhl 2021-03-02 14:35:21 +08:00
parent a2a53cfd42
commit 44fc6281df
11 changed files with 86 additions and 40 deletions

View File

@ -0,0 +1,32 @@
export class ClockNameConst {
/**
*
* @type {string}
*/
public static readonly DRAW_CARD = 'draw_card'
/**
*
* @type {string}
*/
public static readonly SELECT_PET = 'select_pet'
/**
*
* @type {string}
*/
public static readonly PICK_HERO = 'pick_hero'
/**
*
* @type {string}
*/
public static readonly WAITING_PLAYER = 'waiting_player'
/**
*
* @type {string}
*/
public static readonly RESTART_SCHEDULE = 'restart_schedule'
/**
*
* @type {string}
*/
public static readonly EAT_ROUND = 'eat_round'
}

View File

@ -10,6 +10,7 @@ import { Card } from '../schema/Card'
import { Wait } from './Wait'
import { StateTypeEnum } from '../enums/StateTypeEnum'
import { RoomOptions } from '../../cfg/RoomOptions'
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
@ -58,7 +59,7 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
}
let targetCard
if (target) {
if (!new RoomOptions().canEat({advMode: this.state.advMode})) {
if (!new RoomOptions().canEat({ advMode: this.state.advMode })) {
this.room.send(client, 'discard_card_s2c', {
errcode: 7,
errmsg: '当前游戏不允许吃牌'
@ -105,7 +106,7 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
//停止出牌计时, 并更新player.extraTime;
let elapsedTime = this.room.stopSchedule('draw_card')
let elapsedTime = this.room.stopSchedule(ClockNameConst.DRAW_CARD)
if (elapsedTime >= 0) {
let maxTime = new GameEnv().maxDiscardTime * 1000
let count = elapsedTime - maxTime
@ -159,7 +160,7 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮')
self.room.dispatcher.dispatch(new TurnEndCommand())
}
this.room.beginSchedule(time, timeOverSelectPet, `select_pet`)
this.room.beginSchedule(time, timeOverSelectPet, ClockNameConst.SELECT_PET)
}
}
}

View File

@ -1,30 +1,35 @@
import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {GameEnv} from "../../cfg/GameEnv";
import {debugRoom} from "../../common/Debug";
import {DiscardCommand} from "./DiscardCommand";
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
import { GameEnv } from '../../cfg/GameEnv'
import { debugRoom } from '../../common/Debug'
import { DiscardCommand } from './DiscardCommand'
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
*/
export class DrawCommand extends Command<CardGameState, {}> {
async execute() {
let sessionId = this.state.currentTurn;
this.room.addCard(sessionId, new GameEnv().roundDrawNum, 0);
let maxTime = new GameEnv().maxDiscardTime * 1000;
let player = this.state.players.get(sessionId);
let self = this;
let timeOverDraw = function () {
if (sessionId == self.state.currentTurn) {
let client = self.room.getClient(sessionId);
let card = player.cards.values().next().value;
debugRoom('出牌时间到, 自动出牌: ', card.id);
player.extraTime = 0;
if (client) {
self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1});
}
}
async execute() {
let sessionId = this.state.currentTurn
this.room.addCard(sessionId, new GameEnv().roundDrawNum, 0)
let maxTime = new GameEnv().maxDiscardTime * 1000
let player = this.state.players.get(sessionId)
let self = this
let timeOverDraw = function () {
if (sessionId == self.state.currentTurn) {
let client = self.room.getClient(sessionId)
let card = player.cards.values().next().value
debugRoom('出牌时间到, 自动出牌: ', card.id)
player.extraTime = 0
if (client) {
self.room.dispatcher.dispatch(new DiscardCommand(), {
client,
cards: [card.id],
dtype: 1
})
}
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, `draw_card`);
}
}
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD)
}
}

View File

@ -8,6 +8,7 @@ import {Player} from "../schema/Player";
import {Card} from "../schema/Card";
import gameUtil from "../../utils/game.util";
import {StateTypeEnum} from "../enums/StateTypeEnum";
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
* ,
@ -33,7 +34,7 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
}
if (giveUpCount >= playerCount) {
// 所有人都放弃了, 则取消定时, 直接进入下一轮
this.room.stopSchedule('eat_round');
this.room.stopSchedule(ClockNameConst.EAT_ROUND);
// if (this.room.mainClock?.active) {
// this.room.mainClock.clear();
// }
@ -72,7 +73,7 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
if (player != null && (isFirst || timeUp)) {
debugRoom(`真的开始吃牌了: ${player.id}, 场上牌: ${this.state.cards.size}, isFirst: ${isFirst}, timeUp: ${timeUp}`);
// 如果有吃牌计时的话,停止吃牌计时
this.room.stopSchedule('eat_round');
this.room.stopSchedule(ClockNameConst.EAT_ROUND);
let currentPlayer = this.state.players.get(this.state.currentTurn);
currentPlayer.cardQueue.clear();
player.cardQueue.clear();
@ -95,7 +96,7 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
self.room.dispatcher.dispatch(new TurnEndCommand());
}
self.room.beginSchedule(time, timeOverSelectPet, `select_pet`);
self.room.beginSchedule(time, timeOverSelectPet, ClockNameConst.SELECT_PET);
let fromPlayer = this.state.players.get(this.state.currentTurn);
// 成功后广播吃牌成功消息
let client = this.room.getClient(player.id);

View File

@ -4,6 +4,7 @@ import {Client} from "colyseus";
import {PlayReadyCommand} from "./PlayReadyCommand";
import {PlayerStateConst} from "../../constants/PlayerStateConst";
import {error} from "../../common/Debug";
import { ClockNameConst } from '../../constants/ClockNameConst'
export class GameRestartCommand extends Command<CardGameState, {client: Client}> {
@ -14,7 +15,7 @@ export class GameRestartCommand extends Command<CardGameState, {client: Client}>
this.state.restartCount ++;
player.state = PlayerStateConst.PLAYER_NORMAL;
if (this.state.restartCount >= this.room.maxClients) {
this.room.stopSchedule('restart_schedule');
this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE);
}
} else {
error(`${client.sessionId} not found!!`)

View File

@ -12,6 +12,7 @@ import {StateTypeEnum} from "../enums/StateTypeEnum";
import {reportGameResult} from "../../common/WebApi";
import {BaseConst} from "../../constants/BaseConst";
import {Player} from "../schema/Player";
import { ClockNameConst } from '../../constants/ClockNameConst'
class GameResult{
public id: string
@ -184,13 +185,13 @@ export class GameResultCommand extends Command<CardGameState, {}> {
}
}
let time = new GameEnv().waitingPlayerTime * 1000;
self.room.beginSchedule(time, timeOutWaitingPlayer, 'waiting_player');
self.room.beginSchedule(time, timeOutWaitingPlayer, ClockNameConst.WAITING_PLAYER);
} else { // 如果4个人都点击了重开, 理论上不存在这种情况
error(`所有人都点击了重新开始, 为啥还没开始游戏???`);
}
}
let time = new GameEnv().gameResultTime * 1000;
this.room.beginSchedule(time, resultTimeOver, 'restart_schedule');
this.room.beginSchedule(time, resultTimeOver, ClockNameConst.RESTART_SCHEDULE);
let saveData: any;
try{
saveData = (await self.reportGameResult(winner, mvp.id, results.get(mvp).mvpScore, results)).data.data;

View File

@ -5,6 +5,7 @@ import {GameEnv} from "../../cfg/GameEnv";
import {debugRoom} from "../../common/Debug";
import {EatConfirmCommand} from "./EatConfirmCommand";
import {PlayerStateConst} from "../../constants/PlayerStateConst";
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
@ -27,7 +28,7 @@ export class NextSubCommand extends Command<CardGameState, {}> {
debugRoom('吃牌时间到, 进入下一轮')
self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true});
}
this.room.beginSchedule(time, timeOverEat, `eat_round`);
this.room.beginSchedule(time, timeOverEat, ClockNameConst.EAT_ROUND);
}
}

View File

@ -7,6 +7,7 @@ import {GameEnv} from "../../cfg/GameEnv";
import {BaseConst} from "../../constants/BaseConst";
import {getRandom} from "../../utils/number.util";
import { getUserInfo, randomUserInfo } from '../../common/WebApi'
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
@ -84,13 +85,13 @@ export class OnJoinCommand extends Command<CardGameState, {
}
}
let time = new GameEnv().waitingPlayerTime * 1000;
self.room.beginSchedule(time, timeOutWaitingPlayer, 'waiting_player');
self.room.beginSchedule(time, timeOutWaitingPlayer, ClockNameConst.WAITING_PLAYER);
} else if (this.room.clientCount() > 1 && this.room.clientCount() < this.room.maxClients) {
let moreTime = new GameEnv().waitingPlayerOnePlus * 1000;
self.room.addScheduleTime(moreTime, 'play_join', 'waiting_player')
self.room.addScheduleTime(moreTime, 'play_join', ClockNameConst.WAITING_PLAYER)
}
if (this.state.players.size >= this.room.maxClients) {
this.room.stopSchedule('waiting_player');
this.room.stopSchedule(ClockNameConst.WAITING_PLAYER);
this.room.lock().then(() => {
});
this.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE);

View File

@ -7,6 +7,7 @@ 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'
/**
*
@ -26,8 +27,8 @@ export class PlayReadyCommand extends Command<CardGameState, {
}
// 如果所有人的状态都为已准备状态, 则开始发牌
if (readyCount >= this.room.maxClients) {
this.room.stopSchedule('restart_schedule');
this.room.stopSchedule('waiting_player');
this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE);
this.room.stopSchedule(ClockNameConst.WAITING_PLAYER);
// 比大小, 确定先手
// return [new PrepareCommand()];
// let i = 0;
@ -59,7 +60,7 @@ export class PlayReadyCommand extends Command<CardGameState, {
}
}
let time = new GameEnv().pickHeroTime * 1000;
this.room.beginSchedule(time, pickHeroTimeOut, 'pick_hero');
this.room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO);
}
}

View File

@ -8,6 +8,7 @@ import {error} from "../../common/Debug";
import {GameEnv} from "../../cfg/GameEnv";
import {StateTypeEnum} from "../enums/StateTypeEnum";
import {getCardGroup} from "../../common/WebApi";
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
@ -91,7 +92,7 @@ export class SelectHeroCommand extends Command<CardGameState, { client: Client,
}
}
if (readyCount >= this.room.maxClients) {
this.room.stopSchedule('pick_hero');
this.room.stopSchedule(ClockNameConst.PICK_HERO);
return [new BeginGameCommand()];
}
}

View File

@ -8,6 +8,7 @@ import { RoomOptions } from '../../cfg/RoomOptions'
import assistantUtil from '../../utils/assistant.util'
import { GameStateConst } from '../../constants/GameStateConst'
import { debugRoom } from '../../common/Debug'
import { ClockNameConst } from '../../constants/ClockNameConst'
/**
*
@ -73,7 +74,7 @@ export class SelectPetCommand extends Command<CardGameState, {
return
}
//停止选随从计时, 并更新player.extraTime;
let elapsedTime = this.room.stopSchedule('select_pet')
let elapsedTime = this.room.stopSchedule(ClockNameConst.SELECT_PET)
if (elapsedTime >= 0) {
let count = elapsedTime - new GameEnv().playerActTime * 1000
let newCount = player.extraTime - Math.min(count, 0)