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 = nowTime; this.idx = 1; 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 = item.randItem(); if (randItem) { const itemId = randItem['itemId']; const tokenType = randItem['tokenType']; await this.processOne(bcutils.BC_FUNC_GUILD, name, presentMeta, itemId, tokenType, seqId, this.genIdx()); } } } async processOne(funcId, name, presentMeta, itemId, tokenType, seqId) { const tokenId = bcutils.genTokenId( this.gameId, funcId, this.nowTime, 1, idx ); const sented = await this.isSented(name, presentMeta, seqId); if (!sented) { const {err} = await this.conn.insert( 't_present', [ ['token_id', tokenId], ['batch_id', name], ['row_id', presentMeta['id']], ['seq_id', seqId], ['account', presentMeta['account']], ['game_id', 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() { if (idx < bcutils.BC_MAX_TOKEN_IDX) { ++idx; } else { idx = 1; ++this.tokenTime; } } } module.exports = Present;