You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.5 KiB

  1. --- 金币场相关PHP请求接口
  2. local PHPCmd = require("luaScript.Protocol.Coin.CoinPhpCmd")
  3. local CoinData = require("luaScript.Views.Coin.Data.CoinData")
  4. local CoinEvents = require("luaScript.Views.Coin.Constant.CoinEvents")
  5. local CoinDefined = require("luaScript.Views.Coin.Constant.CoinDefined")
  6. local CoinProtocolPhp = class("CoinProtocolPhp")
  7. --- CoinProtocolPhp:ctor
  8. function CoinProtocolPhp:ctor()
  9. self.phpUrl = getGlobalPhpUrl()
  10. CoinData.getInstance():reset() -- 第一次初始化实例
  11. end
  12. --- CoinProtocolPhp:requestGameServerConfig 请求获取游戏等级配置
  13. function CoinProtocolPhp:requestGameServerConfig()
  14. local params = {
  15. action = PHPCmd.PHP_GAME_SERVER_CONFIG,
  16. app = getAppId(),
  17. uid = app.user.loginInfo.uid,
  18. gameVer = ""
  19. }
  20. for _, gameId in pairs(GAME_IDS) do
  21. if gameId ~= GAME_IDS.More then
  22. local ver = app.subGameManager:isInstaller(gameId) and app.subGameManager.GameVersions[gameId] or -1
  23. params.gameVer = params.gameVer .. gameId .. "," .. ver .. "|"
  24. end
  25. end
  26. print("CoinProtocolPhp:requestGameServerConfig() ", table.tostring(params))
  27. httpPost(
  28. self.phpUrl,
  29. params,
  30. function(status, response)
  31. if status ~= "successed" then
  32. return
  33. end
  34. local data = json.decode(response)
  35. if not (data and data.code == 200 and data.result) then
  36. return
  37. end
  38. local result = data.result
  39. logD("CoinProtocolPhp:requestGameServerConfig response :", table.tostring(result))
  40. local gameData = {}
  41. gameData.ruleList = {}
  42. local rankImgType = {}
  43. for gameid, content in pairs(result) do
  44. gameData.ruleList[tonumber(gameid)] = content
  45. for gameRank, v in pairsByKeys(content) do
  46. local list = string.sub(gameRank, -1)
  47. local first = string.sub(gameRank, 0, 1)
  48. local gameRule = tonumber(list)
  49. first = tonumber(first)
  50. if gameRule then
  51. local isSame = false
  52. for k, rule in ipairs(gameData.ruleList[tonumber(gameid)]) do
  53. if rule == gameRule then
  54. isSame = true
  55. break
  56. end
  57. end
  58. local isSameImg = false
  59. for k, rule in ipairs(rankImgType) do
  60. if k == first then
  61. isSameImg = true
  62. break
  63. end
  64. end
  65. if not isSame then
  66. if gameData.ruleList[tonumber(gameid)] and gameData.ruleList[tonumber(gameid)][gameRank] then
  67. gameData.ruleList[tonumber(gameid)][gameRank].ruleID = gameRule
  68. end
  69. end
  70. if not isSameImg then
  71. rankImgType[first] = v.icon
  72. end
  73. end
  74. end
  75. end
  76. logD("CoinProtocolPhp:requestGameServerConfig response22 :", table.tostring(gameData))
  77. CoinData.getInstance():setGameData(gameData)
  78. --下载图片
  79. logD("需要下载的图片列表:rankImgType:", table.tostring(rankImgType))
  80. for k, url in pairs(rankImgType) do
  81. local fileName = "coingame" .. k .. ".png"
  82. local fullPath = cc.FileUtils:getInstance():getWritablePath() .. fileName
  83. if not cc.FileSystem:fileExists(fullPath) then
  84. dowloadImageFile(url, fileName, nil)
  85. else
  86. logD(fileName .. "已下载过,无须重复下载!")
  87. end
  88. end
  89. app:dispatchEvent({name = CoinEvents.EVENT_RESPONSE_GAME_SERVER_CONFIG})
  90. end
  91. )
  92. end
  93. -- 请求金币游戏活动
  94. function CoinProtocolPhp:requestCoinGameActivity(id)
  95. local appVersion = getAppVersionNum()
  96. local params =
  97. {
  98. action = PHPCmd.PHP_DAY_FULI,
  99. apkver = appVersion,
  100. uid = app.user.loginInfo.uid,
  101. app = getAppId(),
  102. }
  103. print("CoinProtocolPhp:requestCoinGameActivity() ", table.tostring(params))
  104. httpPost(self.phpUrl, params, function (status, response)
  105. if status == "successed" then
  106. local data = json.decode(response)
  107. if data and data.result then
  108. print("CoinProtocolPhp:requestCoinGameActivityResponse() ", table.tostring(data.result))
  109. --数据解析,解析为:每日福利,免费金币两个模块。其中每日福利需要排序显示
  110. local coinGameActivity = data.result
  111. local shareFreeTimes = 0
  112. for k,v in pairs(data.result) do
  113. --免费分享的次数
  114. if v.type == CoinDefined.COIN_GAME_ACTIVITY.TYPE_FREE_COIN then
  115. shareFreeTimes = toNumber(v.need) - toNumber(v.progrss)
  116. end
  117. if v.type == CoinDefined.COIN_GAME_ACTIVITY.TYPE_SHARE_GAME or v.type == CoinDefined.COIN_GAME_ACTIVITY.TYPE_FREE_COIN then
  118. if v.ext then
  119. if v.ext.share_pic ~= "" then
  120. local fileName = getImageNameFromUrl(v.ext.share_pic)
  121. local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName
  122. if not cc.FileSystem:fileExists(fullPath) then
  123. getImageFromUrlWithTime(v.ext.share_pic, fileName, nil, nil)
  124. else
  125. logD(fileName.."已下载过,无须重复下载!");
  126. end
  127. v.ext.share_pic = fileName
  128. end
  129. end
  130. end
  131. end
  132. coinGameActivity.shareFreeTimes = shareFreeTimes;
  133. CoinData.getInstance():setGameActivityData(coinGameActivity)
  134. --logD("requestCoinGameActivity:",table.tostring(self.coinGameActivity))
  135. app:dispatchEvent({name = CoinEvents.EVENT_RESPONSE_GAME_ACTIVITY_CONFIG,response = data.result});
  136. end
  137. end
  138. end)
  139. end
  140. function CoinProtocolPhp:requestTaskJiangLi(type,callback)
  141. local params =
  142. {
  143. action = PHPCmd.PHP_DAY_GET_TASK_PRIZE,
  144. token = app.user.loginInfo.token,
  145. uid = app.user.loginInfo.uid,
  146. app = getAppId(),
  147. type = type,
  148. }
  149. print("CoinProtocolPhp:requestTaskJiangLi() ", table.tostring(params))
  150. httpPost(self.phpUrl, params, function (status, response)
  151. if status == "successed" then
  152. local data = json.decode(response)
  153. if data and data.result then
  154. dump(data,"requestTaskJiangLi")
  155. if callback then
  156. callback(data)
  157. end
  158. end
  159. end
  160. end)
  161. end
  162. return CoinProtocolPhp