LeagueAnsweringView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 EffectUI _effectUI1;
  13. private EffectUI _effectUI2;
  14. private LeagueDataManager _dataManager;
  15. private List<long> _memberIds;
  16. public override void Dispose()
  17. {
  18. EffectUIPool.Recycle(_effectUI1);
  19. _effectUI1 = null;
  20. EffectUIPool.Recycle(_effectUI2);
  21. _effectUI2 = null;
  22. if (_ui != null)
  23. {
  24. _ui.Dispose();
  25. _ui = null;
  26. }
  27. base.Dispose();
  28. }
  29. protected override void OnInit()
  30. {
  31. base.OnInit();
  32. packageName = UI_LeagueAnsweringUI.PACKAGE_NAME;
  33. _ui = UI_LeagueAnsweringUI.Create();
  34. this.viewCom = _ui.target;
  35. isfullScreen = true;
  36. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("lm_beijing2");
  37. _ui.m_btnBack.onClick.Add(OnBtnBackClick);
  38. _ui.m_btnRule.onClick.Add(OnBtnRuleClick);
  39. _ui.m_btnSend.onClick.Add(OnBtnSendClick);
  40. _ui.m_listResult.itemRenderer = RenderListResultItem;
  41. _ui.m_listJoin.itemRenderer = RenderListJoinItem;
  42. _ui.m_listChat.itemRenderer = RenderListChatItem;
  43. _ui.m_listChat.itemProvider = GetListChatItemResource;
  44. _ui.m_listChat.SetVirtual();
  45. _ui.m_txtResult.onFocusOut.Add(OnFocuseOut);
  46. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_holder, "ui_league", "Answer_bgtx");
  47. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_holderHead, "ui_league", "Answer_Succeed");
  48. }
  49. protected override void AddEventListener()
  50. {
  51. base.AddEventListener();
  52. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_START, UpdateQuestion);
  53. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_END, UpdateResult);
  54. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateResult);
  55. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateJoinList);
  56. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_MEMBER_CHANGE, UpdateJoinList);
  57. EventAgent.AddEventListener(ConstMessage.NOTICE_CHAT_MESSAGE, UpdateChatList);
  58. }
  59. protected override async void OnShown()
  60. {
  61. base.OnShown();
  62. _dataManager = LeagueDataManager.Instance;
  63. bool result = await LeagueSproxy.ReqJoinAnswer();
  64. if (result)
  65. {
  66. UpdateJoinList();
  67. }
  68. UpdateQuestionState();
  69. UpdateChatList();
  70. Timers.inst.Add(1, 0, UpdateActiveTime);
  71. }
  72. protected override void OnHide()
  73. {
  74. base.OnHide();
  75. Timers.inst.Remove(UpdateAnswerTime);
  76. Timers.inst.Remove(UpdateResultTime);
  77. Timers.inst.Remove(UpdateActiveTime);
  78. _ui.m_holderHead.visible = false;
  79. }
  80. protected override void RemoveEventListener()
  81. {
  82. base.RemoveEventListener();
  83. }
  84. private void OnBtnBackClick()
  85. {
  86. if (_dataManager.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.Ending)
  87. {
  88. LeagueSproxy.ReqQuitAnswer().Coroutine();
  89. ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  90. }
  91. else
  92. {
  93. AlertUI.Show("退出活动会损失奖励哦,是否仍要退出?")
  94. .SetLeftButton(true, "否")
  95. .SetRightButton(true, "是", (object data) =>
  96. {
  97. LeagueSproxy.ReqQuitAnswer().Coroutine();
  98. ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  99. });
  100. }
  101. }
  102. private void OnBtnRuleClick()
  103. {
  104. ViewManager.Show<LeagueAnswerRewardView>();
  105. }
  106. private async void OnBtnSendClick()
  107. {
  108. if (string.IsNullOrEmpty(_ui.m_txtChat.text))
  109. {
  110. PromptController.Instance.ShowFloatTextPrompt("还没有输入想发送的内容哦");
  111. return;
  112. }
  113. bool result = await ChatSProxy.ReqSendChatMsg(ChatType.LeagueQuestion, _ui.m_txtChat.text);
  114. if (result)
  115. {
  116. _ui.m_txtChat.text = "";
  117. UpdateChatList();
  118. }
  119. }
  120. private void UpdateActiveTime(object param)
  121. {
  122. long curTime = TimeHelper.ServerNow();
  123. long endTime = 0;
  124. string str = "";
  125. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End
  126. || LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.AnswerEnd)
  127. {
  128. endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionCloseTime);
  129. str = "房间剩余时间 ";
  130. }
  131. else
  132. {
  133. endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionEndTime);
  134. str = "活动剩余时间 ";
  135. }
  136. if (endTime - curTime < 0 && LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End)
  137. {
  138. Timers.inst.Remove(UpdateResultTime);
  139. ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  140. return;
  141. }
  142. _ui.m_txtTime.text = string.Format("{0}{1}", str, TimeUtil.FormattingTimeTo_mmss(endTime - curTime));
  143. }
  144. private void UpdateQuestionState()
  145. {
  146. if (_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.Open)//答题中
  147. {
  148. UpdateQuestion();
  149. }
  150. else//cd中、答题结束
  151. {
  152. UpdateResult();
  153. }
  154. UpdateJoinList();
  155. }
  156. private void UpdateQuestion()
  157. {
  158. _ui.m_holderHead.visible = false;
  159. _ui.m_txtRightCount.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); ;
  160. _ui.m_txtRightCount1.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars(); ;
  161. _ui.m_ctrlQuestionState.selectedIndex = 0;
  162. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  163. LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  164. _ui.m_txtAnswerNum.text = string.Format("第{0}/{1}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionCount), GlobalCfgArray.globalCfg.leagueQuestionNum);
  165. _ui.m_ctrlHasIcon.selectedIndex = string.IsNullOrEmpty(questionCfg.res) ? 1 : 0;
  166. _ui.m_loaIocn.url = string.IsNullOrEmpty(questionCfg.res) ? "" : ResPathUtil.GetIconPath(questionCfg.res, "png");
  167. _ui.m_txtContent.text = questionCfg.question;
  168. bool isAnswer = !string.IsNullOrEmpty(_dataManager.LastAnswerRoleData.MyAnswer) && _dataManager.LastAnswerRoleData.LastQuestionId == _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  169. _ui.m_txtResult.text = isAnswer ? _dataManager.LastAnswerRoleData.MyAnswer : "";
  170. _ui.m_txtResult.enabled = !(isAnswer && _dataManager.LastAnswerRoleData.MyAnswer != questionCfg.answerCorrect);
  171. _ui.m_imgFault.visible = isAnswer && _dataManager.LastAnswerRoleData.MyAnswer != questionCfg.answerCorrect;
  172. _ui.m_ctrlQuestionType.selectedIndex = questionCfg.type;
  173. if (questionCfg.type == 1)
  174. {
  175. _ui.m_listResult.numItems = questionCfg.answerArr.Length;
  176. }
  177. _ui.m_txtAnswerTime.text = "00:" + GlobalCfgArray.globalCfg.leagueQuestionRunTime.ToString();
  178. Timers.inst.Remove(UpdateAnswerTime);
  179. Timers.inst.Add(1, 0, UpdateAnswerTime);
  180. }
  181. private void UpdateAnswerTime(object param)
  182. {
  183. long curTime = TimeHelper.ServerNow();
  184. long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime);
  185. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End
  186. || LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.AnswerEnd
  187. || endTime - curTime < 0)
  188. {
  189. Debug.Log("endTime:" + endTime + " curTime:" + curTime);
  190. Timers.inst.Remove(UpdateAnswerTime);
  191. return;
  192. }
  193. _ui.m_txtAnswerTime.text = TimeUtil.FormattingTimeTo_mmss((int)(endTime - curTime));
  194. }
  195. private void UpdateResult()
  196. {
  197. _ui.m_ctrlResult.selectedIndex = _dataManager.LastAnswerRoleData.LastQuestionResult;
  198. _ui.m_txtRightCount.text = string.Format("全员已答对{0}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount));
  199. _ui.m_txtRightCount.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars();
  200. _ui.m_txtRightCount1.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString()).FlushVars();
  201. _ui.m_txtCdTime.SetVar("value", GlobalCfgArray.globalCfg.leagueQuestionNextTime.ToString()).FlushVars();
  202. _ui.m_ctrlQuestionState.selectedIndex = 1;
  203. if (_dataManager.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.SomeBodyRight)
  204. {
  205. _ui.m_comRightHead.target.visible = true;
  206. _ui.m_imgFail.visible = false;
  207. OtherRoleInfoData roleInfoData = _dataManager.GetMemberRoleInfo(_dataManager.LastAnswerRoleData.RightRoleId);
  208. ItemCfg headCfg = ItemCfgArray.Instance.GetCfg(roleInfoData.headId == 0 ? ConstItemID.HEADID : roleInfoData.headId);
  209. _ui.m_comRightHead.m_loaIcon.url = ResPathUtil.GetHeadPath(headCfg.res);
  210. _ui.m_txtInfo.text = string.Format("{0} 答对了!", roleInfoData.roleName);
  211. _ui.m_holderHead.visible = true;
  212. Timers.inst.Remove(UpdateResultTime);
  213. Timers.inst.Add(1, 0, UpdateResultTime);
  214. }
  215. else if (_dataManager.LastAnswerRoleData.LastQuestionResult == LastQuestionResult.NobodyRight)
  216. {
  217. _ui.m_comRightHead.target.visible = false;
  218. _ui.m_imgFail.visible = true;
  219. _ui.m_txtInfo.text = "哎呀,没人答对呀,大家加油鸭~";
  220. Timers.inst.Remove(UpdateResultTime);
  221. Timers.inst.Add(1, 0, UpdateResultTime);
  222. }
  223. else
  224. {
  225. _ui.m_comRightHead.target.visible = false;
  226. _ui.m_imgFail.visible = false;
  227. if (_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionCount) == GlobalCfgArray.globalCfg.leagueQuestionNum)//全部答完
  228. {
  229. _ui.m_txtInfo.text = "全部题目答完啦~";
  230. }
  231. else//答题时间结束
  232. {
  233. _ui.m_txtInfo.text = "活动结束啦~";
  234. }
  235. }
  236. }
  237. private void UpdateResultTime(object param)
  238. {
  239. long curTime = TimeHelper.ServerNow();
  240. long endTime = LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatusEndTime);
  241. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) != LeagueQuestionStatus.WaitNext
  242. || endTime - curTime < 0)
  243. {
  244. Timers.inst.Remove(UpdateResultTime);
  245. return;
  246. }
  247. _ui.m_txtCdTime.SetVar("value", (((endTime - curTime) / 1000) + 1).ToString()).FlushVars();
  248. }
  249. private void UpdateJoinList()
  250. {
  251. _memberIds = new List<long>(_dataManager.ListAnsweringDatas.Keys);
  252. _ui.m_listJoin.numItems = _memberIds.Count;
  253. _ui.m_txtJoinNum.text = string.Format("当前参与人员:{0}/{1}", _memberIds.Count, _dataManager.LeagueData.LeagueMemberDatas.Count);
  254. }
  255. private void UpdateChatList()
  256. {
  257. _memberIds.Sort((long a, long b) =>
  258. {
  259. int countA = _dataManager.ListAnsweringDatas[a];
  260. int countB = _dataManager.ListAnsweringDatas[b];
  261. if (countA > countB) return -1;
  262. if (countB > countA) return 1;
  263. return countB - countA;
  264. });
  265. _ui.m_listChat.numItems = ChatDataManager.Instance.GetChatDatas(ChatType.LeagueQuestion).Count;
  266. _ui.m_listChat.scrollPane.ScrollBottom(true);
  267. }
  268. private void RenderListResultItem(int index, GObject obj)
  269. {
  270. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  271. LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId); ;
  272. UI_Button123 item = UI_Button123.Proxy(obj);
  273. int data = index + 1;
  274. item.target.title = string.Format("{0}.{1}", data, cfg.answerArr[index]);
  275. string myAnswer = _dataManager.LastAnswerRoleData.MyAnswer;
  276. bool isAnswer = !string.IsNullOrEmpty(_dataManager.LastAnswerRoleData.MyAnswer) && _dataManager.LastAnswerRoleData.LastQuestionId == _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  277. item.target.touchable = !isAnswer;
  278. item.m_c1.selectedIndex = isAnswer && myAnswer == data.ToString() && myAnswer != cfg.answerCorrect.ToString() ? 0 : 1;
  279. if (item.target.data == null)
  280. {
  281. item.target.onClick.Add(OnBtnChooseClick);
  282. }
  283. item.target.data = data;
  284. UI_Button123.ProxyEnd();
  285. }
  286. private async void OnBtnChooseClick(EventContext context)
  287. {
  288. GObject obj = context.sender as GObject;
  289. string answer = obj.data.ToString();
  290. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  291. LeagueQuestionCfg cfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  292. bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, answer);
  293. if (result)
  294. {
  295. _ui.m_listResult.numItems = cfg.answerArr.Length;
  296. }
  297. }
  298. private async void OnFocuseOut()
  299. {
  300. if (string.IsNullOrEmpty(_ui.m_txtResult.text))
  301. {
  302. PromptController.Instance.ShowFloatTextPrompt("还没有输入答案哦");
  303. return;
  304. }
  305. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  306. bool result = await LeagueSproxy.ReqAnswerQuestion(questionId, _ui.m_txtResult.text);
  307. if (result)
  308. {
  309. LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  310. _ui.m_txtResult.enabled = false;
  311. _ui.m_imgFault.visible = _ui.m_txtResult.text != questionCfg.answerCorrect;
  312. }
  313. }
  314. private void RenderListJoinItem(int index, GObject obj)
  315. {
  316. OtherRoleInfoData roleInfo = _dataManager.GetMemberRoleInfo(_memberIds[index]);
  317. UI_ListAnswerJoinItem item = UI_ListAnswerJoinItem.Proxy(obj);
  318. RoleInfoManager.Instance.UpdateHead(item.m_comHead, roleInfo.headId, roleInfo.headBorderId);
  319. item.m_txtName.text = roleInfo.roleName;
  320. item.m_txtAnswerCount.text = _dataManager.ListAnsweringDatas[roleInfo.roleId].ToString();
  321. UI_ListAnswerJoinItem.ProxyEnd();
  322. }
  323. private void RenderListChatItem(int index, GObject obj)
  324. {
  325. ChatData chatData = ChatDataManager.Instance.ChatDatas[ChatType.LeagueQuestion][index];
  326. OtherRoleInfoData roleInfo = chatData.RoleInfo;
  327. UI_ListChatAnswerItem item = UI_ListChatAnswerItem.Proxy(obj);
  328. RoleInfoManager.Instance.UpdateHeadWithLv(item.m_comHead, roleInfo.headId, roleInfo.headBorderId, roleInfo.roleLv);
  329. item.m_txtName.text = roleInfo.roleName;
  330. int roldRank = _memberIds.IndexOf(roleInfo.roleId);
  331. item.m_c1.selectedIndex = roldRank < 3 && _dataManager.ListAnsweringDatas[roleInfo.roleId] > 0 ? roldRank : 3;
  332. string content = "";
  333. for (int i = 0; i < chatData.Content.Length; i++)
  334. {
  335. string str = i == 12 * (i + 1) ? chatData.Content[i] + "/n" : chatData.Content[i].ToString();
  336. content += str;
  337. }
  338. item.m_txtChatContent.text = chatData.Content;
  339. UI_ListChatAnswerItem.ProxyEnd();
  340. }
  341. private string GetListChatItemResource(int index)
  342. {
  343. if (ChatDataManager.Instance.GetChatDatas(ChatType.LeagueQuestion)[index].RoleInfo.roleId == RoleDataManager.roleId)
  344. return "ui://League/ListChatAnswerItemMine";
  345. else
  346. return "ui://League/ListChatAnswerItem";
  347. }
  348. }
  349. }