public static CampaignRun FromCompressed(string value)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(value);

        using (MemoryStream stream = new(bytes))
        {
            return(CampaignRun.FromCompressed(stream));
        }
    }
        public async Task <IDataAccessDataResponse> GetResponse(HttpContext httpContext, XDocument xml)
        {
            uint userId = httpContext.IsAuthenicatedPr3User();

            if (userId > 0)
            {
                XElement data = xml.Element("Params");
                if (data != null)
                {
                    uint   levelId      = (uint?)data.Element("p_level_id") ?? throw new DataAccessProcedureMissingData();
                    uint   levelVersion = (uint?)data.Element("p_level_version") ?? throw new DataAccessProcedureMissingData();
                    string recordRun    = (string)data.Element("p_recorded_run") ?? throw new DataAccessProcedureMissingData();
                    int    finishTime   = (int?)data.Element("p_finish_time") ?? throw new DataAccessProcedureMissingData();

                    CampaignRun campaignRun = null;
                    using (MemoryStream compressedMemoryStream = new MemoryStream(Convert.FromBase64String(recordRun)))
                    {
                        using (InflaterInputStream inflater = new InflaterInputStream(compressedMemoryStream))
                        {
                            using (MemoryStream uncompressedMemoryStream = new MemoryStream())
                            {
                                inflater.CopyTo(uncompressedMemoryStream);

                                campaignRun = JsonConvert.DeserializeObject <CampaignRun>(Encoding.UTF8.GetString(uncompressedMemoryStream.ToArray()));
                            }
                        }
                    }

                    if (campaignRun != null)
                    {
                        PlayerUserData playerUserData = await UserManager.TryGetUserDataByIdAsync(userId);

                        if (campaignRun.Username != playerUserData.Username)
                        {
                            return(new DataAccessErrorResponse("Invalid username"));
                        }

                        await CampaignManager.SaveCampaignRun(userId, levelId, levelVersion, recordRun, finishTime);

                        return(new DataAccessSaveCampaignRun3Response());
                    }
                    else
                    {
                        throw new DataAccessProcedureMissingData();
                    }
                }
                else
                {
                    throw new DataAccessProcedureMissingData();
                }
            }
            else
            {
                return(new DataAccessErrorResponse("You are not logged in!"));
            }
        }
Exemplo n.º 3
0
    public async Task <IDataAccessDataResponse> GetResponseAsync(HttpContext httpContext, XDocument xml)
    {
        uint userId = httpContext.IsAuthenicatedPr3User();

        if (userId > 0)
        {
            XElement data = xml.Element("Params");
            if (data != null)
            {
                uint   levelId      = (uint?)data.Element("p_level_id") ?? throw new DataAccessProcedureMissingData();
                uint   levelVersion = (uint?)data.Element("p_level_version") ?? throw new DataAccessProcedureMissingData();
                string recordRun    = (string)data.Element("p_recorded_run") ?? throw new DataAccessProcedureMissingData();
                int    finishTime   = (int?)data.Element("p_finish_time") ?? throw new DataAccessProcedureMissingData();

                CampaignRun campaignRun = CampaignRun.FromCompressed(recordRun);

                if (campaignRun != null)
                {
                    PlayerUserData playerUserData = await UserManager.TryGetUserDataByIdAsync(userId);

                    if (campaignRun.Username != playerUserData.Username)
                    {
                        return(new DataAccessErrorResponse("Invalid username"));
                    }

                    await CampaignManager.SaveCampaignRunAsync(userId, levelId, levelVersion, recordRun, finishTime);

                    return(new DataAccessSaveCampaignRun3Response());
                }
                else
                {
                    throw new DataAccessProcedureMissingData();
                }
            }
            else
            {
                throw new DataAccessProcedureMissingData();
            }
        }
        else
        {
            return(new DataAccessErrorResponse("You are not logged in!"));
        }
    }
 internal void AddRun(uint userId, int time, CampaignRun run)
 {
     this.Rows.Add(new UserCampaignRun(userId, time, run));
 }