示例#1
0
        public IHttpActionResult PutData(PutDataRequest request)
        {
            try
            {
                var sw = Stopwatch.StartNew();

                if (request.ApiKey != ApiAdminKey)
                {
                    Log.Warn($"Invalid API key on {nameof(PutData)}: {request.ApiKey}");
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content      = new StringContent("Incorrect key! Your try was logged."),
                        ReasonPhrase = "Incorrect key"
                    });
                }

                if (request.Context == "Player")
                {
                    var player = request.GetObject <Player>();
                    if (player == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null Player was sent."),
                            ReasonPhrase = "Null Player"
                        });
                    }

                    var connectionString = ConfigurationManager.ConnectionStrings["Store"].ConnectionString;
                    var db = new KeyStore(connectionString);
                    db.Set(player);
                }
                else
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content      = new StringContent("Invalid Context."),
                        ReasonPhrase = "Invalid Context."
                    });
                }


                Log.Debug($"PutData in {sw.Elapsed}");
                return(Ok());
            }
            catch (HttpResponseException ex)
            {
                Log.Error("PutData", ex);
                throw;
            }
            catch (Exception ex)
            {
                Log.Error("PutData", ex);
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(ex.Message),
                    ReasonPhrase = "Put data failed"
                });
            }
        }
示例#2
0
        public IHttpActionResult PutData(PutDataRequest request)
        {
            try
            {
                var sw = Stopwatch.StartNew();

                if (request.ApiKey != ApiAdminKey)
                {
                    Log.Warn($"Invalid API key on {nameof(PutData)}: {request.ApiKey}");
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content      = new StringContent("Incorrect key! Your try was logged."),
                        ReasonPhrase = "Incorrect key"
                    });
                }

                if (request.Context == PutDataRequestContext.Player)
                {
                    var o = request.GetObject <Player>();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null Player was sent."),
                            ReasonPhrase = "Null Player"
                        });
                    }

                    var connectionString = ConfigurationManager.ConnectionStrings["Store"].ConnectionString;
                    var db = new KeyStore(connectionString);
                    db.Set(o);
                }
                else if (request.Context == PutDataRequestContext.Leaderboard)
                {
                    if (string.IsNullOrWhiteSpace(request.Title))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent($"A title is required for context {PutDataRequestContext.Leaderboard}."),
                            ReasonPhrase = "No Title"
                        });
                    }

                    var o = request.GetObject <Leader[]>();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null Leader[] was sent."),
                            ReasonPhrase = "Null Leaders"
                        });
                    }

                    var file = Path.Combine(GlobalHelper.DataFolder, "Tanks", $"{request.Title}.Leaders.json");
                    var json = JsonConvert.SerializeObject(o, Formatting.Indented);

                    RetryPolicy.Default.ExecuteAction(() => { File.WriteAllText(file, json, Encoding.UTF8); });
                }
                else if (request.Context == PutDataRequestContext.TankMoE)
                {
                    if (string.IsNullOrWhiteSpace(request.Title))
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent($"A title is required for context {PutDataRequestContext.TankMoE}."),
                            ReasonPhrase = "No Title"
                        });
                    }

                    var o = request.GetObject <IDictionary <long, TankMoe> >();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null Leader[] was sent."),
                            ReasonPhrase = "Null Leaders"
                        });
                    }

                    var file = Path.Combine(GlobalHelper.DataFolder, "MoE", $"{request.Title}.moe.json");
                    var json = JsonConvert.SerializeObject(o, Formatting.Indented);

                    RetryPolicy.Default.ExecuteAction(() => { File.WriteAllText(file, json, Encoding.UTF8); });
                }
                else if (request.Context == PutDataRequestContext.TankWN8)
                {
                    var o = request.GetObject <Wn8ExpectedValues>();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null WN8 was sent."),
                            ReasonPhrase = "Null WN8"
                        });
                    }

                    RetryPolicy.Default.ExecuteAction(() => { o.ToFile(GlobalHelper.DataFolder); });
                }
                else if (request.Context == PutDataRequestContext.TankReference)
                {
                    var o = request.GetObject <TankReference>();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null TankReference was sent."),
                            ReasonPhrase = "Null TankReference"
                        });
                    }

                    RetryPolicy.Default.ExecuteAction(() => { o.ToFile(GlobalHelper.DataFolder); });
                }
                else if (request.Context == PutDataRequestContext.Clan)
                {
                    var o = request.GetObject <Clan>();
                    if (o == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable)
                        {
                            Content      = new StringContent("Null Clan was sent."),
                            ReasonPhrase = "Null Clan"
                        });
                    }

                    RetryPolicy.Default.ExecuteAction(() => { o.ToFile(GlobalHelper.DataFolder); });
                }
                else
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content      = new StringContent("Invalid Context."),
                        ReasonPhrase = "Invalid Context."
                    });
                }

                Log.Debug($"PutData in {sw.Elapsed}");
                return(Ok());
            }
            catch (HttpResponseException ex)
            {
                Log.Error("PutData", ex);
                throw;
            }
            catch (Exception ex)
            {
                Log.Error("PutData", ex);
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(ex.Message),
                    ReasonPhrase = "Put data failed"
                });
            }
        }