-- 用来记录比赛流程,适用用比赛重播 -- 用单例模式会好点,直接用GameRecordController.getInstance() 而不要用 new -- round, 轮,site,局,userInfo中保留每轮合计数据,每轮数据可获得每轮的详细数据文件名,包含此轮每局的数据 GameRecordController = class("GameRecordController") GameRecordController.k_max_record_num = 10 -- 深度封装,解析json local function encodeJson(tables) if type(tables) ~= "table" then return tables end local tt = {} for k, v in pairs(tables) do if type(v) == "table" then tt[k] = encodeJson(v) else tt[k] = v end -- 数字开始key调用json.encode会有问题,所以加入了一个前缀 if type(k) == "number" then tt["_ddnum_"..k] = tt[k] tt[k] = nil end end return json.encode(tt) end local function decodeJson(string) if type(string) ~= "string" then return string end local map = json.decode(string) local tt = {} if type(map) == "table" then for k, v in pairs(map) do local value = v if string.find(v, "{") then value = decodeJson(v) end if string.find(k, "_ddnum_") then local str = string.gsub(k, "_ddnum_", "") tt[tonumber(str)] = value else if value == {} then tt[k] = v else tt[k] = value end end end end return tt end function GameRecordController.getInstance() if not GameRecordController.m_instance then GameRecordController.m_instance = GameRecordController:new() end return GameRecordController.m_instance end -- 开启记录模式 function GameRecordController:ctor() end -- 中途加入的以加入房间的时间为准 function GameRecordController:setMyUserId(uid, userInfo) -- log("GameRecordController:setMyUserId", uid, table.tostring(userInfo)) self.m_myUserId = uid self.m_userNameMap = self.m_userNameMap or {} self.m_userNameMap[uid] = userInfo.nickname end function GameRecordController:setBeginTime() saveUserInfo("GameRoundLastBeginTime", os.time()) end function GameRecordController:addOtherUserId(uid, userInfo) -- log("GameRecordController:addOtherUserId", uid, table.tostring(userInfo)) self.m_otherUserIds = self.m_otherUserIds or {} if uid and uid ~= self.m_myUserId then local haveThisUid = false for _, id in pairs(self.m_otherUserIds) do if id == uid then haveThisUid = true end end if not haveThisUid then table.insert(self.m_otherUserIds, uid) end self.m_userNameMap = self.m_userNameMap or {} self.m_userNameMap[uid] = userInfo.nickname end end -- 获取游戏开局时间 function GameRecordController:getBeginTime() local beginTime = loadUserInfo("GameRoundLastBeginTime") return beginTime end -- 获取当前房间号 function GameRecordController:getTotalTableId() return self.m_totalTableId end -- 每局的开始时间 function GameRecordController:setTotalTime(time) -- log("GameRecordController:setTotalTime", time) self.m_totalTime = time end -- 记录当前房间号,只有当玩家进入房间成功以及重连回来时才会记录房间号 function GameRecordController:setTotalTableId(totalTableId) -- log("GameRecordController:setTotalTableId", totalTableId) self.m_totalTableId = totalTableId end -- 记录当前局数和总局数 function GameRecordController:setGameNum(gameStartCount, totalGameNum) -- log("GameRecordController:setGameNum", gameStartCount, totalGameNum) self.m_gameStartCount = gameStartCount self.m_totalGameNum = totalGameNum if self.m_gameStartCount == 1 then if self.m_totalTime then saveUserInfo("GameRoundLastBeginTime", self.m_totalTime) end end end function GameRecordController:setSumGameNum(sumGameNum) -- log("GameRecordController:setSumTableId", sumGameNum) self.m_sumGameNum = sumGameNum end function GameRecordController:setGameOverData(response) log("GameRecordController:setGameOverData", table.tostring(response)) -- 记录我的当前积分,保持此轮总数据 if type(response) ~= "table" then return end local userTurnMoneyMaps = {} for k,v in pairs(response.userCountList) do if v.nUserId == self.m_myUserId then self.m_totalMoney = v.nTotalMoney end userTurnMoneyMaps[v.nUserId] = v.nTurnMoney end self:writeRoundData() local data = { userTurnMoneyMaps = encodeJson(userTurnMoneyMaps), gameStartCount = self.m_gameStartCount, totalTime = self.m_totalTime, } self:writeSiteData(data) end -- 通過房間號和當前局數讀取數據 function GameRecordController:readSiteData(beginTime, tableId) local totalRoundFileName = "totalRoundFileName"..beginTime..tableId local GameRecordSiteNum = loadUserXml("GameRecordSiteSumNum", totalRoundFileName) -- log("xiabo__s", GameRecordSiteNum) -- log("xiabo__s", beginTime) -- log("xiabo__s", tableId) GameRecordSiteDatas = {} if GameRecordSiteNum then GameRecordSiteNum = tonumber(GameRecordSiteNum) if GameRecordSiteNum and GameRecordSiteNum >= 1 then for i = 1, GameRecordSiteNum do local totalSiteData = loadUserXml("GameRecordSiteData"..i, totalRoundFileName) if type(totalSiteData) == "string" then -- log("xiabo", totalSiteData) totalSiteData = decodeJson(totalSiteData) end if type(totalSiteData) == "table" and next(totalSiteData) then -- log("xiabo", table.tostring(totalSiteData)) -- log("xiabo", totalSiteData.userTurnMoneyMaps) if type(totalSiteData.userTurnMoneyMaps) == "string" then totalSiteData.userTurnMoneyMaps = decodeJson(totalSiteData.userTurnMoneyMaps) end -- log("xiabo", table.tostring( totalSiteData.userTurnMoneyMaps)) table.insert(GameRecordSiteDatas, totalSiteData) end end end end -- log("GameRecordController:readSiteData data", table.tostring(GameRecordSiteDatas)) return GameRecordSiteDatas end function GameRecordController:writeSiteData(data) log("GameRecordController:writeSiteData data", table.tostring(data)) -- 先获取这一轮对应的文件名 local beginTime = loadUserInfo("GameRoundLastBeginTime") local totalRoundFileName = "totalRoundFileName"..beginTime..self.m_totalTableId -- log("GameRecordController", totalRoundFileName) local GameRecordSiteNum = loadUserXml("GameRecordSiteSumNum", totalRoundFileName) -- log("GameRecordController", GameRecordSiteNum) GameRecordSiteNum = tonumber(GameRecordSiteNum or "") or 0 local num = math.min(GameRecordSiteNum + 1, data.gameStartCount) saveUserXml("GameRecordSiteSumNum", num, totalRoundFileName) saveUserXml("GameRecordSiteData"..num, encodeJson(data), totalRoundFileName) -- log("GameRecordController:writeSiteData end") -- local data = self:readSiteData(beginTime, self.m_totalTableId) -- log("xiabo_data", table.tostring(data)) end -- 每一场对应一个文件名,key值为beginTime和totalTableId,最近需要展示的数据对应的key值会保存在UserInfo中 function GameRecordController:readRoundData() -- log("GameRecordController:readRoundData") -- 先獲取場次數據 local GameRecordRoundNum = loadUserInfo("GameRecordRoundSumNum") -- log("xiabo_ss", GameRecordRoundNum) local GameRecordRoundDatas = {} if GameRecordRoundNum then GameRecordRoundNum = tonumber(GameRecordRoundNum) if GameRecordRoundNum and GameRecordRoundNum >= 1 then for i = 1, GameRecordRoundNum do -- log("xiabo", GameRecordRoundNum) local totalRoundData = loadUserInfo("GameRecordRoundData"..i) -- log("xiabo", type(totalRoundData)) if type(totalRoundData) == "string" then -- log("xiabo",totalRoundData) totalRoundData = decodeJson(totalRoundData) end -- log("xiabo", table.tostring(totalRoundData)) if type(totalRoundData) == "table" and next(totalRoundData) then totalRoundData.otherUserIds = decodeJson(totalRoundData.otherUserIds) totalRoundData.userNameMap = decodeJson(totalRoundData.userNameMap) end if totalRoundData and totalRoundData ~= "" then table.insert(GameRecordRoundDatas, totalRoundData) end end end end -- log("GameRecordController:readRoundData", table.tostring(GameRecordRoundDatas)) return GameRecordRoundDatas end function GameRecordController:writeRoundData() local value = { beginTime = loadUserInfo("GameRoundLastBeginTime"), totalTableId = self.m_totalTableId, myUserId = self.m_myUserId, otherUserIds = encodeJson(self.m_otherUserIds), totalMoney = self.m_totalMoney, totalGameNum = self.m_totalGameNum, userNameMap = encodeJson(self.m_userNameMap), } log("GameRecordController:writeRoundData", table.tostring(value)) -- 先獲取場次數據 local GameRecordRoundDatas = self:readRoundData() local haveThisData = false for k, v in pairs(GameRecordRoundDatas) do v.otherUserIds = encodeJson(v.otherUserIds) v.userNameMap = encodeJson(v.userNameMap) -- 如果有开始时间和桌数相同的,判断为同一轮 if v.beginTime == value.beginTime then haveThisData = true GameRecordRoundDatas[k] = value end end if not haveThisData then table.insert(GameRecordRoundDatas, value) end -- 排序,时间进的放前面,如果大于最高数,干掉后面的 local sortFun = function(list1, list2) return tonumber(list1.beginTime) > tonumber(list2.beginTime) end table.sort(GameRecordRoundDatas, sortFun) while #GameRecordRoundDatas > GameRecordController.k_max_record_num do -- 从列表中删除旧数据,并且刪除对应的XML文件 local removeData = table.remove(GameRecordRoundDatas) local removeBeginTime = removeData.beginTime local removeTotalTableId = removeData.totalTableId local removeFileName = "totalRoundFileName"..removeBeginTime..removeTotalTableId deleteXmlFile(removeFileName) end -- log("GameRecordRoundSumNum", #GameRecordRoundDatas) -- log("GameRecordRoundSumNum", table.tostring(GameRecordRoundDatas)) saveUserInfo("GameRecordRoundSumNum", #GameRecordRoundDatas) for k, v in pairs(GameRecordRoundDatas) do saveUserInfo("GameRecordRoundData" .. k, encodeJson(v)) end end