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