重构代码
This commit is contained in:
parent
b6e446c274
commit
fd6c7ea2b6
@ -32,7 +32,7 @@ export class RoomOptions {
|
|||||||
* @param opt
|
* @param opt
|
||||||
*/
|
*/
|
||||||
public autoDiscard(opt?: any) {
|
public autoDiscard(opt?: any) {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import {Command} from "@colyseus/command";
|
import { Command } from '@colyseus/command'
|
||||||
import {CardGameState} from "../schema/CardGameState";
|
import { CardGameState } from '../schema/CardGameState'
|
||||||
import {GameStateConst} from "../../constants/GameStateConst";
|
import { GameStateConst } from '../../constants/GameStateConst'
|
||||||
import gameUtil from "../../utils/game.util";
|
import gameUtil from '../../utils/game.util'
|
||||||
import {GameEnv} from "../../cfg/GameEnv";
|
import { GameEnv } from '../../cfg/GameEnv'
|
||||||
import {NextTurnCommand} from "./NextTurnCommand";
|
import { NextTurnCommand } from './NextTurnCommand'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始游戏
|
* 开始游戏
|
||||||
@ -12,28 +12,28 @@ import {NextTurnCommand} from "./NextTurnCommand";
|
|||||||
*/
|
*/
|
||||||
export class BeginGameCommand extends Command<CardGameState, {}> {
|
export class BeginGameCommand extends Command<CardGameState, {}> {
|
||||||
|
|
||||||
async execute() {
|
async execute() {
|
||||||
this.state.maxCardId = 0;
|
this.state.maxCardId = 0
|
||||||
let card0 = gameUtil.initCardQue(1, this.state.advMode);
|
let card0 = gameUtil.initCardQue(1, this.state.advMode)
|
||||||
let card1 = gameUtil.initCardQue(card0.length + 1, this.state.advMode);
|
let card1 = gameUtil.initCardQue(card0.length + 1, this.state.advMode)
|
||||||
let cardAll = card0.concat(card1);
|
let cardAll = card0.concat(card1)
|
||||||
cardAll.randomSort();
|
cardAll.randomSort()
|
||||||
//FixMe:: 移除
|
//FixMe:: 移除
|
||||||
// let card0 = gameUtil.initSampleCards(1)
|
// let card0 = gameUtil.initSampleCards(1)
|
||||||
// this.state.cardQueue = card0;
|
// this.state.cardQueue = card0;
|
||||||
this.state.cardQueue = cardAll;
|
this.state.cardQueue = cardAll
|
||||||
let i = 0;
|
let i = 0
|
||||||
for (let [id] of this.state.players) {
|
for (let [id] of this.state.players) {
|
||||||
this.room.addCard(id, new GameEnv().playerInitNums[i++], 0);
|
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()];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 { 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 gameUtil from '../../utils/game.util'
|
||||||
import {Card} from "../schema/Card";
|
import { Card } from '../schema/Card'
|
||||||
import {PlayerStateConst} from "../../constants/PlayerStateConst";
|
import { PlayerStateConst } from '../../constants/PlayerStateConst'
|
||||||
import {GameEnv} from "../../cfg/GameEnv";
|
import { GameEnv } from '../../cfg/GameEnv'
|
||||||
import {GameStateConst} from "../../constants/GameStateConst";
|
import { GameStateConst } from '../../constants/GameStateConst'
|
||||||
import {NextTurnCommand} from "./NextTurnCommand";
|
import { NextTurnCommand } from './NextTurnCommand'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开局换卡
|
* 开局换卡
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
// return player !== undefined && gameUtil.checkCardsExists(player.cards, cards);
|
// return player !== undefined && gameUtil.checkCardsExists(player.cards, cards);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
execute({ client, cards } = this.payload) {
|
execute({ client, cards } = this.payload) {
|
||||||
let sessionId = client.sessionId;
|
let sessionId = client.sessionId
|
||||||
let player = this.state.players.get(sessionId);
|
let player = this.state.players.get(sessionId)
|
||||||
if (this.state.gameState != GameStateConst.STATE_CHANGE_CARD) {
|
if (this.state.gameState != GameStateConst.STATE_CHANGE_CARD) {
|
||||||
this.room.sChangeCard(client,{errcode: 1, errmsg: '当前不在换卡阶段'});
|
this.room.sChangeCard(client, { errcode: 1, errmsg: '当前不在换卡阶段' })
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if (player.state != PlayerStateConst.PLAYER_SELECT_HERO) {
|
if (player.state != PlayerStateConst.PLAYER_SELECT_HERO) {
|
||||||
this.room.sChangeCard(client,{errcode: 2, errmsg: '当前玩家已换卡'});
|
this.room.sChangeCard(client, { errcode: 2, errmsg: '当前玩家已换卡' })
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
const maxCount = new GameEnv().cardChangeNum;
|
const maxCount = new GameEnv().cardChangeNum
|
||||||
if (cards.length > maxCount) {
|
if (cards.length > maxCount) {
|
||||||
this.room.sChangeCard(client,{errcode: 3, errmsg: '换卡数量超过限制'});
|
this.room.sChangeCard(client, { errcode: 3, errmsg: '换卡数量超过限制' })
|
||||||
return;
|
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()];
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 { debugRoom } from '../../common/Debug'
|
||||||
import { DiscardCommand } from './DiscardCommand'
|
import { DiscardCommand } from './DiscardCommand'
|
||||||
import { ClockNameConst } from '../../constants/ClockNameConst'
|
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 self = this
|
||||||
let timeOverDraw = function () {
|
let timeOverDraw = function () {
|
||||||
if (sessionId == self.state.currentTurn) {
|
if (sessionId == self.state.currentTurn) {
|
||||||
let client = self.room.getClient(sessionId)
|
if (new RoomOptions().autoDiscard()) {
|
||||||
let card = player.cards.values().next().value
|
let client = self.room.getClient(sessionId)
|
||||||
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
let card = player.cards.values().next().value
|
||||||
player.extraTime = 0
|
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
||||||
if (client) {
|
player.extraTime = 0
|
||||||
self.room.dispatcher.dispatch(new DiscardCommand(), {
|
if (client) {
|
||||||
client,
|
self.room.dispatcher.dispatch(new DiscardCommand(), {
|
||||||
cards: [card.id],
|
client,
|
||||||
dtype: 1
|
cards: [card.id],
|
||||||
})
|
dtype: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debugRoom('出牌时间到, 下个玩家开始')
|
||||||
|
self.room.dispatcher.dispatch(new TurnEndCommand())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD)
|
this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user