54 lines
2.2 KiB
TypeScript
54 lines
2.2 KiB
TypeScript
import {Command} from "@colyseus/command";
|
|
import {CardGameState} from "../schema/CardGameState";
|
|
import {Client} from "colyseus";
|
|
import gameUtil from "../../utils/game.util";
|
|
import {GameStateConst} from "../../constants/GameStateConst";
|
|
import {EatConfirmCommand} from "./EatConfirmCommand";
|
|
import {GameEnv} from "../../cfg/GameEnv";
|
|
|
|
/**
|
|
* 吃牌
|
|
*/
|
|
export class EatCardCommand extends Command<CardGameState, { client: Client, cards: [string], target: string }> {
|
|
execute({ client, cards, target } = this.payload) {
|
|
const player = this.state.players.get(client.sessionId);
|
|
if (!player) {
|
|
this.room.send(client,'eat_card_s2c', {errcode: 1, errmsg: 'player不存在或者'});
|
|
return;
|
|
}
|
|
if (!gameUtil.checkCardsExists(player.cards, cards)) {
|
|
this.room.send(client,'eat_card_s2c', {errcode: 2, errmsg: '要出的牌在手牌中不存在'});
|
|
return;
|
|
}
|
|
if (this.state.currentTurn == client.sessionId) {
|
|
this.room.send(client,'eat_card_s2c', {errcode: 3, errmsg: '不能吃自己的牌'});
|
|
return;
|
|
}
|
|
if (!this.state.cards.has(target + '')) {
|
|
this.room.send(client,'eat_card_s2c', {errcode: 4, errmsg: '找不到要吃的牌'});
|
|
return;
|
|
}
|
|
let tmpCards = [];
|
|
for (let id of cards) {
|
|
tmpCards.push(player.cards.get(id + ''));
|
|
}
|
|
tmpCards.push(this.state.cards.get(target + ''));
|
|
if (!gameUtil.checkDiscard(tmpCards, new GameEnv().otherEatCount)) {
|
|
this.room.send(client,'discard_card_s2c', {errcode: 5, errmsg: '不符合吃牌规则'});
|
|
return;
|
|
}
|
|
if (this.state.gameState !== GameStateConst.STATE_BEGIN_EAT) {
|
|
this.room.send(client,'discard_card_s2c', {errcode: 7, errmsg: '不是吃牌轮'});
|
|
return;
|
|
}
|
|
//将吃牌数据临时保存
|
|
if (this.state.tmpActionMap.has(client.sessionId)) {
|
|
this.room.send(client,'discard_card_s2c', {errcode: 8, errmsg: '不可更改操作'});
|
|
return ;
|
|
}
|
|
this.state.tmpActionMap.set(client.sessionId, cards);
|
|
return [new EatConfirmCommand().setPayload({timeUp: false})];
|
|
}
|
|
|
|
}
|