奇遇随机添加权重

This commit is contained in:
luying
2020-10-14 17:19:09 +08:00
parent c06f422297
commit 57d60be3a2
5 changed files with 784 additions and 64 deletions

View File

@@ -48,17 +48,19 @@ import { GOOD_TYPE } from '../consts/consts';
let [gid, count, frequency] = arr;
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
result = { gid: parseInt(gid), count: parseInt(count), frequency: parseInt(frequency) };
break;
}
case 'eventSuitLevel': {
let [min, max] = arr;
if(isNaN(min) || isNaN(max)) throw new Error('格式错误');
result = { min: parseInt(min), max: parseInt(max) };
break;
}
case 'attribute': {
let [id, value] = arr;
if(isNaN(id) || isNaN(value)) throw new Error('格式错误');
result = { id: parseInt(id), value: parseInt(value) };
break;
}
}
return result;
@@ -170,3 +172,21 @@ export function decodeIdCntArrayStr(str: string, multi: number) {
return ce;
}
export function getRandomWithWeight(randomList: any) {
let len = randomList.reduce((pre, cur) => {
return pre + cur.weight||1;
}, 0);
let index = Math.floor(Math.random() * len);
let result = { dic: null, index: -1 };
for(let i = 0; i < randomList.length; i++) {
let {weight = 0} = randomList[i];
if(index < weight) {
result.dic = randomList[i];
result.index = i;
break;
}
index -= weight;
}
return result
}