LeagueAnsweringView.cs 19 KB

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