aozhiwei 304d4f5ac7 1
2022-04-21 13:48:23 +08:00

55 lines
1.4 KiB
JavaScript

const assert = require('assert');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const basewrap = require('./basewrap');
class LuckyBox extends basewrap.BaseWrap {
static idTypeHash = {
}
_init0(factory) {
const key = this['box_id'] + '_' + this['type'];
if (!utils.hasKey(LuckyBox.idTypeHash, key)) {
LuckyBox.idTypeHash[key] = {
'total_space': 0,
'items': []
};
}
assert(this['weight'] > 0);
LuckyBox.idTypeHash[key]['total_space'] += this['weight'];
LuckyBox.idTypeHash[key]['items'].push({
'space': LuckyBox.idTypeHash[key]['total_space'],
'meta': this
});
const itemMeta = factory.getMetaByKey('Item', this['item_id']);
assert(itemMeta);
assert(itemMeta.getNftType() != bcutils.NONE_TYPE);
assert(itemMeta.getNftType() == this['type']);
}
static randLuckBox(boxId, tokenType) {
const key = boxId + '_' + tokenType;
if (!utils.hasKey(LuckyBox.idTypeHash, key)) {
console.log(1111, key);
return 0;
}
const data = LuckyBox.idTypeHash[key];
assert(data['total_space'] > 0);
console.log(data);
const rand = utils.randRange(0, data['total_space'] - 1);
for (let i in data['items']) {
const item = data['items'][i];
if (rand < item['space']) {
return item['meta']['item_id'];
}
}
console.log(22222, rand);
return 0;
}
}
module.exports = LuckyBox;