增加比大小的游戏模式
This commit is contained in:
parent
9fb61f7545
commit
43ccef0d88
@ -20,5 +20,9 @@ export class GameStateConst {
|
||||
public static readonly STATE_ROUND_RESULT = 8;
|
||||
// 游戏结束
|
||||
public static readonly STATE_GAME_OVER = 9;
|
||||
// 比大小游戏
|
||||
public static readonly STATE_DICE_TURN = 10;
|
||||
// 比大小结果
|
||||
public static readonly STATE_DICE_RESULT = 11;
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import gameUtil from '../../utils/game.util'
|
||||
import { GameEnv } from '../../cfg/GameEnv'
|
||||
import { NextTurnCommand } from './NextTurnCommand'
|
||||
import { debugRoom } from '../../common/Debug'
|
||||
import { DiceNextTurnCommand } from './DiceNextTurnCommand'
|
||||
|
||||
/**
|
||||
* 开始游戏
|
||||
@ -62,7 +63,11 @@ export class BeginGameCommand extends Command<CardGameState, {}> {
|
||||
const cardChangeTime = new GameEnv().cardChangeTime
|
||||
await this.delay(cardChangeTime * 1000)
|
||||
if (this.state.gameState == GameStateConst.STATE_CHANGE_CARD) {
|
||||
return [new NextTurnCommand()]
|
||||
if (this.state.mode === 4) {
|
||||
return [new NextTurnCommand()]
|
||||
} else {
|
||||
return [new DiceNextTurnCommand()]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
55
src/rooms/commands/DiceCommand.ts
Normal file
55
src/rooms/commands/DiceCommand.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { Client } from 'colyseus'
|
||||
import { error } from '../../common/Debug'
|
||||
import { DiceResultCommand } from './DiceResultCommand'
|
||||
import { GameStateConst } from '../../constants/GameStateConst'
|
||||
|
||||
/**
|
||||
* 出一张牌比大小
|
||||
*/
|
||||
export class DiceCommand extends Command<CardGameState, { client: Client, cards: [string] }> {
|
||||
async execute({ client, cards } = this.payload) {
|
||||
let tmpActionMap = this.state.tmpActionMap
|
||||
const player = this.state.players.get(client.sessionId)
|
||||
if (this.state.gameState !== GameStateConst.STATE_DICE_TURN) {
|
||||
this.room.send(client, 'dice_card_s2c', {
|
||||
errcode: 11,
|
||||
errmsg: '不是出牌轮'
|
||||
})
|
||||
return
|
||||
}
|
||||
if (tmpActionMap.has(player.id)) {
|
||||
this.room.send(client, 'dice_card_s2c', {
|
||||
errcode: 12,
|
||||
errmsg: '本轮已经出牌'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let cardIds = []
|
||||
for (let id of cards) {
|
||||
if (player.cards.has(id + '')) {
|
||||
if (!player.cards.get(id + '').number) {
|
||||
error(`${ player.id } 的手牌 ${ id } 数据有问题`)
|
||||
continue
|
||||
}
|
||||
const card = player.cards.get(id + '').clone()
|
||||
player.cards.delete(id + '')
|
||||
cardIds.push(card)
|
||||
} else {
|
||||
error(`${ player.id } 出的牌 ${ id } 在手牌中不存在`)
|
||||
this.room.send(client, 'dice_card_s2c', {
|
||||
errcode: 12,
|
||||
errmsg: `要出的牌 ${ id } 在手牌中不存在`
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
this.state.tmpActionMap.set(client.sessionId, cardIds)
|
||||
return [new DiceResultCommand().setPayload({ timeUp: false })]
|
||||
|
||||
}
|
||||
|
||||
}
|
10
src/rooms/commands/DiceDrawCommand.ts
Normal file
10
src/rooms/commands/DiceDrawCommand.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
|
||||
export class DiceDrawCommand extends Command<CardGameState, {}> {
|
||||
async execute() {
|
||||
for (let [id] of this.state.players) {
|
||||
this.room.addCard(id, 2, 0)
|
||||
}
|
||||
}
|
||||
}
|
12
src/rooms/commands/DiceNextTurnCommand.ts
Normal file
12
src/rooms/commands/DiceNextTurnCommand.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { GameStateConst } from '../../constants/GameStateConst'
|
||||
import { DiceDrawCommand } from './DiceDrawCommand'
|
||||
|
||||
export class DiceNextTurnCommand extends Command<CardGameState, {}> {
|
||||
async execute() {
|
||||
this.state.round += 1
|
||||
this.state.updateGameState(GameStateConst.STATE_DICE_TURN)
|
||||
return [new DiceDrawCommand()]
|
||||
}
|
||||
}
|
43
src/rooms/commands/DiceResultCommand.ts
Normal file
43
src/rooms/commands/DiceResultCommand.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { GameStateConst } from '../../constants/GameStateConst'
|
||||
import { DiceTurnEndCommand } from './DiceTurnEndCommand'
|
||||
import { Card } from '../schema/Card'
|
||||
import { CardType } from '../../cfg/enums/CardType'
|
||||
|
||||
|
||||
export class DiceResultCommand extends Command<CardGameState, { timeUp: boolean }> {
|
||||
async execute({timeUp} = this.payload) {
|
||||
const tmpActionMap = this.state.tmpActionMap
|
||||
if (tmpActionMap.size >= this.room.maxClients) {
|
||||
// 开始比较
|
||||
this.state.updateGameState(GameStateConst.STATE_DICE_RESULT)
|
||||
let max = 0
|
||||
let winner
|
||||
let resultMap: any = {}
|
||||
let cardIds = []
|
||||
for (let [pid, cardArr] of tmpActionMap) {
|
||||
const cards = cardArr as Card[]
|
||||
let ap = 0
|
||||
let doubleCount = 1
|
||||
for (const card of cards) {
|
||||
if (card.type == CardType.general || card.type == CardType.variable_unit) {
|
||||
ap += card.number
|
||||
} else {
|
||||
doubleCount += 1
|
||||
}
|
||||
cardIds.push(card.id)
|
||||
}
|
||||
ap = ap * doubleCount
|
||||
resultMap.push([pid, cardIds])
|
||||
if (ap > max) {
|
||||
max = ap
|
||||
winner = pid
|
||||
}
|
||||
}
|
||||
this.room.broadcast('dice_result_s2c', {winner, tatal: max, results: resultMap})
|
||||
await this.delay(2 * 1000)
|
||||
return [new DiceTurnEndCommand()];
|
||||
}
|
||||
}
|
||||
}
|
9
src/rooms/commands/DiceTurnEndCommand.ts
Normal file
9
src/rooms/commands/DiceTurnEndCommand.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Command } from '@colyseus/command'
|
||||
import { CardGameState } from '../schema/CardGameState'
|
||||
import { DiceNextTurnCommand } from './DiceNextTurnCommand'
|
||||
|
||||
export class DiceTurnEndCommand extends Command<CardGameState, {}> {
|
||||
async execute() {
|
||||
return [new DiceNextTurnCommand()]
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ export class EatCardCommand extends Command<CardGameState, { client: Client, car
|
||||
error(`${player.id} 的手牌 ${id} 数据有问题`);
|
||||
continue;
|
||||
}
|
||||
cardIds.push(id);
|
||||
cardIds.push(player.cards.get(id + ''));
|
||||
tmpCards.push(player.cards.get(id + ''));
|
||||
} else {
|
||||
error(`${player.id} 出的牌 ${id} 在手牌中不存在`)
|
||||
|
@ -42,7 +42,7 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
||||
}
|
||||
|
||||
let player:Player;
|
||||
let cards: string[] = [];
|
||||
let cards: Card[] = [];
|
||||
let isFirst = true;
|
||||
// 所有死亡玩家已在吃牌开始的时候至为放弃, 所以这里可以不用考虑死亡玩家
|
||||
for (let i = 1; i < pids.length; i ++) {
|
||||
@ -51,7 +51,7 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
||||
if (tmpActionMap.has(pid)) {
|
||||
if (typeof tmpActionMap.get(pid) != 'number') {
|
||||
player = this.state.players.get(pid);
|
||||
cards = tmpActionMap.get(pid) as string[];
|
||||
cards = tmpActionMap.get(pid) as Card[];
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@ -79,14 +79,14 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
||||
player.cardQueue.clear();
|
||||
player.statData.zinc(StateTypeEnum.EATCOUNT, 1);
|
||||
player.cardQueue.push(this.state.cards.values().next().value.clone());
|
||||
for (let id of cards) {
|
||||
if (!player.cards.has(id + '')) {
|
||||
for (let _card of cards) {
|
||||
if (!player.cards.has(_card.id + '')) {
|
||||
continue;
|
||||
}
|
||||
let card = player.cards.get(id + '').clone();
|
||||
let card = _card.clone();
|
||||
// this.state.cards.set(id + '', card);
|
||||
player.cardQueue.push(card);
|
||||
gameUtil.deleteCardFromPlayer(player, id);
|
||||
gameUtil.deleteCardFromPlayer(player, _card.id);
|
||||
}
|
||||
let cardArr: Card[] = [...this.state.cards.values()];
|
||||
let time = new GameEnv().playerActTime * 1000 + player.extraTime;
|
||||
|
@ -73,7 +73,7 @@ export class CardGameState extends Schema {
|
||||
/**
|
||||
* 用于临时存放吃牌的数据
|
||||
*/
|
||||
tmpActionMap: Map<string, string[] | number> = new Map()
|
||||
tmpActionMap: Map<string, Card[] | number> = new Map()
|
||||
/**
|
||||
* 点击重开的玩家数量
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user