コード例 #1
0
        public async Task <TeamDTO> CreateTeamAsync(CreateTeamDTO createTeamDTO)
        {
            var teamId = Guid.NewGuid();

            var(key, salt) = CreateKeyAndSalt(createTeamDTO.Passphrase);
            var team = new Team()
            {
                TeamUserId = createTeamDTO.TeamId,
                TeamName   = createTeamDTO.Name,
                TeamId     = teamId,
                Passphrase = key,
                Salt       = salt
            };

            dbContext.Team.Add(team);

            foreach (var member in createTeamDTO.Members)
            {
                var teamPlayer = new TeamPlayer()
                {
                    PlayerId = Guid.NewGuid(),
                    TeamId   = teamId,
                    Name     = member.Name
                };
                dbContext.TeamPlayer.Add(teamPlayer);

                team.Members.Add(teamPlayer);
            }

            await dbContext.SaveChangesAsync();

            return(team.ToDto());
        }
コード例 #2
0
        public async Task <GameDTO> StartGameAsync(CreateGameDTO gameRequest)
        {
            var game = new Game()
            {
                TeamId     = gameRequest.TeamId,
                Name       = gameRequest.Name,
                GameId     = Guid.NewGuid(),
                Date       = DateTime.Now,
                GamePlayer = new List <GamePlayer>()
            };

            if (game.TeamId != null)
            {
                var teamPlayers = await dbContext.TeamPlayer
                                  .AsNoTracking()
                                  .Where(tp => tp.TeamId == game.TeamId)
                                  .ToListAsync();

                foreach (var player in gameRequest.Players)
                {
                    if (player.PlayerId.HasValue && !teamPlayers.Select(tp => tp.PlayerId).Contains(player.PlayerId.Value))
                    {
                        throw new OperationUnauthorizedException("Specified player not in team!");
                    }
                }
            }

            var gamePlayers = gameRequest.Players
                              .Select(p => new GamePlayer(p.Name)
            {
                GameId = game.GameId, PlayerId = p.PlayerId ?? Guid.NewGuid()
            })
                              .ToList();

            RandomizePlayerPosition(gamePlayers);

            foreach (var player in gamePlayers)
            {
                game.GamePlayer.Add(player);
            }

            dbContext.Game.Add(game);
            await dbContext.SaveChangesAsync();

            return(await this.GetAsync(game.GameId));;
        }
コード例 #3
0
        public async Task <RoundDTO> AddRound(CreateRoundDTO createRoundRequest)
        {
            var round  = Round.FromCreateRoundRequest(createRoundRequest);
            var gameId = round.GameId;

            var lastRound = await dbContext.Round
                            .AsNoTracking()
                            .OrderBy(r => r.RoundNumber)
                            .LastOrDefaultAsync(r => r.GameId == gameId);

            round.RoundNumber = (lastRound?.RoundNumber ?? 0) + 1;

            var gamePlayers = await dbContext.GamePlayer
                              .AsNoTracking()
                              .Where(g => g.GameId == gameId)
                              .OrderBy(p => p.Position)
                              .ToListAsync();

            ScoreBoard scoreBoard;

            if (lastRound == null)
            {
                var gameinit = new GameInitializer(gamePlayers.Select(p => p.PlayerId));
                scoreBoard = gameinit.StartGame(gameId);
            }
            else
            {
                var lastRoundResults = await dbContext.RoundResult
                                       .Where(r => r.RoundId == lastRound.RoundId)
                                       .ToListAsync();

                scoreBoard = ScoreBoard.FromRound(lastRoundResults);
            }

            var tarokRound = TarokRound.FromRound(round);

            var scores = scoreBoard.ApplyTarokRound(tarokRound);

            // TODO in case of Klop the client sent results are already here. No like.
            round.RoundResult.Clear();
            foreach (var player in gamePlayers)
            {
                var playerScore = scores[player.PlayerId];

                round.RoundResult.Add(new RoundResult()
                {
                    RoundId           = round.RoundId,
                    GameId            = gameId,
                    PlayerId          = player.PlayerId,
                    PlayerScore       = playerScore.Score,
                    PlayerRadelcCount = playerScore.RadelcCount,
                    PlayerRadelcUsed  = playerScore.UsedRadelcCount,
                    RoundScoreChange  = playerScore.RoundScoreChange
                });
            }

            dbContext.Round.Add(round);
            await dbContext.SaveChangesAsync();

            return(round.ToDto());
        }