修改出牌,吃牌规则, 增加一些注释
This commit is contained in:
parent
0483e1cb0b
commit
c3b189bbdf
@ -42,7 +42,7 @@ export class GeneralRoom extends Room {
|
|||||||
|
|
||||||
this.onMessage("select_pet_c2s", (client, message) => {
|
this.onMessage("select_pet_c2s", (client, message) => {
|
||||||
console.log('select_pet from ', client.sessionId, message);
|
console.log('select_pet from ', client.sessionId, message);
|
||||||
this.dispatcher.dispatch(new SelectPetCommand(), {client, cardId: message.cardId});
|
this.dispatcher.dispatch(new SelectPetCommand(), {client, cardId: message.cardId, playerId: message.player, pos: message.pos, effCards: message.effCards });
|
||||||
});
|
});
|
||||||
|
|
||||||
this.onMessage("select_hero_c2s", (client, message) => {
|
this.onMessage("select_hero_c2s", (client, message) => {
|
||||||
|
@ -6,7 +6,11 @@ import gameUtil from "../../utils/game.util";
|
|||||||
import {singleton} from "../../common/Singleton";
|
import {singleton} from "../../common/Singleton";
|
||||||
import {GameEnv} from "../../cfg/GameEnv";
|
import {GameEnv} from "../../cfg/GameEnv";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始游戏
|
||||||
|
* 1. 生成卡组
|
||||||
|
* 2. 将游戏状态改为 STATE_CHANGE_CARD
|
||||||
|
*/
|
||||||
export class BeginGameCommand extends Command<CardGameState, {}> {
|
export class BeginGameCommand extends Command<CardGameState, {}> {
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
|
@ -8,6 +8,9 @@ import {singleton} from "../../common/Singleton";
|
|||||||
import {GameEnv} from "../../cfg/GameEnv";
|
import {GameEnv} from "../../cfg/GameEnv";
|
||||||
import {GameStateConst} from "../../constants/GameStateConst";
|
import {GameStateConst} from "../../constants/GameStateConst";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开局换卡
|
||||||
|
*/
|
||||||
export class ChangeCardCommand extends Command<CardGameState, { client: Client, cards: [string] }> {
|
export class ChangeCardCommand extends Command<CardGameState, { client: Client, cards: [string] }> {
|
||||||
validate({ client, cards } = this.payload) {
|
validate({ client, cards } = this.payload) {
|
||||||
const player = this.state.players.get(client.sessionId);
|
const player = this.state.players.get(client.sessionId);
|
||||||
|
@ -29,11 +29,19 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
|
|||||||
client.send('discard_card_s2c', {errcode: 3, errmsg: '不是当前轮'});
|
client.send('discard_card_s2c', {errcode: 3, errmsg: '不是当前轮'});
|
||||||
return;
|
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();
|
this.state.cards.clear();
|
||||||
for (let id of cards) {
|
for (let id of cards) {
|
||||||
this.state.cards.set(id, this.state.players.get(client.sessionId).cards.get(id));
|
this.state.cards.set(id, player.cards.get(id));
|
||||||
this.state.players.get(client.sessionId).cards.delete(id);
|
player.cards.delete(id);
|
||||||
this.state.players.get(client.sessionId).cardSet.delete(id);
|
player.cardSet.delete(id);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 这说明是当前轮正常出牌,
|
* 这说明是当前轮正常出牌,
|
||||||
|
@ -4,6 +4,9 @@ import {singleton} from "../../common/Singleton";
|
|||||||
import {GameEnv} from "../../cfg/GameEnv";
|
import {GameEnv} from "../../cfg/GameEnv";
|
||||||
import gameUtil from "../../utils/game.util";
|
import gameUtil from "../../utils/game.util";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正常的抽卡
|
||||||
|
*/
|
||||||
export class DrawCommand extends Command<CardGameState, {}> {
|
export class DrawCommand extends Command<CardGameState, {}> {
|
||||||
execute() {
|
execute() {
|
||||||
let sessionId = this.state.currentTurn;
|
let sessionId = this.state.currentTurn;
|
||||||
|
@ -3,7 +3,9 @@ import {CardGameState} from "../schema/CardGameState";
|
|||||||
import {Client} from "colyseus";
|
import {Client} from "colyseus";
|
||||||
import gameUtil from "../../utils/game.util";
|
import gameUtil from "../../utils/game.util";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 吃牌
|
||||||
|
*/
|
||||||
export class EatCardCommand extends Command<CardGameState, { client: Client, cards: [string], target: string }> {
|
export class EatCardCommand extends Command<CardGameState, { client: Client, cards: [string], target: string }> {
|
||||||
execute({ client, cards, target } = this.payload) {
|
execute({ client, cards, target } = this.payload) {
|
||||||
const player = this.state.players.get(client.sessionId);
|
const player = this.state.players.get(client.sessionId);
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
import { Command } from "@colyseus/command";
|
import { Command } from "@colyseus/command";
|
||||||
import { CardGameState } from "../schema/CardGameState";
|
import { CardGameState } from "../schema/CardGameState";
|
||||||
import {NextTurnCommand} from "./NextTurnCommand";
|
import {NextTurnCommand} from "./NextTurnCommand";
|
||||||
|
import {GameStateConst} from "../../constants/GameStateConst";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下一个吃牌轮
|
||||||
|
*/
|
||||||
export class NextSubCommand extends Command<CardGameState, {}> {
|
export class NextSubCommand extends Command<CardGameState, {}> {
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
this.state.gameState = 3;
|
this.state.gameState = GameStateConst.STATE_BEGIN_EAT;
|
||||||
const sessionIds = [...this.state.players.keys()];
|
const sessionIds = [...this.state.players.keys()];
|
||||||
let nextSubTurn = this.state.subTurn ?
|
let nextSubTurn = this.state.subTurn ?
|
||||||
sessionIds[(sessionIds.indexOf(this.state.subTurn) + 1) % sessionIds.length]
|
sessionIds[(sessionIds.indexOf(this.state.subTurn) + 1) % sessionIds.length]
|
||||||
|
@ -2,6 +2,9 @@ import {Command} from "@colyseus/command";
|
|||||||
import {CardGameState} from "../schema/CardGameState";
|
import {CardGameState} from "../schema/CardGameState";
|
||||||
import {DrawCommand} from "./DrawCommand";
|
import {DrawCommand} from "./DrawCommand";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下一轮
|
||||||
|
*/
|
||||||
export class NextTurnCommand extends Command<CardGameState, {}> {
|
export class NextTurnCommand extends Command<CardGameState, {}> {
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
|
@ -3,6 +3,9 @@ import {CardGameState} from "../schema/CardGameState";
|
|||||||
import {Player} from "../schema/Player";
|
import {Player} from "../schema/Player";
|
||||||
import {Client} from "colyseus";
|
import {Client} from "colyseus";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 玩家成功加入房间
|
||||||
|
*/
|
||||||
export class OnJoinCommand extends Command<CardGameState, {
|
export class OnJoinCommand extends Command<CardGameState, {
|
||||||
client: Client
|
client: Client
|
||||||
}> {
|
}> {
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
import {Command} from "@colyseus/command";
|
import {Command} from "@colyseus/command";
|
||||||
import {CardGameState} from "../schema/CardGameState";
|
import {CardGameState} from "../schema/CardGameState";
|
||||||
import {Client} from "colyseus";
|
import {Client} from "colyseus";
|
||||||
import gameUtil from "../../utils/game.util";
|
|
||||||
import {GeneralRoom} from "../GeneralRoom";
|
|
||||||
import {singleton} from "../../common/Singleton";
|
|
||||||
import {GameEnv} from "../../cfg/GameEnv";
|
|
||||||
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
||||||
import {GameStateConst} from "../../constants/GameStateConst";
|
import {GameStateConst} from "../../constants/GameStateConst";
|
||||||
import {PrepareCommand} from "./PrepareCommand";
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 玩家已准备
|
||||||
|
*/
|
||||||
export class PlayReadyCommand extends Command<CardGameState, {
|
export class PlayReadyCommand extends Command<CardGameState, {
|
||||||
client: Client
|
client: Client
|
||||||
}> {
|
}> {
|
||||||
|
@ -3,7 +3,9 @@ import {CardGameState} from "../schema/CardGameState";
|
|||||||
import {GameStateConst} from "../../constants/GameStateConst";
|
import {GameStateConst} from "../../constants/GameStateConst";
|
||||||
import {BeginGameCommand} from "./BeginGameCommand";
|
import {BeginGameCommand} from "./BeginGameCommand";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开局随机发牌比大小, 确定先手
|
||||||
|
*/
|
||||||
export class PrepareCommand extends Command<CardGameState, {}> {
|
export class PrepareCommand extends Command<CardGameState, {}> {
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
|
@ -4,6 +4,9 @@ import {Client} from "colyseus";
|
|||||||
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
||||||
import {BeginGameCommand} from "./BeginGameCommand";
|
import {BeginGameCommand} from "./BeginGameCommand";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 选择英雄
|
||||||
|
*/
|
||||||
export class SelectHeroCommand extends Command<CardGameState, {client: Client, heroId: number}> {
|
export class SelectHeroCommand extends Command<CardGameState, {client: Client, heroId: number}> {
|
||||||
execute({ client, heroId } = this.payload) {
|
execute({ client, heroId } = this.payload) {
|
||||||
let player = this.state.players.get(client.sessionId);
|
let player = this.state.players.get(client.sessionId);
|
||||||
|
@ -3,8 +3,16 @@ import { CardGameState } from "../schema/CardGameState";
|
|||||||
import {Client} from "colyseus";
|
import {Client} from "colyseus";
|
||||||
import {NextTurnCommand} from "./NextTurnCommand";
|
import {NextTurnCommand} from "./NextTurnCommand";
|
||||||
|
|
||||||
export class SelectPetCommand extends Command<CardGameState, {client: Client, cardId: string}> {
|
/**
|
||||||
execute({client, cardId}: {client: Client, cardId: string}) {
|
* 选择随从或者法术
|
||||||
|
*/
|
||||||
|
export class SelectPetCommand extends Command<CardGameState, {client: Client,
|
||||||
|
cardId: string,
|
||||||
|
playerId: string,
|
||||||
|
pos: number,
|
||||||
|
effCards: string[]
|
||||||
|
}> {
|
||||||
|
execute({client, cardId, playerId, pos, effCards}: {client: Client, cardId: string, playerId: string, pos: number, effCards: string[]}) {
|
||||||
let sessionId = this.state.currentTurn;
|
let sessionId = this.state.currentTurn;
|
||||||
let player = this.state.players.get(sessionId);
|
let player = this.state.players.get(sessionId);
|
||||||
let ap = 0;
|
let ap = 0;
|
||||||
|
@ -13,6 +13,9 @@ export class Pet extends Schema {
|
|||||||
@type("string")
|
@type("string")
|
||||||
type: string;
|
type: string;
|
||||||
|
|
||||||
|
@type("string")
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
@ -81,6 +81,51 @@ let gameUtil = {
|
|||||||
player.cardSet.add(card.id);
|
player.cardSet.add(card.id);
|
||||||
}
|
}
|
||||||
return cards;
|
return cards;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* 检查出牌是否符合规则
|
||||||
|
* @param cardsLst
|
||||||
|
*/
|
||||||
|
checkDiscard(cardsLst: Card[]) {
|
||||||
|
if (cardsLst.length == 0 || cardsLst.length == 2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
let numSet: number[] = [];
|
||||||
|
cardsLst.forEach(function (value) {
|
||||||
|
if (value.number < 11) {
|
||||||
|
numSet.push(value.number);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (numSet.length === 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (numSet.length < 3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
numSet.sort((a, b) => {
|
||||||
|
return a - b;
|
||||||
|
});
|
||||||
|
let preNum;
|
||||||
|
let sequence = true; //是否是顺序
|
||||||
|
let same = true; //是否是相同
|
||||||
|
for (let num of numSet) {
|
||||||
|
if (preNum) {
|
||||||
|
if (num !== (preNum + 1) && sequence) {
|
||||||
|
sequence = false;
|
||||||
|
}
|
||||||
|
if (num !== preNum && same) {
|
||||||
|
same = false;
|
||||||
|
}
|
||||||
|
if (!sequence && !same) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preNum = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
export default gameUtil;
|
export default gameUtil;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user