58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
const utils = require('j7/utils');
|
|
const bcutils = require('j7/bcutils');
|
|
const basewrap = require('./basewrap');
|
|
const metaFactory = require('./factory');
|
|
|
|
class Present extends basewrap.BaseWrap {
|
|
|
|
_init0 () {
|
|
const items = this['items'].split('|');
|
|
let totalSpace = 0;
|
|
this._items = [];
|
|
items.forEach((tmpStr) => {
|
|
const item = tmpStr.split(':');
|
|
item[1] = parseInt(item[1]);
|
|
totalSpace += item[1];
|
|
const itemMeta = metaFactory.getMetaByKey('Item', item[0]);
|
|
if (!itemMeta) {
|
|
throw 'itemMeta is null' + this;
|
|
}
|
|
const nftType = itemMeta.getNftType();
|
|
if (nftType == bcutils.NONE_TYPE) {
|
|
throw 'itemMeta nft type is null' + this;
|
|
}
|
|
this._items.push({
|
|
'space': totalSpace,
|
|
'itemId': item[0],
|
|
'tokenType': nftType
|
|
});
|
|
});
|
|
if (!(totalSpace > 0)) {
|
|
throw 'total not > 0' + this
|
|
}
|
|
this._totalSpace = totalSpace;
|
|
}
|
|
|
|
randItem() {
|
|
const rand = utils.randRange(0, this._totalSpace);
|
|
for (let i in this._items) {
|
|
const item = this._items[i];
|
|
if (rand < item['space']) {
|
|
return item;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
getCount() {
|
|
return this['quantity'];
|
|
}
|
|
|
|
getTags() {
|
|
return this['tags'];
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Present;
|