Пример #1
0
        public IHttpActionResult PutUsuario(long id, Usuario usuario)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != usuario.Id)
            {
                return(BadRequest());
            }

            db.Entry(usuario).State = System.Data.Entity.EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UsuarioExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public string FixConnData(string key)
        {
            if (!isValidKey(key))
            {
                return("NO");
            }

            using (var ctx = new apiContext())
            {
                //another layer as otherwise it fills the server's memory :O
                int sessions_count          = ctx.ServerSessions.Count();
                int session_selection_count = 5000;

                for (int session_current_count = 0; session_current_count < sessions_count; session_current_count = session_current_count + session_selection_count)
                {
                    var session_target = session_current_count + session_selection_count;
                    var sessions       = ctx.ServerSessions
                                         .Include(c => c.ServerSessionsDataConn)
                                         .Where(c => c.SessionId >= session_current_count && c.SessionId < session_target)
                                         .Where(c => c.SessionEnd.HasValue && c.ServerSessionsDataConn.Count > 0)
                                         .OrderByDescending(c => c.SessionId)
                                         .ToList();

                    foreach (var session in sessions)
                    {
                        var list = session.ServerSessionsDataConn.OrderBy(c => c.Stamp).ToList();
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i] != list.First())
                            {
                                TimeSpan span = list[i].Stamp - list[i - 1].Stamp;
                                list[i].Duration = (int)span.TotalSeconds;
                            }
                            else
                            {
                                //this is the first entry, use the session start
                                TimeSpan span = list[i].Stamp - session.SessionStart;
                                list[i].Duration = (int)span.TotalSeconds;
                            }
                        }
                    }

                    //Server does not survive big transactions, so we will have to force smaller transactions
                    ctx.SaveChanges();
                    logger.LogInformation("FixConnData: " + session_target.ToString() + "/" + sessions_count.ToString());
                }

                ctx.SaveChanges();
            }


            return("OK");
        }
Пример #3
0
        private apiContext InitializeDbContext(IConfiguration config)
        {
            var options = new DbContextOptionsBuilder <apiContext>()
                          .UseInMemoryDatabase(databaseName: "ApiDatabase" + Guid.NewGuid())
                          .Options;

            var ctx = new apiContext(Configuration, options);

            var admin = new User()
            {
                Name     = "Administrador",
                Email    = "*****@*****.**",
                IsActive = true,
            };

            var customer = new Customer()
            {
                Name     = "Olavo Neto",
                Email    = "*****@*****.**",
                IsActive = true
            };

            ctx.Users.Add(admin);
            ctx.Customers.Add(customer);
            ctx.SaveChanges();

            return(ctx);
        }
Пример #4
0
        public string CollatePlayerCounts(string key)
        {
            if (!isValidKey(key))
            {
                return("NO");
            }

            //This is dumb as hell but we have to do it
            //Get all player data

            var data = context.ServerPlayercounts.OrderBy(c => c.Date).ToList();

            if (data.Count > 0)
            {
                DateTime FirstTime = data.First().Date;
                //Make a proper date
                DateTime LastTime = new DateTime(FirstTime.Year, FirstTime.Month, FirstTime.Day, FirstTime.Hour, 0, 0);
                List <UpdatePlayerCountStruct> NewData = new List <UpdatePlayerCountStruct>();
                foreach (var item in data)
                {
                    if (item.Date >= LastTime.AddHours(1))
                    {
                        //There is likely to be huge gaps due to downtimes, so we have to consider it.
                        LastTime = new DateTime(item.Date.Year, item.Date.Month, item.Date.Day, item.Date.Hour, 0, 0);
                        NewData.Add(new UpdatePlayerCountStruct {
                            Date = LastTime, Count = item.PlayerCount
                        });
                    }
                    else
                    {
                        //Check the current value or create it if it's the first item
                        if (NewData.Any(c => c.Date == LastTime))
                        {
                            var it = NewData.SingleOrDefault(c => c.Date == LastTime);
                            if (item.PlayerCount > it.Count)
                            {
                                it.Count = item.PlayerCount;
                            }
                        }
                        else
                        {
                            //Create the initial record
                            NewData.Add(new UpdatePlayerCountStruct {
                                Date = LastTime, Count = item.PlayerCount
                            });
                        }
                    }
                }

                //Destroy all current data
                context.ServerPlayercounts.RemoveRange(context.ServerPlayercounts.ToList());
                context.SaveChanges();

                List <ServerPlayercounts> NewEntities = new List <ServerPlayercounts>();
                //Insert our new data
                foreach (var item in NewData)
                {
                    var it = new ServerPlayercounts();
                    it.Date        = item.Date;
                    it.PlayerCount = item.Count;
                    NewEntities.Add(it);
                }
                context.AddRange(NewEntities);
                context.SaveChanges();
                return("OK");
            }

            return("Nothing.");
        }
Пример #5
0
        public string CollateConnData(string key)
        {
            //What this does is remove multiple entries for the same system. Players change systems quite often and we do not need pinpoint accuracy of where they were at a given second.
            if (!isValidKey(key))
            {
                return("NO");
            }

            using (var ctx = new apiContext())
            {
                //another layer as otherwise it fills the server's memory :O
                int sessions_count          = ctx.ServerSessions.Count();
                int session_selection_count = 5000;

                for (int session_current_count = 0; session_current_count < sessions_count; session_current_count = session_current_count + session_selection_count)
                {
                    var session_target = session_current_count + session_selection_count;
                    var sessions       = ctx.ServerSessions
                                         .Include(c => c.ServerSessionsDataConn)
                                         .Where(c => c.SessionId >= session_current_count && c.SessionId < session_target)
                                         .Where(c => c.SessionEnd.HasValue && c.ServerSessionsDataConn.Count > 0)
                                         .OrderByDescending(c => c.SessionId)
                                         .ToList();

                    foreach (var session in sessions)
                    {
                        var list          = session.ServerSessionsDataConn.OrderBy(c => c.Stamp).ToList();
                        var ListOfSystems = new List <string>();
                        foreach (var item in list)
                        {
                            if (!ListOfSystems.Contains(item.Location.ToUpper()))
                            {
                                ListOfSystems.Add(item.Location.ToUpper());
                            }
                        }

                        foreach (var item in ListOfSystems)
                        {
                            //Use the oldest entry to keep a minimum of info
                            var Entries = list.Where(c => c.Location.ToUpper() == item).OrderByDescending(c => c.Stamp).ToList();
                            if (Entries.Count > 1)
                            {
                                var First = Entries.First();
                                foreach (var entry in Entries.Where(c => c.Id != First.Id).ToList())
                                {
                                    First.Duration += entry.Duration;
                                    ctx.ServerSessionsDataConn.Remove(entry);
                                }
                            }
                            //Don't care if there is not more than one entry
                        }
                    }

                    //Server does not survive big transactions, so we will have to force smaller transactions
                    ctx.SaveChanges();
                    logger.LogInformation("CollateConnData: " + session_target.ToString() + "/" + sessions_count.ToString());
                }

                ctx.SaveChanges();
            }

            return("OK");
        }
Пример #6
0
        public JsonResult Index(string key)
        {
            var model = new UpdateModel();

            if (!isValidKey(key))
            {
                model.Error = Ressources.ApiResource.UnauthorizedAccess;
                return(Json(model));
            }

            //Get the current data
            string Url = config.Value.ApiSettings.JsonLocation;
            UpdateResponseModel Result = new UpdateResponseModel();

            Result.Players = new List <UpdateResponsePlayerModel>();
            bool Success = false;

            using (var client = new HttpClient())
            {
                //Perhaps we can change this part of the API to async, but it could lead to problems with database access, so for now let's not

                client.BaseAddress = new Uri(Url);
                var request = client.GetAsync("player_status.json");
                request.Wait();

                if (request.Result.IsSuccessStatusCode)
                {
                    Success = true;
                    //This transformation is a bit of a hack, we will discard it when we can change the game server's response structure
                    var      Dic = JsonConvert.DeserializeObject <UpdateResponseRawModel>(request.Result.Content.ReadAsStringAsync().Result);
                    DateTime dt  = DateTime.ParseExact(Dic.Timestamp, "yyyyMMddTHHmmss", CultureInfo.InvariantCulture);
                    Result.Timestamp = dt;

                    foreach (var item in Dic.Players)
                    {
                        var mdl = new UpdateResponsePlayerModel();
                        mdl.Name   = item.Key;
                        mdl.Id     = item.Value.Id;
                        mdl.Ip     = item.Value.Ip;
                        mdl.Lag    = item.Value.Lag;
                        mdl.Loss   = item.Value.Loss;
                        mdl.Ping   = item.Value.Ping;
                        mdl.Ship   = item.Value.Ship;
                        mdl.System = item.Value.System;
                        Result.Players.Add(mdl);
                    }
                }
            }

            //We are done receiving the data, we can now update our database
            //We are more likely to have a successful request

            if (Success)
            {
                List <string> ProcessedPlayers = new List <string>();
                var           ActivePlayers    = context.ServerSessions.Include(c => c.ServerSessionsDataConn).Where(c => !c.SessionEnd.HasValue).ToList();
                //First, we're going to see if we have sessions to end or update
                foreach (var item in ActivePlayers)
                {
                    var      last_system = item.ServerSessionsDataConn.LastOrDefault();
                    TimeSpan Diff        = Result.Timestamp - last_system.Stamp;

                    //Is the player still online?
                    if (!Result.Players.Any(c => c.Name == item.PlayerName))
                    {
                        //Nope, end the session and compile stats
                        item.SessionEnd = DateTime.Now;
                        //We'll be able to remove these ternary operations later, but for now we have to do it as otherwise it will crash due to open sessions on the current database without any info about the new data tables
                        if (item.ServerSessionsDataConn.Count > 0)
                        {
                            item.PlayerLagAvg   = (int)item.ServerSessionsDataConn.Average(c => c.Lag);
                            item.PlayerLossAvg  = (int)item.ServerSessionsDataConn.Average(c => c.Loss);
                            item.PlayerPingAvg  = (int)item.ServerSessionsDataConn.Average(c => c.Ping);
                            item.PlayerLastShip = item.ServerSessionsDataConn.LastOrDefault().Ship;
                            item.ServerSessionsDataConn.LastOrDefault().Duration += (int)Diff.TotalSeconds;
                        }
                    }
                    else
                    {
                        var PlayerInfo = Result.Players.SingleOrDefault(c => c.Name == item.PlayerName);
                        //We're moving the amount of entries to one per system change instead of one per minute. This will improve performance with minimal differences.
                        //Not checking for null because there is always at least one entry
                        if (last_system.Location == PlayerInfo.System)
                        {
                            //The player hasn't changed systems. Update the current information.
                            last_system.Lag       = (last_system.Lag + PlayerInfo.Lag) / 2;
                            last_system.Loss      = (last_system.Loss + PlayerInfo.Loss) / 2;
                            last_system.Ping      = (last_system.Ping + PlayerInfo.Ping) / 2;
                            last_system.Ship      = "test";
                            last_system.Duration += (int)Diff.TotalSeconds;
                            last_system.Stamp     = Result.Timestamp;
                        }
                        else
                        {
                            //The player has changed systems
                            //Update the duration of the previous system
                            last_system.Duration += (int)Diff.TotalSeconds;

                            //Create a new system entry
                            var system = new ServerSessionsDataConn();
                            system.SessionId = item.SessionId;
                            system.Stamp     = Result.Timestamp;
                            system.Ship      = PlayerInfo.Ship;
                            system.Location  = PlayerInfo.System;
                            system.Ping      = PlayerInfo.Ping;
                            system.Lag       = PlayerInfo.Lag;
                            system.Loss      = PlayerInfo.Loss;
                            system.Duration  = 0;
                            item.ServerSessionsDataConn.Add(system);
                        }
                    }

                    ProcessedPlayers.Add(item.PlayerName);
                }

                //We've handled the active players. Now lets handle the new players
                foreach (var item in Result.Players.Where(c => !ProcessedPlayers.Contains(c.Name)))
                {
                    var Session = new ServerSessions();
                    context.ServerSessions.Add(Session);

                    Session.PlayerName     = item.Name;
                    Session.PlayerId       = item.Id;
                    Session.SessionIp      = item.Ip;
                    Session.SessionStart   = Result.Timestamp;
                    Session.PlayerLagAvg   = 0;
                    Session.PlayerLossAvg  = 0;
                    Session.PlayerPingAvg  = 0;
                    Session.PlayerLastShip = item.Ship;
                    //We do not compile stats here !!!

                    var system = new ServerSessionsDataConn();
                    system.Session  = Session;
                    system.Stamp    = Result.Timestamp;
                    system.Duration = 0;
                    system.Location = item.System;
                    system.Ship     = item.Ship;
                    system.Lag      = item.Lag;
                    system.Loss     = item.Loss;
                    system.Ping     = item.Ping;

                    Session.ServerSessionsDataConn.Add(system);
                }

                context.SaveChanges();
                model.Error = "OK";
                return(Json(model));
            }
            else
            {
                //Has the request failed? If so, end all current sessions.
                var ActivePlayers = context.ServerSessions.Where(c => !c.SessionEnd.HasValue).ToList();
                foreach (var item in ActivePlayers)
                {
                    item.SessionEnd = DateTime.Now;
                }

                context.SaveChanges();
                model.Error = Ressources.ApiResource.UpdateRequestFailed;
                return(Json(model));
            }
        }
Пример #7
0
 public bool create(khoahoc item)
 {
     _context.khoahoc.Add(item);
     _context.SaveChanges();
     return(true);
 }
Пример #8
0
 public bool create(Nganh nganh)
 {
     _context.Nganh.Add(nganh);
     _context.SaveChanges();
     return(true);
 }