public void GetServerStats(HttpListenerContext listenerContext) { try { string url = getUrl(listenerContext); ServerStats results = db.GetServerStats(url); if (results == null) { listenerContext.Response.StatusCode = (int)HttpStatusCode.NotFound; } else { string json = JsonConvert.SerializeObject(results); listenerContext.Response.StatusCode = (int)HttpStatusCode.OK; listenerContext.Response.ContentType = "application/json"; using (var writer = new StreamWriter(listenerContext.Response.OutputStream)) { writer.Write(json); } } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{DateTime.Now:dd.MM.yyyy HH:mm:ss}: {e.Message}\r\n"); Console.ResetColor(); listenerContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } finally { listenerContext.Response.Close(); } }
public Server(string endpoint, ServerInfo serverInfo) { this.endpoint = endpoint; info = serverInfo; Matches = new Dictionary <string, MatchInfo>(); Stats = new ServerStats(); }
private static KeyValuePair <bool, string> GetServerStats(string dbName, string endpoint) { using (var db = new DbModel(dbName)) { var server = db.Servers .Where(serverCheck => serverCheck.Endpoint == endpoint) .FirstOrDefault(); if (server == default(Servers)) { return(new KeyValuePair <bool, string>(false, "Server with enpoint = '" + endpoint + "' not found")); } else { var matchesCountGroupByDate = server.Matches .GroupBy(match => new DateTime(match.Timestamp.Year, match.Timestamp.Month, match.Timestamp.Day)) .Select(group => group.Count()); var playersCount = server.Matches .Select(match => match.Scoreboard.Count); ServerStats ss = new ServerStats() { totalMatchesPlayed = server.Matches.Count, maximumMatchesPerDay = matchesCountGroupByDate.Count() == 0 ? 0 : matchesCountGroupByDate.Max(), averageMatchesPerDay = matchesCountGroupByDate.Count() == 0 ? 0 : matchesCountGroupByDate.Average(), maximumPopulation = playersCount.Count() == 0 ? 0 : playersCount.Max(), averagePopulation = playersCount.Count() == 0 ? 0 : playersCount.Average(), top5GameModes = GetTop5GameModes(server), top5Maps = GetTop5Maps(server), }; return(new KeyValuePair <bool, string>(true, JsonConvert.SerializeObject(ss))); } } }