j7/bcutils.js
aozhiwei 67cea0e388 1
2022-04-16 22:27:14 +08:00

67 lines
1.8 KiB
JavaScript

const utils = require('./utils');
const BC_BASE_GAME_ID = 2006;
const BC_FUNC_CREATION = 1;
const BC_EPOCH = 1643212800;
function isValidBcGameId(gameId) {
return gameID > BASE_BC_GAME_ID && gameID < BC_BASE_GAME_ID + 99;
}
function isValidBcTime(time) {
return time >= BC_EPOCH && time < BC_EPOCH + 3600 * 24 * 365 * 20;
}
function isValidBcFuncId(funcId) {
return funcId > 0 && funcId < 10;
}
function toBcTime(time) {
return time - BC_EPOCH;
}
function toBcGameId(gameId) {
return gameId - BC_BASE_GAME_ID;
}
function genTokenId(gameId, funcId, time, subIdx, idx) {
const tokenId =
funcId.toString() +
utils.pad(toBcGameId(gameId)) +
utils.pad(toBcTime(time)) +
utils.pad(subIdx, 1) +
utils.pad(1 + (idx % 99999), 5);
return tokenId;
}
function isValidTokenId(tokenId) {
return utils.isPureNumberStr(tokenId.toString()) && tokenId.toString().length == 18;
}
function setTokenIdSubIdx(tokenId, idx) {
if (!isValidTokenId(tokenId)) {
throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx;
}
if (idx < 0 || idx > 9 || idx.toString().length != 1) {
throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx;
}
throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx;
}
function isSameAddress(a, b) {
return a.toLowerCase() == b.toLowerCase();
}
exports.BC_BASE_GAME_ID = BC_BASE_GAME_ID;
exports.BC_FUNC_CREATION = BC_FUNC_CREATION;
exports.BC_EPOCH = BC_EPOCH;
exports.isValidBcGameId = isValidBcGameId;
exports.isValidBcTime = isValidBcTime;
exports.isValidBcFuncId = isValidBcFuncId;
exports.toBcTime = toBcTime;
exports.toBcGameId = toBcGameId;
exports.genTokenId = genTokenId;
exports.isValidTokenId = isValidTokenId;
exports.setTokenIdSubIdx = setTokenIdSubIdx;
exports.isSameAddress = isSameAddress;