Example #1
0
        public static Round FromCreateRoundRequest(CreateRoundDTO round)
        {
            var roundId = Guid.NewGuid();

            return(new Round
            {
                RoundId = roundId,
                ContraFactor = round.ContraFactor,
                Difference = round.ScoreDifference,
                GameId = round.GameId,
                GameType = round.GameType,
                IsKlop = round.IsKlop,
                LeadPlayerId = round.LeadPlayerId,
                SupportingPlayerId = round.SupportingPlayerId,
                PagatFangPlayerId = round.PagatFangPlayerId,
                MondFangPlayerId = round.MondFangPlayerId,
                Won = round.Won,
                RoundModifier = round.Modifiers?.Select(m =>
                                                        new RoundModifier(
                                                            m.ModifierType,
                                                            m.Team,
                                                            roundId,
                                                            m.Announced,
                                                            m.ContraFactor)).ToList(),
                RoundResult = round.KlopResults.Select(r =>
                                                       new RoundResult()
                {
                    GameId = round.GameId, PlayerId = r.PlayerId, PlayerScore = r.Score, RoundId = roundId
                }).ToList()
            });
        }
Example #2
0
        public async Task PostRound()
        {
            var p1 = this.fixture.team.Members[0];
            var p2 = this.fixture.team.Members[1];
            var p3 = this.fixture.team.Members[2];

            var createRoundDto = new CreateRoundDTO()
            {
                GameId             = this.fixture.gameId,
                ContraFactor       = 1,
                GameType           = 30,
                IsKlop             = false,
                LeadPlayerId       = p1.PlayerId,
                SupportingPlayerId = p2.PlayerId,
                PagatFangPlayerId  = p3.PlayerId,
                ScoreDifference    = 20,
                Won       = true,
                Modifiers = new List <ModifierDTO>()
                {
                    new ModifierDTO()
                    {
                        Announced    = false,
                        ContraFactor = Shared.Enums.Contra.None,
                        ModifierType = ModifierTypeDbEnum.PAGAT_ULTIMO,
                        Team         = Shared.Enums.TeamModifier.Playing
                    },
                    new ModifierDTO()
                    {
                        Announced    = true,
                        ContraFactor = Shared.Enums.Contra.Contra,
                        ModifierType = ModifierTypeDbEnum.TRULA,
                        Team         = Shared.Enums.TeamModifier.Playing
                    },
                    new ModifierDTO()
                    {
                        Announced    = false,
                        ContraFactor = Shared.Enums.Contra.None,
                        ModifierType = ModifierTypeDbEnum.KRALJI,
                        Team         = Shared.Enums.TeamModifier.NonPlaying
                    },
                }
            };

            var content  = RequestBodyFromObject(createRoundDto);
            var response = await fixture.Client.PostAsync($"/api/scoreboard", content);

            response.EnsureSuccessStatusCode();

            var result = await DeserializeResponse <RoundDTO>(response);

            result.Data.Modifiers.Should().HaveCount(3, "we posted a round with three modifiers");
            result.Data.RoundResults.Should().HaveCount(4, " there's three four players in the game.");
            result.Data.RoundNumber.Should().Be(1, "it's the first round");
            result.Data.LeadPlayerId.Should().Be(p1.PlayerId, " player one was the leading player");
            result.Data.SupportingPlayerId.Should().Be(p2.PlayerId, "player two was the supporintg player");
            result.Data.PagatFangPlayerId.Should().Be(p3.PlayerId, "player three had his pagat stolen as the last card");
            result.Data.RoundResults.First(p => p.PlayerId == p1.PlayerId).PlayerScore.Should().Be(105, " i say so");
        }
        public async Task <ActionResult <ResponseDTO <RoundDTO> > > PostRound(CreateRoundDTO createRoundRequest)
        {
            if (!await CheckTeamId(createRoundRequest.GameId))
            {
                return(StatusCode(403));
            }

            var round = await scoreboardService.AddRound(createRoundRequest);

            await hub.Clients.Group(round.GameId.ToString()).SendAsync("updateScoreBoard", round);

            return(Ok(round));
        }
        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());
        }