CDK.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. local DB = require("common.DB")
  2. local CommonDB = require("common.CommonDB")
  3. local CdkFixExcel = require("excel.cdkFix")
  4. local LuaMongo = _G.lua_mongo
  5. local Code = {"0","7","9","8","1","4","3","2","5","6"}
  6. local batchList = {6,7,8} -- 批次位置
  7. local idxList = {3,4,5} -- 序号位置
  8. local CodeMap = {
  9. ["0"] = 1,
  10. ["7"] = 2,
  11. ["9"] = 3,
  12. ["8"] = 4,
  13. ["1"] = 5,
  14. ["4"] = 6,
  15. ["3"] = 7,
  16. ["2"] = 8,
  17. ["5"] = 9,
  18. ["6"] = 10,
  19. }
  20. local function checkCDKParam(finishTime,cdkID)
  21. local now = os.time()
  22. if now > finishTime then
  23. return
  24. end
  25. return true
  26. end
  27. local function genCDKBatch(cnt,useCnt,cdkID)
  28. local bat = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
  29. local db = {
  30. batch = bat + 1,
  31. cnt = cnt,
  32. useCnt = useCnt,
  33. info = cdkID,
  34. useCDKList = {},
  35. }
  36. LuaMongo.insert(DB.db_cdk, db)
  37. CommonDB.setCDKBatch(bat + 1) -- 将最新的cdkBatch保存
  38. return bat
  39. end
  40. local function transNumber(num)
  41. local first,second = 0,0
  42. if num > 100 then
  43. first = math.floor(num / 100)
  44. end
  45. if num > 10 then
  46. local t = num - (first * 100)
  47. second = math.floor(t / 10)
  48. end
  49. local thrid = num - (first * 100) + (second * 10)
  50. return Code[first + 1] .. Code[second + 1] .. Code[thrid + 1]
  51. end
  52. local function genCDKCode(batch,index)
  53. return Code[math.random(2,10)] .. Code[math.random(10)] ..transNumber(index) .. transNumber(batch) .. Code[math.random(10)] .. Code[math.random(10)]
  54. end
  55. local function deCDKCode(cdk)
  56. 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)
  57. 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)
  58. return num,batch
  59. end
  60. -- 生成一个批次的激活码
  61. -- 批次最大999 ,每次最多生成999个激活码
  62. --[[
  63. param1 = cnt 生成CDK数量
  64. param2 = useCnt 当前批次能够使用最大数量
  65. param3 = cdkId 道具信息
  66. ]]
  67. function genCDK(cnt,useCnt,cdkId)
  68. useCnt = useCnt or 1
  69. cnt = cnt < 1000 and cnt or 999
  70. local bat = genCDKBatch(cnt,useCnt,cdkId)
  71. local fileName = "cdk"..bat
  72. local f,err = io.open(fileName,"w")
  73. if not f then
  74. print("open file failed ",err)
  75. return
  76. end
  77. local idx = 0
  78. local data = "cdk : \n"
  79. while true do
  80. if idx > cnt then
  81. break
  82. end
  83. local cdk = genCDKCode(bat,idx)
  84. data = data .. cdk .. "\n"
  85. idx = idx + 1
  86. end
  87. f:write(data)
  88. end
  89. -- 使用cdk
  90. function useCDK(cdk)
  91. end
  92. -- 检查CDK
  93. function checkCDK(cdk)
  94. local num,batch = deCDKCode(cdk)
  95. local curBatch = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
  96. if curBatch < batch then
  97. return "batch error"
  98. end
  99. QueryCDK = { batch = { ["$eq"] = batch} }
  100. LuaMongo.find(DB.db_cdk,QueryCDK)
  101. local cdkBatchData = {}
  102. LuaMongo.next(cdkBatchData)
  103. if cdkBatchData.cnt < num then
  104. return "invalid cdk"
  105. end
  106. if cdkBatchData.useCDKList[num] then
  107. return "cdk used"
  108. end
  109. return nil, cdkBatchData
  110. end