local DB = require("common.DB") local CommonDB = require("common.CommonDB") local CdkFixExcel = require("excel.cdkFix") local LuaMongo = _G.lua_mongo local Lang = require("common.Lang") local Code = {"0","7","9","8","1","4","3","2","5","6"} local batchList = {6,7,8} -- 批次位置 local idxList = {3,4,5} -- 序号位置 local CodeMap = { ["0"] = 1, ["7"] = 2, ["9"] = 3, ["8"] = 4, ["1"] = 5, ["4"] = 6, ["3"] = 7, ["2"] = 8, ["5"] = 9, ["6"] = 10, } local function checkCDKParam(finishTime,cdkID) local now = os.time() if now > finishTime then return end return true end local function genCDKBatch(cnt,useCnt,cdkID) local bat = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found") local db = { batch = bat + 1, cnt = cnt, useCnt = useCnt, info = cdkID, useCDKList = {}, } LuaMongo.insert(DB.db_cdk, db) CommonDB.setCDKBatch(bat + 1) -- 将最新的cdkBatch保存 return bat end local function transNumber(num) local first,second = 0,0 if num >= 100 then first = math.floor(num / 100) end if num >= 10 then local t = num - (first * 100) second = math.floor(t / 10) end local thrid = num - (first * 100) - (second * 10) return Code[first + 1] .. Code[second + 1] .. Code[thrid + 1] end local function genCDKCode(batch,index) return Code[math.random(2,10)] .. Code[math.random(10)] ..transNumber(index) .. transNumber(batch) .. Code[math.random(10)] .. Code[math.random(10)] end local function deCDKCode(cdk) local num = (CodeMap[string.sub(cdk,3,3) ] - 1) * 100 + (CodeMap[string.sub(cdk,4,4)] - 1) * 10 + (CodeMap[string.sub(cdk,5,5) ] - 1) local batch = (CodeMap[string.sub(cdk,6,6) ] - 1) * 100 + (CodeMap[string.sub(cdk,7,7) ] - 1) * 10 + (CodeMap[string.sub(cdk,8,8) ] - 1) return num,batch end -- 生成一个批次的激活码 -- 批次最大999 ,每次最多生成999个激活码 --[[ param1 = cnt 生成CDK数量 param2 = useCnt 当前批次能够使用最大数量 param3 = cdkId 道具信息 ]] function genCDK(cnt,useCnt,cdkId) useCnt = useCnt or 1 cnt = cnt < 1000 and cnt or 999 local bat = genCDKBatch(cnt,useCnt,cdkId) local fileName = "cdk"..bat local f,err = io.open(fileName,"w") if not f then print("open file failed ",err) return end local idx = 0 local data = "cdk : \n" while true do if idx > cnt then break end local cdk = genCDKCode(bat,idx) data = data .. cdk .. "\n" idx = idx + 1 end f:write(data) end -- 使用cdk function useCDK(cdkBatchData,cdk) local _,batch = deCDKCode(cdk) QueryCDK = { batch = { ["$eq"] = batch}} LuaMongo.update(DB.db_cdk,QueryCDK,{ ["$set"] = { ["useCDKList"] = cdkBatchData.useCDKList }, ["$unset"] = nil }) end -- 检查CDK function checkCDK(cdk) local num,batch = deCDKCode(cdk) local curBatch = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found") if curBatch < batch then return Lang.CDK_BATCH_ERR end QueryCDK = { batch = { ["$eq"] = batch} } LuaMongo.find(DB.db_cdk,QueryCDK) local cdkBatchData = {} LuaMongo.next(cdkBatchData) if cdkBatchData.cnt < num then return Lang.CDK_INVALID_ERR end if cdkBatchData.useCDKList[num] then return Lang.CDK_ERR4 end return nil, cdkBatchData end