/// <summary> /// Loads data. /// </summary> /// <param name="success">Callback triggers on successful data load.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine LoadData(SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.loadData); www.OnSuccess += delegate { Hashtable result = www.result as Hashtable; if (result["data"] == null) { drDebug.LogWarning("No data exists in the data store to load"); return; } if (data == null) { data = new Dictionary<string, object>(); } StoreReturnData(result["data"] as Hashtable); drDebug.Log("Loaded data"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error loading data: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Fetches the names of the available leaderboards, saving them in the drLeaderboards.available array. /// </summary> /// <param name="success">Callback triggers on successful data save.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine FetchAvailableLeaderboards(dimeRocker.SuccessHandler success, dimeRocker.ErrorHandler error) { drWWW www = new drWWW(drAPI.leaderboardList); www.OnSuccess += delegate { ArrayList result = www.result as ArrayList; if (result.Count == 0) { availableLeaderboards = new string[0]; drDebug.LogWarning("No leaderboards have been set up"); return; } availableLeaderboards = new string[result.Count]; for (int i = 0; i < result.Count; i++) { availableLeaderboards[i] = result[i].ToString(); } drDebug.Log("Fetched leaderboards available"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error listing available leaderboards: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Awards an achievement. /// </summary> /// <param name="achievementName">The name of the achievement to award.</param> /// <param name="success">Callback triggers on successful award.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine AwardAchievement(string achievementName, SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.awardAchievement); www.AddField("name", achievementName); www.OnSuccess += delegate { if (!(bool)www.result) { // This should never occur, but the check is here just in case drDebug.LogWarning("Did not award achievement \"" + achievementName + "\""); return; } if (!_awardedAchievements.Contains(achievementName)) { _awardedAchievements.Add(achievementName); } drClient.RefreshCanvasComponent(drClient.CanvasComponent.Achievements); drDebug.Log("Awarded achievement " + achievementName); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error awarding achievement \"" + achievementName + "\": " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Constructs a new API struct. /// </summary> /// <param name="version">The version number.</param> /// <param name="path">The path.</param> /// <param name="method">The request type.</param> /// <param name="sign">Does the call require a sha1 signature</param> /// <param name="sendToken">Does the call require a request token</param> public API(int version, string path, drWWW.Method method, bool sign, bool sendToken) { this.version = version; this.path = path; this.method = method; this.sign = sign; this.sendToken = sendToken; }
/// <summary> /// Fetches tokens. /// </summary> public static Coroutine FetchTokens() { drWWW www = new drWWW(drAPI.tokenGenerate); www.AddField("amount", 25); www.OnSuccess += delegate { ArrayList result = www.result as ArrayList; foreach (string token in result) { tokens.Enqueue(token); } drDebug.Log("Fetched tokens"); }; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching tokens: " + errorMessage); }; return www.Fetch(); }
/// <summary> /// Submits a score to the leaderboard. /// </summary> /// <param name="score">The player's score.</param> /// <param name="success">Callback triggers on successful score submission.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine SubmitScore(int score, SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.submitScore); www.AddField("score", score); www.OnSuccess += delegate { if (!(bool)www.result) { drDebug.LogWarning("Score not recorded; it did not beat the user's current high score"); return; } drClient.RefreshCanvasComponent(drClient.CanvasComponent.Leaderboard); drClient.RefreshCanvasComponent(drClient.CanvasComponent.ActivityFeed); drDebug.Log("Submitted new score"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error submitting score: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Creates a WWW object prepopulated with fields common to all calls. /// </summary> /// <param name="api">The call's API.</param> /// <param name="name">The name of the counter.</param> /// <param name="userIds">The user IDs.</param> /// <returns>The WWW object.</returns> static drWWW GetWWW(drAPI.API api, string name, int[] userIds) { drWWW www = new drWWW(api); www.AddField("name", name); if (userIds != null && userIds.Length > 0) { string userIdsJson = JSON.JsonEncode(userIds); www.AddField("uids", userIdsJson); } return www; }
/// <summary> /// Removes an item from the inventory. A common use for this is consumable items (health packs, powerups, etc.) that get used up. /// </summary> /// <param name="itemName">The name of the item.</param> /// <param name="quantity">The quantity of the item to remove.</param> /// <param name="success">Callback triggers on successful remove.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine RemoveItem(string itemName, int quantity, SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.removeItem); www.AddField("name", itemName); www.AddField("quantity", quantity); www.OnSuccess += delegate { if (!(bool)www.result) { drDebug.LogWarning("Item " + itemName + " not removed from inventory"); return; } Item itemToRemove = GetItem(itemName); if (itemToRemove == null) { drDebug.LogWarning("Item " + itemName + " is not contained in your inventory"); return; } itemToRemove.quantity -= quantity; if (itemToRemove.quantity < 0) { drDebug.LogWarning("Item " + itemName + " has a quantity less than " + quantity); } // Remove the item entirely if its quantity is zero if (itemToRemove.quantity <= 0) { _inventory.Remove(itemToRemove); } drClient.RefreshCanvasComponent(drClient.CanvasComponent.Inventory); drDebug.Log("Item " + itemName + " removed from inventory"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error removing item: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// FeFetches the user's awarded achievements. /// </summary> /// <param name="success">Callback triggers on successful fetch.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine FetchAchievements(SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.fetchAchievements); www.OnSuccess += delegate { Hashtable result = www.result as Hashtable; Hashtable achievements = result["achievements"] as Hashtable; ArrayList awarded = achievements["awarded"] as ArrayList; ArrayList unawarded = achievements["unawarded"] as ArrayList; _awardedAchievements = new List<string>(); foreach (Hashtable achievement in awarded) { _awardedAchievements.Add(achievement["name"] as string); } drDebug.Log("Fetched " + awarded.Count + " awarded achievements of " + (awarded.Count + unawarded.Count) + " total"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching achievements: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Saves data with the specified key. /// </summary /// <param name="key">The key to save the data by.</param> /// <param name="data">The data to save.</param> /// <param name="success">Callback triggers on successful data save.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine SaveDataByKey(string key, object data, SuccessHandler success, ErrorHandler error) { Hashtable dataTable = new Hashtable(); dataTable.Add("key", key); dataTable.Add("value", data); string json = JSON.JsonEncode(dataTable); drWWW www = new drWWW(drAPI.saveDataByKey); www.AddField("data", json); www.OnSuccess += delegate { if (!(bool)www.result) { drDebug.LogWarning("No data saved with key " + key); return; } Hashtable toSave = new Hashtable(); toSave.Add(key, data); StoreReturnData(toSave); drDebug.Log("Saved data with key " + key); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error saving data: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Submits an item to the activity feed. /// </summary> /// <param name="message">The text to display (140 character maximum).</param> /// <param name="success">Callback triggers on successful activity feed submission.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine SubmitActivity(string message, SuccessHandler success, ErrorHandler error) { if (message == null || message == "") { drDebug.LogWarning("Message not submitted to activity feed: a null or empty message was supplied"); return null; } else if (message.Length > 140) { drDebug.LogWarning("Message not submitted to activity feed: amessage have to be less than or equal to 140 characters"); return null; } drWWW www = new drWWW(drAPI.addCustomActivity); www.AddField("message", message); www.OnSuccess += delegate { if (!(bool)www.result) { // This should never occur, but the check is here just in case drDebug.LogWarning("Message not submitted to activity feed"); return; } drClient.RefreshCanvasComponent(drClient.CanvasComponent.ActivityFeed); drDebug.Log("Message submitted to activity feed"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error submitting to activity feed: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Creates a WWW object prepopulated with fields common to all calls. /// </summary> /// <param name="api">The call's API.</param> /// <param name="ns">The namespace of the data.</param> /// <param name="userId">The user's ID.</param> /// <returns>The WWW object.</returns> static drWWW GetWWW(drAPI.API api, string ns, int? userId) { drWWW www = new drWWW(api); www.AddField("namespace", ns); if (userId.HasValue) { www.AddField("uid", userId.Value); } return www; }
/// <summary> /// Constructs a new API struct. /// </summary> /// <param name="version">The version number.</param> /// <param name="path">The path.</param> /// <param name="method">The request type.</param> public API(int version, string path, drWWW.Method method) { this.version = version; this.path = path; this.method = method; }
/// <summary> /// Fetches the server time. /// </summary> /// <param name="success">Callback triggers on successful session fetch.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine FetchSession(SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.fetchSession); www.OnSuccess += delegate { Hashtable result = www.result as Hashtable; drClient.sessionId = result["_token"].ToString(); drDebug.Log("Fetched session " + drClient.sessionId); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching session: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Saves data. /// </summary /// <param name="data">The data to save.</param> /// <param name="success">Callback triggers on successful data save.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine SaveData(Hashtable data, SuccessHandler success, ErrorHandler error) { string json = JSON.JsonEncode(data); drWWW www = new drWWW(drAPI.saveData); www.AddField("data", json); www.OnSuccess += delegate { if (!(bool)www.result) { drDebug.LogWarning("No data saved"); return; } StoreReturnData(data); drDebug.Log("Data saved"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error saving data: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Fetches the items in the inventory. /// </summary> /// <param name="success">Callback triggers on successful fetch.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine FetchItems(SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.fetchItems); www.OnSuccess += delegate { Hashtable result = www.result as Hashtable; _inventory = new List<Item>(); foreach (Hashtable item in result["items"] as ArrayList) { _inventory.Add(new Item(item)); } drDebug.Log("Fetched " + inventory.Length + " inventory items"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching items: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Fetches the server time. /// </summary> /// <param name="success">Callback triggers on successful time fetch.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine FetchServerTime(SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.fetchServerTime); www.OnSuccess += delegate { serverTimeAtLastFetch = drUtil.ConvertFromUnixTimestamp((double)www.result); timeFetched = System.DateTime.Now; drDebug.Log("Fetched time from server"); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching server time: " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Submits a score to the leaderboard. /// </summary> /// <param name="leaderboardName">The leaderboard to post the score to.</param> /// <param name="score">The user's score.</param> /// <param name="values">Arbitrary values to attach to the score (maximum three).</param> /// <param name="success">Callback triggers on successful data save.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine PostScore(string leaderboardName, int score, string[] values, dimeRocker.SuccessHandler success, dimeRocker.ErrorHandler error) { drWWW www = new drWWW(drAPI.leaderboardPostScore); www.AddField("name", leaderboardName); www.AddField("score", score); if (values != null) { for (int i = 0; i < Mathf.Max(values.Length, 3); i++) { // Skip empty or null values if (values[i] == null || values[i] == "") { continue; } www.AddField("v" + (i + 1), values[i]); } } www.OnSuccess += delegate { int rank = 0; int.TryParse(www.result.ToString(), out rank); if (rank == 0) { drDebug.LogWarning("User did not rank"); return; } drDebug.Log("Score posted to leaderboard " + leaderboardName + " with rank " + rank); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error posting score to leaderboard " + leaderboardName + ": " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Fetches the leaderboard scores. /// </summary> /// <param name="leaderboardName">The name of the leaderboard.</param> /// <param name="offset">The entry to start from. A negative number will return from the end instead. Default is 0.</param> /// <param name="length">The number of entries to return. Default is 10.</param> /// <param name="success">Callback triggers on successful data save.</param> /// <param name="error">Callback triggers on error.</param> static Coroutine FetchLeaderboardEntries(string leaderboardName, int? offset, int? length, dimeRocker.SuccessHandler success, dimeRocker.ErrorHandler error) { drWWW www = new drWWW(drAPI.leaderboardGet); www.AddField("name", leaderboardName); if (offset.HasValue) { www.AddField("offset", offset.Value); } if (length.HasValue) { www.AddField("length", length.Value); } www.OnSuccess += delegate { ArrayList result = www.result as ArrayList; List<Entry> entries = new List<Entry>(); foreach (Hashtable entryData in result) { Entry entry = new Entry(entryData); entries.Add(entry); } Leaderboard lb = GetLeaderboard(leaderboardName); if (lb == null) { lb = new Leaderboard(leaderboardName); fetchedLeaderboards.Add(lb); } lb.AddEntries(offset.HasValue ? offset.Value + 1 : 1, entries.ToArray()); drDebug.Log("Scores fetched from leaderboard " + leaderboardName); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error fetching scores from leaderboard " + leaderboardName + ": " + errorMessage); }; www.OnError += error; return www.Fetch(); }
/// <summary> /// Creates a WWW object prepopulated with fields common to all calls. /// </summary> /// <param name="api">The call's API.</param> /// <param name="name">The name of the wallet.</param> /// <returns>The WWW object.</returns> static drWWW GetWWW(drAPI.API api, string name) { drWWW www = new drWWW(api); www.AddField("name", name); return www; }
/// <summary> /// Loads data with the specified key. /// </summary> /// <param name="key">The key to load the data by.</param> /// <param name="success">Callback triggers on successful data load.</param> /// <param name="error">Callback triggers on error.</param> public static Coroutine LoadDataByKey(string key, SuccessHandler success, ErrorHandler error) { drWWW www = new drWWW(drAPI.loadDataByKey); www.AddField("key", key); www.OnSuccess += delegate { Hashtable result = www.result as Hashtable; if (result["data"] == null) { drDebug.LogWarning("No data with key " + key + " exists in the data store to load"); return; } StoreReturnData(result["data"] as Hashtable); drDebug.Log("Loaded data with key " + key); }; www.OnSuccess += success; www.OnError += delegate (string errorMessage) { drDebug.LogError("Error loading data with key " + key + ": " + errorMessage); }; www.OnError += error; return www.Fetch(); }