private static async Task <KeyValuePair <bool, string> > PutMatchInfo(string dbName, string data, string endpoint, string timestamp) { MatchInfo matchInfo = JsonConvert.DeserializeObject <MatchInfo>(data); 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")); } var gameMode = server.Modes .Where(mode => mode.Mode == matchInfo.gameMode) .FirstOrDefault(); if (gameMode == default(GameModes)) { return(new KeyValuePair <bool, string>(false, "This game mode [" + matchInfo.gameMode + "] is not support on server [" + server.Name + "]")); } var match = db.Matches .Where(matchCheck => matchCheck.Server.Endpoint == endpoint && matchCheck.Timestamp.ToString() == timestamp) .FirstOrDefault(); if (match == default(Matches)) { match = new Matches() { Server = server, GameMode = gameMode, Map = matchInfo.map, FragLimit = matchInfo.fragLimit, TimeLimit = matchInfo.timeLimit, TimeElapsed = matchInfo.timeElapsed, Timestamp = DateTime.Parse(timestamp).ToUniversalTime() } } ; else { return(new KeyValuePair <bool, string>(false, "Match with equal timestamp are exist on this server")); } int playerPosition = 0; foreach (ScoreboardInfo si in matchInfo.scoreboard) { var player = db.Players .Where(playerCheck => playerCheck.Name.ToLower() == si.name.ToLower()) .FirstOrDefault(); if (player == default(Players)) { player = new Players() { Name = si.name } } ; playerPosition++; Scoreboards score = new Scoreboards() { Match = match, Player = player, Frags = si.frags, Kills = si.kills, Death = si.deaths, ScoreboardPercent = ((double)(matchInfo.scoreboard.Count - playerPosition) / (double)(matchInfo.scoreboard.Count - 1)) * 100.0 }; player.Scores.Add(score); match.Scoreboard.Add(score); db.Scoreboards.Add(score); } server.Matches.Add(match); gameMode.Matches.Add(match); db.Matches.Add(match); await db.SaveChangesAsync(); } return(new KeyValuePair <bool, string>(true, "")); }
public void PutMatchInfo(string endpoint, DateTime timestamp, MatchInfo info) { databaseContext.PutMatchInfo(endpoint, timestamp, info); }