增加出牌和吃牌的超时限制

This commit is contained in:
zhl 2020-12-07 19:47:35 +08:00
parent 04b3e87901
commit a8c44ea05a
9 changed files with 62 additions and 12 deletions

5
src/global.d.ts vendored
View File

@ -18,13 +18,16 @@ import {BattleHandler} from "./rooms/logic/Handler/BattleHandler";
import {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo";
import {PartResultMsg} from "./message/PartResult";
import {RemovePetMsg} from "./message/RemovePetMsg";
import {Dispatcher} from "@colyseus/command";
import {Delayed} from "@gamestdio/timer/lib/Delayed";
/**
* GeneralRoom
*/
declare module "colyseus" {
interface Room {
battleMan: BattleHandler;
dispatcher: Dispatcher;
mainClock: Delayed;
/**
* sessionId获取client
* @param sessionId

View File

@ -12,6 +12,7 @@ import {EatCardCommand} from "./commands/EatCardCommand";
import {GiveUpCommand} from "./commands/GiveUpCommand";
import {BattleHandler} from "./logic/Handler/BattleHandler";
import {debugRoom} from "../common/Debug";
import {Delayed} from "@gamestdio/timer/lib/Delayed";
export class GeneralRoom extends Room {
@ -19,6 +20,8 @@ export class GeneralRoom extends Room {
maxClients = 4;
clientMap = new Map();
battleMan = new BattleHandler();
// 用于游戏过程中各种计时器, 使用该计时器的前提是, 只针对当前操作玩家
mainClock: Delayed;
onCreate (options: any) {
let cs = new CardGameState();

View File

@ -4,6 +4,8 @@ import gameUtil from "../../utils/game.util";
import {Client} from "colyseus";
import {NextSubCommand} from "./NextSubCommand";
import {GameStateConst} from "../../constants/GameStateConst";
import {singleton} from "../../common/Singleton";
import {GameEnv} from "../../cfg/GameEnv";
/**
*
@ -39,6 +41,14 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
return;
}
this.state.cards.clear();
//停止计时, 并更新player.extraTime;
if (this.room.mainClock) {
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
let count = this.room.mainClock.elapsedTime - maxTime;
let newCount = player.extraTime - count;
player.extraTime = Math.max(newCount, 0);
this.room.mainClock.clear();
}
for (let id of cards) {
this.state.cards.set(id+'', player.cards.get(id + ''));
player.cards.delete(id + '');

View File

@ -3,7 +3,7 @@ import {CardGameState} from "../schema/CardGameState";
import {singleton} from "../../common/Singleton";
import {GameEnv} from "../../cfg/GameEnv";
import gameUtil from "../../utils/game.util";
import {error} from "../../common/Debug";
import {debugRoom, error} from "../../common/Debug";
import {DiscardCommand} from "./DiscardCommand";
/**
@ -13,14 +13,19 @@ export class DrawCommand extends Command<CardGameState, {}> {
async execute() {
let sessionId = this.state.currentTurn;
this.room.addCard(sessionId, singleton(GameEnv).roundDrawNum, 0);
let maxTime = singleton(GameEnv).maxDiscardTime;
await this.delay(maxTime * 1000);
if (sessionId == this.state.currentTurn) {
let client = this.room.getClient(sessionId);
error('出牌时间到, 自动出牌');
let player = this.state.players.get(sessionId);
let card = player.cards.values().next().value;
return [new DiscardCommand().setPayload({client, cards: [card.id], dtype: 1})]
}
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
let player = this.state.players.get(sessionId);
let self = this;
this.room.mainClock = this.clock.setTimeout(function () {
self.room.mainClock.clear();
if (sessionId == self.state.currentTurn) {
let client = self.room.getClient(sessionId);
let card = player.cards.values().next().value;
debugRoom('出牌时间到, 自动出牌: ', card.id);
self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1});
}
}, maxTime + player.extraTime)
// await this.delay((maxTime + player.extraTime) * 1000);
}
}

View File

@ -12,6 +12,9 @@ export class GiveUpCommand extends Command<CardGameState, {client: Client}> {
this.state.giveUpCount += 1;
this.room.broadcast('give_up_eat_s2c', {player: client.sessionId});
if (this.state.giveUpCount >= this.room.maxClients - 1) {
if (this.room.mainClock) {
this.room.mainClock.clear();
}
return [new TurnEndCommand()];
}
}

View File

@ -1,15 +1,25 @@
import { Command } from "@colyseus/command";
import { CardGameState } from "../schema/CardGameState";
import {GameStateConst} from "../../constants/GameStateConst";
import {singleton} from "../../common/Singleton";
import {GameEnv} from "../../cfg/GameEnv";
import {NextTurnCommand} from "./NextTurnCommand";
import {TurnEndCommand} from "./TurnEndCommand";
/**
*
*/
export class NextSubCommand extends Command<CardGameState, {}> {
execute() {
async execute() {
this.state.gameState = GameStateConst.STATE_BEGIN_EAT;
this.state.giveUpCount = 0;
let time = singleton(GameEnv).maxEatTime * 1000;
let self = this;
this.room.mainClock = this.clock.setTimeout(function (){
self.room.mainClock.clear();
self.room.dispatcher.dispatch(new TurnEndCommand());
}, time);
// const sessionIds = [...this.state.players.keys()];
// let nextSubTurn = this.state.subTurn ?
// sessionIds[(sessionIds.indexOf(this.state.subTurn) + 1) % sessionIds.length]

View File

@ -23,6 +23,14 @@ export class NextTurnCommand extends Command<CardGameState, {}> {
if (this.state.currentTurn
&& sessionIds.indexOf(this.state.currentTurn) == (sessionIds.length - 1)) {
this.state.round += 1;
// 所有玩家根据配置, 增加5点灵活值
if (this.state.round > 0) {
let moreRoundTime = singleton(GameEnv).roundExtTime * 1000;
let maxTime = 20 * 1000;
for (let [key, p] of this.state.players) {
p.extraTime = Math.min(p.extraTime + moreRoundTime * 1000, maxTime);
}
}
}
this.state.currentTurn = (this.state.currentTurn)
? sessionIds[(sessionIds.indexOf(this.state.currentTurn) + 1) % sessionIds.length]

View File

@ -6,6 +6,8 @@ import {BeginGameCommand} from "./BeginGameCommand";
import {BattleHandler} from "../logic/Handler/BattleHandler";
import {BaseConst} from "../../constants/BaseConst";
import {error} from "../../common/Debug";
import {singleton} from "../../common/Singleton";
import {GameEnv} from "../../cfg/GameEnv";
/**
*
@ -27,6 +29,7 @@ export class SelectHeroCommand extends Command<CardGameState, {client: Client, h
}
player.state = PlayerStateConst.PLAYER_SELECT_HERO;
player.hp = unitData.hero_hp;
player.extraTime = singleton(GameEnv).maxExtTime * 1000;
let heroPet = player.pets.get('0');
heroPet.ap = unitData.powernum;
heroPet.state = 1;

View File

@ -50,6 +50,11 @@ export class Player extends Schema {
*/
@type("number")
team: number;
/**
* , ,
*/
@type("number")
extraTime: number;
/**
*
*/