local PKDef = require("pk_base.luaScript.PKDef") -- 解散界面 local RoomStartWaitView = class("RoomStartWaitView", cc.UIView) local stateColor = { agreeColor = cc.c4b(16,160,0,255), noAgreeColor = cc.c4b(219,56,35,255) } local function getUserNameById(userId) local userInfo = app.room:getUserInfo(userId) userInfo = type(userInfo) == "string" and json.decode(userInfo) or userInfo if not userInfo then return end local userName = getSubStringNickname(userInfo.nickname) or userId return userName end function RoomStartWaitView:ctor(response,callback) RoomStartWaitView.super.ctor(self); self.nleftStartGameTimeout = response.nleftStartGameTimeout self.response = response self.callback = callback end function RoomStartWaitView:onEnter() RoomStartWaitView.super.onEnter(self) local ui = loadUI("pk_base/res/ui/ui_fangjian/pk_ui_speedStart.ui"); self.ui = ui; self:addChild(ui); -- 同意 self.ui.Items.Button_1:registerClick(handler(self , self.onClickAgree)) -- 拒绝 self.ui.Items.Button_2:registerClick(handler(self , self.onClickRefuse)) self.ui.Items.Layout_Player_Item:setVisible(false) local layout = self.ui.Items.Layout_Player self.sateList = {} layout:removeAllChildren() for k,v in pairs(app.room.roomInfo.memberList) do local itemEx = self.ui.Items.Layout_Player_Item:getCopied() itemEx.Items = getUIItems(itemEx) local userInfo = json.decode(v.userInfo) if userInfo then -- 玩家名字 itemEx.Items.Text_name:setText(getSubStringNickname(userInfo.nickname)) --头像 setPlayerHeadImage(k, userInfo.headimgurl, itemEx.Items.ImageView_head) end self.sateList[app.room:getViewIdByUserId(k)] = itemEx.Items.Text_state layout:addChild(itemEx) end --默认时间总为60 local timer = self.nleftStartGameTimeout local totalTime = 60 --进度条 --[[ self.ui.Items.LoadingBar:setPercent(timer/totalTime * 100) self.loadingBarTimer = cc.Director:getInstance():getScheduler():scheduleScriptFunc(function() timer = timer - 1 if self.ui.Items.LoadingBar then self.ui.Items.LoadingBar:setPercent(timer/totalTime * 100) end if timer == 0 then self:cleanup() end end,1.0,false)--]] self.ui.Items.Text_3:setText(tostring(timer)) self.loadingBarTimer = cc.Director:getInstance():getScheduler():scheduleScriptFunc(function() timer = timer - 1 if self.ui.Items.Text_3 then self.ui.Items.Text_3:setText(tostring(timer)) end if timer == 0 then self:cleanup() end end,1.0,false) layout:requestDoLayout() layout:doLayout() self:updateState(self.response) local happenUserName = getUserNameById(self.happenUserId) self.ui.Items.Text_1:setText("【" .. happenUserName .. "】申请立刻开局,是否同意?") end function RoomStartWaitView:initData(response) self.choiceData = {} for k,v in pairs(response) do local index = app.room:getViewIdByUserId(v.uid) if v.optType == PKDef.SPEED_START_GAME.START_OPT_TYPE_INIT or v.optType == PKDef.SPEED_START_GAME.START_OPT_TYPE_INITEX then self.choiceData[index] = { isChoice = false, isAgree = false } elseif v.optType == PKDef.SPEED_START_GAME.START_OPT_TYPE_REQ then self.happenUserId = v.uid self.choiceData[index] = { isChoice = true, isAgree = true } elseif v.optType == PKDef.SPEED_START_GAME.START_OPT_TYPE_AGREE then self.choiceData[index] = { isChoice = true, isAgree = true } elseif v.optType == PKDef.SPEED_START_GAME.START_OPT_TYPE_DISAGREE then self.choiceData[index] = { isChoice = true, isAgree = false } end end end function RoomStartWaitView:updateState(response,callTip) self.callTip = callTip self:initData(response.playerList) local myUserId = app.room.roomInfo.nUserId local myViewId = app.room:getViewIdByUserId(myUserId) if self.choiceData[myViewId].isChoice or self.happenUserId == myUserId then -- 我已经选择过了或者我是发起申请的那个 self.ui.Items.Button_1:setVisible(false) self.ui.Items.Button_2:setVisible(false) else -- 我还没选择 self.ui.Items.Button_1:setVisible(true) self.ui.Items.Button_2:setVisible(true) end local index = 0; local isNoAgree = false for viewId,v in pairs(self.choiceData) do if v.isAgree then self.sateList[viewId]:setTextColor(stateColor.agreeColor) self.sateList[viewId]:setText("已同意") else if v.isChoice then self.sateList[viewId]:setTextColor(stateColor.agreeColor) self.sateList[viewId]:setText("已拒绝") isNoAgree = true if self.callTip then local nUserId = app.room:getUserIdByViewId(viewId) local name = getUserNameById(nUserId) local txt = "玩家".. name .."未同意立刻开局,建议等待其他小伙伴再开局哦。" self.callTip(txt) end end end if v.isChoice then index = index + 1 end end if index >= app.room:getCurMaxPlayer() or isNoAgree then self:cleanup() end end -- 同意 function RoomStartWaitView:onClickAgree() app.room:sendSpeedStartCmd(PKDef.SPEED_START_GAME.START_OPT_TYPE_AGREE) end -- 拒绝 function RoomStartWaitView:onClickRefuse() app.room:sendSpeedStartCmd(PKDef.SPEED_START_GAME.START_OPT_TYPE_DISAGREE) end -- 获取我的选择 function RoomStartWaitView:cleanup() self:runDelay(0.01, function() self:removeView(); end ) end function RoomStartWaitView:removeView() if self.loadingBarTimer then cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.loadingBarTimer) end if self.callback then self.callback() end self:removeFromParent() end -- function RoomStartWaitView:onExit() if self.loadingBarTimer then cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.loadingBarTimer) end end return RoomStartWaitView;