Created
December 11, 2015 16:26
-
-
Save ReaperUnreal/562c925965e42474f79b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using BVG.ExtensionMethods; | |
using System.IO; | |
using SimpleJSON; | |
using UnityEngine; | |
using BVG; | |
public class AccountController : Singleton<AccountController> { | |
public int AccountId { get; protected set; } | |
public int CoinBalance { get; protected set; } | |
public int CashBalance { get; protected set; } | |
public delegate void OnCurrencyUpdated(int coins, int cash); | |
public NetworkController networkController; | |
public OnCurrencyUpdated CurrencyChangeHandlers; | |
private bool currencyUpdateNeeded; | |
override protected void Awake() { | |
base.Awake(); | |
networkController = gameObject.AddComponent<NetworkController>() as NetworkController; | |
currencyUpdateNeeded = false; | |
CurrencyChangeHandlers += (coins, cash) => {}; | |
} | |
void Start() { | |
SmartFoxController.AddCommandDelegate("updateCoinBalance", OnCoinUpdate); | |
SmartFoxController.AddCommandDelegate("updateCashBalance", OnCashUpdate); | |
} | |
void OnDisable() { | |
SmartFoxController.RemoveCommandDelegate("updateCoinBalance", OnCoinUpdate); | |
SmartFoxController.RemoveCommandDelegate("updateCashBalance", OnCashUpdate); | |
} | |
public int GetCurrency(CurrencyTypeEnum type) { | |
if (type == CurrencyTypeEnum.CASH) { | |
return CashBalance; | |
} else { | |
return CoinBalance; | |
} | |
} | |
private void OnCoinUpdate(JSONNode data) { | |
int balance = data["balance"].AsInt; | |
CoinBalance = balance; | |
currencyUpdateNeeded = true; | |
Debug.Log("Got coin update: " + balance); | |
} | |
private void OnCashUpdate(JSONNode data) { | |
int balance = data["balance"].AsInt; | |
CashBalance = balance; | |
currencyUpdateNeeded = true; | |
Debug.Log ("Got cash update: " + balance); | |
} | |
void Update() { | |
if (currencyUpdateNeeded) { | |
CurrencyChangeHandlers(CoinBalance, CashBalance); | |
currencyUpdateNeeded = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment