public bool CheckDBAgent(ulong steamID) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); return(db.Agents.Where(x => x.SteamID == steamID).Any()); }
public async Task ArchiveRoomData(List <Rooms> rooms) { if (rooms.Count != 0) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); foreach (Rooms room in rooms) { try { room.DBCreatorSteamId = room.Creator.SteamID; room.DBCurrentMap = room.CurrentMap.Id; if (room.Password != null) { room.Passworded = true; } else { room.Passworded = false; } Rooms dbRoom = db.Rooms.Where(x => x.RoomId == room.RoomId).FirstOrDefault(); if (dbRoom == null) { db.Rooms.Add(room); } else { dbRoom.Name = room.Name; dbRoom.Passworded = room.Passworded; dbRoom.Region = room.Region; dbRoom.Official = room.Official; dbRoom.Ranked = room.Ranked; dbRoom.Version = room.Version; dbRoom.Position = room.Position; dbRoom.AgentCount = room.AgentCount; dbRoom.CreatorId = room.CreatorId; dbRoom.DBCreatorSteamId = room.DBCreatorSteamId; dbRoom.LastUpdate = room.LastUpdate; dbRoom.DBCurrentMap = room.CurrentMap.Id; db.Update(dbRoom); } await db.SaveChangesAsync(); } catch (Exception e1) { Log.Error(e1, $"ROOM ID: {room.RoomId} | Name: {room.Name} | Creator: {room.Creator.Name}"); } } } else { await Task.CompletedTask; } }
public async Task <List <WorkshopMap> > GetMapsFromDBAsync() { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); List <WorkshopMap> workshopMaps = new List <WorkshopMap>(); workshopMaps = await db.WorkshopMaps.OrderByDescending(x => x.Favorited).ToListAsync(); return(workshopMaps); }
/// <summary> /// Return an agent that is stored in the database. /// </summary> /// <param name="steamID">SteamID64.</param> /// <returns>An awaitable Task.</returns> public async Task <AgentsDB> GetDBAgentAsync(ulong steamID) { AgentsDB agent = new AgentsDB(); using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); if (this.CheckDBAgent(steamID)) { agent = db.Agents.Where(x => x.SteamID == steamID).FirstOrDefault(); } else { await this.StoreAgentDBAsync(steamID); } return(agent); }
public async Task <string> GetDBWorkshopMapCreator(ulong id) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); AgentsDB agent = new AgentsDB(); if (this.agentService.CheckDBAgent(id)) { agent = await this.agentService.GetDBAgentAsync(id); } else { await this.agentService.StoreAgentDBAsync(id); agent = await this.agentService.GetDBAgentAsync(id); } return(agent.Name); }
public async Task <List <WorkshopMap> > GetRecentlyUpdatedOrAddedAsync() { List <WorkshopMap> workshopMaps = await this.GetAllMapsAsync(); List <WorkshopMap> updatedMaps = new List <WorkshopMap>(); using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); foreach (WorkshopMap map in workshopMaps) { map.TimeCreated = UnixTimeStampToDateTime(System.Convert.ToDouble(map.UploadDate)); map.TimeUpdated = UnixTimeStampToDateTime(System.Convert.ToDouble(map.MapUpdated)); map.CreatorSteamID = System.Convert.ToUInt64(map.APICreator, CultureInfo.CurrentCulture); // if we have the map already stored in db, check if there is an update. if (db.WorkshopMaps.Any(x => x.FileID == map.FileID)) { // Updated map WorkshopMap dbWorkshopMap = db.WorkshopMaps.Where(x => x.FileID == map.FileID).FirstOrDefault(); if (dbWorkshopMap.TimeUpdated.Date > map.TimeUpdated) { updatedMaps.Add(map); db.WorkshopMaps.Update(map); } await db.SaveChangesAsync(); } else { // New map updatedMaps.Add(map); db.WorkshopMaps.Add(map); Log.Information("[WORKSHOP] Stored new map in database."); await db.SaveChangesAsync(); } } return(updatedMaps); }
public async Task StoreAllMaps(List <WorkshopMap> maps) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); foreach (WorkshopMap map in maps) { map.TimeCreated = UnixTimeStampToDateTime(System.Convert.ToDouble(map.UploadDate)); map.TimeUpdated = UnixTimeStampToDateTime(System.Convert.ToDouble(map.MapUpdated)); map.CreatorSteamID = System.Convert.ToUInt64(map.APICreator, CultureInfo.CurrentCulture); if (db.WorkshopMaps.Any(x => x.FileID == map.FileID)) { db.WorkshopMaps.Update(map); Log.Information("[WORKSHOP] Updated Workshop Map stored in database."); } else { db.WorkshopMaps.Add(map); Log.Information("[WORKSHOP] Stored new map in database."); } } await db.SaveChangesAsync(); }
/// <summary> /// Collects and stores the agent_history from all Package Account Users. /// </summary> /// <returns>Jack shit.</returns> public async Task ScrapeHistoricalData() { // Obtain all package accounts with their steam IDs List <PackageAccount> packageAccounts = this.accountService.QueryAccounts(); // Convert SteamIDs from PackageAccounts to Agents using DB. List <Agent> agents = new List <Agent>(); foreach (PackageAccount account in packageAccounts) { agents.Add(await this.GetAgentProfileAsync(account.SteamID)); } foreach (Agent dbAgent in agents) { // Collect Agent Stats AgentStats statsResponse = await this.GetAgentStatsAsync(dbAgent.SteamID); // Collect Agent Votes List <AgentVotes> votesResponse = await this.QueryAgentVotes(dbAgent.SteamID); // Collect Profile Information Agent agentResponse = await this.GetAgentProfileAsync(dbAgent.SteamID); // If the player's latest lastUpdate value is greater than what we have stored in the database if (statsResponse.LastUpdate > dbAgent.LastUpdate && agentResponse.LoginCount > dbAgent.LoginCount) { AgentHistory agentHistory = new AgentHistory() { SteamID = dbAgent.SteamID, MatchesWon = statsResponse.MatchesWon, MatchesLost = statsResponse.MatchesLost, RoundsLost = statsResponse.RoundsLost, RoundsTied = statsResponse.RoundsTied, RoundsWonElim = statsResponse.RoundsWonElim, RoundsWonCapture = statsResponse.RoundsWonCapture, RoundsWonHack = statsResponse.RoundsWonHack, RoundsWonTimer = statsResponse.RoundsWonTimer, RoundsWonCustom = statsResponse.RoundsWonCustom, TimePlayed = statsResponse.TimePlayed, Kills = statsResponse.Kills, TeamKills = statsResponse.TeamKills, Deaths = statsResponse.Deaths, Arrests = statsResponse.Arrests, GotArrested = statsResponse.GotArrested, Captures = statsResponse.Captures, NetworkHacks = statsResponse.NetworkHacks, Survivals = statsResponse.Survivals, Suicides = statsResponse.Suicides, Knockdowns = statsResponse.Knockdowns, GotKnockedDown = statsResponse.GotKnockedDown, TeamKnockdowns = statsResponse.TeamKnockdowns, TeamDamage = statsResponse.TeamDamage, LevelXP = statsResponse.LevelXP, TotalXP = statsResponse.TotalXP, Level = statsResponse.Level, PositiveVotes = votesResponse.FirstOrDefault().PositiveVotes, NegativeVotes = votesResponse.FirstOrDefault().NegativeVotes, TotalVotes = votesResponse.FirstOrDefault().ReceivedVotes, LoginCount = dbAgent.LoginCount, LastLogin = dbAgent.LastLogin, Timestamp = DateTime.Now, }; // Store new Agent Stat History in DB using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); try { db.AgentHistory.Add(agentHistory); await db.SaveChangesAsync(); } catch (Exception e) { Log.Error(e, $"Failed to store agent histoy in database. Agent: {agentHistory.ID}"); } // Update Agent to include most recent XP. // dbAgent.LastUpdate = DateTime.Now; // dbAgent.XP = statsResponse.TotalXP; // try // { // db.Agents.Update(dbAgent); // await db.SaveChangesAsync(); // } // catch (Exception e) // { // Log.Error(e, $"Failed to update agent details in database. Agent: {dbAgent.Name} | ID: {dbAgent.SteamID}"); // } } } }
/// <summary> /// Stores a new Agent DB into the database. Will update if there is an existing entry, but there shouldn't be. /// </summary> /// <param name="steamID">SteamID64.</param> /// <returns>An awaitable task.</returns> public async Task StoreAgentDBAsync(ulong steamID) { // get profile Agent profileResponse = await this.GetAgentProfileAsync(steamID); // get stats AgentStats statsResponse = await this.GetAgentStatsAsync(steamID); // get votes List <AgentVotes> votesResponse = await this.QueryAgentVotes(steamID); // add to db AgentsDB agentsDB = new AgentsDB() { // BEGIN PROFILE INFO SteamID = profileResponse.SteamID, SteamAvatar = profileResponse.AvatarURL, ID = profileResponse.IntruderID, Role = profileResponse.Role, Name = profileResponse.Name, LoginCount = profileResponse.LoginCount, FirstLogin = profileResponse.FirstLogin, LastLogin = profileResponse.LastLogin, LastUpdate = profileResponse.LastUpdate, // BEGIN STATS INFO MatchesWon = statsResponse.MatchesWon, MatchesLost = statsResponse.MatchesLost, RoundsLost = statsResponse.RoundsLost, RoundsTied = statsResponse.RoundsTied, RoundsWonElim = statsResponse.RoundsWonElim, RoundsWonCapture = statsResponse.RoundsWonCapture, RoundsWonHack = statsResponse.RoundsWonHack, RoundsWonTimer = statsResponse.RoundsWonTimer, RoundsWonCustom = statsResponse.RoundsWonCustom, TimePlayed = statsResponse.TimePlayed, Kills = statsResponse.Kills, TeamKills = statsResponse.TeamKills, Deaths = statsResponse.Deaths, Arrests = statsResponse.Arrests, GotArrested = statsResponse.GotArrested, Captures = statsResponse.Captures, Pickups = statsResponse.Pickups, NetworkHacks = statsResponse.NetworkHacks, Survivals = statsResponse.Survivals, Suicides = statsResponse.Suicides, Knockdowns = statsResponse.Knockdowns, GotKnockedDown = statsResponse.GotKnockedDown, TeamKnockdowns = statsResponse.TeamKnockdowns, TeamDamage = statsResponse.TeamDamage, Level = statsResponse.Level, LevelXP = statsResponse.LevelXP, LevelXPRequired = statsResponse.LevelXPRequired, TotalXP = statsResponse.TotalXP, // BEGIN VOTES PositiveVotes = votesResponse.ElementAt(0).PositiveVotes, NegativeVotes = votesResponse.ElementAt(0).NegativeVotes, TotalVotes = votesResponse.ElementAt(0).ReceivedVotes, Timestamp = DateTime.Now, }; using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); try { db.Agents.Add(agentsDB); await db.SaveChangesAsync(); } catch (DbUpdateException) { db.Agents.Update(agentsDB); await db.SaveChangesAsync(); } await Task.CompletedTask; }
public List <AgentsDB> GetDBAgentsAsync(string orderBy) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); List <AgentsDB> agentsDBs = new List <AgentsDB>(); switch (orderBy) { case "match": case "matches": case "matches won": agentsDBs = db.Agents.OrderByDescending(x => x.MatchesWon).Take(10).ToList(); break; case "matches lost": agentsDBs = db.Agents.OrderByDescending(x => x.MatchesLost).Take(10).ToList(); break; case "rounds lost": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsLost).Take(10).ToList(); break; case "rounds tied": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsTied).Take(10).ToList(); break; case "kills": agentsDBs = db.Agents.OrderByDescending(x => x.Kills).Take(10).ToList(); break; case "deaths": agentsDBs = db.Agents.OrderByDescending(x => x.Deaths).Take(10).ToList(); break; case "arrests": agentsDBs = db.Agents.OrderByDescending(x => x.Arrests).Take(10).ToList(); break; case "team kills": agentsDBs = db.Agents.OrderByDescending(x => x.TeamKills).Take(10).ToList(); break; case "captures": agentsDBs = db.Agents.OrderByDescending(x => x.Captures).Take(10).ToList(); break; case "hacks": case "network hacks": agentsDBs = db.Agents.OrderByDescending(x => x.NetworkHacks).Take(10).ToList(); break; case "survivals": agentsDBs = db.Agents.OrderByDescending(x => x.Survivals).Take(10).ToList(); break; case "suicides": agentsDBs = db.Agents.OrderByDescending(x => x.Suicides).Take(10).ToList(); break; case "login count": agentsDBs = db.Agents.OrderByDescending(x => x.LoginCount).Take(10).ToList(); break; case "pickups": agentsDBs = db.Agents.OrderByDescending(x => x.Pickups).Take(10).ToList(); break; case "votes": agentsDBs = db.Agents.OrderByDescending(x => x.TotalVotes).Take(10).ToList(); break; case "xp": agentsDBs = db.Agents.OrderByDescending(x => x.TotalXP).Take(10).ToList(); break; case "team damage": agentsDBs = db.Agents.OrderByDescending(x => x.TeamDamage).Take(10).ToList(); break; case "team knockdowns": agentsDBs = db.Agents.OrderByDescending(x => x.TeamKnockdowns).Take(10).ToList(); break; case "arrested": agentsDBs = db.Agents.OrderByDescending(x => x.GotArrested).Take(10).ToList(); break; case "got knocked down": case "knocked down": agentsDBs = db.Agents.OrderByDescending(x => x.GotArrested).Take(10).ToList(); break; case "rounds won capture": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsWonCapture).Take(10).ToList(); break; case "rounds won hack": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsWonHack).Take(10).ToList(); break; case "rounds won elim": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsWonElim).Take(10).ToList(); break; case "rounds won timer": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsWonTimer).Take(10).ToList(); break; case "rounds won custom": agentsDBs = db.Agents.OrderByDescending(x => x.RoundsWonCustom).Take(10).ToList(); break; case "positive votes": agentsDBs = db.Agents.OrderByDescending(x => x.PositiveVotes).Take(10).ToList(); break; case "negative votes": agentsDBs = db.Agents.OrderByDescending(x => x.NegativeVotes).Take(10).ToList(); break; default: agentsDBs = db.Agents.OrderByDescending(x => x.TotalXP).Take(10).ToList(); break; } return(agentsDBs); }
public async Task UpdateDBAgentAsync(Agent agent) { using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); AgentsDB dbAgent = await this.GetDBAgentAsync(agent.SteamID); if (this.CheckDBAgent(agent.SteamID)) { if (DateTime.Compare(agent.LastLogin, dbAgent.LastLogin) == 1) { // Agent has updated stats. lets update our DB. // get stats AgentStats statsResponse = await this.GetAgentStatsAsync(agent.SteamID); // get votes List <AgentVotes> votesResponse = await this.QueryAgentVotes(agent.SteamID); if (dbAgent.Name != agent.Name) { dbAgent.OldAgentName = dbAgent.Name; } // BEGIN PROFILE INFO dbAgent.SteamAvatar = agent.AvatarURL; dbAgent.Role = agent.Role; dbAgent.Name = agent.Name; dbAgent.LoginCount = agent.LoginCount; dbAgent.LastLogin = agent.LastLogin; dbAgent.LastUpdate = agent.LastUpdate; // BEGIN STATS INFO dbAgent.MatchesWon = statsResponse.MatchesWon; dbAgent.MatchesLost = statsResponse.MatchesLost; dbAgent.RoundsLost = statsResponse.RoundsLost; dbAgent.RoundsTied = statsResponse.RoundsTied; dbAgent.RoundsWonElim = statsResponse.RoundsWonElim; dbAgent.RoundsWonCapture = statsResponse.RoundsWonCapture; dbAgent.RoundsWonHack = statsResponse.RoundsWonHack; dbAgent.RoundsWonTimer = statsResponse.RoundsWonTimer; dbAgent.RoundsWonCustom = statsResponse.RoundsWonCustom; dbAgent.TimePlayed = statsResponse.TimePlayed; dbAgent.Kills = statsResponse.Kills; dbAgent.TeamKills = statsResponse.TeamKills; dbAgent.Deaths = statsResponse.Deaths; dbAgent.Arrests = statsResponse.Arrests; dbAgent.GotArrested = statsResponse.GotArrested; dbAgent.Captures = statsResponse.Captures; dbAgent.Pickups = statsResponse.Pickups; dbAgent.NetworkHacks = statsResponse.NetworkHacks; dbAgent.Survivals = statsResponse.Survivals; dbAgent.Suicides = statsResponse.Suicides; dbAgent.Knockdowns = statsResponse.Knockdowns; dbAgent.GotKnockedDown = statsResponse.GotKnockedDown; dbAgent.TeamKnockdowns = statsResponse.TeamKnockdowns; dbAgent.TeamDamage = statsResponse.TeamDamage; dbAgent.Level = statsResponse.Level; dbAgent.LevelXP = statsResponse.LevelXP; dbAgent.LevelXPRequired = statsResponse.LevelXPRequired; dbAgent.TotalXP = statsResponse.TotalXP; // BEGIN VOTES dbAgent.PositiveVotes = votesResponse.ElementAt(0).PositiveVotes; dbAgent.NegativeVotes = votesResponse.ElementAt(0).NegativeVotes; dbAgent.TotalVotes = votesResponse.ElementAt(0).ReceivedVotes; dbAgent.Timestamp = DateTime.Now; db.Agents.Update(dbAgent); } else if (DateTime.Compare(agent.LastLogin, dbAgent.LastLogin) == 0) { // Same date, do nothing. } } else { await this.StoreAgentDBAsync(agent.SteamID); } await db.SaveChangesAsync(); }
/// <summary> /// Collects and stores the agent_history from all Package Account Users. /// </summary> /// <returns>Jack shit.</returns> public async Task PopulateAgentTableAsync() { // Convert SteamIDs from PackageAccounts to Agents using DB. List <Agent> agents = await this.GetAllAgents(null, null, null, 101, 100); using IServiceScope scope = this.scopeFactory.CreateScope(); using IntruderContext db = scope.ServiceProvider.GetRequiredService <IntruderContext>(); foreach (Agent dbAgent in agents) { if (db.Agents.Where(x => x.SteamID == dbAgent.SteamID).AsNoTracking().FirstOrDefault() == null || db.Agents.Any(x => x.SteamID != dbAgent.SteamID) || db.Agents.Any(x => x.ID != dbAgent.IntruderID)) { // Collect Agent Stats AgentStats statsResponse = await this.GetAgentStatsAsync(dbAgent.SteamID); // Collect Agent Votes List <AgentVotes> votesResponse = await this.QueryAgentVotes(dbAgent.SteamID); AgentsDB agentsDB = new AgentsDB { // BEGIN PROFILE INFO SteamID = dbAgent.SteamID, SteamAvatar = dbAgent.AvatarURL, ID = dbAgent.IntruderID, Role = dbAgent.Role, Name = dbAgent.Name, LoginCount = dbAgent.LoginCount, FirstLogin = dbAgent.FirstLogin, LastLogin = dbAgent.LastLogin, LastUpdate = dbAgent.LastUpdate, // BEGIN STATS INFO MatchesWon = statsResponse.MatchesWon, MatchesLost = statsResponse.MatchesLost, RoundsLost = statsResponse.RoundsLost, RoundsTied = statsResponse.RoundsTied, RoundsWonElim = statsResponse.RoundsWonElim, RoundsWonCapture = statsResponse.RoundsWonCapture, RoundsWonHack = statsResponse.RoundsWonHack, RoundsWonTimer = statsResponse.RoundsWonTimer, RoundsWonCustom = statsResponse.RoundsWonCustom, TimePlayed = statsResponse.TimePlayed, Kills = statsResponse.Kills, TeamKills = statsResponse.TeamKills, Deaths = statsResponse.Deaths, Arrests = statsResponse.Arrests, GotArrested = statsResponse.GotArrested, Captures = statsResponse.Captures, Pickups = statsResponse.Pickups, NetworkHacks = statsResponse.NetworkHacks, Survivals = statsResponse.Survivals, Suicides = statsResponse.Suicides, Knockdowns = statsResponse.Knockdowns, GotKnockedDown = statsResponse.GotKnockedDown, TeamKnockdowns = statsResponse.TeamKnockdowns, TeamDamage = statsResponse.TeamDamage, Level = statsResponse.Level, LevelXP = statsResponse.LevelXP, LevelXPRequired = statsResponse.LevelXPRequired, TotalXP = statsResponse.TotalXP, // BEGIN VOTES PositiveVotes = votesResponse.ElementAt(0).PositiveVotes, NegativeVotes = votesResponse.ElementAt(0).NegativeVotes, TotalVotes = votesResponse.ElementAt(0).ReceivedVotes, Timestamp = DateTime.Now, }; try { db.Agents.Add(agentsDB); await db.SaveChangesAsync(); } catch (DbUpdateException) { db.Agents.Update(agentsDB); await db.SaveChangesAsync(); } } else { Console.WriteLine($"{dbAgent.Name} IS ALREADY STORED. INTRUDER ID: {dbAgent.IntruderID}"); } } }