RegisterCodeMIX.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections;
  3. using Prime31;
  4. using UnityEngine;
  5. public class RegisterCodeMIX : MonoBehaviour
  6. {
  7. public enum ActivateStates
  8. {
  9. OtherError,
  10. RequestOrNetworkError,
  11. CouponAlreadyReedemed,
  12. BadInput,
  13. Done
  14. }
  15. private static RegisterCodeMIX _instance;
  16. public static RegisterCodeMIX Instance
  17. {
  18. get
  19. {
  20. if (!_instance)
  21. {
  22. _instance = new GameObject("_RegisterCodeMIX").AddComponent<RegisterCodeMIX>();
  23. DontDestroyOnLoad(_instance.gameObject);
  24. }
  25. return _instance;
  26. }
  27. }
  28. internal void Awake()
  29. {
  30. if (_instance && _instance != this)
  31. {
  32. Destroy(gameObject);
  33. return;
  34. }
  35. _instance = this;
  36. }
  37. private void TEST_CALL()
  38. {
  39. RegisterCodeMIX.Instance.Activate("N5CV3J7C4", (state, error) =>
  40. {
  41. Debug.LogWarning(string.Format("{0}:{1}", state, error));
  42. switch (state)
  43. {
  44. case ActivateStates.OtherError:
  45. //todo: handle error
  46. break;
  47. case ActivateStates.RequestOrNetworkError:
  48. //todo: handle error
  49. break;
  50. case ActivateStates.CouponAlreadyReedemed:
  51. //todo: handle error
  52. break;
  53. case ActivateStates.BadInput:
  54. //todo: handle error
  55. break;
  56. case ActivateStates.Done:
  57. //todo: EVERYTHING GOES WELL
  58. break;
  59. }
  60. });
  61. }
  62. public void Activate(string code, Action<ActivateStates, string> onDone)
  63. {
  64. StartCoroutine(ActivateRoutine(code, onDone));
  65. }
  66. private IEnumerator ActivateRoutine(string code, Action<ActivateStates, string> onDone)
  67. {
  68. if (string.IsNullOrEmpty(code))
  69. {
  70. onDone(ActivateStates.BadInput, "empty code");
  71. yield break;
  72. }
  73. //if (!Application.isEditor)
  74. //{
  75. var www = new WWW("https://game.gamatic.com/CokeCaps/MIX/activator.php?code=" + code);
  76. yield return www;
  77. if (!string.IsNullOrEmpty(www.error))
  78. {
  79. onDone(ActivateStates.RequestOrNetworkError, www.error);
  80. }
  81. else
  82. {
  83. try
  84. {
  85. var response = (JsonObject)Json.decode(www.text);
  86. var errorCode = response["errorCode"] == null ? "" : response["errorCode"].ToString();
  87. var errorDesc = response["errorDesc"] == null ? "" : response["errorDesc"].ToString();
  88. var redeemed = response["redeemed"] == null ? "" : response["redeemed"].ToString();
  89. if (string.IsNullOrEmpty(errorCode) && string.IsNullOrEmpty(errorDesc))
  90. {
  91. _capsDone++;
  92. onDone(ActivateStates.Done, "");
  93. KHD.FlurryAnalyticsTest.Instance.CapCode();
  94. }
  95. else
  96. {
  97. if (errorCode == "406")
  98. {
  99. onDone(ActivateStates.CouponAlreadyReedemed, errorDesc);
  100. }
  101. else if (errorCode == "400")
  102. {
  103. onDone(ActivateStates.BadInput, errorDesc);
  104. }
  105. else
  106. {
  107. onDone(ActivateStates.OtherError, errorDesc);
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. onDone(ActivateStates.RequestOrNetworkError, ex.Message);
  114. }
  115. }
  116. //}
  117. //else
  118. //{
  119. // _capsDone++;
  120. // onDone(ActivateStates.Done, "");
  121. //}
  122. }
  123. private static int _capsDone;
  124. public static bool CanRegisterMore
  125. {
  126. get { return _capsDone < 5; }
  127. }
  128. }