机器人操作延迟从配置中读取

This commit is contained in:
zhl 2020-12-16 18:12:24 +08:00
parent deca1a4125
commit 4e5ae6d464
8 changed files with 80 additions and 18 deletions

View File

@ -42,6 +42,10 @@ export class GameEnv {
public waitingPlayerOnePlus: number;
// 英雄选择时间
public pickHeroTime: number;
// 机器人操作最小时间
public robotActTimeMin: number;
// 机器人操作最大时间
public robotActTimeMax: number;
public init(data: Map<number, BaseCfg>) {
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;
}
}

View File

@ -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;

52
src/decorators/cfg.ts Normal file
View File

@ -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);
})
};
}
}

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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<CardGameState, { client: Client, car
if (cardArr[0].type == 1) {
return [new NextSubCommand()];
} else {
return [new TurnEndCommand()];
return [new Wait().setPayload(3000), new TurnEndCommand()];
}
} else {
let cardArr: Card[] = [...this.state.cards.values()];

View File

@ -147,7 +147,6 @@ let assistantUtil = {
* @private
*/
async selectPet(dstPlayer: Player, state: CardGameState) {
await this.delay(5, 0.2);
let cards = [...state.cards.values()];
let result;
let effectMap: Map<number, EffectCardCfg> = global.$cfg.get(BaseConst.EFFECTCARD);