card_svr/src/rooms/commands/DiscardCommand.ts
2020-12-11 20:15:54 +08:00

101 lines
4.5 KiB
TypeScript

import { Command } from "@colyseus/command";
import { CardGameState } from "../schema/CardGameState";
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";
import {debugRoom} from "../../common/Debug";
import {TurnEndCommand} from "./TurnEndCommand";
import {Card} from "../schema/Card";
/**
* 出牌
* type: 0, 正常的抽牌
* type: 1, 到时间后, 自动的抽牌
*/
export class DiscardCommand extends Command<CardGameState, { client: Client, cards: [string], dtype: number }> {
// validate({ client, cards } = this.payload) {
// const player = this.state.players.get(client.sessionId);
// return player !== undefined && gameUtil.checkCardsExists(player.cards, cards);
// }
execute({ client, cards , dtype} = this.payload) {
const player = this.state.players.get(client.sessionId);
if (!player) {
client.send('discard_card_s2c', {errcode: 1, errmsg: 'player不存在'});
return;
}
if (!gameUtil.checkCardsExists(player.cards, cards)) {
client.send('discard_card_s2c', {errcode: 2, errmsg: '要出的牌在手牌中不存在'});
return;
}
if (this.state.currentTurn != client.sessionId) {
client.send('discard_card_s2c', {errcode: 3, errmsg: '不是当前轮'});
return;
}
let tmpCards = [];
for (let id of cards) {
tmpCards.push(player.cards.get(id + ''));
}
if (!gameUtil.checkDiscard(tmpCards)) {
client.send('discard_card_s2c', {errcode: 4, errmsg: '出牌不符合规则'});
return;
}
this.state.cards.clear();
//停止出牌计时, 并更新player.extraTime;
let elapsedTime = this.room.stopSchedule();
if (elapsedTime >= 0) {
let maxTime = singleton(GameEnv).maxDiscardTime * 1000;
let count = elapsedTime - maxTime;
let newCount = player.extraTime - Math.min(count, 0);
player.extraTime = Math.max(newCount, 0);
}
// 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 + '');
player.cardSet.delete(id + '');
}
/**
* 这说明是当前轮正常出牌,
* 如果出一张牌的话, 进入胡牌轮
* 否则直接进入选随从轮
*/
client.send('discard_card_s2c', {errcode: 0, cards: cards, type: dtype})
if (cards.length === 1) {
let cardArr: Card[] = [...this.state.cards.values()];
this.room.battleMan.onCardDiscarded(player, cardArr[0])
return [new NextSubCommand()];
} else {
let cardArr: Card[] = [...this.state.cards.values()];
this.room.battleMan.onCardLinkOver(player, cardArr);
this.state.updateGameState(GameStateConst.STATE_PICK_PET);
let self = this;
client.send('eat_card_s2c', {player: player.id, errcode: 0, errmsg: ''});
let time = singleton(GameEnv).playerActTime * 1000 + player.extraTime;
// 开启选随从计时, 计时结束后结束当前轮
let timeOverSelectPet = function () {
player.extraTime = 0;
debugRoom('选随从或者法术时间到, 自动出牌, 自动进入下一轮');
self.room.dispatcher.dispatch(new TurnEndCommand());
}
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()];
}
}
}