From 0483e1cb0b3e44d1e76b6ef5c6b39930e5ed91a5 Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 2 Dec 2020 14:27:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=83=E7=89=8C=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/GameStateConst.ts | 2 + src/constants/PlayerStateConst.ts | 2 + src/rooms/GeneralRoom.ts | 13 ++++++- src/rooms/commands/ChangeCardCommand.ts | 2 +- src/rooms/commands/DiscardCommand.ts | 49 ++++++++++++++----------- src/rooms/commands/EatCardCommand.ts | 25 +++++++++++++ src/rooms/commands/PlayReadyCommand.ts | 3 +- src/rooms/commands/SelectHeroCommand.ts | 23 ++++++++++++ src/rooms/schema/CardGameState.ts | 6 +-- 9 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 src/rooms/commands/EatCardCommand.ts create mode 100644 src/rooms/commands/SelectHeroCommand.ts diff --git a/src/constants/GameStateConst.ts b/src/constants/GameStateConst.ts index 5889f24..3ebd7db 100644 --- a/src/constants/GameStateConst.ts +++ b/src/constants/GameStateConst.ts @@ -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; diff --git a/src/constants/PlayerStateConst.ts b/src/constants/PlayerStateConst.ts index 7f72f6c..548e817 100644 --- a/src/constants/PlayerStateConst.ts +++ b/src/constants/PlayerStateConst.ts @@ -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; } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 84903d4..2f398d5 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -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, diff --git a/src/rooms/commands/ChangeCardCommand.ts b/src/rooms/commands/ChangeCardCommand.ts index 4ea306c..20eaa2a 100644 --- a/src/rooms/commands/ChangeCardCommand.ts +++ b/src/rooms/commands/ChangeCardCommand.ts @@ -23,7 +23,7 @@ export class ChangeCardCommand extends Command { - 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()]; } - } } diff --git a/src/rooms/commands/EatCardCommand.ts b/src/rooms/commands/EatCardCommand.ts new file mode 100644 index 0000000..8e4300f --- /dev/null +++ b/src/rooms/commands/EatCardCommand.ts @@ -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 { + 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; + } + + } + +} diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index 673e133..9e1f529 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -25,7 +25,8 @@ export class PlayReadyCommand extends Command= this.room.maxClients) { // 比大小, 确定先手 - return [new PrepareCommand()]; + // return [new PrepareCommand()]; + this.room.state.gameState = GameStateConst.CHANGE_HERO; } } diff --git a/src/rooms/commands/SelectHeroCommand.ts b/src/rooms/commands/SelectHeroCommand.ts new file mode 100644 index 0000000..32fd974 --- /dev/null +++ b/src/rooms/commands/SelectHeroCommand.ts @@ -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 { + 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()]; + } + } +} diff --git a/src/rooms/schema/CardGameState.ts b/src/rooms/schema/CardGameState.ts index 300446c..31a1ea0 100644 --- a/src/rooms/schema/CardGameState.ts +++ b/src/rooms/schema/CardGameState.ts @@ -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(); /** - * 游戏装备 - * 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;