LeagueAnsweringView.cs 19 KB

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