Example #1
0
    public static PResponse Process(WWW www)
    {
        if (www == null)
        {
            return(PResponse.GeneralError(1));
        }

        if (!String.IsNullOrEmpty(www.error))
        {
            return(PResponse.GeneralError(www.error));
        }

        if (string.IsNullOrEmpty(www.text))
        {
            return(PResponse.Error(1));
        }

        var results = (Dictionary <string, object>)PJSON.JsonDecode(www.text);

        if (!results.ContainsKey("success") || !results.ContainsKey("errorcode"))
        {
            return(PResponse.GeneralError(1));
        }

        var response = new PResponse();

        response.success   = (bool)results["success"];
        response.errorcode = (int)(double)results["errorcode"];
        response.json      = results;
        return(response);
    }
    private IEnumerator SendSaveRequest(string section, string action, Dictionary <string, object> postdata, Action <PResponse> callback)
    {
        WWW www = PRequest.Prepare(section, action, postdata);

        yield return(www);

        PResponse response = PRequest.Process(www);

        callback(response);
    }
Example #3
0
    private IEnumerator SendListRequest <T>(Dictionary <string, object> postdata,
                                            Action <List <T>, int, PResponse> callback) where T : PlayerChallenge, new()
    {
        var www = PRequest.Prepare(SECTION, LIST, postdata);

        yield return(www);

        List <T> challenges    = null;
        int      numchallenges = 0;
        var      response      = default(QuickResponse <ListChallengeResponse <T> >);

        if (PRequest.WWWSuccess(www))
        {
            string data = www.text;

            Thread t = new Thread(() =>
            {
                response = PRequest.FastProcessUnityThread <ListChallengeResponse <T> >(data);
            });

            t.Start();

            // wait for thread to finish
            while (t.ThreadState == ThreadState.Running)
            {
                yield return(null);
            }
        }
        else
        {
            response = new QuickResponse <ListChallengeResponse <T> > {
                success = false, errorcode = 1
            };
        }

        if (response.success)
        {
            var challengeArray = response.ResponseObject.challenges;

            challenges = new List <T>();

            for (int i = 0; i < challengeArray.Length; i++)
            {
                challenges.Add(challengeArray[i]);
            }

            numchallenges = challenges.Count;
        }

        var resp = new PResponse {
            errorcode = response.errorcode, success = response.success
        };

        callback(challenges, numchallenges, resp);
    }
Example #4
0
    //=============================================================================

    void ScoreSubmitComplete(PResponse Response)
    {
        if (Response.success)
        {
            Debug.Log("Score saved!");
        }
        else
        {
            // Submission failed because of response.errormessage with response.errorcode
            Debug.Log("Score failed to save: " + Response.errormessage);
        }
    }
    /// <summary>
    /// Rates a level
    /// </summary>
    /// <param name="levelid">The LevelID</param>
    /// <param name="rating">The rating</param>
    /// <param name="callback">Callback function</param>
    public void Rate(string levelid, int rating, Action <PResponse> callback)
    {
        if (rating < 1 || rating > 10)
        {
            callback(PResponse.Error(401));
            return;
        }

        var postdata = new Dictionary <string, object>
        {
            { "levelid", levelid },
            { "rating", rating }
        };

        Playtomic.API.StartCoroutine(SendRateRequest(postdata, callback));
    }
Example #6
0
    private IEnumerator SendSaveLoadRequest <T>(string action,
                                                Dictionary <string, object> postdata,
                                                Action <T, PResponse> callback) where T : PlayerChallenge
    {
        var www = PRequest.Prepare(SECTION, action, postdata);

        yield return(www);

        var response  = default(QuickResponse <UpdateChallengeResponse <T> >);
        var challenge = default(T);

        if (PRequest.WWWSuccess(www))
        {
            string data = www.text;

            Thread t = new Thread(() =>
            {
                response = PRequest.FastProcessUnityThread <UpdateChallengeResponse <T> >(data);
            });

            t.Start();

            // wait for thread to finish
            while (t.ThreadState == ThreadState.Running)
            {
                yield return(null);
            }
        }
        else
        {
            response = new QuickResponse <UpdateChallengeResponse <T> > {
                success = false, errorcode = 1
            };
        }

        if (response.success)
        {
            challenge = response.ResponseObject.challenge;
        }

        var resp = new PResponse {
            success = response.success, errorcode = response.errorcode
        };

        callback(challenge, resp);
    }
Example #7
0
    //=============================================================================

    void ScoreListComplete(List <PlayerScore> Scores, int TotalScores, PResponse Response)
    {
        bool bVerbose = true;

        if (Response.success)
        {
            if (bVerbose)
            {
                Debug.Log("Score list ok: " + Scores.Count + "/" + TotalScores);
            }
            if (bVerbose)
            {
                foreach (PlayerScore CurScore in Scores)
                {
                    try
                    {
                        string Country = CurScore.fields.GetString("country");
                        //int Stage = CurScore.fields.GetInt( "stage" );

                                                #if UNITY_EDITOR
                        Debug.Log(CurScore.rank + ") " + CurScore.playername + "(" + Country + ") - " + CurScore.points + " " + CurScore.perpage);
                                                #endif
                    }
                    catch
                    {
                        Debug.Log("Error reading score data");
                    }
                }
            }

            if (GetScoreboardSuccessEvent != null)
            {
                GetScoreboardSuccessEvent(TotalScores, Scores);
            }
        }
        else
        {
            Debug.Log("Failed to retrieve scorelist: " + Response.errormessage);

            if (GetScoreboardFailEvent != null)
            {
                GetScoreboardFailEvent();
            }
        }
    }
Example #8
0
    private IEnumerator getReplay <T>(string section, string action, Dictionary <string, object> postdata, Action <T, PResponse> callback)
    {
        var www = PRequest.Prepare(section, action, postdata);

        yield return(www);

        var replay   = default(T);
        var response = new QuickResponse <GetReplayResponse <T> >();

        if (PRequest.WWWSuccess(www))
        {
            string data = www.text;

            // spool deserialization off to another thread, allows UI to continue updating in the meantime
            Thread t = new Thread(() =>
            {
                response = PRequest.FastProcessUnityThread <GetReplayResponse <T> >(data);
            });
            t.Start();

            // wait for thread to finish
            while (t.ThreadState == ThreadState.Running)
            {
                yield return(null);
            }

            if (response.success)
            {
                replay = response.ResponseObject.challenge["replay"];
            }
        }
        else
        {
            response = new QuickResponse <GetReplayResponse <T> > {
                success = false, errorcode = 1
            };
        }

        var resp = new PResponse {
            errorcode = response.errorcode, success = response.success
        };

        callback(replay, resp);
    }
Example #9
0
    private IEnumerator SendUpdate <T>(string section,
                                       string action,
                                       Dictionary <string, object> postdata,
                                       Action <bool, PResponse> callback) where T : PlayerChallenge, new()
    {
        var www = PRequest.Prepare(section, action, postdata);

        yield return(www);

        var response = default(QuickResponse <ReplaySentResponse>);

        if (PRequest.WWWSuccess(www))
        {
            string data = www.text;
            Thread t    = new Thread(() =>
            {
                response = PRequest.FastProcessUnityThread <ReplaySentResponse>(data);
            });

            t.Start();

            // wait for thread to finish
            while (t.ThreadState == ThreadState.Running)
            {
                yield return(null);
            }
        }
        else
        {
            response = new QuickResponse <ReplaySentResponse> {
                success = false, errorcode = 1
            };
        }

        var resp = new PResponse {
            errorcode = response.errorcode, success = response.success
        };

        callback(response.success, resp);
    }
Example #10
0
    public static PResponse Process(WWW www)
    {
        if(www == null)
            return PResponse.GeneralError(1);

        if (www.error != null)
            return PResponse.GeneralError(www.error);

        if (string.IsNullOrEmpty(www.text))
            return PResponse.Error(1);

        var results = (Dictionary<string,object>)PJSON.JsonDecode(www.text);

        if(!results.ContainsKey("success") || !results.ContainsKey("errorcode"))
            return PResponse.GeneralError(1);

        var response = new PResponse();
        response.success = (bool)results["success"];
        response.errorcode = (int)(double)results["errorcode"];
        response.json = results;
        return response;
    }