Exemplo n.º 1
0
        private async Task AcceptInvitationAsync(ClubInvitation invitation, Guid userId)
        {
            var membership = new ClubMembership {
                UserId = userId, ClubId = invitation.ClubId
            };
            await _context.AddAsync(membership);

            _context.Remove(invitation);
            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task <Club> Add(string clubName)
        {
            var membership = new ClubMembership {
                User    = await _userService.GetLoggedInUserAsync(),
                IsAdmin = true
            };
            var club = new Club {
                ClubName    = clubName,
                Memberships = new List <ClubMembership>
                {
                    membership
                }
            };
            var entity = await _context.AddAsync(club);

            await _context.SaveChangesAsync();

            return(entity.Entity);
        }
        public async Task InvokeAsync(HttpContext context, LamaContext lamaContext)
        {
            if (context.User.FindFirst(ClaimTypes.NameIdentifier) != null)
            {
                var user = new ApiUser();
                var id   = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                if (!(await lamaContext.Users.AnyAsync(x => x.UserId == Guid.Parse(id))))
                {
                    user.UserId    = Guid.Parse(id);
                    user.UserName  = context.User.FindFirst("emails").Value;
                    user.GivenName = context.User.FindFirst(ClaimTypes.GivenName).Value;
                    await lamaContext.AddAsync(user);

                    await lamaContext.SaveChangesAsync();
                }
            }
            await _next(context);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateGame(int?configuration)
        {
            var user = await _userManager.GetUserAsync(this.User);

            if (user is null)
            {
                return(BadRequest("Please join server first"));
            }

            var nGame = new StoredGame()
            {
                Completed   = false,
                Aborted     = false,
                CreatedBy   = user,
                CreatedTime = DateTime.UtcNow,
                Name        = NameGen.GetRandomName(),
                Players     = new List <StoredGamePlayers>()
            };

            _context.Games.Add(nGame);
            await _context.SaveChangesAsync();

            var config = GameConfiguration.DefaultLama;

            if (configuration.HasValue && configuration.Value == 1)
            {
                config = GameConfiguration.NegativeLama;
            }

            var g = new Game(nGame.Id, nGame.Name, config);

            Games.Add(g);
            g.StatusChanged += GameStatusChanged;
            g.GameEnded     += GameEnded;
            return(Ok(g.Id));
        }
Exemplo n.º 5
0
        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);
            }
        }