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

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 {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo";
import {PartResultMsg} from "./message/PartResult"; import {PartResultMsg} from "./message/PartResult";
import {RemovePetMsg} from "./message/RemovePetMsg"; import {RemovePetMsg} from "./message/RemovePetMsg";
import {Dispatcher} from "@colyseus/command";
import {Delayed} from "@gamestdio/timer/lib/Delayed";
/** /**
* GeneralRoom * GeneralRoom
*/ */
declare module "colyseus" { declare module "colyseus" {
interface Room { interface Room {
battleMan: BattleHandler; battleMan: BattleHandler;
dispatcher: Dispatcher;
mainClock: Delayed;
/** /**
* sessionId获取client * sessionId获取client
* @param sessionId * @param sessionId

View File

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

View File

@ -4,6 +4,8 @@ import gameUtil from "../../utils/game.util";
import {Client} from "colyseus"; import {Client} from "colyseus";
import {NextSubCommand} from "./NextSubCommand"; import {NextSubCommand} from "./NextSubCommand";
import {GameStateConst} from "../../constants/GameStateConst"; 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; return;
} }
this.state.cards.clear(); 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) { for (let id of cards) {
this.state.cards.set(id+'', player.cards.get(id + '')); this.state.cards.set(id+'', player.cards.get(id + ''));
player.cards.delete(id + ''); player.cards.delete(id + '');

View File

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

View File

@ -1,15 +1,25 @@
import { Command } from "@colyseus/command"; import { Command } from "@colyseus/command";
import { CardGameState } from "../schema/CardGameState"; import { CardGameState } from "../schema/CardGameState";
import {GameStateConst} from "../../constants/GameStateConst"; 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, {}> { export class NextSubCommand extends Command<CardGameState, {}> {
execute() { async execute() {
this.state.gameState = GameStateConst.STATE_BEGIN_EAT; this.state.gameState = GameStateConst.STATE_BEGIN_EAT;
this.state.giveUpCount = 0; 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()]; // const sessionIds = [...this.state.players.keys()];
// let nextSubTurn = this.state.subTurn ? // let nextSubTurn = this.state.subTurn ?
// sessionIds[(sessionIds.indexOf(this.state.subTurn) + 1) % sessionIds.length] // 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 if (this.state.currentTurn
&& sessionIds.indexOf(this.state.currentTurn) == (sessionIds.length - 1)) { && sessionIds.indexOf(this.state.currentTurn) == (sessionIds.length - 1)) {
this.state.round += 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) this.state.currentTurn = (this.state.currentTurn)
? sessionIds[(sessionIds.indexOf(this.state.currentTurn) + 1) % sessionIds.length] ? 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 {BattleHandler} from "../logic/Handler/BattleHandler";
import {BaseConst} from "../../constants/BaseConst"; import {BaseConst} from "../../constants/BaseConst";
import {error} from "../../common/Debug"; 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.state = PlayerStateConst.PLAYER_SELECT_HERO;
player.hp = unitData.hero_hp; player.hp = unitData.hero_hp;
player.extraTime = singleton(GameEnv).maxExtTime * 1000;
let heroPet = player.pets.get('0'); let heroPet = player.pets.get('0');
heroPet.ap = unitData.powernum; heroPet.ap = unitData.powernum;
heroPet.state = 1; heroPet.state = 1;

View File

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