CDK.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Lang = require("common.Lang")
  6. local Code = { "0","c","e","u","m",
  7. "k","d","7","x","f",
  8. "9","j","w","6","8",
  9. "t","1","h","4","p",
  10. "y","3","2","b","5","n"}
  11. local batchList = {6,7,8} -- 批次位置
  12. local idxList = {3,4,5} -- 序号位置
  13. local CodeMap = {
  14. ["0"] = 1,["c"] = 2,["e"] = 3,["u"] = 4,["m"] = 5,
  15. ["k"] = 6,["d"] = 7,["7"] = 8,["x"] = 9,["f"] = 10,
  16. ["9"] = 11,["j"] = 12,["w"] = 13,["6"] = 14,["8"] = 15,
  17. ["t"] = 16,["1"] = 17,["h"] = 18,["4"] = 19,["p"] = 20,
  18. ["y"] = 21,["3"] = 22,["2"] = 23,["b"] = 24,["5"] = 25,["n"] = 26
  19. }
  20. local unit = 20
  21. local bit = unit
  22. local bit2 = unit * unit
  23. local function checkCDKParam(finishTime,cdkID)
  24. local now = os.time()
  25. if now > finishTime then
  26. return
  27. end
  28. return true
  29. end
  30. local function genCDKBatch(cnt,useCnt,cdkID)
  31. local bat = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
  32. local db = {
  33. batch = bat + 1,
  34. cnt = cnt,
  35. useCnt = useCnt,
  36. info = cdkID,
  37. useCDKList = {},
  38. }
  39. LuaMongo.insert(DB.db_cdk, db)
  40. CommonDB.setCDKBatch(bat + 1) -- 将最新的cdkBatch保存
  41. return bat + 1
  42. end
  43. local function transNumber(num)
  44. local first,second = 0,0
  45. if num >= bit2 then
  46. first = math.floor(num / bit2)
  47. end
  48. if num >= bit then
  49. local t = num - (first * bit2)
  50. second = math.floor(t / bit)
  51. end
  52. local third = num - (first * bit2) - (second * bit)
  53. --return Code[first + 1] .. Code[second + 1] .. Code[thrid + 1]
  54. return first,second,third
  55. end
  56. local function calcNumber(first,second,third)
  57. if first > unit or second > unit or third > unit then
  58. return -1
  59. end
  60. return first * bit2 + second * bit + third
  61. end
  62. local function transNum2Code(num)
  63. local f,s,t = transNumber(num)
  64. return Code[f + 1]..Code[s + 1]..Code[t + 1]
  65. end
  66. local function genCDKCode(batch,index)
  67. return Code[math.random(2,#Code)] .. Code[math.random(#Code)] ..transNum2Code(index) .. transNum2Code(batch) .. Code[math.random(#Code)] .. Code[math.random(#Code)]
  68. end
  69. local function deCDKCode(cdk)
  70. local num_f_code,num_s_code,num_t_code = CodeMap[string.sub(cdk,3,3) ],CodeMap[string.sub(cdk,4,4) ],CodeMap[string.sub(cdk,5,5) ]
  71. if not num_f_code or not num_s_code or not num_t_code then
  72. return false
  73. end
  74. num_f_code = num_f_code - 1
  75. num_s_code = num_s_code - 1
  76. num_t_code = num_t_code - 1
  77. local bat_f_code,bat_s_code,bat_t_code = CodeMap[string.sub(cdk,6,6) ],CodeMap[string.sub(cdk,7,7) ],CodeMap[string.sub(cdk,8,8) ]
  78. if not bat_f_code or not bat_s_code or not bat_t_code then
  79. return false
  80. end
  81. bat_f_code = bat_f_code - 1
  82. bat_s_code = bat_s_code - 1
  83. bat_t_code = bat_t_code - 1
  84. local num = calcNumber(num_f_code,num_s_code,num_t_code)
  85. if num < 0 then
  86. return false
  87. end
  88. local batch = calcNumber(bat_f_code,bat_s_code,bat_t_code)
  89. if batch < 0 then
  90. return false
  91. end
  92. --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)
  93. --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)
  94. return true , num,batch
  95. end
  96. -- 生成一个批次的激活码
  97. -- 批次最大999 ,每次最多生成999个激活码
  98. --[[
  99. param1 = cnt 生成CDK数量
  100. param2 = useCnt 当前批次能够使用最大数量
  101. param3 = cdkId 道具信息
  102. ]]
  103. function genCDK(cnt,useCnt,cdkId)
  104. useCnt = useCnt or 1
  105. cnt = cnt < 1000 and cnt or 999
  106. local bat = genCDKBatch(cnt,useCnt,cdkId)
  107. local fileName = "cdk"..bat
  108. local f,err = io.open(fileName,"w")
  109. if not f then
  110. print("open file failed ",err)
  111. return
  112. end
  113. local idx = 0
  114. local data = "cdk : \n"
  115. while true do
  116. if idx > cnt then
  117. break
  118. end
  119. local cdk = genCDKCode(bat,idx)
  120. data = data .. cdk .. "\n"
  121. idx = idx + 1
  122. end
  123. f:write(data)
  124. end
  125. -- 使用cdk
  126. function useCDK(cdkBatchData,cdk)
  127. local ok,_,batch = deCDKCode(cdk)
  128. QueryCDK = { batch = { ["$eq"] = batch}}
  129. LuaMongo.update(DB.db_cdk,QueryCDK,{
  130. ["$set"] = {
  131. ["useCDKList"] = cdkBatchData.useCDKList
  132. },
  133. ["$unset"] = nil
  134. })
  135. end
  136. -- 检查CDK
  137. function checkCDK(cdk)
  138. local ok,num,batch = deCDKCode(cdk)
  139. if not ok then
  140. return Lang.CDK_INVALID_ERR
  141. end
  142. local curBatch = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
  143. if curBatch < batch then
  144. return Lang.CDK_BATCH_ERR
  145. end
  146. QueryCDK = { batch = { ["$eq"] = batch} }
  147. LuaMongo.find(DB.db_cdk,QueryCDK)
  148. local cdkBatchData = {}
  149. LuaMongo.next(cdkBatchData)
  150. if cdkBatchData.cnt < num then
  151. return Lang.CDK_INVALID_ERR
  152. end
  153. if cdkBatchData.useCDKList[num] then
  154. return Lang.CDK_ERR4
  155. end
  156. return nil, cdkBatchData
  157. end