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.

157 lines
3.6 KiB

  1. local ProtocolZhanJiGame = class("ProtocolZhanJiGame", require("luaScript.Protocol.ProtocolZhanJi"))
  2. --[[
  3. 子游戏战绩协议父类
  4. 构造的时候传入子游戏的ID,和战绩保存的文件名
  5. 获取最新战绩的时候调用类型和页数,大多数游戏是不需要传参数的
  6. --]]
  7. local InterfaceKey =
  8. {
  9. idx = "gamb.idx", -- 获取战绩ID列表
  10. }
  11. function ProtocolZhanJiGame:ctor(gameId, fileName)
  12. ProtocolZhanJiGame.super.ctor(self);
  13. self.zhanJiIdxs = {}
  14. self.otherIdxs = {}
  15. self.gameId = gameId
  16. self.fileName = fileName;
  17. self.shareUrl = "http://m.xiaopengol.com/replay.php" --分享战绩链接
  18. self:loadFromFile()
  19. end
  20. function ProtocolZhanJiGame:init()
  21. ProtocolZhanJiGame.super.init(self)
  22. self:getIdxList()
  23. end
  24. -- 获取最新的战绩列表的key_list
  25. function ProtocolZhanJiGame:getIdxList(mode, page)
  26. local tt =
  27. {
  28. action = InterfaceKey.idx, -- 接口名
  29. app = getAppId(), -- appId
  30. uid = app.user.loginInfo.uid, -- userId
  31. gameid = self.gameId, -- gameId
  32. mode = mode or 0, -- 玩法类型,黄十八使用到,默认为0既无特殊玩法
  33. page = page or 1, -- 页码
  34. }
  35. logD("ProtocolZhanJiGame:getIdxList()", table.tostring(tt))
  36. httpPost(self.phpUrl, tt, function(status, response)
  37. self:getIdxlistResponse(status, response, page)
  38. end);
  39. end
  40. -- 获取战绩ID列表的回复
  41. -- 并和本地数据进行比较,找出本地未下载的部分进行下载
  42. function ProtocolZhanJiGame:getIdxlistResponse(status, response, page)
  43. logD("ProtocolZhanJiGame:getIdxlistResponse()", status, response)
  44. local ret, ttResult = checkPhpResponse(status, response)
  45. if not ret then
  46. logE("ProtocolZhanJiGame:getIdxlistResponse()", tostring(ttResult))
  47. return
  48. end
  49. local page = ttResult.page
  50. local tpage = ttResult.tpage
  51. self.tpage = tpage;
  52. -- 只保存属于自己的战绩
  53. local myUid = app.user.loginInfo.uid
  54. if not self.zhanJiIdxs[myUid] then
  55. self.zhanJiIdxs[myUid] = {}
  56. end
  57. local idxs = {}
  58. for k,pid in pairs(ttResult.idx) do
  59. table.insert(idxs, pid)
  60. end
  61. self.zhanJiIdxs[myUid] = idxs;
  62. if ttResult.url and ttResult.url~="" then
  63. self.shareUrl = ttResult.url
  64. end
  65. self:checkAndDownload(ttResult.idx);
  66. end
  67. -- 获取属于自己的战绩
  68. function ProtocolZhanJiGame:getZhanJiList()
  69. -- 首先找到对应的 idx_list
  70. local myUid = app.user.loginInfo.uid
  71. if not self.zhanJiIdxs[myUid] then
  72. return {}
  73. end
  74. local function sortFuncEx(a,b)
  75. local numA = toNumber(a.endtime)
  76. local numB = toNumber(b.endtime)
  77. if numA ~= nil and numB ~= nil then
  78. return numA > numB
  79. else
  80. return a > b
  81. end
  82. end
  83. local zhanjiList = {}
  84. local idx_list = self.zhanJiIdxs[myUid]
  85. for _,pid in pairs(idx_list) do
  86. if self.zhanjiInfoList[pid] then
  87. table.insert(zhanjiList,self.zhanjiInfoList[pid])
  88. end
  89. end
  90. table.sort(zhanjiList,sortFuncEx)
  91. return zhanjiList;
  92. end
  93. function ProtocolZhanJiGame:requestOtherZhanji(idx)
  94. local idxs = {}
  95. if idx then
  96. table.insert(idxs,idx)
  97. -- self.lastSearchIdx = idx
  98. table.insert(self.otherIdxs,idx)
  99. end
  100. if #self.otherIdxs==0 then
  101. return
  102. end
  103. self:checkAndDownload(idxs)
  104. end
  105. function ProtocolZhanJiGame:getZhanJiListOther()
  106. local zhanjiList = {}
  107. local function sortFuncEx(a,b)
  108. local numA = toNumber(a.endtime)
  109. local numB = toNumber(b.endtime)
  110. if numA ~= nil and numB ~= nil then
  111. return numA > numB
  112. else
  113. return a > b
  114. end
  115. end
  116. local idx_list = self.zhanJiIdxs[myUid]
  117. for _,pid in pairs(self.otherIdxs) do
  118. if self.zhanjiInfoList[pid] then
  119. table.insert(zhanjiList,self.zhanjiInfoList[pid])
  120. end
  121. end
  122. table.sort(zhanjiList,sortFuncEx)
  123. return zhanjiList;
  124. end
  125. return ProtocolZhanJiGame;