完善多次吃牌的逻辑
This commit is contained in:
parent
fb1923f421
commit
fcc0b9de53
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
33
src/rooms/commands/ManualTurnEndCommand.ts
Normal file
33
src/rooms/commands/ManualTurnEndCommand.ts
Normal 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()]
|
||||
}
|
||||
|
||||
}
|
@ -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()]
|
||||
}
|
||||
|
@ -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
57
src/utils/clock.util.ts
Normal 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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user