--GameAppBase 读的这个 require("luaScript.Protocol.ProtocolCommon") require("luaScript.Protocol.Config.ProtocolConfigCmd") require("luaScript.Protocol.Config.ProtocolConfigMessage") local ServerConfigs = class("ServerConfigs", require("luaScript.Protocol.Protocol")) function ServerConfigs:ctor(net) ServerConfigs.super.ctor(self , net, ModuleId); -- php 通道相关 self.phpIndex = 0 self.phpCallbackList = {}; self.isGetGameVerList = false -- 是否已经初始化完成 self._isInited = false --是否是改变密码 self._changePW = false -- 大厅的配置信息 self.notice = {}; --{"",""} self.scroll = ""; -- self.areano = ""; -- -- 子游戏的房卡消耗 self.subGameCosts = {} -- 子游戏的版本信息 self.subGameVersions = {} -- 从gameServer获取的游戏列表 self.configs = {} --活动配置 self.missions = {} -- 从php获取的游戏列表 self.subGameList = {} self.webGameList = table.loadFromLocFile("WebGameList.lua") or {} self.clientConfig = table.loadFromLocFile("ClientConfig.lua") or { myHead = "http://cnplayer.ddpenghu.com/client/user/loginpost?wechatid=8&wechatmpid=8", -- 我的装扮 webgamb = RomSetting.ZhanJiUrl, -- 网页战绩 } -- 服务器下发大厅配置 self:defPushMsg{cmd = ConfigCmd.GAME_COMMAND_SEND_SERVER_CONF, reader = ServerHallConfigResponse, func = handler(self , self.onServerHallConfigResponse)}; -- 服务器下发子游戏配置 self:defPushMsg{cmd = ConfigCmd.GAME_COMMAND_SEND_GAME_CONF, reader = ServerGamesConfigResponse, func = handler(self , self.onServerGamesConfigResponse)}; -- PHP 通道 self:defPushMsg{cmd = ConfigCmd.GAME_COMMAND_PHP, reader = StringPacket, func = handler(self , self.onPhpMsgResponse)}; end -- 初始化 function ServerConfigs:init() logD("ServerConfigs:init()") self.isGetHallConfigSuccess = false self.isGetGamesConfigSuccess = false if not self._isInited then --发送301,返回303 ,304 self:requestGetServerConfig() else self:initSuccessed() end end -- 初始化完成 function ServerConfigs:initSuccessed() logD("ServerConfigs:initSuccessed()") if self._changePW then --是否是修改密码 logD("ServerConfigs:initSuccessed changePW()") app.user:dispatchEvent({name = "changePW"}) self._changePW = false else self:dispatchEvent({name = "initSuccessed"}) end end -- 获取子游戏列表 -- @return {1,2,3,4,5,6,7,8} function ServerConfigs:getSubGameIds() local subGameIds = {} for gameId, v in pairs(self.subGameCosts) do table.insert(subGameIds, gameId) end table.sort(subGameIds) return subGameIds end -- 获取某个子游戏的房卡消耗数据 function ServerConfigs:getSubGameCostInfo(gameId) gameId = tonumber(gameId) if gameId and gameId > 0 then return self.subGameCosts[gameId] else return nil end end -- 获取某个子游戏的版本信息 function ServerConfigs:getSubGameVersionInfo(gameId) gameId = tonumber(gameId) if gameId and gameId > 0 then return self.subGameVersions[gameId] else return nil end end -- 获取某个子游戏的服务器版本 function ServerConfigs:getSubGameVersionNum(gameId) local versionInfo = self:getSubGameVersionInfo(gameId) if versionInfo and versionInfo.ver then return tonumber(versionInfo.ver) else return -1 end end -- 是否正在改变密码的情况 function ServerConfigs:getChangePWState() return self._changePW end function ServerConfigs:setChangePWState(bChangePW) self._changePW = bChangePW end --------------------------------------------------------------------------- -------------------------------- PHP 通道 -------------------------------- --------------------------------------------------------------------------- -- ttInfo : 传送给PHP的数据 -- callback : 处理后的回调 function ServerConfigs:sendPhpMsg(ttInfo, callback) if not ttInfo or type(ttInfo) ~= "table" then return end -- 索引++ self.phpIndex = self.phpIndex + 1; -- 记录回调索引 local callbackKey = self.phpIndex .. "_" .. (ttInfo.pSSq or "0") self.phpCallbackList[callbackKey] = callback -- 改变发往服务器的索引 ttInfo.pSSq = callbackKey local strContent = json.encode(ttInfo) -- 发送消息到服务器 local request = StringPacket:new() request.stringValue = strContent; logD("ServerConfigs:sendPhpMsg() request = " .. table.tostring(request)) self:sendResponse{cmd = ConfigCmd.GAME_COMMAND_PHP , sender = request}; end function ServerConfigs:onPhpMsgResponse(status, response) logD("ServerConfigs:onPhpMsgResponse()", tostring(status), table.tostring(response)) if status ~= 0 then return end local ttResult = json.decode(response.stringValue) if not ttResult then return end local index = tostring(ttResult.pSSq) if not index then return end if not self.phpCallbackList[index] then return end self.phpCallbackList[index](0, ttResult); self.phpCallbackList[index] = nil; end --------------------------------------------------------------------------- -------------------------------- 配置相关 -------------------------------- --------------------------------------------------------------------------- -- 获取配置信息 function ServerConfigs:requestGetServerConfig() local request = ServerConfigRequest:new() request.appId = getAppId() log("ServerConfigs:requestGetServerConfig() request = ", table.tostring(request)) self:sendResponse{cmd = ConfigCmd.GAME_COMMAND_GET_SERVER_CONF , sender = request}; end -- 服务器下发大厅配置 function ServerConfigs:onServerHallConfigResponse(status, response) logD("ServerConfigs:ServerHallConfigResponse()", tostring(status), table.tostring(response)) local ttResult = json.decode(response.strContent) -- 公告信息 self.notice = {} local notice = tostring(ttResult.notice) or "" if notice ~= "" then local arr = string.split(notice, "\n") for k,v in pairs(arr) do table.insert(self.notice, v) end end -- 滚动公告 self.scroll = tostring(ttResult.scroll) or "" -- 游戏ID self.gameId = tonumber(ttResult.gameId) or 0 -- 区域信息 self.areano = tonumber(ttResult.areano) or 0 -- 记录获取大厅配置成功 self.isGetHallConfigSuccess = true self:onGetServerConfigSuccessed(); end -- 服务器下发子游戏配置 function ServerConfigs:onServerGamesConfigResponse(status, response) log("ServerConfigs:onServerGamesConfigResponse()", tostring(status), table.tostring(response)) self.configs = {} self.subGameCosts = {} for _, value in pairs(response.gameServerConfigList) do local costConfig = json.decode(value) if costConfig and type(costConfig) == "table" and costConfig.gameid then --[[ -- 基本属性 costConfig = { gameid = 18, --游戏ID isFree = 1, --免费标签 gameRound = "4,6,8", --支持的局数 requireCards = "0,0,0", --房主支付消耗的房卡数量 AArequireCards = "0,0,0", --AA支付消耗的房卡数量 } --]] --连打房间解散时间,黄十八用 if costConfig.contiTimer then app.user.contiTimer = costConfig.contiTimer end self.configs[tonumber(costConfig.gameid)] = costConfig; --[[ -- 转换成 costInfo = { {round = 4, cost = 0, aaCost = 0}, {round = 4, cost = 0, aaCost = 0}, {round = 4, cost = 0, aaCost = 0}, } --]] local costInfo = {} local Rounds = {} local Costs = {} local AACosts = {} if costConfig.gameRound then Rounds = string.split(costConfig.gameRound, ",") end if costConfig.requireCards then Costs = string.split(costConfig.requireCards, ",") end if costConfig.AArequireCards then AACosts = string.split(costConfig.AArequireCards, ",") end for idx,round in pairs(Rounds) do if tonumber(round) == 24 and tonumber(costConfig.gameid) == 2 then else table.insert(costInfo, {round = tonumber(round), cost = tonumber(Costs[idx]), aaCost = tonumber(AACosts[idx])}) end end self.subGameCosts[tonumber(costConfig.gameid)] = costInfo end end self.isGetGamesConfigSuccess = true; self:onGetServerConfigSuccessed(); end -- 获取配置完成后的处理 function ServerConfigs:onGetServerConfigSuccessed() if self.isGetHallConfigSuccess and self.isGetGamesConfigSuccess then -- 初始化子游戏配置 if app.subGameConfigManager then app.subGameConfigManager:init() end -- 初始化子游戏版本信息 self:initSubGameVersions(); -- 初始化地区对应的游戏列表 self:initSubGameList(); self:initSuccessed() end end --------------------------------------------------------------------------- -------------------------------- 子游戏版本 ------------------------------- --------------------------------------------------------------------------- -- 初始化子游戏版本信息 function ServerConfigs:initSubGameVersions() -- 首先从本地缓存中加载上一次的配置 self:loadSubGameVersionsFromFile() -- 然后从服务器拉取最新的数据 self:requestGetSubGameVersions() end function ServerConfigs:loadSubGameVersionsFromFile() self.subGameVersions = table.loadFromLocFile("SubGameVersions.lua") or {} logD("ServerConfigs:loadSubGameVersionsFromFile()", table.tostring(self.subGameVersions)) end function ServerConfigs:saveSubGameVersionsToFile() logD("ServerConfigs:saveSubGameVersionsToFile()", table.tostring(self.subGameVersions)) table.saveToLocFile(self.subGameVersions, "SubGameVersions.lua") end -- 获取子游戏版本信息 function ServerConfigs:requestGetSubGameVersions() local params = { action = "system.games", app = getAppId(), uid = loadUserId(), apkver = "",-- 获取子游戏版本号时不再依赖APK的版本号 - zzb,20190715 installGameVer = "", isNew =1, } for gameId,v in pairs(self.subGameCosts) do local version = app.subGameManager:getSubGameVersionNum(gameId) params.installGameVer = params.installGameVer..gameId..","..version.."|" end --基础包 local baseIds={1001,1002,1003} for k,gameId in pairs(baseIds) do local version = app.subGameManager:getSubGameVersionNum(gameId) params.installGameVer = params.installGameVer..gameId..","..version.."|" end logD("ServerConfigs:requestGetSubGameVersions() ", table.tostring(params)); self:sendPhpMsg(params, handler(self, self.onGetSubGameVersionsResponse)) end function ServerConfigs:onGetSubGameVersionsResponse(status, ttResult) logD("ServerConfigs:onGetSubGameVersionsResponse() " .. table.tostring(ttResult)) self.getsubGameList = true if ttResult and tonumber(ttResult.code) == 200 and ttResult.result and type(ttResult.result) == "table" then if ttResult.result.new and type(ttResult.result.new) == "table" then for gameId,v in pairs(ttResult.result.new) do local gameId = tonumber(gameId) self.subGameVersions[gameId] = v end end -- 将最新的数据写入到本地缓存 self:saveSubGameVersionsToFile(); end end --------------------------------------------------------------------------- -------------------------------- 子游戏列表 ------------------------------- --------------------------------------------------------------------------- -- 获取指定子游戏的配置信息 -- 可能返回 nil function ServerConfigs:getSubGameConfig(gameId) if not gameId then return nil end return self.subGameList[gameId] end -- 获取指定web子游戏的配置信息 -- 可能返回 nil function ServerConfigs:getWebGameConfig(gameId) for i,v in pairs(self.webGameList) do if v.gameId == gameId then return v end end return nil end function ServerConfigs:requestClientConfig(callback) logD("ServerConfigs:requestClientConfig()") local params = { action = "system.clicfg", app = getAppId(), uid = loadUserId(), } logD("ServerConfigs:requestClientConfig() ", table.tostring(params)); app.waitDialogManager:showWaitNetworkDialog("请求配置...") self:sendPhpMsg(params, function(status,response) app.waitDialogManager:closeWaitNetworkDialog() logD("ServerConfigs:requestClientConfigResponse() ", table.tostring(response)); if tonumber(response.code)==200 then if response.result and table.nums(response.result)>0 then self.clientConfig = response.result table.saveToLocFile(self.clientConfig, "ClientConfig.lua") app.config.Setting.appDownloadUrl = self.clientConfig.download math.randomseed(tostring(os.time()):reverse():sub(1, 6)); local wx = response.result.wx or {}; if table.nums(wx) > 0 then local idx = math.random(table.nums(wx)) if wx[idx] then app.plugin:initWxShare(wx[idx].appid, wx[idx].secret); else app.plugin:initWxShare(wx.appid, wx.secret); end else if app.plugin and wx.appid and wx.secret then app.plugin:initWxShare(wx.appid, wx.secret); end end end end if callback then callback() end app:dispatchEvent({name = "onRequestClientConfig"}) end) end function ServerConfigs:requestWebGameUrl(gameId,callback) local function getOrientation (url) local orientation = 1; local index = string.find(url,"?") if index then local params=string.sub(url,index+1,string.len(url)) local tmp = string.split(params,"&"); local args = {}; for k, v in pairs(tmp) do local arr = string.split(v, "="); if arr[1] == "orientation" then orientation = arr[2] or 1; break; end end end return orientation; end local params = { action = "platform.geturl", app = getAppId(), uid = loadUserId(), tinyid = gameId, } logD("ProtocolSubGame:requestWebGameList() ", table.tostring(params)); self:sendPhpMsg(params,function (status,response) dump(response) if tonumber(response.code)==200 then local orientation = getOrientation(response.result); if callback then callback(response.result, tonumber(orientation)) end else if callback then callback() end end end) end function ServerConfigs:getGameGroupConfig(gameId, regionCode) regionCode = regionCode or 0 local subGameList = self.regionGameList[regionCode] or self.subGameList if subGameList[gameId] then local strGameConfig = subGameList[gameId].gameRule if strGameConfig and strGameConfig~="" then local gameConfig = json.decode(strGameConfig) if gameConfig and gameConfig.group and gameConfig.group.games then return gameConfig.group end end end return nil end function ServerConfigs:getWebGameGroupConfig(gameId) for i,v in pairs(self.webGameList) do if v.gameId == gameId then local strGameConfig = v.gamecfg if strGameConfig and strGameConfig~="" then local gameConfig = json.decode(strGameConfig) if gameConfig and gameConfig.group then return gameConfig.group end end end end return nil end --- -- 子游戏是否在合集里面 -- @return boolean -- function ServerConfigs:isWebGameInGroup (gameId) local isExist = false for i,v in pairs(self.webGameList) do local groupConfig = self:getWebGameGroupConfig(v.gameId) or {} local games = groupConfig.games or {} for _, id in ipairs(games) do if tonumber(id) == tonumber(gameId) then isExist = true break end end if isExist then break end end return isExist end function ServerConfigs:isNewGame(gameId) if self.subGameList[gameId] then local strGameConfig = self.subGameList[gameId].gameRule if strGameConfig and strGameConfig~="" then local gameConfig = json.decode(strGameConfig) if gameConfig and gameConfig.isNew then return gameConfig.isNew end end end return false end --- -- 获取子游戏列表 -- @return -- function ServerConfigs:getSubGameList() return self.subGameList end --- -- 获取子游戏列表(没有子游戏具体信息) -- @return -- function ServerConfigs:getGameList(regionCode) regionCode = regionCode or 0 local gameList = {} local baseIds={ [1001]=true, [1002]=true, [1003]=true } if isIOSReviewVersion() then -- 添加更多游戏的显示 local gameInfo = { gameId = 0; gameName = "更多游戏"; sortIndex = 9999; show = true; visible = true; } self.subGameList[gameInfo.gameId] = gameInfo end local subGameList = self.regionGameList[regionCode] or self.subGameList local tt = {} for k,v in pairs(subGameList) do if v.visible and v.showInHall == 1 then if v.other_name and v.other_name ~= "" then if not tt[v.other_name] then tt[v.other_name] = {} end table.insert(tt[v.other_name],{gameId = v.gameId, sort = v.sortIndex ,gameType = "sub"}) elseif v.gameId and not baseIds[v.gameId] then table.insert(gameList,{gameId = v.gameId, sort = v.sortIndex ,gameType = "sub"}) end end end for k,v in pairs(tt) do --求最小排序 table.sort(v, function (a,b) return a.sort0 then local config = self.popConfigs[1] table.remove(self.popConfigs,1) return config end return nil end function ServerConfigs:initPopConfig() if self.popConfigs then return end --弹出配置 if cc.Application:getInstance():getTargetPlatform() == 0 then -- self.popConfigs = {"luaScript.Views.Main.HongBaoKa.HongBaoKaView", "luaScript.Views.Activity.ActivityMainView"} self.popConfigs = {} else local popWindows = PhpSetting["pop_window"] or {} table.sort(popWindows, function (a, b) return a.index < b.index end) local configs = {} for _, v in ipairs(popWindows or {}) do if v then table.insert(configs, v.view) end end self.popConfigs = configs; end end -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- function ServerConfigs:initSubGameList() -- 所有的游戏 self.subGameList = {} -- 各区域对应的游戏列表 self.regionGameList = {} -- 先充本地缓存中读取数据 self:loadSubGameListFromFile(); -- 再从服务器获取最新的数据 self:requestGameList(0, function () -- 再根据当前选择地区,再拉取对应地区的游戏列表 local regionCode = cc.UserDefault:getInstance():getIntegerForKey("address_code_" .. app.config.RomSetting.Platform) self:requestGameList(regionCode) end) end function ServerConfigs:saveSubGameListToFile() logD("ServerConfigs:saveSubGameListToFile()", table.tostring(self.subGameList)) table.saveToLocFile(self.subGameList, "SubGameList.lua") end function ServerConfigs:loadSubGameListFromFile() self.subGameList = table.loadFromLocFile("SubGameList.lua") or {} logD("ServerConfigs:loadSubGameListFromFile()", table.tostring(self.subGameList)) end function ServerConfigs:requestGameList(regionCode, isFirstRequest, callback) -- 保存所有地区的游戏列表数据 self.regionGameList = self.regionGameList or {} -- 默认获取全国的数据 regionCode = regionCode or 0 if self.regionGameList[regionCode] then -- 如果已经请求过该地区的数据,则不再请求游戏列表 self:dispatchEvent({name = "getSubGameListSuccessed"}) app.waitDialogManager:closeWaitNetworkDialog() return end local params = { action = "system.uniongameinfo", app = getAppId(), uid = loadUserId(), region = regionCode, } logD("ServerConfigs:requestGameList(), params = ", table.tostring(params)) self:sendPhpMsg(params, function(status, response) logD("ServerConfigs:requestGameList(), response = ", table.tostring(response)) app.waitDialogManager:closeWaitNetworkDialog() if tonumber(response.code) == 200 then local subGameList = {} if response.result.game then for k,v in pairs(response.result.game) do local gameId = tonumber(k) local gamecfg = v.gamecfg local gamecfgData = json.decode(gamecfg) local gameInfo = {} gameInfo.gameId = gameId gameInfo.gameName = tostring(v.name) gameInfo.gameIcon = tostring(v.icon) gameInfo.gameRule = tostring(v.gamecfg) gameInfo.sortIndex = tonumber(v.sort) gameInfo.other_name = tostring(v.other_name) gameInfo.show = true --游戏列表的显示逻辑用visible,创建房间的逻辑用show if gamecfgData then if gamecfgData.group and gamecfgData.group.show == "0" then gameInfo.visible = false else gameInfo.visible = true end if not gamecfgData.showInHall then gameInfo.showInHall = 1 else gameInfo.showInHall = tonumber(gamecfgData.showInHall) end else gameInfo.visible = true gameInfo.showInHall = 1 end subGameList[gameId] = gameInfo end if regionCode == 0 then self.subGameList = subGameList; self:saveSubGameListToFile() end end -- 记录已经拉取过的地区游戏列表 self.regionGameList[regionCode] = subGameList self.webGameList = {} if response.result.tiny then for gameId,v in pairs(response.result.tiny) do v.gameId = tonumber(gameId) local gamecfgData = json.decode(v.gamecfg) if gamecfgData then local uids = gamecfgData.uids local uid = app.user.loginInfo.uid if uids then for _, vv in pairs(uids) do if tonumber(vv) == uid then table.insert(self.webGameList, v) break end end else table.insert(self.webGameList, v) end else table.insert(self.webGameList, v) end end table.saveToLocFile(self.webGameList, "WebGameList.lua") end self:dispatchEvent({name = "getSubGameListSuccessed"}) end end) end return ServerConfigs