LeagueAnsweringView.cs 20 KB

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