LeagueAnsweringView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // this.modal = true;
  30. isfullScreen = true;
  31. _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("lm_beijing2");
  32. _ui.m_btnBack.onClick.Add(OnBtnBackClick);
  33. _ui.m_btnRule.onClick.Add(OnBtnRuleClick);
  34. _ui.m_btnSend.onClick.Add(OnBtnSendClick);
  35. _ui.m_listResult.itemRenderer = RenderListResultItem;
  36. _ui.m_listJoin.itemRenderer = RenderListJoinItem;
  37. _ui.m_listChat.itemRenderer = RenderListChatItem;
  38. _ui.m_listChat.itemProvider = GetListChatItemResource;
  39. _ui.m_listChat.SetVirtual();
  40. _ui.m_txtResult.onFocusOut.Add(OnFocuseOut);
  41. }
  42. protected override void AddEventListener()
  43. {
  44. base.AddEventListener();
  45. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_START, UpdateQuestion);
  46. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_END, UpdateResult);
  47. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateResult);
  48. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_RESULT_CHANGE, UpdateJoinList);
  49. EventAgent.AddEventListener(ConstMessage.LEAGUE_ANSWER_MEMBER_CHANGE, UpdateJoinList);
  50. // EventAgent.AddEventListener(ConstMessage.LEAGUE_NUMBERIC_CHANGE, UpdateQuestionState);
  51. EventAgent.AddEventListener(ConstMessage.NOTICE_CHAT_MESSAGE, UpdateChatList);
  52. }
  53. protected override async void OnShown()
  54. {
  55. base.OnShown();
  56. _dataManager = LeagueDataManager.Instance;
  57. // _ui.m_listChat.numItems = ChatDataManager.Instance.GetChatDatas(ChatType.LeagueQuestion).Count;
  58. // _ui.m_listChat.scrollPane.ScrollBottom();
  59. UpdateQuestionState();
  60. UpdateChatList();
  61. bool result = await LeagueSproxy.ReqJoinAnswer();
  62. if (result)
  63. {
  64. UpdateJoinList();
  65. }
  66. UpdateChatList();
  67. Timers.inst.Add(1, 0, UpdateActiveTime);
  68. }
  69. protected override void OnHide()
  70. {
  71. base.OnHide();
  72. Timers.inst.Remove(UpdateAnswerTime);
  73. Timers.inst.Remove(UpdateResultTime);
  74. Timers.inst.Remove(UpdateActiveTime);
  75. }
  76. protected override void RemoveEventListener()
  77. {
  78. base.RemoveEventListener();
  79. }
  80. private void OnBtnBackClick()
  81. {
  82. AlertUI.Show("退出活动会损失奖励哦,是否仍要退出?")
  83. .SetLeftButton(true, "否")
  84. .SetRightButton(true, "是", (object data) =>
  85. {
  86. LeagueSproxy.ReqQuitAnswer().Coroutine();
  87. ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  88. });
  89. }
  90. private void OnBtnRuleClick()
  91. {
  92. ViewManager.Show<LeagueAnswerRewardView>();
  93. }
  94. private async void OnBtnSendClick()
  95. {
  96. bool result = await ChatSProxy.ReqSendChatMsg(ChatType.LeagueQuestion, _ui.m_txtChat.text);
  97. if (result)
  98. {
  99. _ui.m_txtChat.text = "";
  100. UpdateChatList();
  101. }
  102. }
  103. private void UpdateActiveTime(object param)
  104. {
  105. long curTime = TimeHelper.ServerNow();
  106. long endTime = 0;
  107. string str = "";
  108. if (LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.End
  109. || LeagueDataManager.Instance.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.AnswerEnd)
  110. {
  111. endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionCloseTime);
  112. str = "房间剩余时间 ";
  113. }
  114. else
  115. {
  116. endTime = TimeUtil.GetCurDayTime(GlobalCfgArray.globalCfg.leagueQuestionEndTime);
  117. str = "活动剩余时间 ";
  118. }
  119. // if (endTime - curTime < 0)
  120. // {
  121. // Timers.inst.Remove(UpdateResultTime);
  122. // ViewManager.GoBackFrom(typeof(LeagueAnsweringView).FullName);
  123. // return;
  124. // }
  125. _ui.m_txtTime.text = string.Format("{0}{1}", str, TimeUtil.FormattingTime7(endTime - curTime));
  126. }
  127. private void UpdateQuestionState()
  128. {
  129. if (_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionStatus) == LeagueQuestionStatus.Open)//答题中
  130. {
  131. UpdateQuestion();
  132. }
  133. else//cd中、答题结束
  134. {
  135. UpdateResult();
  136. }
  137. UpdateJoinList();
  138. }
  139. private void UpdateQuestion()
  140. {
  141. // _ui.m_txtRightCount.text = string.Format("全员已答对{0}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount));
  142. _ui.m_txtRightCount.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  143. _ui.m_txtRightCount1.SetVar("value", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionTrueCount).ToString());
  144. _ui.m_ctrlQuestionState.selectedIndex = 0;
  145. int questionId = (int)_dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  146. LeagueQuestionCfg questionCfg = LeagueQuestionCfgArray.Instance.GetCfg(questionId);
  147. _ui.m_txtAnswerNum.text = string.Format("第{0}/{1}题", _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionCount), GlobalCfgArray.globalCfg.leagueQuestionNum);
  148. _ui.m_ctrlHasIcon.selectedIndex = string.IsNullOrEmpty(questionCfg.res) ? 1 : 0;
  149. _ui.m_loaIocn.url = string.IsNullOrEmpty(questionCfg.res) ? "" : ResPathUtil.GetIconPath(questionCfg.res, "png");
  150. _ui.m_txtContent.text = questionCfg.question;
  151. bool isAnswer = !string.IsNullOrEmpty(_dataManager.LastAnswerRoleData.MyAnswer) && _dataManager.LastAnswerRoleData.LastQuestionId == _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  152. _ui.m_txtResult.text = isAnswer ? _dataManager.LastAnswerRoleData.MyAnswer : "";
  153. _ui.m_txtResult.enabled = !(isAnswer && _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 = _dataManager.LastAnswerRoleData.LastQuestionResult;
  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(_dataManager.LastAnswerRoleData.MyAnswer) && _dataManager.LastAnswerRoleData.LastQuestionId == _dataManager.GetNumeriValue(LeagueNumericType.LeagueQuestionId);
  250. // item.m_c1.selectedIndex= !isAnswer;
  251. item.target.touchable = !isAnswer;
  252. item.m_c1.selectedIndex = isAnswer && 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. }