using System.Collections.Generic; using System.Linq; using ET; using FairyGUI; using Hutool; using UI.League; using UnityEngine; namespace GFGGame { //联盟答题中 public class LeagueAnsweringView : BaseWindow { private UI_LeagueAnsweringUI _ui; private EffectUI _effectUI1; private EffectUI _effectUI2; private List _memberIds; //该玩家id列表,会按照答题数量降序排列 public override void Dispose() { EffectUIPool.Recycle(_effectUI1); _effectUI1 = null; EffectUIPool.Recycle(_effectUI2); _effectUI2 = null; if (_ui != null) { _ui.Dispose(); _ui = null; } base.Dispose(); } protected override void OnInit() { base.OnInit(); packageName = UI_LeagueAnsweringUI.PACKAGE_NAME; _ui = UI_LeagueAnsweringUI.Create(); this.viewCom = _ui.target; isfullScreen = true; isReturnView = true; _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("tjbg"); _ui.m_btnBack.onClick.Add(OnBtnBackClick); _ui.m_btnRule.onClick.Add(OnBtnRuleClick); _ui.m_btnSend.target.onClick.Add(OnBtnSendClick); _ui.m_listResult.itemRenderer = RenderListResultItem; _ui.m_listJoin.itemRenderer = RenderListJoinItem; _ui.m_listChat.itemRenderer = RenderListChatItem; _ui.m_listChat.itemProvider = GetListChatItemResource; _ui.m_listChat.SetVirtual(); //_ui.m_txtResult.onFocusOut.Add(OnFocuseOut); _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_holder, "ui_league", "Answer_bgtx"); _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_holderHead, "ui_league", "Answer_Succeed"); } protected override void AddEventListener() { base.AddEventListener(); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_START, UpdateQuestion); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_END, UpdateResult); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateResult); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateJoinList); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_MEMBER_CHANGE, UpdateJoinList); EventAgent.AddEventListener(ConstMessage.NOTICE_CHAT_MESSAGE, UpdateChatList); EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_INFO, ReferInfo); } protected override void RemoveEventListener() { EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_START, UpdateQuestion); EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_END, UpdateResult); EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateResult); EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateJoinList); EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_MEMBER_CHANGE, UpdateJoinList); EventAgent.RemoveEventListener(ConstMessage.NOTICE_CHAT_MESSAGE, UpdateChatList); EventAgent.RemoveEventListener(ConstMessage.LEAGUE_ANSWER_INFO, ReferInfo); base.RemoveEventListener(); } protected override void OnShown() { base.OnShown(); LeagueSproxy.ReqJoinAnswer().Coroutine(); } protected override void OnHide() { base.OnHide(); Timers.inst.Remove(UpdateAnswerTime); Timers.inst.Remove(UpdateResultTime); Timers.inst.Remove(UpdateActiveTime); _ui.m_holderHead.visible = false; } private void ReferInfo() { UpdateJoinList(); UpdateQuestionState(); UpdateChatList(); Timers.inst.Add(1, 0, UpdateActiveTime); } private void OnBtnBackClick() { if (LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.Ending) { LeagueSproxy.ReqQuitAnswer().Coroutine(); // ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName); Hide(); } else { AlertUI.Show("退出活动会损失奖励哦,是否仍要退出?") .SetLeftButton(true, "否") .SetRightButton(true, "是", (object data) => { LeagueSproxy.ReqQuitAnswer().Coroutine(); // ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName); Hide(); }); } } private void OnBtnRuleClick() { ViewManager.Show(); } private async void OnBtnSendClick() { if (_ui.m_btnSend.m_c1.selectedIndex == 1) { PromptController.Instance.ShowFloatTextPrompt("消息发送频繁"); return; } if (string.IsNullOrEmpty(_ui.m_txtChat.text)) { PromptController.Instance.ShowFloatTextPrompt("还没有输入想发送的内容哦"); return; } bool result = await ChatSProxy.ReqSendChatMsg(ChatType.LeagueQuestion, _ui.m_txtChat.text); if (result) { _ui.m_txtChat.text = ""; UpdateChatList(); int num = GlobalCfgArray.globalCfg.chatTime; _ui.m_btnSend.m_timeStr.text = num.ToString(); _ui.m_btnSend.m_c1.selectedIndex = 1; Timers.inst.Add(1, 10, (param) => { num--; _ui.m_btnSend.m_timeStr.text = num.ToString(); if (num == 0) { _ui.m_btnSend.m_c1.selectedIndex = 0; } }); } } private void UpdateActiveTime(object param) { long curTime = TimeHelper.ServerNow(); long endTime = 0; string str = ""; //leagueQuestionStartTimeArr与leagueQuestionCloseTimeArr与leagueQuestionEndTimeArr数组长度相同,且索引对应。跟曾威定的 int curIndex = DateUtil.GetNearestStartTimeIndex(GlobalCfgArray.globalCfg.leagueQuestionCloseTimeArr); if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End || LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.AnswerEnd) { endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionCloseTimeArr[curIndex]); str = "房间剩余时间 "; } else { endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionEndTimeArr[curIndex]); str = "活动剩余时间 "; } if (endTime - curTime < 0 && LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End) { Timers.inst.Remove(UpdateResultTime); // ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName); Hide(); return; } _ui.m_txtTime.text = string.Format("{0}[color=#E07748]{1}[/color]", str, TimeUtil.FormattingTimeTo_mmss(endTime - curTime)); } private void UpdateQuestionState() { if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.Open)//答题中 { UpdateQuestion(); } else//cd中、答题结束 { UpdateResult(); } UpdateJoinList(); } private void UpdateQuestion() { _ui.m_holderHead.visible = false; _ui.m_txtRightCount.SetVar("value", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); ; _ui.m_txtRightCount1.SetVar("value", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); ; _ui.m_ctrlQuestionState.selectedIndex = 0; int questionId = (int)LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId); _ui.m_txtAnswerNum.text = string.Format("第{0}/{1}题", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionCount), GlobalCfgArray.globalCfg.leagueQuestionNum); _ui.m_ctrlHasIcon.selectedIndex = string.IsNullOrEmpty(questionCfg.res) ? 1 : 0; _ui.m_loaIocn.url = string.IsNullOrEmpty(questionCfg.res) ? "" : ResPathUtil.GetIconPath(questionCfg.res, "png"); _ui.m_txtContent.text = questionCfg.question; bool isAnswer = !string.IsNullOrEmpty(LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer) && LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionId == LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); _ui.m_txtResult.text = isAnswer ? LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer : ""; _ui.m_txtResult.enabled = !(isAnswer && LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer != questionCfg.answerCorrect); _ui.m_imgFault.visible = isAnswer && LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer != questionCfg.answerCorrect; _ui.m_ctrlQuestionType.selectedIndex = questionCfg.type; if (questionCfg.type == 1) { _ui.m_listResult.numItems = questionCfg.answerArr.Length; } _ui.m_txtAnswerTime.text = "00:" + GlobalCfgArray.globalCfg.leagueQuestionRunTime.ToString(); Timers.inst.Remove(UpdateAnswerTime); Timers.inst.Add(1, 0, UpdateAnswerTime); } private void UpdateAnswerTime(object param) { long curTime = TimeHelper.ServerNow(); long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime); if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End || LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.AnswerEnd || endTime - curTime < 0) { Debug.Log("endTime:" + endTime + " curTime:" + curTime); Timers.inst.Remove(UpdateAnswerTime); return; } _ui.m_txtAnswerTime.text = TimeUtil.FormattingTimeTo_mmss((int)(endTime - curTime)); } private void UpdateResult() { _ui.m_ctrlResult.selectedIndex = LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionResult; _ui.m_txtRightCount.text = string.Format("全员已答对[color=#AF5638]{0}[/color]题", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount)); _ui.m_txtRightCount.SetVar("value", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); _ui.m_txtRightCount1.SetVar("value", LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); _ui.m_txtCdTime.SetVar("value", GlobalCfgArray.globalCfg.leagueQuestionNextTime.ToString()).FlushVars(); _ui.m_ctrlQuestionState.selectedIndex = 1; if (LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.SomeBodyRight) { _ui.m_comRightHead.target.visible = true; _ui.m_imgFail.visible = false; OtherRoleInfoData roleInfoData = LeagueDataManager.Instance.GetMemberRoleInfo(LeagueDataManager.Instance.LastAnswerRoleData.RightRoleId); ItemCfg headCfg = ItemCfgArray.Instance.GetCfg(roleInfoData.headId == 0 ? ConstItemID.HEADID : roleInfoData.headId); _ui.m_comRightHead.m_loaIcon.url = ResPathUtil.GetHeadPath(headCfg.res); _ui.m_txtInfo.text = string.Format("{0} [color=#915E15]答对了![/color]", roleInfoData.roleName); _ui.m_holderHead.visible = true; Timers.inst.Remove(UpdateResultTime); Timers.inst.Add(1, 0, UpdateResultTime); } else if (LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.NobodyRight) { _ui.m_comRightHead.target.visible = false; _ui.m_imgFail.visible = true; _ui.m_txtInfo.text = "哎呀,没人答对呀,大家加油鸭~"; Timers.inst.Remove(UpdateResultTime); Timers.inst.Add(1, 0, UpdateResultTime); } else { _ui.m_comRightHead.target.visible = false; _ui.m_imgFail.visible = false; if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionCount) == GlobalCfgArray.globalCfg.leagueQuestionNum)//全部答完 { _ui.m_txtInfo.text = "全部题目答完啦,活动结束啦~"; } else//答题时间结束 { _ui.m_txtInfo.text = "活动结束啦~"; } } } private void UpdateResultTime(object param) { long curTime = TimeHelper.ServerNow(); long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime); if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) != LeagueQuestionStatus.WaitNext || endTime - curTime < 0) { Timers.inst.Remove(UpdateResultTime); return; } _ui.m_txtCdTime.SetVar("value", (((endTime - curTime) / 1000) + 1).ToString()).FlushVars(); } private void UpdateJoinList() { _memberIds = LeagueDataManager.Instance.ListAnsweringDatas .OrderByDescending(pair => pair.Value) // 按值(答题数量)降序排序 .Select(pair => pair.Key) .ToList(); _ui.m_listJoin.numItems = _memberIds.Count; _ui.m_txtJoinNum.text = string.Format("当前参与人员:{0}/{1}", _memberIds.Count, LeagueDataManager.Instance.LeagueData.LeagueMemberDatas.Count); } private void UpdateChatList() { _memberIds.Sort((long a, long b) => { int countA = LeagueDataManager.Instance.ListAnsweringDatas[a]; int countB = LeagueDataManager.Instance.ListAnsweringDatas[b]; if (countA > countB) return -1; if (countB > countA) return 1; return countB - countA; }); _ui.m_listChat.numItems = ChatDataManager.Instance.GetChatDatas(ChatType.LeagueQuestion).Count; _ui.m_listChat.scrollPane.ScrollBottom(true); } private void RenderListResultItem(int index, GObject obj) { int questionId = (int)LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId); ; UI_Button123 item = UI_Button123.Proxy(obj); int data = index + 1; item.target.title = string.Format("{0}.{1}", data, cfg.answerArr[index]); //测试 正确答案 //if (data == int.Parse(cfg.answerCorrect)) //{ // item.target.title = string.Format("{0}.{1} 正确", data, cfg.answerArr[index]); //} string myAnswer = LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer; bool isAnswer = !string.IsNullOrEmpty(LeagueDataManager.Instance.LastAnswerRoleData.MyAnswer) && LeagueDataManager.Instance.LastAnswerRoleData.LastQuestionId == LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); item.target.touchable = !isAnswer; item.m_c1.selectedIndex = isAnswer && myAnswer == data.ToString() && myAnswer != cfg.answerCorrect.ToString() ? 0 : 1; if (item.target.data == null) { item.target.onClick.Add(OnBtnChooseClick); } item.target.data = data; UI_Button123.ProxyEnd(); } private async void OnBtnChooseClick(EventContext context) { GObject obj = context.sender as GObject; string answer = obj.data.ToString(); int questionId = (int)LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId); bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, answer); if (result) { _ui.m_listResult.numItems = cfg.answerArr.Length; } } private async void OnFocuseOut() { if (string.IsNullOrEmpty(_ui.m_txtResult.text)) { PromptController.Instance.ShowFloatTextPrompt("还没有输入答案哦"); return; } int questionId = (int)LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionId); bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, _ui.m_txtResult.text); if (result) { LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId); _ui.m_txtResult.enabled = false; _ui.m_imgFault.visible = _ui.m_txtResult.text != questionCfg.answerCorrect; } } private void RenderListJoinItem(int index, GObject obj) { OtherRoleInfoData roleInfo = LeagueDataManager.Instance.GetMemberRoleInfo(_memberIds[index]); UI_ListAnswerJoinItem item = UI_ListAnswerJoinItem.Proxy(obj); RoleInfoManager.Instance.UpdateHead(item.m_comHead, roleInfo.headId, roleInfo.headBorderId); item.m_txtName.text = roleInfo.roleName; item.m_txtAnswerCount.text = LeagueDataManager.Instance.ListAnsweringDatas[roleInfo.roleId].ToString(); UI_ListAnswerJoinItem.ProxyEnd(); } private void RenderListChatItem(int index, GObject obj) { ChatData chatData = ChatDataManager.Instance.ChatDatas[ChatType.LeagueQuestion][index]; OtherRoleInfoData roleInfo = chatData.RoleInfo; UI_ListChatAnswerItem item = UI_ListChatAnswerItem.Proxy(obj); RoleInfoManager.Instance.UpdateHeadWithLv(item.m_comHead, roleInfo.headId, roleInfo.headBorderId, roleInfo.roleLv); item.m_txtName.text = roleInfo.roleName; int roldRank = _memberIds.IndexOf(roleInfo.roleId); item.m_c1.selectedIndex = roldRank < 3 && roldRank > 0 ? roldRank : 3; string content = ""; for (int i = 0; i < chatData.Content.Length; i++) { string str = i == 12 * (i + 1) ? chatData.Content[i] + "/n" : chatData.Content[i].ToString(); content += str; } item.m_txtChatContent.text = chatData.Content; UI_ListChatAnswerItem.ProxyEnd(); } private string GetListChatItemResource(int index) { if (ChatDataManager.Instance.GetChatDatas(ChatType.LeagueQuestion)[index].RoleInfo.roleId == RoleDataManager.roleId) return "ui://League/ListChatAnswerItemMine"; else return "ui://League/ListChatAnswerItem"; } } }