using System; using System.Collections; using Prime31; using UnityEngine; public class RegisterCodeMIX : MonoBehaviour { public enum ActivateStates { OtherError, RequestOrNetworkError, CouponAlreadyReedemed, BadInput, Done } private static RegisterCodeMIX _instance; public static RegisterCodeMIX Instance { get { if (!_instance) { _instance = new GameObject("_RegisterCodeMIX").AddComponent(); DontDestroyOnLoad(_instance.gameObject); } return _instance; } } internal void Awake() { if (_instance && _instance != this) { Destroy(gameObject); return; } _instance = this; } private void TEST_CALL() { RegisterCodeMIX.Instance.Activate("N5CV3J7C4", (state, error) => { Debug.LogWarning(string.Format("{0}:{1}", state, error)); switch (state) { case ActivateStates.OtherError: //todo: handle error break; case ActivateStates.RequestOrNetworkError: //todo: handle error break; case ActivateStates.CouponAlreadyReedemed: //todo: handle error break; case ActivateStates.BadInput: //todo: handle error break; case ActivateStates.Done: //todo: EVERYTHING GOES WELL break; } }); } public void Activate(string code, Action onDone) { StartCoroutine(ActivateRoutine(code, onDone)); } private IEnumerator ActivateRoutine(string code, Action onDone) { if (string.IsNullOrEmpty(code)) { onDone(ActivateStates.BadInput, "empty code"); yield break; } //if (!Application.isEditor) //{ var www = new WWW("https://game.gamatic.com/CokeCaps/MIX/activator.php?code=" + code); yield return www; if (!string.IsNullOrEmpty(www.error)) { onDone(ActivateStates.RequestOrNetworkError, www.error); } else { try { var response = (JsonObject)Json.decode(www.text); var errorCode = response["errorCode"] == null ? "" : response["errorCode"].ToString(); var errorDesc = response["errorDesc"] == null ? "" : response["errorDesc"].ToString(); var redeemed = response["redeemed"] == null ? "" : response["redeemed"].ToString(); if (string.IsNullOrEmpty(errorCode) && string.IsNullOrEmpty(errorDesc)) { _capsDone++; onDone(ActivateStates.Done, ""); KHD.FlurryAnalyticsTest.Instance.CapCode(); } else { if (errorCode == "406") { onDone(ActivateStates.CouponAlreadyReedemed, errorDesc); } else if (errorCode == "400") { onDone(ActivateStates.BadInput, errorDesc); } else { onDone(ActivateStates.OtherError, errorDesc); } } } catch (Exception ex) { onDone(ActivateStates.RequestOrNetworkError, ex.Message); } } //} //else //{ // _capsDone++; // onDone(ActivateStates.Done, ""); //} } private static int _capsDone; public static bool CanRegisterMore { get { return _capsDone < 5; } } }