CDK.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. local Config = require("Config")
  2. Json = Json or require("common.Json")
  3. local Util = require("common.Util")
  4. local ObjHuman = require("core.ObjHuman")
  5. local Broadcast = require("broadcast.Broadcast")
  6. local Lang = require("common.Lang")
  7. local Msg = require("core.Msg")
  8. local MailManager = require("mail.MailManager")
  9. local CdkFixExcel = require("excel.cdkFix")
  10. local ProjectLogic = require("platform.ProjectLogic")
  11. local BagLogic = require("bag.BagLogic")
  12. --1 getCDKInfo/getCDKInfoRet 参数 code ts account
  13. --返回 ret(1成功) code ts account id item endDate type limitCnt serverIndexs projectName
  14. --2 setCDKUse/setCDKUseRet 参数 code ts account
  15. --返回 ret (1成功) code ts account id item
  16. --ret
  17. --1:成功
  18. --2:激活码已使用
  19. --3:激活码长度不对
  20. --4:没有这个激活码
  21. --5:账号有误
  22. --6:请求失效
  23. --7:更新数据失败
  24. --8:未知错误
  25. --9:SIGN_ERROR
  26. local s2aParam = {}
  27. local CODE_PHP = "/api/cdk.php?a="
  28. function isOpen(human)
  29. return true
  30. end
  31. function CG_CDK(human,msg)
  32. local code = msg.code
  33. if cdkFixDo(human,code) then
  34. return
  35. end
  36. getCDKInfo(human, code)
  37. end
  38. function getCDKInfo(human, code)
  39. local now = os.time()
  40. s2aParam.code = code
  41. s2aParam.ts = now
  42. s2aParam.account = human.db.account
  43. s2aParam.action = "getCDKInfo"
  44. s2aParam.api_cbMethod = "getCDKInfoRet"
  45. _G.thread_http.send(CODE_PHP,Json.Encode(s2aParam))
  46. end
  47. function getCDKInfoRet(oJsonInput)
  48. --print("getCDKInfoRet begin")
  49. --require("common.Util").printTable(oJsonInput)
  50. local code = oJsonInput.code -- string
  51. local ts = tonumber(oJsonInput.ts) -- number
  52. local account = oJsonInput.account -- string
  53. local ret = tonumber(oJsonInput.ret) -- number
  54. local id = tonumber(oJsonInput.id) -- number
  55. local endDate = nil -- table
  56. if oJsonInput.endDate and oJsonInput.endDate ~= "" then
  57. endDate = oJsonInput.endDate
  58. end
  59. local typeTemp = tonumber(oJsonInput.type) or 0 -- 0一个cdk只能用一次a玩家用了b玩家就不能再用这个key了 1一个cdk可以让多个玩家反复使用
  60. local limitCnt = tonumber(oJsonInput.limitCnt) or 0 -- 这个id的礼包同一个玩家可以领取的次数 0表示无限次
  61. local serverIndexs = nil -- table
  62. if oJsonInput.serverIndexs and oJsonInput.serverIndexs ~= "" then
  63. serverIndexs = oJsonInput.serverIndexs
  64. end
  65. local projectName = oJsonInput.projectName or ""
  66. local human = ObjHuman.onlineAccount[account]
  67. if not human then
  68. return
  69. end
  70. if ret == 2 then
  71. return Broadcast.sendErr(human, Lang.CDK_ERR4)
  72. end
  73. if ret ~= 1 then
  74. return Broadcast.sendErr(human, Lang.CDK_ERR1)
  75. end
  76. if serverIndexs and (not Util.tableIsEmpty(serverIndexs)) then
  77. local canUse = false
  78. for k,v in ipairs(serverIndexs) do
  79. if Config.SVR_INDEX >= v[1] and Config.SVR_INDEX <= v[2] then
  80. canUse = true
  81. break
  82. end
  83. end
  84. if not canUse then
  85. return Broadcast.sendErr(human, Lang.CDK_ERR1)
  86. end
  87. end
  88. if endDate then
  89. local now = os.time()
  90. local endTime = os.time(endDate) + 12 * 3600
  91. if now > endTime then
  92. --过期
  93. return Broadcast.sendErr(human, Lang.CDK_ERR2)
  94. end
  95. end
  96. local useCnt = human.db.cdk and human.db.cdk[id] or 0
  97. if typeTemp ~= 0 then
  98. limitCnt = 1 -- 一对多的礼包一定限制1次,防止刷!
  99. end
  100. if limitCnt > 0 and useCnt >= limitCnt then
  101. return Broadcast.sendErr(human, Lang.CDK_ERR3)
  102. end
  103. if typeTemp == 0 then
  104. setCDKUse(human, code)
  105. else
  106. local item = oJsonInput.item -- table
  107. if item == nil or type(item) ~= "table" then
  108. assert(nil, "getCDKInfoRet item nil" .. code)
  109. end
  110. human.db.cdk = human.db.cdk or {}
  111. human.db.cdk[id] = 1
  112. MailManager.add(MailManager.SYSTEM, human.db._id, Lang.CDK_TITLE, Lang.CDK_CONTENT, item, Lang.CDK_TITLE)
  113. Broadcast.sendErr(human, Lang.CDK_SUCC)
  114. local msgRet = Msg.gc.GC_CDK
  115. Msg.send(msgRet, human.fd)
  116. end
  117. end
  118. function setCDKUse(human, code)
  119. local s2aParam = {}
  120. local now = os.time()
  121. s2aParam.code = code
  122. s2aParam.ts = now
  123. s2aParam.account = human.db.account
  124. s2aParam.action = "setCDKUse"
  125. s2aParam.api_cbMethod = "setCDKUseRet"
  126. _G.thread_http.send(CODE_PHP,Json.Encode(s2aParam))
  127. end
  128. function setCDKUseRet(oJsonInput)
  129. --print("setCDKUseRet")
  130. --require("common.Util").printTable(oJsonInput)
  131. local code = oJsonInput.code -- string
  132. local ts = tonumber(oJsonInput.ts) -- number
  133. local account = oJsonInput.account -- string
  134. local ret = tonumber(oJsonInput.ret) -- number
  135. local id = tonumber(oJsonInput.id) -- number
  136. local human = ObjHuman.onlineAccount[account]
  137. if human == nil then
  138. return
  139. end
  140. if ret == 2 then
  141. return Broadcast.sendErr(human, Lang.CDK_ERR4)
  142. end
  143. if ret ~= 1 then
  144. return Broadcast.sendErr(human, Lang.CDK_ERR1)
  145. end
  146. local item = oJsonInput.item or "" -- table
  147. if item == nil or type(item) ~= "table"then
  148. assert(nil, "setCDKUseRet item nil" .. code)
  149. end
  150. human.db.cdk = human.db.cdk or {}
  151. human.db.cdk[id] = (human.db.cdk[id] or 0) + 1
  152. --MailManager.add(MailManager.SYSTEM, human.db._id, Lang.CDK_TITLE, Lang.CDK_CONTENT, item, Lang.CDK_TITLE)
  153. BagLogic.addItemList(human, item , "cdk")
  154. local msgRet = Msg.gc.GC_CDK
  155. Msg.send(msgRet,human.fd)
  156. end
  157. local function getCdkFixID(code)
  158. for id,data in pairs(CdkFixExcel.cdkFix) do
  159. if data.code == code then
  160. return id,data
  161. end
  162. end
  163. end
  164. function cdkFixDo(human,code)
  165. local id,conf = getCdkFixID(code)
  166. if not id then
  167. return
  168. end
  169. --使用过
  170. if human.db.cdkFix and human.db.cdkFix[id] then
  171. Broadcast.sendErr(human, Lang.CDK_ERR4)
  172. return true
  173. end
  174. --项目
  175. if not ProjectLogic.inProjectName(conf.projectName) then
  176. return
  177. end
  178. --服务器范围
  179. if next(conf.serverIndexs) then
  180. local canUse = false
  181. for k,v in ipairs(conf.serverIndexs) do
  182. if Config.SVR_INDEX >= v[1] and Config.SVR_INDEX <= v[2] then
  183. canUse = true
  184. break
  185. end
  186. end
  187. if not canUse then
  188. return
  189. end
  190. end
  191. --期限
  192. if next(conf.endDate) then
  193. local now = os.time()
  194. local endTime = os.time(conf.endDate) + 12 * 3600
  195. if now > endTime then
  196. --过期
  197. return
  198. end
  199. end
  200. human.db.cdkFix = human.db.cdkFix or {}
  201. human.db.cdkFix[id] = 1
  202. BagLogic.addItemList(human, conf.item , "cdk")
  203. Msg.send(Msg.gc.GC_CDK, human.fd)
  204. return true
  205. end