コード例 #1
0
        public async Task <IActionResult> GetPlayers()
        {
            // Vytvor pole hracov na serveri
            var players = await context.Players.Where(p => p.GameId == "Blackjack")
                          .Include(p => p.Cards)
                          .ToListAsync();

            var croupierDb = players.Where(p => p.Nickname == "Croupier-Blackjack")
                             .ToArray();

            // Ak uz vsetci hraci ukoncili hru na rade je krupier
            if ((croupierDb[0].Action == Items.Player.EActions.STAND) &&
                players.Where(p => p.Nickname != "Croupier-Blackjack").Any(p => p.State == Items.Player.EState.PLAYING))
            {
                // Vynuluj krupierov profil
                croupierDb[0].Action = Items.Player.EActions.NONE;
                croupierDb[0].State  = Items.Player.EState.PLAYING;

                // Uvolni krupierove karty
                foreach (var card in croupierDb[0].Cards)
                {
                    card.PlayerId = null;
                }

                // Krupier zacina s jednou kartou hru
                await RandomCard(croupierDb[0]);

                // Uloz do Db
                await context.SaveChangesAsync();
            }
            // Ak este hraci hraju postaraj sa o krupierove karty
            else
            {
                if (!players.Where(p => p.State == Items.Player.EState.PLAYING && p.Nickname != "Croupier-Blackjack").Any(p => p.Action != Items.Player.EActions.STAND))
                {
                    await CroupierGame(croupierDb[0]);
                }
            }

            // Vytvor vystup JSON zo zoznamu hracov pri stole
            return(Ok(players.Select(p => new Items.Player
            {
                Nickname = p.Nickname,
                Bet = p.Bet,
                Cards = p.Cards.Select(c => new Items.Card {
                    Suit = c.Suit, Value = c.Value
                }),
                State = p.State,
                Action = p.Action,
                Wallet = p.Wallet
            })));
        }
コード例 #2
0
        public async Task <IActionResult> CreatePlayer([FromBody] Items.Player player)
        {
            if (!await context.Players.ContainsAsync(player))
            {
                // Novy hrac nehra este ziadnu hru
                player.GameId = "Nothing";

                // Novy hraci dostanu 10.000$
                if (player.Wallet <= 0)
                {
                    player.Wallet = 10000;
                }

                // Prida zaznam do Db
                context.Players.Add(player);
                await context.SaveChangesAsync();

                // Vytvoreny uspesne, vrati profil
                return(CreatedAtAction(nameof(GetPlayer), player));
            }

            // Zla poziadavka
            return(BadRequest());
        }