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> > 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 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}"); } } }