LeagueAnsweringView.cs 20 KB

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