Example #1
0
        public async Task SetScore(PlayerProfile profile, Guid levelid, FractionDifficulty diff, int time)
        {
            try
            {
                var ps = new RestParameterSet();
                ps.AddParameterInt("userid", profile.OnlineUserID);
                ps.AddParameterHash("password", profile.OnlinePasswordHash);
                ps.AddParameterString("app_version", GDConstants.Version.ToString());
                ps.AddParameterGuid("levelid", levelid);
                ps.AddParameterInt("difficulty", (int)diff);
                ps.AddParameterInt("leveltime", time);

                ps.AddParameterInt("s0", profile.TotalPoints);
                ps.AddParameterInt("s1", profile.GetWorldPoints(Levels.WORLD_001));
                ps.AddParameterInt("s2", profile.GetWorldPoints(Levels.WORLD_002));
                ps.AddParameterInt("s3", profile.GetWorldPoints(Levels.WORLD_003));
                ps.AddParameterInt("s4", profile.GetWorldPoints(Levels.WORLD_004));
                ps.AddParameterInt("t0", profile.TotalTime);
                ps.AddParameterInt("t1", profile.GetWorldTime(Levels.WORLD_001));
                ps.AddParameterInt("t2", profile.GetWorldTime(Levels.WORLD_002));
                ps.AddParameterInt("t3", profile.GetWorldTime(Levels.WORLD_003));
                ps.AddParameterInt("t4", profile.GetWorldTime(Levels.WORLD_004));
                ps.AddParameterInt("sx", profile.MultiplayerPoints);

                var response = await QueryAsync <QueryResultSetScore>("set-score", ps, RETRY_SETSCORE);

                if (response == null)
                {
                    return;                     // meh - internal server error
                }
                else if (response.result == "success")
                {
                    if (response.update)
                    {
                        MonoSAMGame.CurrentInst.DispatchBeginInvoke(() =>
                        {
                            profile.OnlineRevisionID = response.user.RevID;

                            MainGame.Inst.SaveProfile();

                            if (profile.NeedsReupload)
                            {
                                Reupload(profile).EnsureNoError();
                            }
                        });
                    }
                }
                else if (response.result == "error")
                {
                    ShowErrorCommunication();

                    if (response.errorid == BackendCodes.INTERNAL_EXCEPTION)
                    {
                        return;                         // meh
                    }
                    else if (response.errorid == BackendCodes.WRONG_PASSWORD || response.errorid == BackendCodes.USER_BY_ID_NOT_FOUND)
                    {
                        MonoSAMGame.CurrentInst.DispatchBeginInvoke(() =>
                        {
                            SAMLog.Error("Backend::SS_INVLOGIN", $"Local user cannot login on server ({response.errorid}:{response.errormessage}). Reset local user");

                            // something went horribly wrong
                            // create new user on next run
                            profile.OnlineUserID = -1;

                            MainGame.Inst.SaveProfile();
                        });
                    }
                    else
                    {
                        SAMLog.Error("Backend::SS_ERR", $"SetScore: Error {response.errorid}: {response.errormessage}");
                    }
                }
            }
            catch (RestConnectionException e)
            {
                SAMLog.Warning("Backend::SS_RCE", e);                 // probably no internet
                ShowErrorConnection();

                MonoSAMGame.CurrentInst.DispatchBeginInvoke(() =>
                {
                    profile.NeedsReupload = true;

                    MainGame.Inst.SaveProfile();
                });
            }
            catch (Exception e)
            {
                SAMLog.Error("Backend::SS_E", e);
                ShowErrorCommunication();

                MonoSAMGame.CurrentInst.DispatchBeginInvoke(() =>
                {
                    profile.NeedsReupload = true;

                    MainGame.Inst.SaveProfile();
                });
            }
        }