修改定时器的实现, 增加往定时器增加时间的方法

This commit is contained in:
zhl 2020-12-09 12:03:08 +08:00
parent 66fb0af33a
commit 58c81f7688
7 changed files with 141 additions and 34 deletions

22
src/global.d.ts vendored
View File

@ -182,6 +182,28 @@ declare module "colyseus" {
*/
updatePet(datas: PetInfo[]): void;
/**
* room.mainClock设定任务
* mainClock任何时候只有一个可执行的任务
* @param millisecond
* @param handler
* @param name
*/
beginSchedule(millisecond: number, handler: Function, name: string): void;
/**
* room.mainClock的任务
* mainClock任何时候只有一个可执行的任务
*
*/
stopSchedule(): number;
/**
* room.mainClock增加n秒
* @param millisecond
* @param reason
*/
addScheduleTime(millisecond: number, reason?: string): void;
}
}

View File

@ -10,7 +10,7 @@ import {SelectHeroCommand} from "./commands/SelectHeroCommand";
import {EatCardCommand} from "./commands/EatCardCommand";
import {GiveUpCommand} from "./commands/GiveUpCommand";
import {BattleHandler} from "./logic/Handler/BattleHandler";
import {msgLog} from "../common/Debug";
import {debugRoom, error, msgLog} from "../common/Debug";
import {Delayed} from "@gamestdio/timer/lib/Delayed";
import {IncomingMessage} from "http";
import {PlayerStateConst} from "../constants/PlayerStateConst";
@ -109,4 +109,47 @@ export class GeneralRoom extends Room {
return this.clients.find(client => client.sessionId == sessionId );
}
/**
* room.mainClock设定任务
* mainClock任何时候只有一个可执行的任务
* @param millisecond
* @param handler
* @param name
*/
beginSchedule(millisecond: number, handler: Function, name: string): void {
debugRoom(`begin schedule: `, name, millisecond / 1000);
if (this.mainClock?.active) {
error(`当前已存在进行中的mainClock: ${this.mainClock['args']}`);
}
this.mainClock = this.clock.setTimeout(handler, millisecond , name);
}
/**
* room.mainClock的任务
* mainClock任何时候只有一个可执行的任务
*/
stopSchedule(): number {
debugRoom(`manual stop schedule: ${this.mainClock['args']}`);
if (!this.mainClock.active) {
return -1;
} else {
let time = this.mainClock.elapsedTime;
this.mainClock.clear();
return time;
}
}
/**
* room的mainClock增加n秒
* @param millisecond
* @param reason
*/
addScheduleTime(millisecond: number, reason?: string): void {
debugRoom(`add schedule for ${this.mainClock['args']}, time: ${millisecond/1000}`);
if (this.mainClock?.active) {
this.mainClock.time += millisecond ;
debugRoom(`schedule remain: ${(this.mainClock.time - this.mainClock.elapsedTime)/1000}`);
}
}
}

View File

@ -44,13 +44,20 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
}
this.state.cards.clear();
//停止出牌计时, 并更新player.extraTime;
if (this.room.mainClock?.active) {
let elapsedTime = this.room.stopSchedule();
if (elapsedTime >= 0) {
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
let count = this.room.mainClock.elapsedTime - maxTime;
let newCount = player.extraTime - count;
let count = elapsedTime - maxTime;
let newCount = player.extraTime - Math.min(count, 0);
player.extraTime = Math.max(newCount, 0);
this.room.mainClock.clear();
}
// if (this.room.mainClock?.active) {
// 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 + '');
@ -69,12 +76,18 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
let self = this;
let time = singleton(GameEnv).playerActTime * 1000 + player.extraTime;
// 开启选随从计时, 计时结束后结束当前轮
this.room.mainClock = this.clock.setTimeout(function (){
let timeOverSelectPet = function () {
player.extraTime = 0;
self.room.mainClock.clear();
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
self.room.dispatcher.dispatch(new TurnEndCommand());
}.bind(this), time);
}
this.room.beginSchedule(time, timeOverSelectPet, `select_pet_${client.sessionId}`);
// this.room.mainClock = this.clock.setTimeout(function (){
// player.extraTime = 0;
// self.room.mainClock.clear();
// debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
// self.room.dispatcher.dispatch(new TurnEndCommand());
// }.bind(this), time);
// return [new NextTurnCommand()];
}
}

View File

@ -15,8 +15,7 @@ export class DrawCommand extends Command<CardGameState, {}> {
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();
let timeOverDraw = function () {
if (sessionId == self.state.currentTurn) {
let client = self.room.getClient(sessionId);
let card = player.cards.values().next().value;
@ -24,8 +23,18 @@ export class DrawCommand extends Command<CardGameState, {}> {
player.extraTime = 0;
self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1});
}
}, maxTime + player.extraTime)
// await this.delay((maxTime + player.extraTime) * 1000);
}
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, `draw_card_${sessionId}`);
// 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);
// player.extraTime = 0;
// self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1});
// }
// }, maxTime + player.extraTime)
}
}

View File

@ -31,9 +31,10 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
}
if (giveUpCount >= playerCount) {
// 所有人都放弃了, 则取消定时, 直接进入下一轮
if (this.room.mainClock?.active) {
this.room.mainClock.clear();
}
this.room.stopSchedule();
// if (this.room.mainClock?.active) {
// this.room.mainClock.clear();
// }
return [new TurnEndCommand()];
}
@ -65,9 +66,10 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
// 吃牌逻辑
if (player != null && (isFirst || timeUp)) {
// 如果有吃牌计时的话,停止吃牌计时
if (this.room.mainClock?.active) {
this.room.mainClock.clear();
}
this.room.stopSchedule();
// if (this.room.mainClock?.active) {
// this.room.mainClock.clear();
// }
for (let id of cards) {
this.state.cards.set(id + '', player.cards.get(id + ''));
player.cards.delete(id + '');
@ -79,12 +81,18 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
let self = this;
// 开启选随从计时, 计时结束后结束当前轮
this.room.mainClock = this.clock.setTimeout(function () {
let timeOverSelectPet = function () {
player.extraTime = 0;
self.room.mainClock.clear();
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
self.room.dispatcher.dispatch(new TurnEndCommand());
}, time);
}
self.room.beginSchedule(time, timeOverSelectPet, `select_pet_${player.id}`);
// this.room.mainClock = this.clock.setTimeout(function () {
// player.extraTime = 0;
// self.room.mainClock.clear();
// debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
// self.room.dispatcher.dispatch(new TurnEndCommand());
// }, time);
let fromPlayer = this.state.players.get(this.state.currentTurn);
if (fromPlayer.id !== player.id) {
this.room.battleMan.onCardLinkOver(player, cardArr, fromPlayer);

View File

@ -24,12 +24,17 @@ export class NextSubCommand extends Command<CardGameState, {}> {
let time = singleton(GameEnv).maxEatTime * 1000;
let self = this;
// 启动吃牌定时, 到时后自动进入下一轮
this.room.mainClock = this.clock.setTimeout(function () {
let timeOverEat = function () {
debugRoom('吃牌时间到, 进入下一轮')
self.room.mainClock.clear();
// self.room.dispatcher.dispatch(new TurnEndCommand());
self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true});
}, time);
}
this.room.beginSchedule(time, timeOverEat, `eat_round_${this.state.currentTurn}`);
// this.room.mainClock = this.clock.setTimeout(function () {
// debugRoom('吃牌时间到, 进入下一轮')
// self.room.mainClock.clear();
// // self.room.dispatcher.dispatch(new TurnEndCommand());
// self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true});
// }, time);
}
}

View File

@ -30,9 +30,9 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
continue;
}
let card = player.cards.get(cardId + '');
let neetRemove = false;
let needRemove = false;
if (hasEffectSkill) {
neetRemove = true;
needRemove = true;
if (card.type === 3) {
dbpt_cnt ++;
} else {
@ -41,13 +41,13 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
} else {
if (card.type === 3) {
dbpt_cnt ++;
neetRemove = true;
needRemove = true;
} else if (card.type === 2) {
eff_cnt ++;
neetRemove = true;
needRemove = true;
}
}
if (neetRemove) {
if (needRemove) {
player.cards.delete(cardId + '');
player.cardSet.delete(cardId + '');
}
@ -68,12 +68,18 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
return;
}
//停止选随从计时, 并更新player.extraTime;
if (this.room.mainClock?.active) {
let count = this.room.mainClock.elapsedTime - singleton(GameEnv).playerActTime * 1000;
let newCount = player.extraTime - count;
let elapsedTime = this.room.stopSchedule();
if (elapsedTime >= 0) {
let count = elapsedTime - singleton(GameEnv).playerActTime * 1000;
let newCount = player.extraTime - Math.min(count, 0);
player.extraTime = Math.max(newCount, 0);
this.room.mainClock.clear();
}
// if (this.room.mainClock?.active) {
// let count = this.room.mainClock.elapsedTime - singleton(GameEnv).playerActTime * 1000;
// let newCount = player.extraTime - count;
// player.extraTime = Math.max(newCount, 0);
// this.room.mainClock.clear();
// }
let dstplayer = this.state.players.get(playerId);
let dstpet;
@ -83,6 +89,7 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
let cardpoint = moreAp + ap;
let data = {srcplayer: player, card: targetCard.effect, cardpoint, eff_cnt, dstplayer, dstpet, dbpt_cnt}
this.room.battleMan.useCard(data);
//TODO:: 增加技能延时
return [new TurnEndCommand()];
}