LeagueAnsweringView.cs 18 KB

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