--[[ 军团驻地答题活动 ]] local UIGuildActivityExam = UIViewClass() local OptionChar = {'A', 'B', 'C', 'D'} local FirstScore = DataSheetManager.ConstData:GetDataByID("GuildPartyAnsweringCorrectRankingBonus").IntListValue[1] function UIGuildActivityExam:OnCreated() self.IsChatScrollDragToEnd = true self.lastReqRankTime = 0 self.curQuestionData = nil self.curExamAnswer = nil self.answerTipTimeStr = UIUtils.GetLocalizedTxt(Const_IDS["IDS_GuildParty_Countdown"]) self.nextQuestionTipTimeStr = UIUtils.GetLocalizedTxt(Const_IDS["IDS_GuildParty_WaitingCountdown"]) UIUtils.SetLabel(self.txt_Name, BattleDataGetter.GetPlayerName()) end function UIGuildActivityExam:OnOpened(context) self:InitPanel() self:InitAllChatMsg() self:RefreshExamData() end function UIGuildActivityExam:InitPanel() self:RegistSystemEvent(EventID.UI_GuildParty_ExamChatHistory, self.InitAllChatMsg) self:RegistSystemEvent(EventID.UI_GuildParty_ExamChat, self.OnReceiveNewChatMsg) self:RegistSystemEvent(EventID.UI_GuildParty_ActivityRank, self.RefreshPlayerRankData) self:RegistSystemEvent(EventID.UI_GuildParty_RefreshActivityData, self.RefreshExamData) self:RegistSystemEvent(EventID.UI_KeyboardControl_Enter, self.OnPressEnterKey) self:StartRepeatFunc(self.OnUpdateFun,1) self:OnUpdateFun(0, 1) end function UIGuildActivityExam:OnPressEnterKey() self:OnButtonClicked(self.event_Send) end function UIGuildActivityExam:RefreshExamData() local curExamData, totalCount = CTL.GuildParty.GetCurExamData() if curExamData then self.curQuestionData = curExamData UIUtils.SetLabel(self.lab_Num, string.FormatByIdsKey("IDS_GuildParty_QuestionNum", curExamData.index, totalCount)) local examContent = DataSheetManager.GuildPartyQuestionBank:GetDataByID(curExamData.questionId) if curExamData.answer.isIds == 1 then self.curExamAnswer = curExamData.answer.content self:SetServerOption(curExamData.options) elseif string.isNilOrEmpty(curExamData.answer.content) then self.curExamAnswer = examContent.AnswerText self:SetExcelOption(examContent) else self.curExamAnswer = curExamData.answer.content self:SetServerOption(curExamData.options) end local paramsStr = {} if curExamData.params and #curExamData.params > 0 then for index, param in ipairs(curExamData.params) do if param.isIds == 1 then paramsStr[index] = UIUtils.GetLocalizedTxt(Const_IDS[param.content]) else paramsStr[index] = param.content end end UIUtils.SetLabel(self.txt_Exam, string.FormatByIdsKey(examContent.QuestionText, table.unpack(paramsStr))) else UIUtils.SetLabelText(self.txt_Exam, Const_IDS[examContent.QuestionText]) end end end function UIGuildActivityExam:RefreshPlayerRankData(rankDatas, selfRankDataIndex) self.Scroll_Rank:ReloadAll(rankDatas) if rankDatas[selfRankDataIndex] then --刷新自己的排行信息 UIUtils.SetLabel(self.txt_Rank, selfRankDataIndex) UIUtils.SetLabel(self.txt_Score, rankDatas[selfRankDataIndex].score) else UIUtils.SetLabelText(self.txt_Rank, Const_IDS["IDS_NotInRank"]) UIUtils.SetLabel(self.txt_Score, 0) end end function UIGuildActivityExam:OnBackButtonPressed() self:OnButtonClicked(self.btn_Close) end function UIGuildActivityExam:OnButtonClicked(comp) if comp == self.btn_Close then self:CloseView() elseif comp == self.event_Send then self:SendMsg(self.obj_InputField.text) end end function UIGuildActivityExam:SetRankInfo(cellTable, index, name, score) if index <= 2 then UIUtils.SetActive(cellTable.bg_Rank.gameObject, true) cellTable.bg_Rank:SetState(index) UIUtils.SetActive(cellTable.txt_Rank, false) else UIUtils.SetActive(cellTable.bg_Rank.gameObject, false) UIUtils.SetActive(cellTable.txt_Rank, true) UIUtils.SetLabel(cellTable.txt_Rank, index + 1) end UIUtils.SetActive(cellTable.img_Bg1, index % 2 == 0) UIUtils.SetActive(cellTable.img_Bg2, index % 2 ~= 0) UIUtils.SetLabel(cellTable.txt_Name, name) UIUtils.SetLabel(cellTable.txt_Score, score) end function UIGuildActivityExam:OnCellInit(scrollRect, index, cellTable, data) if scrollRect == self.Scroll_Rank then self:SetRankInfo(cellTable, index, data.name, data.score) elseif scrollRect == self.scroll_Chat then cellTable.lab_Content.text = data.content self:ResizeWidth(cellTable, data) if data.isSelf then UIUtils.SetLabelText(cellTable.lab_Name, Const_IDS.IDS_Chat_Me) else cellTable.lab_Name.text = data.senderName end cellTable.lab_Name.gameObject:SetActive(true) CTL.Character.SetPortraitAndFrame(cellTable.UIHeroHead150, data.portraitId, data.frameId, data.level) self:SetAnswerResult(cellTable, data.answerType, data.isSelf) self:SetRankIndex(cellTable.txt_first, data.rankIndex, data.isSelf) UnityEngine.Canvas.ForceUpdateCanvases() end end function UIGuildActivityExam:SetAnswerResult(cellTable, answerType, isSelf) UIUtils.SetActive(cellTable.group_Icon.gameObject, answerType ~= 0) UIUtils.SetActive(cellTable.img_True, answerType == 1) UIUtils.SetActive(cellTable.img_False, answerType == 2) end function UIGuildActivityExam:SetRankIndex(txt_first, rankIndex, isSelf) if rankIndex and rankIndex == 1 then txt_first.text = string.FormatByIdsId(Const_IDS.IDS_GuildParty_FirstAnswerTip, FirstScore) else txt_first.text = nil end end function UIGuildActivityExam:OnUpdateFun(deltaTime, timeSinceUpdate) local examState, leftTime = CTL.GuildParty.GetCurExamTimeData() if examState == 1 then UIUtils.SetLabel(self.lab_State, string.format(self.answerTipTimeStr, leftTime)) elseif examState == 2 then UIUtils.SetLabel(self.lab_State, string.format(self.nextQuestionTipTimeStr, leftTime)) end --每隔一秒请求排行信息 if timeSinceUpdate - self.lastReqRankTime >= 1 then self.lastReqRankTime = timeSinceUpdate MDL.GuildParty.ReqGetAnswerRank() end end --------------------------------------聊天的处理----------------------------------------------------- function UIGuildActivityExam:InitAllChatMsg() local chatList = self:GetChatMsgList() self.scroll_Chat:ClearCells() local chatDataCount = 0 if chatList == nil or #chatList <= 0 then chatDataCount = 0 else chatDataCount = #chatList end for __, msgData in ipairs(chatList) do if msgData.isSelf then self.scroll_Chat:AddData(msgData, 0) else self.scroll_Chat:AddData(msgData, 1) end end self.scroll_Chat:MoveEnd() end function UIGuildActivityExam:OnReceiveNewChatMsg(chatMsg) if not chatMsg.isSelf then if not self.IsChatScrollDragToEnd then self:AddNewMsg(false) else self:AddNewMsg(true) end else self:AddNewMsg(true) end end function UIGuildActivityExam:GetChatMsgList() return CTL.GuildParty.GetAllChatMsg() end function UIGuildActivityExam:AddNewMsg(isMoveEnd) --由上一条消息判断是否显示时间 local chatList = self:GetChatMsgList() if chatList == nil then return end if isRemove then --超过聊天记录上限,需要重新生成数据 self:CreateChatMsgData() if isMoveEnd then self.scroll_Chat:MoveEnd() else self.scroll_Chat:MoveBegin() end else local newMsg = chatList[#chatList] if newMsg.isSelf then self.scroll_Chat:AddDynamicData(newMsg, 0) else self.scroll_Chat:AddDynamicData(newMsg, 1) end --置底 if isMoveEnd then self.scroll_Chat:MoveEnd() end end end function UIGuildActivityExam:SendMsg(content) if not self.curQuestionData then return end if string.isNilOrEmpty(string.gsub(content, "%s+", "")) then CTL.Tips.AddTipsMsg(Const_IDS["IDS_LiaoTian_Empty"]) return end local sendTime = MDL.GuildParty.GetSendTime() if sendTime > 0 then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Chat_CdTips"], sendTime) return end local channelID = MDL.ChannelChat.GetChannelIdByCfgID(MDL.ChannelChat.ChannelCfgId.GuildPartyExam) local examState = CTL.GuildParty.GetCurExamTimeData() local curExamAnswerState = CTL.GuildParty.GetCurExamAnswerState() if examState == 1 and curExamAnswerState ~= 2 then if string.lower(self.curExamAnswer) == string.lower(content) then CTL.GuildParty.SetCurExamAnswerState(2) CTL.Tips.AddTipsMsg(Const_IDS["IDS_GuildParty_CorrectAnswerTip"]) CTL.GuildParty.SetCurExamAnswerState(2) MDL.GuildParty.SetGuildPartyContent(content) MDL.GuildParty.ReqAnswerQuestion(self.curQuestionData.index, 1) else if curExamAnswerState == -1 then CTL.GuildParty.SetCurExamAnswerState(1) CTL.Tips.AddTipsMsg(Const_IDS["IDS_GuildParty_WrongAnswerTip"]) end MDL.ChannelChat.SendGuildPartyExamMsg(content, 2) MDL.GuildParty.ReqAnswerQuestion(self.curQuestionData.index, 2) end else MDL.GuildParty.SetSendTime() MDL.ChannelChat.SendGuildPartyExamMsg(content, 0) end self.obj_InputField.text = nil end function UIGuildActivityExam:OnScrollBottom(comp, isBottom) self.IsChatScrollDragToEnd = isBottom end --滑动列表是否上滑到顶部 function UIGuildActivityExam:OnEndDrag(comp, eventData) if comp == self.scroll_Chat then self.IsChatScrollDragToEnd = self.scroll_Chat:IsEnd() end end function UIGuildActivityExam:ResizeWidth(cellTable,data) local fixedTxtWidth = 514 local fixedBgWidth = 534 if data.heroId == nil or data.heroId <= 0 then fixedTxtWidth = 630 fixedBgWidth = 650 end cellTable.lab_Content.transform:SetSizeDelta(fixedTxtWidth, nil) cellTable.sprite_ChatMsgBg.transform:SetSizeDelta(fixedBgWidth, nil) local tmp = cellTable.lab_Content.text local width = cellTable.lab_Content.preferredWidth --JWS2-6681 先计算出超链接的长度,再计算出坐标的长度。最后由整个文本的长度 -(超链接 - 坐标文本长度) --如果后期遇到性能问题,可放在C#端计算 local emojiCount = 0 for i in string.gmatch(data.content, "(%[%d%])") do emojiCount = emojiCount + 1 end local externalWidth = emojiCount * 25 --25 为经验值 if width + externalWidth > fixedTxtWidth then cellTable.lab_Content.transform:SetSizeDelta(fixedTxtWidth, nil) cellTable.sprite_ChatMsgBg.transform:SetSizeDelta(fixedBgWidth, nil) else cellTable.lab_Content.transform:SetSizeDelta(width + externalWidth, nil) --20 为经验值 cellTable.sprite_ChatMsgBg.transform:SetSizeDelta(width + externalWidth + 20, nil) end end function UIGuildActivityExam:SetExcelOption(excelData) for i=1, 4 do local idsId = excelData["Answer"..i] if not string.isNilOrEmpty(idsId) then UIUtils.SetLabelText(self.group_Answer[i].txt_Desc, Const_IDS[idsId]) UIUtils.SetActive(self.group_Answer[i].gameObject, true) self.group_Answer[i].lab_Answer.text = OptionChar[i] else UIUtils.SetActive(self.group_Answer[i].gameObject, false) end end end function UIGuildActivityExam:SetServerOption(params) for i=1, 4 do if params[i] and not string.isNilOrEmpty(params[i].content) then self.group_Answer[i].txt_Desc.text = params[i].content UIUtils.SetActive(self.group_Answer[i].gameObject, true) if params[i].content == self.curExamAnswer then self.curExamAnswer = OptionChar[i] end self.group_Answer[i].lab_Answer.text = OptionChar[i] else UIUtils.SetActive(self.group_Answer[i].gameObject, false) end end end return UIGuildActivityExam