You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
3.9 KiB

  1. using System;
  2. using System.Collections;
  3. using CIG;
  4. using CIG.Translation;
  5. using CIGEnums;
  6. using SUISS.Core;
  7. using SUISSEngine;
  8. using UnityEngine;
  9. public class GiftsPopupState : PopupBaseState
  10. {
  11. public override void Init()
  12. {
  13. base.Init();
  14. this._webService = SingletonMonobehaviour<CIGWebService>.Instance;
  15. }
  16. public override void Enter(State oldState)
  17. {
  18. base.Enter(oldState);
  19. if (this.HasUserKey)
  20. {
  21. ((GiftsPopupView)this.View).Initialize(this._webService.UserKey);
  22. }
  23. else
  24. {
  25. ((GiftsPopupView)this.View).Initialize(string.Empty);
  26. if (this._webService != null)
  27. {
  28. this._webService.PullRequestCompleted += this.UpdateView;
  29. this._webService.ForcePullRequest();
  30. }
  31. }
  32. }
  33. public override void Leave(State newState)
  34. {
  35. base.Leave(newState);
  36. ((GiftsPopupView)this.View).Deinitialize();
  37. }
  38. public void OpenInstagram()
  39. {
  40. Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.InstagramUrl);
  41. }
  42. public void OpenFacebook()
  43. {
  44. Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.FacebookUrl);
  45. }
  46. public void OpenTwitter()
  47. {
  48. Application.OpenURL(SingletonMonobehaviour<CIGGameConstants>.Instance.TwitterUrl);
  49. }
  50. public void RedeemCode(string code)
  51. {
  52. if (this._webService == null)
  53. {
  54. UnityEngine.Debug.LogWarning("Cannot redeem code: null web service");
  55. return;
  56. }
  57. code = code.Trim();
  58. if (code.Length > 0)
  59. {
  60. base.StartCoroutine(this.RedeemRoutine(code));
  61. }
  62. }
  63. private bool HasUserKey
  64. {
  65. get
  66. {
  67. return this._webService != null && this._webService.UserKey != null && this._webService.UserKey.Length > 0;
  68. }
  69. }
  70. private void UpdateView()
  71. {
  72. if (this.HasUserKey)
  73. {
  74. ((GiftsPopupView)this.View).Initialize(this._webService.UserKey);
  75. }
  76. }
  77. private void DisableInput()
  78. {
  79. ((GiftsPopupView)this.View).UpdateInput(false);
  80. }
  81. private void EnableInput()
  82. {
  83. if (this._isInState)
  84. {
  85. ((GiftsPopupView)this.View).UpdateInput(true);
  86. }
  87. }
  88. private void ShowErrorPopup(ILocalizedString text)
  89. {
  90. this._fsm.SwitchState<GenericPopupState>().UpdateInfo(null, Localization.Key("oops_something_went_wrong"), text, Localization.Key("ok"), null, null, null, null, true);
  91. }
  92. private void ShowRewardPopup(Currencies reward)
  93. {
  94. base.ClosePopup(delegate()
  95. {
  96. SingletonMonobehaviour<PopupManager>.Instance.ShowReceiveRewardPopup(reward);
  97. });
  98. }
  99. private IEnumerator RedeemRoutine(string code)
  100. {
  101. this.DisableInput();
  102. WWW request = this._webService.RedeemCode(code);
  103. yield return request;
  104. if (request.error != null && request.error.Length > 0)
  105. {
  106. UnityEngine.Debug.LogError("Redeem code error: " + request.error);
  107. this.ShowErrorPopup(Localization.Key("social_code_error"));
  108. }
  109. else
  110. {
  111. string text = request.text;
  112. if (text == "FRIENDCODE_ALREADY_USED")
  113. {
  114. this.ShowErrorPopup(Localization.Key("social_code_already_used_friendcode"));
  115. }
  116. else if (text == "FRIENDCODE_LIMIT_REACHED")
  117. {
  118. this.ShowErrorPopup(Localization.Key("social_code_maximum_reached"));
  119. }
  120. else if (text == "GIFTCODE_ALREADY_USED")
  121. {
  122. this.ShowErrorPopup(Localization.Key("social_code_already_used_giftcode"));
  123. }
  124. else if (text == "GIFTCODE_EXPIRED")
  125. {
  126. this.ShowErrorPopup(Localization.Key("social_code_expired"));
  127. }
  128. else if (text == "UNKNOWN_CODE")
  129. {
  130. this.ShowErrorPopup(Localization.Key("social_code_unknown"));
  131. }
  132. else
  133. {
  134. int num;
  135. Currencies currencies = CIGWebService.ParseCurrencies(text, out num);
  136. if (currencies.IsEmpty())
  137. {
  138. UnityEngine.Debug.LogError("Result did not have currencies: '" + text + "'");
  139. }
  140. else
  141. {
  142. CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
  143. instance.EarnCurrencies(currencies, this);
  144. this.ShowRewardPopup(currencies);
  145. }
  146. }
  147. }
  148. this.EnableInput();
  149. yield break;
  150. }
  151. private CIGWebService _webService;
  152. }