public async Task <IActionResult> Jogo(PlayerAction playerAction, int id)
        {
            HttpClient client = MyGameHTTPClient.Client;
            string     path   = "/api/Play";

            Jogo jogo = Repositorio.DevolverJogo(id); //Devolve o jogo Atual

            PlayApiRequest req  = new PlayApiRequest(jogo.ID, playerAction);
            string         json = JsonConvert.SerializeObject(req);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, path);

            request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(Redirect("/"));
            }

            string json_r = await response.Content.ReadAsStringAsync();

            if (playerAction != PlayerAction.Quit)
            {
                GameApiResponse resposta = JsonConvert.DeserializeObject <GameApiResponse>(json_r);

                jogo.AtualizarJogo(resposta);

                if (jogo.PontosVida == 0 || resposta.Result == RoundResult.SuccessVictory)
                {
                    jogo.ScoreJogo();
                    Repositorio.AdicionarScore(jogo); //NOVO
                    return(View("Score", jogo));
                }
                else
                {
                    return(View(jogo));
                }
            }
            else
            {
                jogo.ScoreJogo();
                Repositorio.AdicionarScore(jogo); //NOVO
                return(View("Score", jogo));
            }
        }
Exemplo n.º 2
0
    public static void OnPlay()
    {
        var main = MAIN.getMain;

        if (main.isWaitingReplyAboutRaffleFromServer)
        {
            Errors.showTest("Error! [Ball Respond] request already sended!");
            return;
        }
        var play = new PlayApiRequest();

        play.Cid = 0;//617;//0; //102;
        play.Cnt = (ushort)Rooms.countTickets;
        play.Rid = Rooms.currentRoom.Id;
        play.Sid = main.sessionID;
        main.network.ApiRequest(Api.CmdName.Play, JsonUtility.ToJson(play));
        main.isWaitingReplyAboutRaffleFromServer = true;
    }
        public IActionResult Index(NewGameApiRequest novoJogador)
        {
            if (ModelState.IsValid)
            {
                //Novo Jogo
                HttpClient          client   = MyHTTPClientNewGame.Client;
                string              path     = "/api/NewGame";
                HttpResponseMessage response = client.PostAsJsonAsync(path, novoJogador).Result;
                if (!response.IsSuccessStatusCode)
                {
                    return(View("Index"));
                }

                PlayApiResponse nr = response.Content.ReadAsAsync <PlayApiResponse>().Result;



                int rd = 0;
                if (nr.PlayerName == "auto1")
                {
                    rd = 1;
                }
                else if (nr.PlayerName == "auto3")
                {
                    rd = 3;
                }
                else if (nr.PlayerName == "auto10")
                {
                    rd = 10;
                }
                else if (nr.PlayerName == "auto0")
                {
                    rd = 100;
                }

                Repository.ClearRounds();



                // Ciclo de rondas
                while (nr.RoundCount < rd && nr.PlayerCredits >= 10)
                {
                    RoundSummary rs = new RoundSummary();
                    rs.Blackjack = false;

                    int initialBet = 0;
                    if (nr.PlayerCredits > 200)
                    {
                        initialBet = 50;
                    }
                    else if (nr.PlayerCredits > 100)
                    {
                        initialBet = 25;
                    }
                    else
                    {
                        initialBet = 10;
                    }

                    rs.InitialCredits = nr.PlayerCredits;

                    PlayApiRequest rq = new PlayApiRequest(nr.GameId, (int)PlayerAction.NewRound, initialBet);
                    response = client.PostAsJsonAsync("/api/Play", rq).Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        return(View("Index"));
                    }

                    nr = response.Content.ReadAsAsync <PlayApiResponse>().Result;

                    rs.Rounds = nr.RoundCount + 1;

                    if (nr.RoundFinalResult == (int)RoundFinalResult.BlackJack)
                    {
                        rs.Blackjack = true;
                    }


                    //Jogadas
                    while (nr.PlayingRound == true && nr.PlayerCredits >= 10)
                    {
                        PlayerAction playerAction;

                        CardMethods card = new CardMethods();

                        int playerHand = card.ValueHands(nr.PlayerHand);
                        int dealerHand = card.ValueHands(nr.Dealerhand);

                        //if (card.ValueHands(nr.PlayerHand) >= 5 && card.ValueHands(nr.PlayerHand) <= 10)
                        //{
                        //    playerAction = PlayerAction.Double;
                        //    rs.Double = true;
                        //    rs.Bet = rs.Bet + rs.Bet;
                        //}
                        //else if (card.ValueHands(nr.PlayerHand) < 5 && card.ValueHands(nr.Dealerhand) == 11)
                        //    playerAction = PlayerAction.Surrender;
                        //else if (card.ValueHands(nr.PlayerHand) <= 16)
                        //    playerAction = PlayerAction.Hit;
                        //else if (card.ValueHands(nr.PlayerHand) >= 17)
                        //    playerAction = PlayerAction.Stand;
                        //else
                        //    playerAction = PlayerAction.Surrender;

                        if (dealerHand >= 9 && playerHand == 16)
                        {
                            playerAction = PlayerAction.Surrender;
                        }
                        else if (dealerHand == 10 && playerHand == 15)
                        {
                            playerAction = PlayerAction.Surrender;
                        }
                        else if (playerHand >= 17 && playerHand <= 21)
                        {
                            playerAction = PlayerAction.Stand;
                        }
                        else if (playerHand == 16 && dealerHand >= 7 && dealerHand <= 8)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 16 && dealerHand >= 2 && dealerHand <= 6)
                        {
                            playerAction = PlayerAction.Stand;
                        }
                        else if (playerHand == 15 && dealerHand == 11)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 15 && dealerHand >= 7 && dealerHand <= 9)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 15 && dealerHand >= 2 && dealerHand <= 6)
                        {
                            playerAction = PlayerAction.Stand;
                        }
                        else if (playerHand >= 12 && playerHand <= 14 && dealerHand >= 7)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand >= 13 && playerHand <= 14 && dealerHand >= 2 && dealerHand <= 6)
                        {
                            playerAction = PlayerAction.Stand;
                        }
                        else if (playerHand == 12 && dealerHand >= 4 && dealerHand <= 6)
                        {
                            playerAction = PlayerAction.Stand;
                        }
                        else if (playerHand == 12 && dealerHand >= 2 && dealerHand <= 3)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 11 && dealerHand == 11)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 11)
                        {
                            if (nr.PlayerCredits >= 10)
                            {
                                playerAction = PlayerAction.Double;
                                rs.Double    = true;
                                rs.Bet       = rs.Bet + rs.Bet;
                            }
                            else
                            {
                                playerAction = PlayerAction.Hit;
                            }
                        }
                        else if (playerHand == 10 && dealerHand >= 10)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 10)
                        {
                            if (nr.PlayerCredits >= 10)
                            {
                                playerAction = PlayerAction.Double;
                                rs.Double    = true;
                                rs.Bet       = rs.Bet + rs.Bet;
                            }
                            else
                            {
                                playerAction = PlayerAction.Hit;
                            }
                        }
                        else if (playerHand == 9 && dealerHand >= 7)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else if (playerHand == 9 && dealerHand >= 3)
                        {
                            if (nr.PlayerCredits >= 10)
                            {
                                playerAction = PlayerAction.Double;
                                rs.Double    = true;
                                rs.Bet       = rs.Bet + rs.Bet;
                            }
                            else
                            {
                                playerAction = PlayerAction.Hit;
                            }
                        }
                        else if (playerHand == 9)
                        {
                            playerAction = PlayerAction.Hit;
                        }
                        else
                        {
                            playerAction = PlayerAction.Hit;
                        }

                        PlayApiRequest req = new PlayApiRequest(nr.GameId, (int)playerAction, initialBet);
                        response = client.PostAsJsonAsync("/api/Play", req).Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            return(View("Index"));
                        }

                        nr = response.Content.ReadAsAsync <PlayApiResponse>().Result;

                        if (playerAction == PlayerAction.Double)
                        {
                            rs.Bet = rs.Bet + rs.Bet;
                        }
                        else
                        {
                            rs.Bet = initialBet;
                        }

                        if (card.ValueHands(nr.Dealerhand) == 21 && nr.Dealerhand.Count == 2)
                        {
                            rs.DealerBlackjack = true;
                        }
                        else
                        {
                            rs.DealerBlackjack = false;
                        }
                    }

                    rs.RoundResult  = nr.RoundFinalResult;
                    rs.FinalCredits = nr.PlayerCredits;
                    Repository.AddRound(rs);
                }

                path = "/api/Play/rGAUUmCfk3vUgfSF/" + nr.GameId;
                HttpResponseMessage resp = client.GetAsync(path).Result;
                if (!resp.IsSuccessStatusCode)
                {
                    return(View("Index"));
                }
                nr = resp.Content.ReadAsAsync <PlayApiResponse>().Result;

                path = "/api/Quit";
                QuitApiRequest reqq = new QuitApiRequest(nr.GameId);
                response = client.PostAsJsonAsync(path, reqq).Result;
                if (!response.IsSuccessStatusCode)
                {
                    return(View("Index"));
                }

                List <RoundSummary> rounds = Repository.Rounds;

                GameSummary g = new GameSummary();
                g.Rounds  = nr.RoundCount;
                g.Credits = nr.PlayerCredits;
                foreach (RoundSummary r in rounds)
                {
                    g.AvgBet = r.Bet + g.AvgBet;
                    if (r.Bet > g.MaxBet)
                    {
                        g.MaxBet = r.Bet;
                    }
                    if (r.Bet < g.MinBet)
                    {
                        g.MinBet = r.Bet;
                    }
                    else
                    {
                        g.MinBet = 10;
                    }
                    if (r.RoundResult == (int)RoundResult.BlackJack)
                    {
                        g.PlayerBlackjack = g.PlayerBlackjack + 1;
                    }
                    if (r.DealerBlackjack == true)
                    {
                        g.DealerBlackjack = g.DealerBlackjack + 1;
                    }
                }
                g.AvgBet = g.AvgBet / rounds.Count();

                ViewBag.Game = g;

                return(View("Result", rounds));
            }
            else
            {
                return(View());
            }
        }
        public IActionResult PlayGame(int id, PlayerAction playerAction, int initialBet)
        {
            if (ModelState.IsValid)
            {
                CardMethods         card     = new CardMethods();
                HttpClient          client   = MyHTTPClientNewGame.Client;
                string              path     = "/api/Play";
                PlayApiRequest      req      = new PlayApiRequest(id, (int)playerAction, initialBet);
                HttpResponseMessage response = client.PostAsJsonAsync(path, req).Result;
                if (!response.IsSuccessStatusCode)
                {
                    return(View("Index"));
                }

                PlayApiResponse nr = response.Content.ReadAsAsync <PlayApiResponse>().Result;

                if (playerAction == PlayerAction.Double)
                {
                    ViewBag.Bet = initialBet * 2;
                }
                else
                {
                    ViewBag.Bet = initialBet;
                }

                if (nr.PlayingRound == false)
                {
                    Game game = Repository.GetGame(id);

                    if (nr.RoundFinalResult == (int)RoundFinalResult.Win)
                    {
                        game.Wins = game.Wins + 1;
                    }
                    else if (nr.RoundFinalResult == (int)RoundFinalResult.Lose)
                    {
                        game.Loses = game.Loses + 1;
                    }
                    else if (nr.RoundFinalResult == (int)RoundFinalResult.Empate)
                    {
                        game.Ties = game.Ties + 1;
                    }
                    else if (nr.RoundFinalResult == (int)RoundFinalResult.BlackJack)
                    {
                        game.BlackJack = game.BlackJack + 1;
                        game.Wins      = game.Wins + 1;
                        ViewBag.Result = ViewBag.Bet * 1.5;
                    }
                    else if (nr.RoundFinalResult == (int)RoundFinalResult.Surrender)
                    {
                        ViewBag.Result = ViewBag.Bet / 2;
                    }

                    game.Rounds  = nr.RoundCount;
                    game.Credits = nr.PlayerCredits;
                    Repository.UpdateGame(game);
                }

                ViewBag.DealerHand = card.ValueHands(nr.Dealerhand);
                ViewBag.PlayerHand = card.ValueHands(nr.PlayerHand);

                return(View(nr));
            }
            else
            {
                return(View());
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> NovoAutoJogo(int rondas)
        {
            HttpClient client = MyGameHTTPClient.Client;
            string     path   = "/api/NewGame";

            string nome = "";

            if (rondas == 3)
            {
                nome = "auto3";
            }
            else if (rondas == 7)
            {
                nome = "auto7";
            }
            else
            {
                nome = "auto0";
            }
            GameApiRequest req  = new GameApiRequest(nome, "S");
            string         json = JsonConvert.SerializeObject(req);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, path);

            request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(Redirect("NovoAutoJogo"));
            }

            string json_r = await response.Content.ReadAsStringAsync();

            GameApiResponse gr = JsonConvert.DeserializeObject <GameApiResponse>(json_r);

            Jogo novoJogo = new Jogo(nome, "S");

            novoJogo.AtualizarJogo(gr);

            //int ronda = 1;
            while (novoJogo.PontosVida != 0 && gr.Result != RoundResult.SuccessVictory)
            {
                path = "/api/Play";

                if (gr.RoundNumber == rondas)
                {
                    novoJogo.Acao = PlayerAction.Quit;
                    break;
                }
                else
                {
                    novoJogo.AutoPlay(gr, rondas);
                }

                PlayApiRequest pedido = new PlayApiRequest(novoJogo.ID, novoJogo.Acao);
                json = JsonConvert.SerializeObject(pedido);

                request         = new HttpRequestMessage(HttpMethod.Post, path);
                request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

                response = await client.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    return(Redirect("/"));
                }

                json_r = await response.Content.ReadAsStringAsync();

                gr = JsonConvert.DeserializeObject <GameApiResponse>(json_r);
                novoJogo.AtualizarJogo(gr);
                //ronda++;
            }
            novoJogo.ScoreJogo();

            return(View("Resultados", novoJogo));
        }