Skip to content

Commit

Permalink
Fix db error when adding duplicate players in a round (#16206)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSmugleaf authored May 7, 2023
1 parent 54ef2b7 commit c48f17a
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions Content.Server/Database/ServerDbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,23 +663,16 @@ public async Task AddRoundPlayers(int id, Guid[] playerIds)
{
await using var db = await GetDb();

var round = await db.DbContext.Round
.Include(round => round.Players)
.SingleAsync(round => round.Id == id);

var players = await db.DbContext.Player
// ReSharper disable once SuggestVarOrType_Elsewhere
Dictionary<Guid, int> players = await db.DbContext.Player
.Where(player => playerIds.Contains(player.UserId))
.ToListAsync();
.ToDictionaryAsync(player => player.UserId, player => player.Id);

var playerSet = new HashSet<Guid>(round.Players.Select(player => player.UserId));
foreach (var player in players)
foreach (var player in playerIds)
{
if (playerSet.Contains(player.UserId))
{
continue;
}

round.Players.Add(player);
await db.DbContext.Database.ExecuteSqlAsync($"""
INSERT INTO player_round (players_id, rounds_id) VALUES ({players[player]}, {id}) ON CONFLICT DO NOTHING
""");
}

await db.DbContext.SaveChangesAsync();
Expand Down

0 comments on commit c48f17a

Please sign in to comment.