コード例 #1
0
ファイル: UserService.cs プロジェクト: NTZemp/M151.ClubWebApp
        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();
        }
コード例 #2
0
ファイル: ClubsService.cs プロジェクト: NTZemp/Lama.Api
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: GamesController.cs プロジェクト: winterleitner/lama
        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));
        }
コード例 #5
0
ファイル: GamesController.cs プロジェクト: winterleitner/lama
        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);
            }
        }