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.

284 lines
11 KiB

  1. -- 用来记录比赛流程,适用用比赛重播
  2. -- 用单例模式会好点,直接用GameRecordController.getInstance() 而不要用 new
  3. -- round, 轮,site,局,userInfo中保留每轮合计数据,每轮数据可获得每轮的详细数据文件名,包含此轮每局的数据
  4. GameRecordController = class("GameRecordController")
  5. GameRecordController.k_max_record_num = 10
  6. -- 深度封装,解析json
  7. local function encodeJson(tables)
  8. if type(tables) ~= "table" then return tables end
  9. local tt = {}
  10. for k, v in pairs(tables) do
  11. if type(v) == "table" then
  12. tt[k] = encodeJson(v)
  13. else
  14. tt[k] = v
  15. end
  16. -- 数字开始key调用json.encode会有问题,所以加入了一个前缀
  17. if type(k) == "number" then
  18. tt["_ddnum_"..k] = tt[k]
  19. tt[k] = nil
  20. end
  21. end
  22. return json.encode(tt)
  23. end
  24. local function decodeJson(string)
  25. if type(string) ~= "string" then return string end
  26. local map = json.decode(string)
  27. local tt = {}
  28. if type(map) == "table" then
  29. for k, v in pairs(map) do
  30. local value = v
  31. if string.find(v, "{") then
  32. value = decodeJson(v)
  33. end
  34. if string.find(k, "_ddnum_") then
  35. local str = string.gsub(k, "_ddnum_", "")
  36. tt[tonumber(str)] = value
  37. else
  38. if value == {} then
  39. tt[k] = v
  40. else
  41. tt[k] = value
  42. end
  43. end
  44. end
  45. end
  46. return tt
  47. end
  48. function GameRecordController.getInstance()
  49. if not GameRecordController.m_instance then
  50. GameRecordController.m_instance = GameRecordController:new()
  51. end
  52. return GameRecordController.m_instance
  53. end
  54. -- 开启记录模式
  55. function GameRecordController:ctor()
  56. end
  57. -- 中途加入的以加入房间的时间为准
  58. function GameRecordController:setMyUserId(uid, userInfo)
  59. -- log("GameRecordController:setMyUserId", uid, table.tostring(userInfo))
  60. self.m_myUserId = uid
  61. self.m_userNameMap = self.m_userNameMap or {}
  62. self.m_userNameMap[uid] = userInfo.nickname
  63. end
  64. function GameRecordController:setBeginTime()
  65. saveUserInfo("GameRoundLastBeginTime", os.time())
  66. end
  67. function GameRecordController:addOtherUserId(uid, userInfo)
  68. -- log("GameRecordController:addOtherUserId", uid, table.tostring(userInfo))
  69. self.m_otherUserIds = self.m_otherUserIds or {}
  70. if uid and uid ~= self.m_myUserId then
  71. local haveThisUid = false
  72. for _, id in pairs(self.m_otherUserIds) do
  73. if id == uid then
  74. haveThisUid = true
  75. end
  76. end
  77. if not haveThisUid then
  78. table.insert(self.m_otherUserIds, uid)
  79. end
  80. self.m_userNameMap = self.m_userNameMap or {}
  81. self.m_userNameMap[uid] = userInfo.nickname
  82. end
  83. end
  84. -- 获取游戏开局时间
  85. function GameRecordController:getBeginTime()
  86. local beginTime = loadUserInfo("GameRoundLastBeginTime")
  87. return beginTime
  88. end
  89. -- 获取当前房间号
  90. function GameRecordController:getTotalTableId()
  91. return self.m_totalTableId
  92. end
  93. -- 每局的开始时间
  94. function GameRecordController:setTotalTime(time)
  95. -- log("GameRecordController:setTotalTime", time)
  96. self.m_totalTime = time
  97. end
  98. -- 记录当前房间号,只有当玩家进入房间成功以及重连回来时才会记录房间号
  99. function GameRecordController:setTotalTableId(totalTableId)
  100. -- log("GameRecordController:setTotalTableId", totalTableId)
  101. self.m_totalTableId = totalTableId
  102. end
  103. -- 记录当前局数和总局数
  104. function GameRecordController:setGameNum(gameStartCount, totalGameNum)
  105. -- log("GameRecordController:setGameNum", gameStartCount, totalGameNum)
  106. self.m_gameStartCount = gameStartCount
  107. self.m_totalGameNum = totalGameNum
  108. if self.m_gameStartCount == 1 then
  109. if self.m_totalTime then
  110. saveUserInfo("GameRoundLastBeginTime", self.m_totalTime)
  111. end
  112. end
  113. end
  114. function GameRecordController:setSumGameNum(sumGameNum)
  115. -- log("GameRecordController:setSumTableId", sumGameNum)
  116. self.m_sumGameNum = sumGameNum
  117. end
  118. function GameRecordController:setGameOverData(response)
  119. log("GameRecordController:setGameOverData", table.tostring(response))
  120. -- 记录我的当前积分,保持此轮总数据
  121. if type(response) ~= "table" then return end
  122. local userTurnMoneyMaps = {}
  123. for k,v in pairs(response.userCountList) do
  124. if v.nUserId == self.m_myUserId then
  125. self.m_totalMoney = v.nTotalMoney
  126. end
  127. userTurnMoneyMaps[v.nUserId] = v.nTurnMoney
  128. end
  129. self:writeRoundData()
  130. local data = {
  131. userTurnMoneyMaps = encodeJson(userTurnMoneyMaps),
  132. gameStartCount = self.m_gameStartCount,
  133. totalTime = self.m_totalTime,
  134. }
  135. self:writeSiteData(data)
  136. end
  137. -- 通過房間號和當前局數讀取數據
  138. function GameRecordController:readSiteData(beginTime, tableId)
  139. local totalRoundFileName = "totalRoundFileName"..beginTime..tableId
  140. local GameRecordSiteNum = loadUserXml("GameRecordSiteSumNum", totalRoundFileName)
  141. -- log("xiabo__s", GameRecordSiteNum)
  142. -- log("xiabo__s", beginTime)
  143. -- log("xiabo__s", tableId)
  144. GameRecordSiteDatas = {}
  145. if GameRecordSiteNum then
  146. GameRecordSiteNum = tonumber(GameRecordSiteNum)
  147. if GameRecordSiteNum and GameRecordSiteNum >= 1 then
  148. for i = 1, GameRecordSiteNum do
  149. local totalSiteData = loadUserXml("GameRecordSiteData"..i, totalRoundFileName)
  150. if type(totalSiteData) == "string" then
  151. -- log("xiabo", totalSiteData)
  152. totalSiteData = decodeJson(totalSiteData)
  153. end
  154. if type(totalSiteData) == "table" and next(totalSiteData) then
  155. -- log("xiabo", table.tostring(totalSiteData))
  156. -- log("xiabo", totalSiteData.userTurnMoneyMaps)
  157. if type(totalSiteData.userTurnMoneyMaps) == "string" then
  158. totalSiteData.userTurnMoneyMaps = decodeJson(totalSiteData.userTurnMoneyMaps)
  159. end
  160. -- log("xiabo", table.tostring( totalSiteData.userTurnMoneyMaps))
  161. table.insert(GameRecordSiteDatas, totalSiteData)
  162. end
  163. end
  164. end
  165. end
  166. -- log("GameRecordController:readSiteData data", table.tostring(GameRecordSiteDatas))
  167. return GameRecordSiteDatas
  168. end
  169. function GameRecordController:writeSiteData(data)
  170. log("GameRecordController:writeSiteData data", table.tostring(data))
  171. -- 先获取这一轮对应的文件名
  172. local beginTime = loadUserInfo("GameRoundLastBeginTime")
  173. local totalRoundFileName = "totalRoundFileName"..beginTime..self.m_totalTableId
  174. -- log("GameRecordController", totalRoundFileName)
  175. local GameRecordSiteNum = loadUserXml("GameRecordSiteSumNum", totalRoundFileName)
  176. -- log("GameRecordController", GameRecordSiteNum)
  177. GameRecordSiteNum = tonumber(GameRecordSiteNum or "") or 0
  178. local num = math.min(GameRecordSiteNum + 1, data.gameStartCount)
  179. saveUserXml("GameRecordSiteSumNum", num, totalRoundFileName)
  180. saveUserXml("GameRecordSiteData"..num, encodeJson(data), totalRoundFileName)
  181. -- log("GameRecordController:writeSiteData end")
  182. -- local data = self:readSiteData(beginTime, self.m_totalTableId)
  183. -- log("xiabo_data", table.tostring(data))
  184. end
  185. -- 每一场对应一个文件名,key值为beginTime和totalTableId,最近需要展示的数据对应的key值会保存在UserInfo中
  186. function GameRecordController:readRoundData()
  187. -- log("GameRecordController:readRoundData")
  188. -- 先獲取場次數據
  189. local GameRecordRoundNum = loadUserInfo("GameRecordRoundSumNum")
  190. -- log("xiabo_ss", GameRecordRoundNum)
  191. local GameRecordRoundDatas = {}
  192. if GameRecordRoundNum then
  193. GameRecordRoundNum = tonumber(GameRecordRoundNum)
  194. if GameRecordRoundNum and GameRecordRoundNum >= 1 then
  195. for i = 1, GameRecordRoundNum do
  196. -- log("xiabo", GameRecordRoundNum)
  197. local totalRoundData = loadUserInfo("GameRecordRoundData"..i)
  198. -- log("xiabo", type(totalRoundData))
  199. if type(totalRoundData) == "string" then
  200. -- log("xiabo",totalRoundData)
  201. totalRoundData = decodeJson(totalRoundData)
  202. end
  203. -- log("xiabo", table.tostring(totalRoundData))
  204. if type(totalRoundData) == "table" and next(totalRoundData) then
  205. totalRoundData.otherUserIds = decodeJson(totalRoundData.otherUserIds)
  206. totalRoundData.userNameMap = decodeJson(totalRoundData.userNameMap)
  207. end
  208. if totalRoundData and totalRoundData ~= "" then
  209. table.insert(GameRecordRoundDatas, totalRoundData)
  210. end
  211. end
  212. end
  213. end
  214. -- log("GameRecordController:readRoundData", table.tostring(GameRecordRoundDatas))
  215. return GameRecordRoundDatas
  216. end
  217. function GameRecordController:writeRoundData()
  218. local value = {
  219. beginTime = loadUserInfo("GameRoundLastBeginTime"),
  220. totalTableId = self.m_totalTableId,
  221. myUserId = self.m_myUserId,
  222. otherUserIds = encodeJson(self.m_otherUserIds),
  223. totalMoney = self.m_totalMoney,
  224. totalGameNum = self.m_totalGameNum,
  225. userNameMap = encodeJson(self.m_userNameMap),
  226. }
  227. log("GameRecordController:writeRoundData", table.tostring(value))
  228. -- 先獲取場次數據
  229. local GameRecordRoundDatas = self:readRoundData()
  230. local haveThisData = false
  231. for k, v in pairs(GameRecordRoundDatas) do
  232. v.otherUserIds = encodeJson(v.otherUserIds)
  233. v.userNameMap = encodeJson(v.userNameMap)
  234. -- 如果有开始时间和桌数相同的,判断为同一轮
  235. if v.beginTime == value.beginTime then
  236. haveThisData = true
  237. GameRecordRoundDatas[k] = value
  238. end
  239. end
  240. if not haveThisData then table.insert(GameRecordRoundDatas, value) end
  241. -- 排序,时间进的放前面,如果大于最高数,干掉后面的
  242. local sortFun = function(list1, list2)
  243. return tonumber(list1.beginTime) > tonumber(list2.beginTime)
  244. end
  245. table.sort(GameRecordRoundDatas, sortFun)
  246. while #GameRecordRoundDatas > GameRecordController.k_max_record_num do
  247. -- 从列表中删除旧数据,并且刪除对应的XML文件
  248. local removeData = table.remove(GameRecordRoundDatas)
  249. local removeBeginTime = removeData.beginTime
  250. local removeTotalTableId = removeData.totalTableId
  251. local removeFileName = "totalRoundFileName"..removeBeginTime..removeTotalTableId
  252. deleteXmlFile(removeFileName)
  253. end
  254. -- log("GameRecordRoundSumNum", #GameRecordRoundDatas)
  255. -- log("GameRecordRoundSumNum", table.tostring(GameRecordRoundDatas))
  256. saveUserInfo("GameRecordRoundSumNum", #GameRecordRoundDatas)
  257. for k, v in pairs(GameRecordRoundDatas) do
  258. saveUserInfo("GameRecordRoundData" .. k, encodeJson(v))
  259. end
  260. end