From 4e5ae6d46474d7cdfb6a6aaba79dcb276e36ec73 Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 16 Dec 2020 18:12:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E4=BB=8E=E9=85=8D=E7=BD=AE=E4=B8=AD=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cfg/GameEnv.ts | 6 ++++ src/constants/BaseConst.ts | 4 +++ src/decorators/cfg.ts | 52 ++++++++++++++++++++++++++++ src/robot/Robot.ts | 20 +++++------ src/robot/RobotClient.ts | 10 +++--- src/rooms/GeneralRoom.ts | 2 +- src/rooms/commands/DiscardCommand.ts | 3 +- src/utils/assistant.util.ts | 1 - 8 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 src/decorators/cfg.ts diff --git a/src/cfg/GameEnv.ts b/src/cfg/GameEnv.ts index 9fab6f4..ce74450 100644 --- a/src/cfg/GameEnv.ts +++ b/src/cfg/GameEnv.ts @@ -42,6 +42,10 @@ export class GameEnv { public waitingPlayerOnePlus: number; // 英雄选择时间 public pickHeroTime: number; + // 机器人操作最小时间 + public robotActTimeMin: number; + // 机器人操作最大时间 + public robotActTimeMax: number; public init(data: Map) { this.initCardNum = data.get(BaseConst.INIT_CARD_NUM).value; @@ -64,5 +68,7 @@ export class GameEnv { this.waitingPlayerTime = data.get(BaseConst.WAITING_PLAYER_TIME).value; this.waitingPlayerOnePlus = data.get(BaseConst.WAITING_PLAYER_ONEPLUS).value; this.pickHeroTime = data.get(BaseConst.PICK_HERO_TIME).value; + this.robotActTimeMin = data.get(BaseConst.ROBOT_ACTTIME_MIN).value; + this.robotActTimeMax = data.get(BaseConst.ROBOT_ACTTIME_MAX).value; } } diff --git a/src/constants/BaseConst.ts b/src/constants/BaseConst.ts index 5784ede..78dbb69 100644 --- a/src/constants/BaseConst.ts +++ b/src/constants/BaseConst.ts @@ -39,6 +39,10 @@ export class BaseConst { public static readonly WAITING_PLAYER_ONEPLUS = 99019; // 英雄选择时间 public static readonly PICK_HERO_TIME = 99020; + // 机器人操作最小时间 + public static readonly ROBOT_ACTTIME_MIN = 99021; + // 机器人操作最大时间 + public static readonly ROBOT_ACTTIME_MAX = 99022; diff --git a/src/decorators/cfg.ts b/src/decorators/cfg.ts new file mode 100644 index 0000000..e10772e --- /dev/null +++ b/src/decorators/cfg.ts @@ -0,0 +1,52 @@ +import {singleton} from "../common/Singleton"; +import {GameEnv} from "../cfg/GameEnv"; + +let delayRun = function (max: number, min?: number) { + min = min || 0; + let milliseconds = (Math.random() * (max - min) + min) * 1000 | 0; + console.log(`delay time: ${milliseconds}`) + return new Promise(resolve => setTimeout(resolve, milliseconds)); +} +const baseCfg = singleton(GameEnv); + + +export function wait(type: string) { + return (target: any, + propertyKey: string, + descriptor: PropertyDescriptor) => { + const method = descriptor.value; + descriptor.value = function(...args: any[]) { + // @ts-ignore + let time = baseCfg[type] as number; + const minDelay = baseCfg.robotActTimeMin; + const maxDelay = baseCfg.robotActTimeMax; + let maxTime = maxDelay / 100 * time; + let minTime = minDelay / 100 * time; + delayRun(maxTime, minTime) + .then(() => { + }) + .finally(() => { + return method!.apply(this, args); + }) + + }; + } +} + +export function delay(num: number) { + return (target: any, + propertyKey: string, + descriptor: PropertyDescriptor) => { + const method = descriptor.value; + descriptor.value = function(...args: any[]) { + delayRun(num, 0) + .then(() => { + return method!.apply(this, args); + }) + .catch(err => { + return method!.apply(this, args); + }) + + }; + } +} diff --git a/src/robot/Robot.ts b/src/robot/Robot.ts index 4b480b6..ca98af5 100644 --- a/src/robot/Robot.ts +++ b/src/robot/Robot.ts @@ -11,6 +11,7 @@ import {SkillTargetType} from "../rooms/logic/skill/SkillConst"; import CfgMan from "../rooms/logic/CfgMan"; import gameUtil from "../utils/game.util"; import assistantUtil from "../utils/assistant.util"; +import {wait,delay} from "../decorators/cfg"; export class Robot { host: string; @@ -26,7 +27,6 @@ export class Robot { this.roomId = roomId; this.client = new Client(host); } - async connect() { try { this.room = await this.client.joinById(this.roomId); @@ -121,11 +121,7 @@ export class Robot { } }); } - private delay(max: number, min?: number) { - min = min || 0; - let milliseconds = (Math.random()*(max-min)+min) * 1000 | 0; - return new Promise(resolve => setTimeout(resolve, milliseconds)); - } + private reply(messageType: string, message: any) { this.room.send(messageType, message); } @@ -136,16 +132,17 @@ export class Robot { * 开局选择英雄 * @private */ + @wait('pickHeroTime') private async selectHero() { - await this.delay(2); let data = assistantUtil.randomHero(); this.reply('select_hero_c2s', data); } /** - * 开局装备 + * 开局准备 * @private */ + @delay(2) private async setReady() { this.reply('play_ready_c2s', ''); } @@ -154,9 +151,9 @@ export class Robot { * 开局换牌 * @private */ + @wait('cardChangeTime') private async changeCard() { let cardIds: number[] = []; - await this.delay(2); this.reply('change_card_c2s', { cards: cardIds }); @@ -166,8 +163,8 @@ export class Robot { * 出牌 * @private */ + @wait('maxDiscardTime') private async discard() { - await this.delay(3); let self = this; let cardArr = [...self.player.cards.values()]; let cards = assistantUtil.checkTriple(cardArr); @@ -184,8 +181,8 @@ export class Robot { * 吃牌或者放弃 * @private */ + @wait('maxEatTime') private async eatOrGiveUp() { - await this.delay(2); let targetCard = [...this.room.state.cards.values()][0]; let cardArr = [...this.player.cards.values()]; let tmpCards = assistantUtil.checkTriple(cardArr, targetCard); @@ -230,6 +227,7 @@ export class Robot { * 选择一个法术或者一个随从 * @private */ + @wait('playerActTime') private async selectPet() { let data = await assistantUtil.selectPet(this.player, this.room.state); this.reply('select_pet_c2s', data); diff --git a/src/robot/RobotClient.ts b/src/robot/RobotClient.ts index faa7594..00e4e4e 100644 --- a/src/robot/RobotClient.ts +++ b/src/robot/RobotClient.ts @@ -7,6 +7,7 @@ import {GameStateConst} from "../constants/GameStateConst"; import {Card} from "../rooms/schema/Card"; import {Player} from "../rooms/schema/Player"; import assistantUtil from "../utils/assistant.util"; +import {wait} from "../decorators/cfg"; export class RobotClient implements Client { id: string; @@ -125,8 +126,8 @@ export class RobotClient implements Client { * 出牌 * @private */ + @wait('maxDiscardTime') private async discard() { - await assistantUtil.delay(3); let self = this; let cardArr = [...self.selfPlayer.cards.values()]; let cards = assistantUtil.checkTriple(cardArr); @@ -143,8 +144,8 @@ export class RobotClient implements Client { * 吃牌或者放弃 * @private */ + @wait('maxEatTime') private async eatOrGiveUp() { - await assistantUtil.delay(2); let targetCard = [...this.svrstate.cards.values()][0]; let cardArr = [...this.selfPlayer.cards.values()]; let tmpCards = assistantUtil.checkTriple(cardArr, targetCard); @@ -188,7 +189,6 @@ export class RobotClient implements Client { * @private */ private async setReady() { - await assistantUtil.delay(2.5); this.reply('play_ready_c2s', ''); } @@ -196,8 +196,8 @@ export class RobotClient implements Client { * 开局选择英雄 * @private */ + @wait('pickHeroTime') private async selectHero() { - await assistantUtil.delay(2); let data = assistantUtil.randomHero(); this.reply('select_hero_c2s', data); } @@ -206,6 +206,7 @@ export class RobotClient implements Client { * 开局换牌 * @private */ + @wait('cardChangeTime') private changeCard() { let cardIds: number[] = []; this.reply('change_card_c2s', { @@ -219,6 +220,7 @@ export class RobotClient implements Client { * 选择一个法术或者一个随从 * @private */ + @wait('playerActTime') private async selectPet() { let data = await assistantUtil.selectPet(this.selfPlayer, this.svrstate); this.reply('select_pet_c2s', data) diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index c1dfc4e..76ee8b2 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -20,6 +20,7 @@ import {GameStateConst} from "../constants/GameStateConst"; import {GameRestartCommand} from "./commands/GameRestartCommand"; import {RobotClient} from "../robot/RobotClient"; import axios from 'axios'; +import {wait} from "../decorators/cfg"; export class GeneralRoom extends Room { dispatcher = new Dispatcher(this); @@ -94,7 +95,6 @@ export class GeneralRoom extends Room { }); } - onJoin (client: Client, options: any) { let data = { client: client diff --git a/src/rooms/commands/DiscardCommand.ts b/src/rooms/commands/DiscardCommand.ts index 4c765aa..6f7b759 100644 --- a/src/rooms/commands/DiscardCommand.ts +++ b/src/rooms/commands/DiscardCommand.ts @@ -9,6 +9,7 @@ import {GameEnv} from "../../cfg/GameEnv"; import {debugRoom} from "../../common/Debug"; import {TurnEndCommand} from "./TurnEndCommand"; import {Card} from "../schema/Card"; +import {Wait} from "./Wait"; /** * 出牌 @@ -72,7 +73,7 @@ export class DiscardCommand extends Command = global.$cfg.get(BaseConst.EFFECTCARD);