MessageComp.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. private bool end = false;
  20. private void Start()
  21. {
  22. StartCoroutine(ExecuteEveryFewSeconds());
  23. }
  24. private void OnDestroy()
  25. {
  26. OpenWhile = false;
  27. Debug.Log("Meesage Comp Destroy");
  28. }
  29. public void ClearMessage()
  30. {
  31. ApiComp.Instance.ClearMsgs();
  32. }
  33. IEnumerator ExecuteEveryFewSeconds()
  34. {
  35. while (true)
  36. {
  37. //
  38. if (OpenWhile)
  39. {
  40. // Debug.Log("sync msgs..");
  41. if (EnterHall)
  42. {
  43. ApiComp.Instance.GetMsgList(data =>
  44. {
  45. foreach (Api.Msg msg in data.msgs)
  46. {
  47. // Debug.Log(">>>>"+msg.type+">>"+msg.id);
  48. if (msg.type == MessageTypes.MsgRequestBattle.GetHashCode())
  49. {
  50. OnBattleRequest?.Invoke(msg.msgFromPlayer, msg.content);
  51. }
  52. else if (msg.type == MessageTypes.MsgBattleAgree.GetHashCode())
  53. {
  54. OnBattleAgree?.Invoke(msg.msgFromPlayer, msg.content);
  55. }
  56. else if (msg.type == MessageTypes.MsgBattleStart.GetHashCode())
  57. {
  58. OnBattleStart?.Invoke(msg.content);
  59. }
  60. else if (msg.type == MessageTypes.OnBattleClientMsg.GetHashCode())
  61. {
  62. Debug.Log("zane client msg:" + msg);
  63. OnBattleClientMsg?.Invoke(msg.msgFromPlayer, msg.content);
  64. }
  65. else if (msg.type == MessageTypes.MsgPlayerReady.GetHashCode())
  66. {
  67. OnBattlePlayerReady?.Invoke(msg.msgFromPlayer, msg.content);
  68. }
  69. ApiComp.Instance.EndMessage(msg.id);
  70. }
  71. }, (code, msg) => { });
  72. }
  73. }
  74. // 等待几秒钟
  75. if (end)
  76. {
  77. yield return null;
  78. }
  79. else
  80. {
  81. yield return new WaitForSeconds(IntervalTime);
  82. }
  83. }
  84. }
  85. }
  86. }