修改定时器的实现, 增加往定时器增加时间的方法
This commit is contained in:
parent
66fb0af33a
commit
58c81f7688
22
src/global.d.ts
vendored
22
src/global.d.ts
vendored
@ -182,6 +182,28 @@ declare module "colyseus" {
|
|||||||
*/
|
*/
|
||||||
updatePet(datas: PetInfo[]): void;
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import {SelectHeroCommand} from "./commands/SelectHeroCommand";
|
|||||||
import {EatCardCommand} from "./commands/EatCardCommand";
|
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 {msgLog} from "../common/Debug";
|
import {debugRoom, error, msgLog} from "../common/Debug";
|
||||||
import {Delayed} from "@gamestdio/timer/lib/Delayed";
|
import {Delayed} from "@gamestdio/timer/lib/Delayed";
|
||||||
import {IncomingMessage} from "http";
|
import {IncomingMessage} from "http";
|
||||||
import {PlayerStateConst} from "../constants/PlayerStateConst";
|
import {PlayerStateConst} from "../constants/PlayerStateConst";
|
||||||
@ -109,4 +109,47 @@ export class GeneralRoom extends Room {
|
|||||||
return this.clients.find(client => client.sessionId == sessionId );
|
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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -44,13 +44,20 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
|
|||||||
}
|
}
|
||||||
this.state.cards.clear();
|
this.state.cards.clear();
|
||||||
//停止出牌计时, 并更新player.extraTime;
|
//停止出牌计时, 并更新player.extraTime;
|
||||||
if (this.room.mainClock?.active) {
|
let elapsedTime = this.room.stopSchedule();
|
||||||
|
if (elapsedTime >= 0) {
|
||||||
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
|
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
|
||||||
let count = this.room.mainClock.elapsedTime - maxTime;
|
let count = elapsedTime - maxTime;
|
||||||
let newCount = player.extraTime - count;
|
let newCount = player.extraTime - Math.min(count, 0);
|
||||||
player.extraTime = Math.max(newCount, 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) {
|
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 + '');
|
||||||
@ -69,12 +76,18 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
|
|||||||
let self = this;
|
let self = this;
|
||||||
let time = singleton(GameEnv).playerActTime * 1000 + player.extraTime;
|
let time = singleton(GameEnv).playerActTime * 1000 + player.extraTime;
|
||||||
// 开启选随从计时, 计时结束后结束当前轮
|
// 开启选随从计时, 计时结束后结束当前轮
|
||||||
this.room.mainClock = this.clock.setTimeout(function (){
|
let timeOverSelectPet = function () {
|
||||||
player.extraTime = 0;
|
player.extraTime = 0;
|
||||||
self.room.mainClock.clear();
|
|
||||||
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
|
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
|
||||||
self.room.dispatcher.dispatch(new TurnEndCommand());
|
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()];
|
// return [new NextTurnCommand()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,7 @@ export class DrawCommand extends Command<CardGameState, {}> {
|
|||||||
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
|
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
|
||||||
let player = this.state.players.get(sessionId);
|
let player = this.state.players.get(sessionId);
|
||||||
let self = this;
|
let self = this;
|
||||||
this.room.mainClock = this.clock.setTimeout(function () {
|
let timeOverDraw = function () {
|
||||||
self.room.mainClock.clear();
|
|
||||||
if (sessionId == self.state.currentTurn) {
|
if (sessionId == self.state.currentTurn) {
|
||||||
let client = self.room.getClient(sessionId);
|
let client = self.room.getClient(sessionId);
|
||||||
let card = player.cards.values().next().value;
|
let card = player.cards.values().next().value;
|
||||||
@ -24,8 +23,18 @@ export class DrawCommand extends Command<CardGameState, {}> {
|
|||||||
player.extraTime = 0;
|
player.extraTime = 0;
|
||||||
self.room.dispatcher.dispatch(new DiscardCommand(), {client, cards: [card.id], dtype: 1});
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,10 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
|||||||
}
|
}
|
||||||
if (giveUpCount >= playerCount) {
|
if (giveUpCount >= playerCount) {
|
||||||
// 所有人都放弃了, 则取消定时, 直接进入下一轮
|
// 所有人都放弃了, 则取消定时, 直接进入下一轮
|
||||||
if (this.room.mainClock?.active) {
|
this.room.stopSchedule();
|
||||||
this.room.mainClock.clear();
|
// if (this.room.mainClock?.active) {
|
||||||
}
|
// this.room.mainClock.clear();
|
||||||
|
// }
|
||||||
return [new TurnEndCommand()];
|
return [new TurnEndCommand()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,9 +66,10 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
|||||||
// 吃牌逻辑
|
// 吃牌逻辑
|
||||||
if (player != null && (isFirst || timeUp)) {
|
if (player != null && (isFirst || timeUp)) {
|
||||||
// 如果有吃牌计时的话,停止吃牌计时
|
// 如果有吃牌计时的话,停止吃牌计时
|
||||||
if (this.room.mainClock?.active) {
|
this.room.stopSchedule();
|
||||||
this.room.mainClock.clear();
|
// if (this.room.mainClock?.active) {
|
||||||
}
|
// 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 + '');
|
||||||
@ -79,12 +81,18 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
|||||||
let self = this;
|
let self = this;
|
||||||
|
|
||||||
// 开启选随从计时, 计时结束后结束当前轮
|
// 开启选随从计时, 计时结束后结束当前轮
|
||||||
this.room.mainClock = this.clock.setTimeout(function () {
|
let timeOverSelectPet = function () {
|
||||||
player.extraTime = 0;
|
player.extraTime = 0;
|
||||||
self.room.mainClock.clear();
|
|
||||||
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
|
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
|
||||||
self.room.dispatcher.dispatch(new TurnEndCommand());
|
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);
|
let fromPlayer = this.state.players.get(this.state.currentTurn);
|
||||||
if (fromPlayer.id !== player.id) {
|
if (fromPlayer.id !== player.id) {
|
||||||
this.room.battleMan.onCardLinkOver(player, cardArr, fromPlayer);
|
this.room.battleMan.onCardLinkOver(player, cardArr, fromPlayer);
|
||||||
|
@ -24,12 +24,17 @@ export class NextSubCommand extends Command<CardGameState, {}> {
|
|||||||
let time = singleton(GameEnv).maxEatTime * 1000;
|
let time = singleton(GameEnv).maxEatTime * 1000;
|
||||||
let self = this;
|
let self = this;
|
||||||
// 启动吃牌定时, 到时后自动进入下一轮
|
// 启动吃牌定时, 到时后自动进入下一轮
|
||||||
this.room.mainClock = this.clock.setTimeout(function () {
|
let timeOverEat = function () {
|
||||||
debugRoom('吃牌时间到, 进入下一轮')
|
debugRoom('吃牌时间到, 进入下一轮')
|
||||||
self.room.mainClock.clear();
|
|
||||||
// self.room.dispatcher.dispatch(new TurnEndCommand());
|
|
||||||
self.room.dispatcher.dispatch(new EatConfirmCommand(), {timeUp: true});
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,9 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let card = player.cards.get(cardId + '');
|
let card = player.cards.get(cardId + '');
|
||||||
let neetRemove = false;
|
let needRemove = false;
|
||||||
if (hasEffectSkill) {
|
if (hasEffectSkill) {
|
||||||
neetRemove = true;
|
needRemove = true;
|
||||||
if (card.type === 3) {
|
if (card.type === 3) {
|
||||||
dbpt_cnt ++;
|
dbpt_cnt ++;
|
||||||
} else {
|
} else {
|
||||||
@ -41,13 +41,13 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
|
|||||||
} else {
|
} else {
|
||||||
if (card.type === 3) {
|
if (card.type === 3) {
|
||||||
dbpt_cnt ++;
|
dbpt_cnt ++;
|
||||||
neetRemove = true;
|
needRemove = true;
|
||||||
} else if (card.type === 2) {
|
} else if (card.type === 2) {
|
||||||
eff_cnt ++;
|
eff_cnt ++;
|
||||||
neetRemove = true;
|
needRemove = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (neetRemove) {
|
if (needRemove) {
|
||||||
player.cards.delete(cardId + '');
|
player.cards.delete(cardId + '');
|
||||||
player.cardSet.delete(cardId + '');
|
player.cardSet.delete(cardId + '');
|
||||||
}
|
}
|
||||||
@ -68,12 +68,18 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//停止选随从计时, 并更新player.extraTime;
|
//停止选随从计时, 并更新player.extraTime;
|
||||||
if (this.room.mainClock?.active) {
|
let elapsedTime = this.room.stopSchedule();
|
||||||
let count = this.room.mainClock.elapsedTime - singleton(GameEnv).playerActTime * 1000;
|
if (elapsedTime >= 0) {
|
||||||
let newCount = player.extraTime - count;
|
let count = elapsedTime - singleton(GameEnv).playerActTime * 1000;
|
||||||
|
let newCount = player.extraTime - Math.min(count, 0);
|
||||||
player.extraTime = Math.max(newCount, 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 dstplayer = this.state.players.get(playerId);
|
||||||
let dstpet;
|
let dstpet;
|
||||||
@ -83,6 +89,7 @@ export class SelectPetCommand extends Command<CardGameState, {client: Client,
|
|||||||
let cardpoint = moreAp + ap;
|
let cardpoint = moreAp + ap;
|
||||||
let data = {srcplayer: player, card: targetCard.effect, cardpoint, eff_cnt, dstplayer, dstpet, dbpt_cnt}
|
let data = {srcplayer: player, card: targetCard.effect, cardpoint, eff_cnt, dstplayer, dstpet, dbpt_cnt}
|
||||||
this.room.battleMan.useCard(data);
|
this.room.battleMan.useCard(data);
|
||||||
|
//TODO:: 增加技能延时
|
||||||
return [new TurnEndCommand()];
|
return [new TurnEndCommand()];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user