完善多次吃牌的逻辑

This commit is contained in:
zhl 2021-03-02 17:12:40 +08:00
parent fb1923f421
commit fcc0b9de53
8 changed files with 107 additions and 41 deletions

View File

@ -24,7 +24,7 @@ export class RoomOptions {
* @param opt
*/
public multipleEat(opt?: any) {
return true
return false
}
/**
@ -32,7 +32,7 @@ export class RoomOptions {
* @param opt
*/
public autoDiscard(opt?: any) {
return false
return true
}
}

View File

@ -21,6 +21,7 @@ import { RobotClient } from '../robot/RobotClient'
import { ChangePetCommand } from './commands/ChangePetCommand'
import { createRobot } from '../common/WebApi'
import { Service } from '../service/Service'
import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand'
export class GeneralRoom extends Room {
dispatcher = new Dispatcher(this)
@ -143,6 +144,11 @@ export class GeneralRoom extends Room {
this.broadcast('broadcast_s2c', message, { except: client })
})
this.onMessage('turn_end_c2s', (client) => {
msgLog('turn_end_c2s from ', client.sessionId)
this.dispatcher.dispatch(new ManualTurnEndCommand(), { client })
})
this.onMessage('*', (client, type, message) => {
//
// Triggers when any other type of message is sent,
@ -285,7 +291,7 @@ export class GeneralRoom extends Room {
return -1
}
clock.pause()
return clock.time - clock.elapsedTime
return clock.elapsedTime
}
/**

View File

@ -11,6 +11,7 @@ import { Wait } from './Wait'
import { StateTypeEnum } from '../enums/StateTypeEnum'
import { RoomOptions } from '../../cfg/RoomOptions'
import { ClockNameConst } from '../../constants/ClockNameConst'
import { stopDrawCardClock } from '../../utils/clock.util'
/**
*
@ -106,13 +107,8 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
//停止出牌计时, 并更新player.extraTime;
let elapsedTime = this.room.stopSchedule(ClockNameConst.DRAW_CARD)
if (elapsedTime >= 0) {
let maxTime = new GameEnv().maxDiscardTime * 1000
let count = elapsedTime - maxTime
let newCount = player.extraTime - Math.max(count, 0)
player.extraTime = Math.max(newCount, 0)
}
stopDrawCardClock(this.room, player)
for (let card of tmpCards) {
this.state.cards.set(card.id + '', card)
player.cardQueue.push(card)

View File

@ -1,11 +1,7 @@
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
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'
import { addDrawCardClock } from '../../utils/clock.util'
/**
*
@ -14,30 +10,6 @@ export class DrawCommand extends Command<CardGameState, {}> {
async execute() {
let sessionId = this.state.currentTurn
this.room.addCard(sessionId, new GameEnv().roundDrawNum, 0)
let maxTime = new GameEnv().maxDiscardTime * 1000
let player = this.state.players.get(sessionId)
let self = this
let timeOverDraw = function () {
if (sessionId == self.state.currentTurn) {
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)
addDrawCardClock(this.room)
}
}

View File

@ -0,0 +1,33 @@
import { Command } from '@colyseus/command'
import { CardGameState } from '../schema/CardGameState'
import { TurnEndCommand } from './TurnEndCommand'
import { Client } from 'colyseus'
import { stopDrawCardClock } from '../../utils/clock.util'
import { GameStateConst } from '../../constants/GameStateConst'
/**
*
*/
export class ManualTurnEndCommand extends Command<CardGameState, { client: Client }> {
execute({ client } = this.payload) {
if (this.state.currentTurn !== client.sessionId) {
this.room.send(client, 'turn_end_s2c', {
errcode: 2,
errmsg: `发送消息的玩家不是当前玩家`
})
return
}
if (this.state.gameState !== GameStateConst.STATE_BEGIN_DRAW) {
this.room.send(client, 'turn_end_s2c', {
errcode: 3,
errmsg: `当前状态不适合停止`
})
return
}
const player = this.state.players.get(client.sessionId)
stopDrawCardClock(this.room, player)
return [new TurnEndCommand()]
}
}

View File

@ -9,6 +9,7 @@ import assistantUtil from '../../utils/assistant.util'
import { GameStateConst } from '../../constants/GameStateConst'
import { debugRoom } from '../../common/Debug'
import { ClockNameConst } from '../../constants/ClockNameConst'
import { addDrawCardClock } from '../../utils/clock.util'
/**
*
@ -103,6 +104,7 @@ export class SelectPetCommand extends Command<CardGameState, {
this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW)
this.state.updateGameTurn(player.id, this.state.eatCount + 1)
debugRoom(`more eatcount for player ${player.id}, ${this.state.eatCount}`)
addDrawCardClock(this.room)
} else {
return [new TurnEndCommand()]
}

View File

@ -75,7 +75,7 @@ let assistantUtil = {
let cur = cardIds[i]
i == 0 && tmp.push(cur)
if (i > 0) {
if (cur != tmp[i - 1] + 1) {
if (cur != tmp[tmp.length - 1] + 1) {
tmp.length = 0
}
tmp.push(cur)

57
src/utils/clock.util.ts Normal file
View File

@ -0,0 +1,57 @@
import { Room } from 'colyseus'
import { RoomOptions } from '../cfg/RoomOptions'
import { debugRoom } from '../common/Debug'
import { DiscardCommand } from '../rooms/commands/DiscardCommand'
import { TurnEndCommand } from '../rooms/commands/TurnEndCommand'
import { ClockNameConst } from '../constants/ClockNameConst'
import { GameEnv } from '../cfg/GameEnv'
import { Player } from '../rooms/schema/Player'
/**
*
* @param {Room} room
*/
export function addDrawCardClock(room: Room) {
const sessionId = room.state.currentTurn
const maxTime = new GameEnv().maxDiscardTime * 1000
const player = room.state.players.get(sessionId)
let timeOverDraw = function () {
if (sessionId == room.state.currentTurn) {
if (new RoomOptions().autoDiscard()) {
const client = room.getClient(sessionId)
const card = player.cards.values().next().value
debugRoom('出牌时间到, 自动出牌: ', card.id)
player.extraTime = 0
if (client && card) {
room.dispatcher.dispatch(new DiscardCommand(), {
client,
cards: [card.id],
dtype: 1
})
} else {
debugRoom('出牌时间到, 玩家没有手牌')
room.dispatcher.dispatch(new TurnEndCommand())
}
} else {
debugRoom('出牌时间到, 下个玩家开始')
room.dispatcher.dispatch(new TurnEndCommand())
}
}
}
room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD)
}
/**
* ,
* @param {Room} room
* @param {Player} player
*/
export function stopDrawCardClock(room: Room, player: Player) {
let elapsedTime = room.stopSchedule(ClockNameConst.DRAW_CARD)
if (elapsedTime >= 0) {
let maxTime = new GameEnv().maxDiscardTime * 1000
let count = elapsedTime - maxTime
let newCount = player.extraTime - Math.max(count, 0)
player.extraTime = Math.max(newCount, 0)
}
}