aozhiwei 560bc927ab 1
2022-04-17 10:33:13 +08:00

118 lines
3.0 KiB
JavaScript

const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const BaseService = require('./baseservice');
const metaFactory = require('../metadata/factory');
class Present extends BaseService {
async init() {
this.gameId = 2006;
this.nowTime = utils.getUtcTime();
this.tokenTime = this.nowTime;
this.idx = 0;
//console.log(utils.pad(undefined, 2));
const {err, conn} = await app.getDbConn('MarketDb0');
if (err) {
throw 'db error:' + err;
}
this.conn = conn;
const presentList = metaFactory.getMetaByKey('PresentList', '0')['list'];
for (let i in presentList) {
const name = presentList[i];
const list = [];
metaFactory.traverseMetaList('Present' + name, (item) => {
list.push(item);
return true;
});
await this.processList(name, list);
}
}
async processList(name, list) {
for (let i in list){
const presentMeta = list[i];
console.log(presentMeta);
const randItem = presentMeta.randItem();
if (randItem) {
console.log(11111);
const itemId = randItem['itemId'];
const tokenType = randItem['tokenType'];
for (let ii = 0; ii < presentMeta.getCount(); ++ii) {
await this.processOne(bcutils.BC_FUNC_GUILD,
name,
presentMeta,
itemId,
tokenType,
ii + 1,
this.genIdx());
}
}
}
}
async processOne(funcId, name, presentMeta, itemId, tokenType, seqId, idx) {
const sented = await this.isSented(name, presentMeta, seqId);
if (!sented) {
const tokenId = bcutils.genTokenId(
this.gameId,
funcId,
this.nowTime,
1,
idx
);
console.log(tokenId, presentMeta);
const {err} = await this.conn.insert(
't_present',
[
['bc_mint_tokenid', tokenId],
['batch_id', name],
['row_id', presentMeta['id']],
['seq_id', seqId],
['account', bcutils.toNormalAddress(presentMeta['account'])],
['game_id', this.gameId],
['bc_mint_itemid', itemId],
['bc_mint_token_type', tokenType],
['bc_mint_tags', presentMeta.getTags()],
['createtime', this.nowTime],
['modifytime', this.nowTime]
]
);
}
}
async isSented(name, presentMeta, seqId) {
const {err, row} = await this.conn.ormSelectOne(
't_present',
[
['batch_id', name],
['row_id', presentMeta['id']],
['seq_id', seqId],
]
);
if (err) {
throw err;
}
return row ? true : false;
}
genIdx() {
let newIdx = this.idx;
if (this.idx < bcutils.BC_MAX_TOKEN_IDX) {
newIdx = this.idx;
++this.idx;
} else {
this.idx = 0;
newIdx = this.idx;
++this.tokenTime;
}
return newIdx;
}
}
module.exports = Present;