增加吃牌逻辑

This commit is contained in:
zhl 2020-12-02 14:27:19 +08:00
parent 18c52f8e84
commit 0483e1cb0b
9 changed files with 97 additions and 28 deletions

View File

@ -14,6 +14,8 @@ export class GameStateConst {
public static readonly STATE_CHANGE_CARD = 5;
// 比大小,确定先手
public static readonly DETERMINE_TURN = 6;
// 选英雄阶段
public static readonly CHANGE_HERO = 7;
// 游戏结束
public static readonly STATE_GAME_OVER = 9;

View File

@ -9,5 +9,7 @@ export class PlayerStateConst {
public static readonly PLAYER_OFFLINE = 9;
// 玩家开局已换完牌
public static readonly PLAYER_CHANGE_CARD = 3;
// 玩家已选择英雄
public static readonly PLAYER_SELECT_HERO = 4;
}

View File

@ -7,6 +7,8 @@ import {DiscardCommand} from "./commands/DiscardCommand";
import {NextSubCommand} from "./commands/NextSubCommand";
import {SelectPetCommand} from "./commands/SelectPetCommand";
import {ChangeCardCommand} from "./commands/ChangeCardCommand";
import {SelectHeroCommand} from "./commands/SelectHeroCommand";
import {EatCardCommand} from "./commands/EatCardCommand";
export class GeneralRoom extends Room {
dispatcher = new Dispatcher(this);
@ -28,8 +30,12 @@ export class GeneralRoom extends Room {
console.log('discard_card from ', client.sessionId, message);
this.dispatcher.dispatch(new DiscardCommand(), {client, cards: message.cards});
});
this.onMessage("eat_card_c2s", (client, message) => {
console.log('eat_card from ', client.sessionId, message);
this.dispatcher.dispatch(new EatCardCommand(), {client, cards: message.cards, target: message.target});
});
this.onMessage("give_up_take_c2s", (client, message) => {
this.onMessage("give_up_eat_c2s", (client, message) => {
console.log('give_up_take from ', client.sessionId, message);
this.dispatcher.dispatch(new NextSubCommand(), {});
});
@ -39,6 +45,11 @@ export class GeneralRoom extends Room {
this.dispatcher.dispatch(new SelectPetCommand(), {client, cardId: message.cardId});
});
this.onMessage("select_hero_c2s", (client, message) => {
console.log('select_hero from ', client.sessionId, message);
this.dispatcher.dispatch(new SelectHeroCommand(), {client, heroId: message.heroId});
});
this.onMessage("*", (client, type, message) => {
//
// Triggers when any other type of message is sent,

View File

@ -23,7 +23,7 @@ export class ChangeCardCommand extends Command<CardGameState, { client: Client,
cardToRemove.push(card);
}
let newCards = gameUtil.changeCard(this.state.cardQueue, player, cardToRemove);
client.send('change_card_s2c', {cards: newCards});
client.send('change_card_s2c', {errcode:0, errmsg: '', cards: newCards});
player.state = PlayerStateConst.PLAYER_CHANGE_CARD;
let finishCount = 0;
for (let [sessionId, player] of this.state.players) {

View File

@ -4,43 +4,48 @@ import gameUtil from "../../utils/game.util";
import {Client} from "colyseus";
import {NextTurnCommand} from "./NextTurnCommand";
import {NextSubCommand} from "./NextSubCommand";
import {GameStateConst} from "../../constants/GameStateConst";
/**
*
*/
export class DiscardCommand extends Command<CardGameState, { client: Client, cards: [string] }> {
validate({ client, cards } = this.payload) {
const player = this.state.players.get(client.sessionId);
return player !== undefined && gameUtil.checkCardsExists(player.cards, cards);
}
// validate({ client, cards } = this.payload) {
// const player = this.state.players.get(client.sessionId);
// return player !== undefined && gameUtil.checkCardsExists(player.cards, cards);
// }
execute({ client, cards } = 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;
}
this.state.cards.clear();
for (let id of cards) {
this.state.cards.set(id, this.state.players.get(client.sessionId).cards.get(id));
this.state.players.get(client.sessionId).cards.delete(id);
this.state.players.get(client.sessionId).cardSet.delete(id);
}
if (this.state.currentTurn == client.sessionId) {
/**
* ,
* ,
*
*/
if (cards.length === 1) {
return [new NextSubCommand()];
} else {
this.state.gameState = 4;
// return [new NextTurnCommand()];
}
/**
* ,
* ,
*
*/
if (cards.length === 1) {
return [new NextSubCommand()];
} else {
/**
* id和当前轮id不一直, ,
*/
return [new NextTurnCommand()];
this.state.gameState = GameStateConst.STATE_PICK_PET;
// return [new NextTurnCommand()];
}
}
}

View File

@ -0,0 +1,25 @@
import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {Client} from "colyseus";
import gameUtil from "../../utils/game.util";
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) {
client.send('eat_card_s2c', {errcode: 1, errmsg: 'player不存在或者'});
return;
}
if (!gameUtil.checkCardsExists(player.cards, cards)) {
client.send('eat_card_s2c', {errcode: 2, errmsg: '要出的牌在手牌中不存在'});
return;
}
if (this.state.subTurn != client.sessionId || this.state.currentTurn == client.sessionId) {
client.send('eat_card_s2c', {errcode: 3, errmsg: '不是自己的吃牌轮'});
return;
}
}
}

View File

@ -25,7 +25,8 @@ export class PlayReadyCommand extends Command<CardGameState, {
// 如果所有人的状态都为已准备状态, 则开始发牌
if (readyCount >= this.room.maxClients) {
// 比大小, 确定先手
return [new PrepareCommand()];
// return [new PrepareCommand()];
this.room.state.gameState = GameStateConst.CHANGE_HERO;
}
}

View File

@ -0,0 +1,23 @@
import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {Client} from "colyseus";
import {PlayerStateConst} from "../../constants/PlayerStateConst";
import {BeginGameCommand} from "./BeginGameCommand";
export class SelectHeroCommand extends Command<CardGameState, {client: Client, heroId: number}> {
execute({ client, heroId } = this.payload) {
let player = this.state.players.get(client.sessionId);
player.heroId = heroId;
player.state = PlayerStateConst.PLAYER_SELECT_HERO;
this.room.broadcast('select_hero_s2c', {errocode: 0, errmsg: '', player: client.sessionId, heroId: heroId});
let readyCount = 0;
for (let [sessionId, player] of this.state.players) {
if (player.state === PlayerStateConst.PLAYER_SELECT_HERO) {
readyCount++;
}
}
if (readyCount >= this.room.maxClients) {
return [new BeginGameCommand()];
}
}
}

View File

@ -1,6 +1,7 @@
import { Schema, MapSchema, type } from "@colyseus/schema";
import {Player} from "./Player";
import {Card} from "./Card";
import {GameStateConst} from "../../constants/GameStateConst";
export class CardGameState extends Schema {
@ -8,8 +9,7 @@ export class CardGameState extends Schema {
@type({ map: Player })
players = new MapSchema<Player>();
/**
*
* 0: 等待玩家加入
* , GameStateConst.ts
* 1: 等待玩家准备
* 2: 游戏进行中-
* 3: 游戏进行中-
@ -17,7 +17,7 @@ export class CardGameState extends Schema {
* 9: 游戏结束
*/
@type("number")
gameState: number = 0;
gameState: number = GameStateConst.STATE_WAIT_JOIN;
@type("string")
currentTurn: string;