This commit is contained in:
aozhiwei 2022-04-16 22:27:14 +08:00
parent 33f9c8d06a
commit 67cea0e388
2 changed files with 90 additions and 0 deletions

66
bcutils.js Normal file
View File

@ -0,0 +1,66 @@
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;

View File

@ -5,6 +5,19 @@ const xlsx = require('node-xlsx');
const parseArgs = require('minimist');
const serverEnv = process.env['SERVER_ENV'];
const numberHash = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9
};
function rspErr(rsp, errCode, errMsg) {
rsp.send(jsonEncode({
'errcode': errCode,
@ -214,6 +227,16 @@ function hasKey(obj, key) {
return obj.hasOwnProperty(key);
}
function isPureNumberStr(str) {
for (let i = 0; i < str.length; ++i){
const c = str[i];
if (!hasKey(numberHash, c)) {
return false;
}
}
return true;
}
exports.rspErr = rspErr;
exports.rspOk = rspOk;
exports.rspData = rspData;
@ -244,3 +267,4 @@ exports.getVal = getVal;
exports.excelToJson = excelToJson;
exports.getArgv = getArgv;
exports.hasKey = hasKey;
exports.isPureNumberStr = isPureNumberStr;