CDK.lua 7.9 KB

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