LeagueAnsweringView.cs 20 KB

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