Exemplo n.º 1
0
    /// <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>
    /// 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();
    }
Exemplo n.º 3
0
    /// <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 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;
 }
Exemplo n.º 5
0
    /// <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>
    /// 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;
    }
Exemplo n.º 7
0
    /// <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>
    /// 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>
    /// 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>
    /// 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;
    }
Exemplo n.º 11
0
    /// <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();
    }
Exemplo n.º 12
0
    /// <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();
    }
Exemplo n.º 13
0
    /// <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();
    }