MessageComp.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections;
  3. using Api;
  4. using Plugins.CxShine.Singleton;
  5. using UnityEngine;
  6. namespace Message
  7. {
  8. public class MessageComp : UnitySingleton<MessageComp>
  9. {
  10. public bool OpenWhile = true;
  11. public bool EnterHall = false;
  12. public float IntervalTime = 1.0f;
  13. // public bool MsgConsume = false;
  14. public Action<int, MsgContent> OnBattleRequest;
  15. public Action<int, MsgContent> OnBattleAgree;
  16. public Action<MsgContent> OnBattleStart;
  17. public Action<int, MsgContent> OnBattlePlayerReady;
  18. public Action<int, MsgContent> OnBattleClientMsg;
  19. public Action<int, MsgContent> OnBattlePlayerQuit;
  20. private bool end = false;
  21. private void Start()
  22. {
  23. StartCoroutine(ExecuteEveryFewSeconds());
  24. }
  25. private void OnDestroy()
  26. {
  27. OpenWhile = false;
  28. Debug.Log("Message Comp Destroy");
  29. }
  30. public void ClearMessage()
  31. {
  32. ApiComp.Instance.ClearMsgs();
  33. }
  34. IEnumerator ExecuteEveryFewSeconds()
  35. {
  36. while (true)
  37. {
  38. //
  39. if (OpenWhile)
  40. {
  41. // Debug.Log("sync msgs..");
  42. if (EnterHall)
  43. {
  44. ApiComp.Instance.GetMsgList(data =>
  45. {
  46. foreach (Api.Msg msg in data.msgs)
  47. {
  48. // Debug.Log(">>>>"+msg.type+">>"+msg.id);
  49. if (msg.type == MessageTypes.MsgRequestBattle.GetHashCode())
  50. {
  51. OnBattleRequest?.Invoke(msg.msgFromPlayer, msg.content);
  52. }
  53. else if (msg.type == MessageTypes.MsgBattleAgree.GetHashCode())
  54. {
  55. OnBattleAgree?.Invoke(msg.msgFromPlayer, msg.content);
  56. }
  57. else if (msg.type == MessageTypes.MsgBattleStart.GetHashCode())
  58. {
  59. OnBattleStart?.Invoke(msg.content);
  60. }
  61. else if (msg.type == MessageTypes.OnBattleClientMsg.GetHashCode())
  62. {
  63. Debug.Log("zane client msg:" + msg);
  64. OnBattleClientMsg?.Invoke(msg.msgFromPlayer, msg.content);
  65. }
  66. else if (msg.type == MessageTypes.MsgPlayerReady.GetHashCode())
  67. {
  68. OnBattlePlayerReady?.Invoke(msg.msgFromPlayer, msg.content);
  69. }
  70. else if (msg.type == MessageTypes.PlayerQuitInBattle.GetHashCode())
  71. {
  72. OnBattlePlayerQuit?.Invoke(msg.msgFromPlayer, msg.content);
  73. }
  74. ApiComp.Instance.EndMessage(msg.id);
  75. }
  76. }, (code, msg) => { });
  77. }
  78. }
  79. // 等待几秒钟
  80. if (end)
  81. {
  82. yield return null;
  83. }
  84. else
  85. {
  86. yield return new WaitForSeconds(IntervalTime);
  87. }
  88. }
  89. }
  90. }
  91. }