59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
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;
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
}
|
|
const baseCfg = singleton(GameEnv);
|
|
|
|
/**
|
|
* 根据配置项延迟执行的修饰器
|
|
* @param type 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);
|
|
})
|
|
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据参数延迟执行的修饰器
|
|
* @param num 最大延迟n秒
|
|
*/
|
|
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);
|
|
})
|
|
|
|
};
|
|
}
|
|
}
|