123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 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<RegisterCodeMIX>();
- 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<ActivateStates, string> onDone)
- {
- StartCoroutine(ActivateRoutine(code, onDone));
- }
- private IEnumerator ActivateRoutine(string code, Action<ActivateStates, string> 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, "");
- //FlurryLogger.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; }
- }
- }
|