public IActionResult Test() { var l = new List <StoredGamePlayers>() { new StoredGamePlayers() { Player = new User() { Elo = 1200, UserName = "******" }, Rank = 1 }, new StoredGamePlayers() { Player = new User() { Elo = 1300, UserName = "******" }, Rank = 2 }, new StoredGamePlayers() { Player = new User() { Elo = 1300, UserName = "******" }, Rank = 3 }, new StoredGamePlayers() { Player = new User() { Elo = 1100, UserName = "******" }, Rank = 4 } }; PlayersController.CalculateNewEloRating(l); return(Ok(l)); }
private async void GameEnded(object?sender, EventArgs e) { try { if (sender is not Game) { return; } var game = sender as Game; var contextOptions = new DbContextOptionsBuilder <LamaContext>() .UseNpgsql(Configuration.GetConnectionString("DefaultConnection")) .Options; using var context = new LamaContext(contextOptions, null); var dbGame = context.Games.Find(game.Id); if (dbGame is null) { return; } dbGame.Completed = true; dbGame.EndedTime = DateTime.UtcNow; var winner = game.Players.OrderBy(p => p.Points).ThenBy(p => p.Elo).FirstOrDefault(); if (winner is not null) { dbGame.WinnerId = winner.GetUser().Id; } var players = game.Players.OrderBy(p => p.Points).ThenBy(p => p.Elo).Select(p => new StoredGamePlayers() { Game = dbGame, Player = p.GetUser(), PlayerId = p.GetUser().Id, LeftBeforeEnd = p.HasLeftGame, Points = p.Points }).ToList(); for (int i = 0; i < players.Count; i++) { players.ElementAt(i).Rank = i + 1; } PlayersController.CalculateNewEloRating(players); var ratings = new Dictionary <string, double>(); foreach (var p in players) { ratings.Add(p.PlayerId, p.Player.Elo); var gp = game.Players.FirstOrDefault(x => x.UserName == p.Player.UserName); if (gp != null) { gp.EloChange = p.EloChange; } p.Player = null; } dbGame.Players = players; await context.SaveChangesAsync(); var users = context.GamePlayers.Include(p => p.Player).Where(e => e.GameId == game.Id).ToList(); foreach (var user in users) { user.Player.Elo = ratings[user.PlayerId]; } await context.SaveChangesAsync(); try { game.StatusChanged -= GameStatusChanged; } catch { } } catch (Exception ex) { Console.WriteLine(ex.Message); } }