重构代码
This commit is contained in:
parent
b6e446c274
commit
fd6c7ea2b6
@ -32,7 +32,7 @@ export class RoomOptions {
|
||||
* @param opt
|
||||
*/
|
||||
public autoDiscard(opt?: any) {
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import {Command} from "@colyseus/command";
|
||||
import {CardGameState} from "../schema/CardGameState";
|
||||
import {GameStateConst} from "../../constants/GameStateConst";
|
||||
import gameUtil from "../../utils/game.util";
|
||||
import {GameEnv} from "../../cfg/GameEnv";
|
||||
import {NextTurnCommand} from "./NextTurnCommand";
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { GameStateConst } from '../../constants/GameStateConst'
|
||||
import gameUtil from '../../utils/game.util'
|
||||
import { GameEnv } from '../../cfg/GameEnv'
|
||||
import { NextTurnCommand } from './NextTurnCommand'
|
||||
|
||||
/**
|
||||
* 开始游戏
|
||||
@ -12,28 +12,28 @@ import {NextTurnCommand} from "./NextTurnCommand";
|
||||
*/
|
||||
export class BeginGameCommand extends Command<CardGameState, {}> {
|
||||
|
||||
async execute() {
|
||||
this.state.maxCardId = 0;
|
||||
let card0 = gameUtil.initCardQue(1, this.state.advMode);
|
||||
let card1 = gameUtil.initCardQue(card0.length + 1, this.state.advMode);
|
||||
let cardAll = card0.concat(card1);
|
||||
cardAll.randomSort();
|
||||
//FixMe:: 移除
|
||||
// let card0 = gameUtil.initSampleCards(1)
|
||||
// this.state.cardQueue = card0;
|
||||
this.state.cardQueue = cardAll;
|
||||
let i = 0;
|
||||
for (let [id] of this.state.players) {
|
||||
this.room.addCard(id, new GameEnv().playerInitNums[i++], 0);
|
||||
}
|
||||
this.room.battleMan.onGameStart();
|
||||
this.state.updateGameState(GameStateConst.STATE_CHANGE_CARD);
|
||||
// 超时后结束换卡, 进入下一轮
|
||||
const cardChangeTime = new GameEnv().cardChangeTime;
|
||||
await this.delay(cardChangeTime*1000);
|
||||
if (this.state.gameState == GameStateConst.STATE_CHANGE_CARD) {
|
||||
return [new NextTurnCommand()];
|
||||
}
|
||||
async execute() {
|
||||
this.state.maxCardId = 0
|
||||
let card0 = gameUtil.initCardQue(1, this.state.advMode)
|
||||
let card1 = gameUtil.initCardQue(card0.length + 1, this.state.advMode)
|
||||
let cardAll = card0.concat(card1)
|
||||
cardAll.randomSort()
|
||||
//FixMe:: 移除
|
||||
// let card0 = gameUtil.initSampleCards(1)
|
||||
// this.state.cardQueue = card0;
|
||||
this.state.cardQueue = cardAll
|
||||
let i = 0
|
||||
for (let [id] of this.state.players) {
|
||||
this.room.addCard(id, new GameEnv().playerInitNums[i++], 0)
|
||||
}
|
||||
this.room.battleMan.onGameStart()
|
||||
this.state.updateGameState(GameStateConst.STATE_CHANGE_CARD)
|
||||
// 超时后结束换卡, 进入下一轮
|
||||
const cardChangeTime = new GameEnv().cardChangeTime
|
||||
await this.delay(cardChangeTime * 1000)
|
||||
if (this.state.gameState == GameStateConst.STATE_CHANGE_CARD) {
|
||||
return [new NextTurnCommand()]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,62 +1,62 @@
|
||||
import {Command} from "@colyseus/command";
|
||||
import {CardGameState} from "../schema/CardGameState";
|
||||
import {Client} from "colyseus";
|
||||
import gameUtil from "../../utils/game.util";
|
||||
import {Card} from "../schema/Card";
|
||||
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
||||
import {GameEnv} from "../../cfg/GameEnv";
|
||||
import {GameStateConst} from "../../constants/GameStateConst";
|
||||
import {NextTurnCommand} from "./NextTurnCommand";
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { Client } from 'colyseus'
|
||||
import gameUtil from '../../utils/game.util'
|
||||
import { Card } from '../schema/Card'
|
||||
import { PlayerStateConst } from '../../constants/PlayerStateConst'
|
||||
import { GameEnv } from '../../cfg/GameEnv'
|
||||
import { GameStateConst } from '../../constants/GameStateConst'
|
||||
import { NextTurnCommand } from './NextTurnCommand'
|
||||
|
||||
/**
|
||||
* 开局换卡
|
||||
*/
|
||||
export class ChangeCardCommand 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) {
|
||||
let sessionId = client.sessionId;
|
||||
let player = this.state.players.get(sessionId);
|
||||
if (this.state.gameState != GameStateConst.STATE_CHANGE_CARD) {
|
||||
this.room.sChangeCard(client,{errcode: 1, errmsg: '当前不在换卡阶段'});
|
||||
return;
|
||||
}
|
||||
if (player.state != PlayerStateConst.PLAYER_SELECT_HERO) {
|
||||
this.room.sChangeCard(client,{errcode: 2, errmsg: '当前玩家已换卡'});
|
||||
return;
|
||||
}
|
||||
const maxCount = new GameEnv().cardChangeNum;
|
||||
if (cards.length > maxCount) {
|
||||
this.room.sChangeCard(client,{errcode: 3, errmsg: '换卡数量超过限制'});
|
||||
return;
|
||||
}
|
||||
|
||||
let cardToRemove:Card[] = [];
|
||||
for (let id of cards) {
|
||||
let card = player.cards.get(id + '');
|
||||
if (cardToRemove.length < maxCount) {
|
||||
cardToRemove.push(card);
|
||||
}
|
||||
}
|
||||
let newCards = gameUtil.changeCard(this.state.cardQueue, player, cardToRemove);
|
||||
this.room.sChangeCard(client,{errcode:0, errmsg: '', cards: newCards});
|
||||
player.state = PlayerStateConst.PLAYER_CHANGE_CARD;
|
||||
let finishCount = 0;
|
||||
for (let [, player] of this.state.players) {
|
||||
if (player.state === PlayerStateConst.PLAYER_CHANGE_CARD) {
|
||||
finishCount++;
|
||||
}
|
||||
}
|
||||
if (finishCount >= this.room.maxClients) {
|
||||
// TODO:: 根据 this.state.firstPlayer确定先手
|
||||
// 正式开始游戏, 第一个玩家出牌
|
||||
this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW);
|
||||
return [new NextTurnCommand()];
|
||||
|
||||
}
|
||||
execute({ client, cards } = this.payload) {
|
||||
let sessionId = client.sessionId
|
||||
let player = this.state.players.get(sessionId)
|
||||
if (this.state.gameState != GameStateConst.STATE_CHANGE_CARD) {
|
||||
this.room.sChangeCard(client, { errcode: 1, errmsg: '当前不在换卡阶段' })
|
||||
return
|
||||
}
|
||||
if (player.state != PlayerStateConst.PLAYER_SELECT_HERO) {
|
||||
this.room.sChangeCard(client, { errcode: 2, errmsg: '当前玩家已换卡' })
|
||||
return
|
||||
}
|
||||
const maxCount = new GameEnv().cardChangeNum
|
||||
if (cards.length > maxCount) {
|
||||
this.room.sChangeCard(client, { errcode: 3, errmsg: '换卡数量超过限制' })
|
||||
return
|
||||
}
|
||||
|
||||
let cardToRemove: Card[] = []
|
||||
for (let id of cards) {
|
||||
let card = player.cards.get(id + '')
|
||||
if (cardToRemove.length < maxCount) {
|
||||
cardToRemove.push(card)
|
||||
}
|
||||
}
|
||||
let newCards = gameUtil.changeCard(this.state.cardQueue, player, cardToRemove)
|
||||
this.room.sChangeCard(client, { errcode: 0, errmsg: '', cards: newCards })
|
||||
player.state = PlayerStateConst.PLAYER_CHANGE_CARD
|
||||
let finishCount = 0
|
||||
for (let [, player] of this.state.players) {
|
||||
if (player.state === PlayerStateConst.PLAYER_CHANGE_CARD) {
|
||||
finishCount++
|
||||
}
|
||||
}
|
||||
if (finishCount >= this.room.maxClients) {
|
||||
// TODO:: 根据 this.state.firstPlayer确定先手
|
||||
// 正式开始游戏, 第一个玩家出牌
|
||||
this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW)
|
||||
return [new NextTurnCommand()]
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import { GameEnv } from '../../cfg/GameEnv'
|
||||
import { debugRoom } from '../../common/Debug'
|
||||
import { DiscardCommand } from './DiscardCommand'
|
||||
import { ClockNameConst } from '../../constants/ClockNameConst'
|
||||
import { RoomOptions } from '../../cfg/RoomOptions'
|
||||
import { TurnEndCommand } from './TurnEndCommand'
|
||||
|
||||
/**
|
||||
* 正常的抽卡
|
||||
@ -17,17 +19,23 @@ export class DrawCommand extends Command<CardGameState, {}> {
|
||||
let self = this
|
||||
let timeOverDraw = function () {
|
||||
if (sessionId == self.state.currentTurn) {
|
||||
let client = self.room.getClient(sessionId)
|
||||
let card = player.cards.values().next().value
|
||||
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
||||
player.extraTime = 0
|
||||
if (client) {
|
||||
self.room.dispatcher.dispatch(new DiscardCommand(), {
|
||||
client,
|
||||
cards: [card.id],
|
||||
dtype: 1
|
||||
})
|
||||
if (new RoomOptions().autoDiscard()) {
|
||||
let client = self.room.getClient(sessionId)
|
||||
let card = player.cards.values().next().value
|
||||
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
||||
player.extraTime = 0
|
||||
if (client) {
|
||||
self.room.dispatcher.dispatch(new DiscardCommand(), {
|
||||
client,
|
||||
cards: [card.id],
|
||||
dtype: 1
|
||||
})
|
||||
}
|
||||
} else {
|
||||
debugRoom('出牌时间到, 下个玩家开始')
|
||||
self.room.dispatcher.dispatch(new TurnEndCommand())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD)
|
||||
|
Loading…
x
Reference in New Issue
Block a user