寻宝助战匹配简单机器人

This commit is contained in:
liangtongchuan
2020-11-27 23:27:35 +08:00
parent 369c831846
commit 3e499de1bc
8 changed files with 159 additions and 23 deletions

View File

@@ -242,6 +242,11 @@ export function getRefTime(now = new Date(), hour: number, day = 0) {
return new Date(today + day * 24 * 60 * 60 * 1000);
}
/**
* 从一个数组中随机返回不重复的 cnt 个元素数组
* @param source 原数组
* @param cnt 返回随机元素个数
*/
export function getRandEelm(source: Array<any> = [], cnt = 1): Array<any> {
if (cnt > source.length) return [];
if (cnt === source.length) return source;
@@ -256,6 +261,16 @@ export function getRandEelm(source: Array<any> = [], cnt = 1): Array<any> {
return source.filter((_item, idx) => idxs.has(idx));
}
/**
* 在给定数值的浮动范围中随机一个值
* @param base 基础数值
* @param ratio 随机范围base 值上下浮动 ratio(小于 1 的整数)
* @param decimal 返回值保留的小数位数
*/
export function getRandValue(base: number, ratio: number, decimal = 2): number {
return parseFloat((base * (1 - ratio + Math.random() * ratio * 2)).toFixed(decimal));
}
export function resResult(status: {code: number, simStr: string}, data = null, customMsg = '') {
const { code, simStr } = status;
if (code !== STATUS.SUCCESS.code) {