LeagueAnsweringView.cs 16 KB

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