LeagueAnsweringView.cs 16 KB

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