LeagueAnsweringView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System.Collections.Generic;
  2. using ET;
  3. using FairyGUI;
  4. using UI.League;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. //联盟答题中
  9. public class LeagueAnsweringView : BaseWindow
  10. {
  11. private UI_LeagueAnsweringUI _ui;
  12. private LeagueDataManager _dataManager;
  13. private List<long> _memberIds;
  14. public override void Dispose()
  15. {
  16. if (_ui != null)
  17. {
  18. _ui.Dispose();
  19. _ui = null;
  20. }
  21. base.Dispose();
  22. }
  23. protected override void OnInit()
  24. {
  25. base.OnInit();
  26. packageName = UI_LeagueAnsweringUI.PACKAGE_NAME;
  27. _ui = UI_LeagueAnsweringUI.Create();
  28. this.viewCom = _ui.target;
  29. isfullScreen = true;
  30. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("lm_beijing");
  31. _ui.m_btnBack.onClick.Add(OnBtnBackClick);
  32. _ui.m_btnRule.onClick.Add(OnBtnRuleClick);
  33. _ui.m_btnSend.onClick.Add(OnBtnSendClick);
  34. _ui.m_listResult.itemRenderer = RenderListResultItem;
  35. _ui.m_listJoin.itemRenderer = RenderListJoinItem;
  36. _ui.m_listChat.itemRenderer = RenderListChatItem;
  37. _ui.m_listChat.itemProvider = GetListItemResource;
  38. _ui.m_listChat.SetVirtual();
  39. _ui.m_txtResult.onFocusOut.Add(OnFocuseOut);
  40. }
  41. protected override void AddEventListener()
  42. {
  43. base.AddEventListener();
  44. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_START, UpdateQuestion);
  45. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_END, UpdateResult);
  46. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateResult);
  47. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateJoinList);
  48. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_MEMBER_CHANGE, UpdateJoinList);
  49. EventAgent.AddEventListener(ConstMessage.NOTICE_CHAT_MESSAGE, UpdateChatList);
  50. }
  51. protected override async void OnShown()
  52. {
  53. base.OnShown();
  54. _dataManager = LeagueDataManager.Instance;
  55. _ui.m_listChat.numItems = ChatDataManager.Instance.ChatDatas.ContainsKey(ChatType.LeagueQuestion) ? ChatDataManager.Instance.ChatDatas[ChatType.LeagueQuestion].Count : 0;
  56. _ui.m_listChat.scrollPane.ScrollBottom();
  57. UpdateQuestionState();
  58. bool result = await LeagueSproxy.ReqJoinAnswer();
  59. if (result)
  60. {
  61. UpdateJoinList();
  62. }
  63. }
  64. protected override void OnHide()
  65. {
  66. base.OnHide();
  67. Timers.inst.Remove(UpdateAnswerTime);
  68. }
  69. protected override void RemoveEventListener()
  70. {
  71. base.RemoveEventListener();
  72. }
  73. private void OnBtnBackClick()
  74. {
  75. ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  76. }
  77. private void OnBtnRuleClick()
  78. {
  79. ViewManager.Show<LeagueAnswerRewardView>();
  80. }
  81. private void OnBtnSendClick()
  82. {
  83. }
  84. private void UpdateQuestionState()
  85. {
  86. if (_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.Open)//答题中
  87. {
  88. UpdateQuestion();
  89. }
  90. else//cd中、答题结束
  91. {
  92. UpdateResult();
  93. }
  94. }
  95. private void UpdateQuestion()
  96. {
  97. // _ui.m_txtRightCount.text = string.Format("全员已答对{0}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount));
  98. _ui.m_txtRightCount.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  99. _ui.m_txtRightCount1.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  100. _ui.m_ctrlQuestionState.selectedIndex = 0;
  101. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  102. LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  103. _ui.m_txtAnswerNum.text = string.Format("第{0}/{1}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionCount), GlobalCfgArray.globalCfg.leagueQuestionNum);
  104. _ui.m_ctrlHasIcon.selectedIndex = string.IsNullOrEmpty(questionCfg.res) ? 1 : 0;
  105. _ui.m_loaIocn.url = string.IsNullOrEmpty(questionCfg.res) ? "" : ResPathUtil.GetIconPath(questionCfg.res, "png");
  106. _ui.m_txtContent.text = questionCfg.question;
  107. _ui.m_txtResult.enabled =
  108. !(!string.IsNullOrEmpty(_dataManager.LastAnswerRoleData.MyAnswer) &&
  109. _dataManager.LastAnswerRoleData.LastQuestionId == _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId)
  110. && _dataManager.LastAnswerRoleData.MyAnswer != questionCfg.answerCorrect);
  111. _ui.m_ctrlQuestionType.selectedIndex = questionCfg.type;
  112. if (questionCfg.type == 1)
  113. {
  114. _ui.m_listResult.numItems = questionCfg.answerArr.Length;
  115. }
  116. _ui.m_txtTime.text = "";
  117. Timers.inst.Remove(UpdateAnswerTime);
  118. Timers.inst.Add(1, 0, UpdateAnswerTime);
  119. }
  120. private void UpdateAnswerTime(object param)
  121. {
  122. long curTime = TimeHelper.ServerNow();
  123. long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime);
  124. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) > LeagueQuestionStatus.End
  125. || endTime - curTime < 0)
  126. {
  127. Timers.inst.Remove(UpdateAnswerTime);
  128. return;
  129. }
  130. _ui.m_txtTime.text = TimeUtil.FormattingTime5((int)(endTime - curTime));
  131. }
  132. private void UpdateResult()
  133. {
  134. // _ui.m_txtRightCount.text = string.Format("全员已答对{0}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount));
  135. // _ui.txtRightCount1.text = string.Format("全员已答对{0}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount));
  136. _ui.m_txtRightCount.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  137. _ui.m_txtRightCount1.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  138. _ui.m_ctrlQuestionState.selectedIndex = 1;
  139. _ui.m_ctrlResult.selectedIndex = LastQuestionResult.SomeBodyRight;
  140. if (_dataManager.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.SomeBodyRight)
  141. {
  142. _ui.m_comRightHead.target.visible = true;
  143. _ui.m_imgFail.visible = false;
  144. OtherRoleInfoData roleInfoData = _dataManager.GetMemberRoleInfo(_dataManager.LastAnswerRoleData.RightRoleId);
  145. RoleInfoManager.Instance.UpdateHeadWithLv(_ui.m_comRightHead.target, roleInfoData.headId, roleInfoData.headBorderId, roleInfoData.roleLv);
  146. _ui.m_txtInfo.text = string.Format("{0} 答对了!", roleInfoData.roleName);
  147. Timers.inst.Remove(UpdateResultTime);
  148. Timers.inst.Add(1, 0, UpdateResultTime);
  149. }
  150. else if (_dataManager.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.NobodyRight)
  151. {
  152. _ui.m_comRightHead.target.visible = false;
  153. _ui.m_imgFail.visible = true;
  154. _ui.m_txtInfo.text = "哎呀,没人答对呀,大家加油鸭~";
  155. Timers.inst.Remove(UpdateResultTime);
  156. Timers.inst.Add(1, 0, UpdateResultTime);
  157. }
  158. else
  159. {
  160. _ui.m_comRightHead.target.visible = false;
  161. _ui.m_imgFail.visible = false;
  162. if (_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionCount) == GlobalCfgArray.globalCfg.leagueQuestionNum)//全部答完
  163. {
  164. _ui.m_txtInfo.text = "全部题目答完啦~";
  165. }
  166. else//答题时间结束
  167. {
  168. _ui.m_txtInfo.text = "活动结束啦~";
  169. }
  170. }
  171. }
  172. private void UpdateResultTime(object param)
  173. {
  174. long curTime = TimeHelper.ServerNow();
  175. long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime);
  176. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) != LeagueQuestionStatus.WaitNext
  177. || endTime - curTime < 0)
  178. {
  179. Timers.inst.Remove(UpdateResultTime);
  180. return;
  181. }
  182. // _ui.m_txtTime.text = (endTime - curTime) / 1000 + "秒后开始下一题";
  183. _ui.m_txtTime.SetVar("value", ((endTime - curTime) / 1000).ToString());
  184. }
  185. private void UpdateJoinList()
  186. {
  187. _memberIds = new List<long>(_dataManager.ListAnsweringDatas.Keys);
  188. _ui.m_listJoin.numItems = _memberIds.Count;
  189. _ui.m_txtJoinNum.text = string.Format("当前参与人员:{0}/{1}", _memberIds.Count, _dataManager.LeagueData.LeagueMemberDatas.Count);
  190. }
  191. private void UpdateChatList()
  192. {
  193. _ui.m_listChat.numItems = ChatDataManager.Instance.ChatDatas[ChatType.LeagueQuestion].Count;
  194. _ui.m_listChat.scrollPane.ScrollBottom(true);
  195. }
  196. private void RenderListResultItem(int index, GObject obj)
  197. {
  198. LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.dataArray[index];
  199. UI_Button123 item = UI_Button123.Proxy(obj);
  200. int data = index + 1;
  201. item.target.title = string.Format("{0}.{1}", data, cfg.answerArr[index]);
  202. string myAnswer = _dataManager.LastAnswerRoleData.MyAnswer;
  203. bool isAnswer = !string.IsNullOrEmpty(myAnswer);
  204. item.target.enabled = !isAnswer;
  205. item.m_c1.selectedIndex = myAnswer == data.ToString() && myAnswer != cfg.answerCorrect.ToString() ? 0 : 1;
  206. if (item.target.data == null)
  207. {
  208. item.target.onClick.Add(OnBtnChooseClick);
  209. }
  210. item.target.data = data;
  211. UI_Button123.ProxyEnd();
  212. }
  213. private async void OnBtnChooseClick(EventContext context)
  214. {
  215. GObject obj = context.sender as GObject;
  216. string answer = obj.data.ToString();
  217. ChatSProxy.ReqSendChatMsg(ChatType.LeagueQuestion, answer).Coroutine();
  218. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  219. LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  220. bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, answer);
  221. if (result
  222. && _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId) == _dataManager.LastAnswerRoleData.LastQuestionId
  223. && _dataManager.LastAnswerRoleData.MyAnswer != cfg.answerCorrect)
  224. {
  225. _ui.m_listResult.numItems = cfg.answerArr.Length;
  226. }
  227. }
  228. private async void OnFocuseOut()
  229. {
  230. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  231. bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, _ui.m_txtResult.text);
  232. if (result)
  233. {
  234. _ui.m_txtResult.enabled = false;
  235. }
  236. }
  237. private void RenderListJoinItem(int index, GObject obj)
  238. {
  239. OtherRoleInfoData roleInfo = _dataManager.LeagueData.LeagueMemberDatas[_memberIds[index]].RoleInfo;
  240. UI_ListAnswerJoinItem item = UI_ListAnswerJoinItem.Proxy(obj);
  241. RoleInfoManager.Instance.UpdateHead(item.m_comHead, roleInfo.headId, roleInfo.headBorderId);
  242. item.m_txtName.text = roleInfo.roleName;
  243. item.m_txtAnswerCount.text = _dataManager.ListAnsweringDatas[roleInfo.roleId].ToString();
  244. UI_ListAnswerJoinItem.ProxyEnd();
  245. }
  246. private void RenderListChatItem(int index, GObject obj)
  247. {
  248. ChatData chatData = ChatDataManager.Instance.ChatDatas[ChatType.LeagueQuestion][index];
  249. OtherRoleInfoData roleInfo = _dataManager.LeagueData.LeagueMemberDatas[_memberIds[index]].RoleInfo;
  250. UI_ListChatItem item = UI_ListChatItem.Proxy(obj);
  251. RoleInfoManager.Instance.UpdateHeadWithLv(item.m_comHead, roleInfo.headId, roleInfo.headBorderId, roleInfo.roleLv);
  252. item.m_txtName.text = roleInfo.roleName;
  253. item.m_txtChatContent.text = chatData.Content;
  254. UI_ListChatItem.ProxyEnd();
  255. }
  256. private string GetListItemResource(int index)
  257. {
  258. if (_memberIds[index] == RoleDataManager.roleId)
  259. return "ui://League/ListChatItemMine";
  260. else
  261. return "ui://League/ListChatItem";
  262. }
  263. }
  264. }