LeagueAnsweringView.cs 15 KB

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