CDK.lua 7.7 KB

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