Skip to content

Instantly share code, notes, and snippets.

@ReaperUnreal
Created December 11, 2015 16:26
Show Gist options
  • Save ReaperUnreal/562c925965e42474f79b to your computer and use it in GitHub Desktop.
Save ReaperUnreal/562c925965e42474f79b to your computer and use it in GitHub Desktop.
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