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); }); await this.processList(name, list); } } async processList(name, list) { for (let i in list){ const presentMeta = list[i]; const randItem = presentMeta.randItem(); if (randItem) { 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 tokenId = bcutils.genTokenId( this.gameId, funcId, this.nowTime, 1, idx ); console.log(tokenId); const sented = await this.isSented(name, presentMeta, seqId); if (!sented) { 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], ['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;