将能否吃牌等配置项放入state, 同步至客户端
This commit is contained in:
parent
c745f9165b
commit
e75c040c6c
@ -1,5 +1,11 @@
|
|||||||
import { singleton } from '../decorators/singleton.decorator'
|
import { singleton } from '../decorators/singleton.decorator'
|
||||||
import { GameEnv } from './GameEnv'
|
import { GameEnv } from './GameEnv'
|
||||||
|
import { CardGameState } from '../rooms/schema/CardGameState'
|
||||||
|
import {
|
||||||
|
RULE_CANEAT,
|
||||||
|
RULE_DROPCARD,
|
||||||
|
RULE_MULTIPLEEAT, RULE_SINGLEEAT
|
||||||
|
} from '../constants/GameRuleConst'
|
||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
export class RoomOptions {
|
export class RoomOptions {
|
||||||
@ -9,11 +15,11 @@ export class RoomOptions {
|
|||||||
* @param opt
|
* @param opt
|
||||||
*/
|
*/
|
||||||
public canEat(opt: {advMode: boolean}) {
|
public canEat(opt: {advMode: boolean}) {
|
||||||
let result = false
|
let result = 0
|
||||||
if (opt.advMode && new GameEnv().canEatAdv) {
|
if (opt.advMode && new GameEnv().canEatAdv) {
|
||||||
result = true
|
result = 1
|
||||||
} else if (!opt.advMode && new GameEnv().canEatBase) {
|
} else if (!opt.advMode && new GameEnv().canEatBase) {
|
||||||
result = true
|
result = 1
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -24,7 +30,7 @@ export class RoomOptions {
|
|||||||
* @param opt
|
* @param opt
|
||||||
*/
|
*/
|
||||||
public multipleEat(opt?: any) {
|
public multipleEat(opt?: any) {
|
||||||
return false
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,7 +38,7 @@ export class RoomOptions {
|
|||||||
* @param opt
|
* @param opt
|
||||||
*/
|
*/
|
||||||
public autoDiscard(opt?: any) {
|
public autoDiscard(opt?: any) {
|
||||||
return true
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,7 +46,14 @@ export class RoomOptions {
|
|||||||
* @param opt
|
* @param opt
|
||||||
*/
|
*/
|
||||||
public singleEat(opt?: any) {
|
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())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
21
src/constants/GameRuleConst.ts
Normal file
21
src/constants/GameRuleConst.ts
Normal 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'
|
@ -5,6 +5,7 @@ import { Player } from '../rooms/schema/Player'
|
|||||||
import assistantUtil from '../utils/assistant.util'
|
import assistantUtil from '../utils/assistant.util'
|
||||||
import { delay, wait } from '../decorators/cfg'
|
import { delay, wait } from '../decorators/cfg'
|
||||||
import { RoomOptions } from '../cfg/RoomOptions'
|
import { RoomOptions } from '../cfg/RoomOptions'
|
||||||
|
import { RULE_CANEAT } from '../constants/GameRuleConst'
|
||||||
|
|
||||||
export class Robot {
|
export class Robot {
|
||||||
host: string
|
host: string
|
||||||
@ -164,7 +165,7 @@ export class Robot {
|
|||||||
@wait('maxDiscardTime')
|
@wait('maxDiscardTime')
|
||||||
private async discard() {
|
private async discard() {
|
||||||
let targetCard
|
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) {
|
if (this.room.state.cards.size == 1 && canEat) {
|
||||||
targetCard = [...this.room.state.cards.values()][0]
|
targetCard = [...this.room.state.cards.values()][0]
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import { Player } from '../rooms/schema/Player'
|
|||||||
import assistantUtil from '../utils/assistant.util'
|
import assistantUtil from '../utils/assistant.util'
|
||||||
import { wait } from '../decorators/cfg'
|
import { wait } from '../decorators/cfg'
|
||||||
import { RoomOptions } from '../cfg/RoomOptions'
|
import { RoomOptions } from '../cfg/RoomOptions'
|
||||||
|
import { RULE_CANEAT } from '../constants/GameRuleConst'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端辅助机器人
|
* 服务端辅助机器人
|
||||||
@ -141,7 +142,7 @@ export class RobotClient implements Client {
|
|||||||
@wait('maxDiscardTime')
|
@wait('maxDiscardTime')
|
||||||
private async discard() {
|
private async discard() {
|
||||||
let targetCard
|
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) {
|
if (this.svrstate.cards.size == 1 && canEat) {
|
||||||
targetCard = [...this.svrstate.cards.values()][0]
|
targetCard = [...this.svrstate.cards.values()][0]
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import { ChangePetCommand } from './commands/ChangePetCommand'
|
|||||||
import { createRobot } from '../common/WebApi'
|
import { createRobot } from '../common/WebApi'
|
||||||
import { Service } from '../service/Service'
|
import { Service } from '../service/Service'
|
||||||
import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand'
|
import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand'
|
||||||
|
import { RoomOptions } from '../cfg/RoomOptions'
|
||||||
|
|
||||||
export class GeneralRoom extends Room {
|
export class GeneralRoom extends Room {
|
||||||
dispatcher = new Dispatcher(this)
|
dispatcher = new Dispatcher(this)
|
||||||
@ -60,6 +61,7 @@ export class GeneralRoom extends Room {
|
|||||||
if (options.score) {
|
if (options.score) {
|
||||||
this.score = options.score
|
this.score = options.score
|
||||||
}
|
}
|
||||||
|
new RoomOptions().initGameRule(this.state)
|
||||||
this.battleMan.init(cs, this)
|
this.battleMan.init(cs, this)
|
||||||
this.clock.start()
|
this.clock.start()
|
||||||
this.state.gameState = GameStateConst.STATE_WAIT_JOIN
|
this.state.gameState = GameStateConst.STATE_WAIT_JOIN
|
||||||
|
@ -12,6 +12,7 @@ import { StateTypeEnum } from '../enums/StateTypeEnum'
|
|||||||
import { RoomOptions } from '../../cfg/RoomOptions'
|
import { RoomOptions } from '../../cfg/RoomOptions'
|
||||||
import { ClockNameConst } from '../../constants/ClockNameConst'
|
import { ClockNameConst } from '../../constants/ClockNameConst'
|
||||||
import { stopDrawCardClock } from '../../utils/clock.util'
|
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
|
let targetCard
|
||||||
if (target) {
|
if (target) {
|
||||||
if (!new RoomOptions().canEat({ advMode: this.state.advMode })) {
|
if (!this.state.rules.get(RULE_CANEAT)) {
|
||||||
this.room.send(client, 'discard_card_s2c', {
|
this.room.send(client, 'discard_card_s2c', {
|
||||||
errcode: 7,
|
errcode: 7,
|
||||||
errmsg: '当前游戏不允许吃牌'
|
errmsg: '当前游戏不允许吃牌'
|
||||||
@ -124,8 +125,9 @@ export class DiscardCommand extends Command<CardGameState, { client: Client, car
|
|||||||
cards: cards,
|
cards: cards,
|
||||||
type: dtype
|
type: dtype
|
||||||
})
|
})
|
||||||
|
const singleEat = !!this.state.rules.get(RULE_SINGLEEAT)
|
||||||
if (cards.length === 1
|
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 cardArr: Card[] = [...this.state.cards.values()]
|
||||||
let time = this.room.battleMan.onCardDiscarded(player, cardArr[0])
|
let time = this.room.battleMan.onCardDiscarded(player, cardArr[0])
|
||||||
await this.delay(time)
|
await this.delay(time)
|
||||||
|
@ -10,6 +10,7 @@ import { GameStateConst } from '../../constants/GameStateConst'
|
|||||||
import { debugRoom } from '../../common/Debug'
|
import { debugRoom } from '../../common/Debug'
|
||||||
import { ClockNameConst } from '../../constants/ClockNameConst'
|
import { ClockNameConst } from '../../constants/ClockNameConst'
|
||||||
import { addDrawCardClock } from '../../utils/clock.util'
|
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)
|
let time = this.room.battleMan.useCard(data)
|
||||||
await this.delay(time)
|
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.updateGameState(GameStateConst.STATE_BEGIN_DRAW)
|
||||||
this.state.updateGameTurn(player.id, this.state.eatCount + 1)
|
this.state.updateGameTurn(player.id, this.state.eatCount + 1)
|
||||||
debugRoom(`more eatcount for player ${player.id}, ${this.state.eatCount}`)
|
debugRoom(`more eatcount for player ${player.id}, ${this.state.eatCount}`)
|
||||||
|
@ -83,6 +83,9 @@ export class CardGameState extends Schema {
|
|||||||
*/
|
*/
|
||||||
maxCardId = 0;
|
maxCardId = 0;
|
||||||
|
|
||||||
|
@type({map: "number"})
|
||||||
|
rules = new MapSchema<number>();
|
||||||
|
|
||||||
updateGameState(val: number) {
|
updateGameState(val: number) {
|
||||||
let preVal = this.gameState;
|
let preVal = this.gameState;
|
||||||
this.gameState = val;
|
this.gameState = val;
|
||||||
|
@ -6,6 +6,7 @@ import { TurnEndCommand } from '../rooms/commands/TurnEndCommand'
|
|||||||
import { ClockNameConst } from '../constants/ClockNameConst'
|
import { ClockNameConst } from '../constants/ClockNameConst'
|
||||||
import { GameEnv } from '../cfg/GameEnv'
|
import { GameEnv } from '../cfg/GameEnv'
|
||||||
import { Player } from '../rooms/schema/Player'
|
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)
|
const player = room.state.players.get(sessionId)
|
||||||
let timeOverDraw = function () {
|
let timeOverDraw = function () {
|
||||||
if (sessionId == room.state.currentTurn) {
|
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 client = room.getClient(sessionId)
|
||||||
const card = player.cards.values().next().value
|
const card = player.cards.values().next().value
|
||||||
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
debugRoom('出牌时间到, 自动出牌: ', card.id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user