AntiAddictionPoll.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using Network = TapTap.AntiAddiction.Internal.Network;
  6. namespace TapTap.AntiAddiction
  7. {
  8. /// <summary>
  9. /// 防沉迷轮询器
  10. /// </summary>
  11. internal class AntiAddictionPoll : MonoBehaviour
  12. {
  13. static readonly string ANTI_ADDICTION_POLL_NAME = "AntiAddictionPoll";
  14. static AntiAddictionPoll current;
  15. /// <summary>
  16. /// 轮询间隔,单位:秒
  17. /// </summary>
  18. private static int pollInterval = 2 * 60;
  19. private static Coroutine _pollCoroutine;
  20. private static float? _elpased;
  21. public static bool StartPoll;
  22. internal static void StartUp()
  23. {
  24. if (current == null)
  25. {
  26. GameObject pollGo = new GameObject(ANTI_ADDICTION_POLL_NAME);
  27. DontDestroyOnLoad(pollGo);
  28. current = pollGo.AddComponent<AntiAddictionPoll>();
  29. _elpased = null;
  30. }
  31. if (_pollCoroutine == null)
  32. {
  33. _pollCoroutine = current.StartCoroutine(current.Poll());
  34. StartPoll = true;
  35. }
  36. }
  37. internal static void StartCountdownRemainTime()
  38. {
  39. if (current == null)
  40. {
  41. GameObject pollGo = new GameObject(ANTI_ADDICTION_POLL_NAME);
  42. DontDestroyOnLoad(pollGo);
  43. current = pollGo.AddComponent<AntiAddictionPoll>();
  44. _elpased = null;
  45. }
  46. else
  47. {
  48. return;
  49. }
  50. _elpased = 0;
  51. }
  52. internal static void Logout()
  53. {
  54. StartPoll = false;
  55. _elpased = null;
  56. current?.StopAllCoroutines();
  57. _pollCoroutine = null;
  58. }
  59. private void Update()
  60. {
  61. if (_elpased != null)
  62. {
  63. _elpased += Time.unscaledDeltaTime;
  64. if (_elpased >= 1)
  65. {
  66. _elpased = 0;
  67. if (TapTapAntiAddictionManager.CurrentRemainSeconds != null)
  68. TapTapAntiAddictionManager.CurrentRemainSeconds--;
  69. }
  70. }
  71. }
  72. IEnumerator Poll()
  73. {
  74. while (true)
  75. {
  76. // 上报/检查可玩
  77. Task<PlayableResult> checkPlayableTask = TapTapAntiAddictionManager.CheckPlayableOnPolling();
  78. yield return new WaitUntil(() => checkPlayableTask.IsCompleted);
  79. Debug.LogFormat($"防沉迷 轮询检查是否可玩: {checkPlayableTask.Result.CanPlay} Time: " +
  80. $"{DateTime.Now:hh:mm:ss ddd} 剩余时间(秒): {checkPlayableTask.Result.RemainTime}");
  81. if (!checkPlayableTask.Result.CanPlay)
  82. {
  83. _elpased = null;
  84. break;
  85. }
  86. if (_elpased == null)
  87. _elpased = 0;
  88. yield return new WaitForSeconds(pollInterval - 1);
  89. }
  90. }
  91. /// <summary>
  92. /// 离开或者暂停的时候,发送心跳,方便 server 校对时间
  93. /// </summary>
  94. /// <param name="pauseStatus"></param>
  95. private void OnApplicationPause(bool pauseStatus)
  96. {
  97. SendPlayableRequest();
  98. }
  99. /// <summary>
  100. /// 离开或者暂停的时候,发送心跳,方便 server 校对时间
  101. /// </summary>
  102. private void OnApplicationQuit()
  103. {
  104. SendPlayableRequest();
  105. }
  106. private static void SendPlayableRequest()
  107. {
  108. #pragma warning disable CS4014
  109. Network.CheckPlayable();
  110. #pragma warning restore CS4014
  111. }
  112. }
  113. }