将能否吃牌等配置项放入state, 同步至客户端

This commit is contained in:
zhl 2021-03-04 12:49:58 +08:00
parent c745f9165b
commit e75c040c6c
9 changed files with 59 additions and 12 deletions

View File

@ -1,5 +1,11 @@
import { singleton } from '../decorators/singleton.decorator'
import { GameEnv } from './GameEnv'
import { CardGameState } from '../rooms/schema/CardGameState'
import {
RULE_CANEAT,
RULE_DROPCARD,
RULE_MULTIPLEEAT, RULE_SINGLEEAT
} from '../constants/GameRuleConst'
@singleton
export class RoomOptions {
@ -9,11 +15,11 @@ export class RoomOptions {
* @param opt
*/
public canEat(opt: {advMode: boolean}) {
let result = false
let result = 0
if (opt.advMode && new GameEnv().canEatAdv) {
result = true
result = 1
} else if (!opt.advMode && new GameEnv().canEatBase) {
result = true
result = 1
}
return result
}
@ -24,7 +30,7 @@ export class RoomOptions {
* @param opt
*/
public multipleEat(opt?: any) {
return false
return 0
}
/**
@ -32,7 +38,7 @@ export class RoomOptions {
* @param opt
*/
public autoDiscard(opt?: any) {
return true
return 1
}
/**
@ -40,7 +46,14 @@ export class RoomOptions {
* @param opt
*/
public singleEat(opt?: any) {
return true
return 0
}
public initGameRule(state: CardGameState) {
state.rules.set(RULE_CANEAT, this.canEat({advMode: state.advMode}))
state.rules.set(RULE_MULTIPLEEAT, this.multipleEat())
state.rules.set(RULE_DROPCARD, this.autoDiscard())
state.rules.set(RULE_SINGLEEAT, this.singleEat())
}
}

View File

@ -0,0 +1,21 @@
/**
*
* @type {string}
*/
export const RULE_MULTIPLEEAT = 'multipeat'
/**
*
* @type {string}
*/
export const RULE_DROPCARD = 'dropcard'
/**
*
* @type {string}
*/
export const RULE_CANEAT = 'caneat'
/**
*
* @type {string}
*/
export const RULE_SINGLEEAT = 'singleeat'

View File

@ -5,6 +5,7 @@ import { Player } from '../rooms/schema/Player'
import assistantUtil from '../utils/assistant.util'
import { delay, wait } from '../decorators/cfg'
import { RoomOptions } from '../cfg/RoomOptions'
import { RULE_CANEAT } from '../constants/GameRuleConst'
export class Robot {
host: string
@ -164,7 +165,7 @@ export class Robot {
@wait('maxDiscardTime')
private async discard() {
let targetCard
let canEat = new RoomOptions().canEat({advMode: this.room.state.advMode})
let canEat = !!this.room.state.rules.get(RULE_CANEAT)
if (this.room.state.cards.size == 1 && canEat) {
targetCard = [...this.room.state.cards.values()][0]
}

View File

@ -9,6 +9,7 @@ import { Player } from '../rooms/schema/Player'
import assistantUtil from '../utils/assistant.util'
import { wait } from '../decorators/cfg'
import { RoomOptions } from '../cfg/RoomOptions'
import { RULE_CANEAT } from '../constants/GameRuleConst'
/**
*
@ -141,7 +142,7 @@ export class RobotClient implements Client {
@wait('maxDiscardTime')
private async discard() {
let targetCard
let canEat = new RoomOptions().canEat({advMode: this.svrstate.advMode})
let canEat = !!this.svrstate.rules.get(RULE_CANEAT)
if (this.svrstate.cards.size == 1 && canEat) {
targetCard = [...this.svrstate.cards.values()][0]
}

View File

@ -22,6 +22,7 @@ import { ChangePetCommand } from './commands/ChangePetCommand'
import { createRobot } from '../common/WebApi'
import { Service } from '../service/Service'
import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand'
import { RoomOptions } from '../cfg/RoomOptions'
export class GeneralRoom extends Room {
dispatcher = new Dispatcher(this)
@ -60,6 +61,7 @@ export class GeneralRoom extends Room {
if (options.score) {
this.score = options.score
}
new RoomOptions().initGameRule(this.state)
this.battleMan.init(cs, this)
this.clock.start()
this.state.gameState = GameStateConst.STATE_WAIT_JOIN

View File

@ -12,6 +12,7 @@ import { StateTypeEnum } from '../enums/StateTypeEnum'
import { RoomOptions } from '../../cfg/RoomOptions'
import { ClockNameConst } from '../../constants/ClockNameConst'
import { stopDrawCardClock } from '../../utils/clock.util'
import { RULE_CANEAT, RULE_SINGLEEAT } from '../../constants/GameRuleConst'
/**
*
@ -60,7 +61,7 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
}
let targetCard
if (target) {
if (!new RoomOptions().canEat({ advMode: this.state.advMode })) {
if (!this.state.rules.get(RULE_CANEAT)) {
this.room.send(client, 'discard_card_s2c', {
errcode: 7,
errmsg: '当前游戏不允许吃牌'
@ -124,8 +125,9 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
cards: cards,
type: dtype
})
const singleEat = !!this.state.rules.get(RULE_SINGLEEAT)
if (cards.length === 1
&& !(new RoomOptions().singleEat() && tmpCards.length == 1 && (tmpCards[0].type == 1 || tmpCards[0].type == 11))) {
&& !(singleEat && tmpCards.length == 1 && (tmpCards[0].type == 1 || tmpCards[0].type == 11))) {
let cardArr: Card[] = [...this.state.cards.values()]
let time = this.room.battleMan.onCardDiscarded(player, cardArr[0])
await this.delay(time)

View File

@ -10,6 +10,7 @@ import { GameStateConst } from '../../constants/GameStateConst'
import { debugRoom } from '../../common/Debug'
import { ClockNameConst } from '../../constants/ClockNameConst'
import { addDrawCardClock } from '../../utils/clock.util'
import { RULE_MULTIPLEEAT } from '../../constants/GameRuleConst'
/**
*
@ -100,7 +101,8 @@ export class SelectPetCommand extends Command<CardGameState, {
}
let time = this.room.battleMan.useCard(data)
await this.delay(time)
if (new RoomOptions().multipleEat() && assistantUtil.checkDiscard([...player.cards.values()]).length > 1) {
const multipEat = !!this.state.rules.get(RULE_MULTIPLEEAT)
if (multipEat && assistantUtil.checkDiscard([...player.cards.values()]).length > 1) {
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}`)

View File

@ -83,6 +83,9 @@ export class CardGameState extends Schema {
*/
maxCardId = 0;
@type({map: "number"})
rules = new MapSchema<number>();
updateGameState(val: number) {
let preVal = this.gameState;
this.gameState = val;

View File

@ -6,6 +6,7 @@ import { TurnEndCommand } from '../rooms/commands/TurnEndCommand'
import { ClockNameConst } from '../constants/ClockNameConst'
import { GameEnv } from '../cfg/GameEnv'
import { Player } from '../rooms/schema/Player'
import { RULE_DROPCARD } from '../constants/GameRuleConst'
/**
*
@ -17,7 +18,8 @@ export function addDrawCardClock(room: Room) {
const player = room.state.players.get(sessionId)
let timeOverDraw = function () {
if (sessionId == room.state.currentTurn) {
if (new RoomOptions().autoDiscard()) {
const autoDiscard = !!room.state.rules.get(RULE_DROPCARD)
if (autoDiscard) {
const client = room.getClient(sessionId)
const card = player.cards.values().next().value
debugRoom('出牌时间到, 自动出牌: ', card.id)