Example #1
0
        public async Task <IActionResult> NovaTentativaAsync(Tentativa tentativa)
        {
            if (ModelState.IsValid)
            {
                tentativa.IniciarTentativa();
                Repositorio.AddTentativa(tentativa);

                HttpClient client = MyHttpClient.Client;
                string     path   = "/api/NewGame";

                NewGameRequest newGameRequest = new NewGameRequest(tentativa.Nickname, tentativa.Classe);
                string         json           = JsonConvert.SerializeObject(newGameRequest);

                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();

                GameState gs = JsonConvert.DeserializeObject <GameState>(json_r);

                tentativa.AtualizarDados(gs);


                return(View("AreaDeJogo", tentativa));
            }
            else
            {
                return(View("NovaTentativa"));
            }
        }
Example #2
0
        public async Task <IActionResult> NovaTentativaAutonomaAsync(Tentativa tentativa)
        {
            tentativa.IniciarTentativa();
            Repositorio.AddTentativa(tentativa);

            HttpClient         client         = MyHttpClient.Client;
            string             path           = "/api/NewGame";
            NewGameRequest     newGameRequest = new NewGameRequest(tentativa.Nickname, tentativa.Classe);
            string             json           = JsonConvert.SerializeObject(newGameRequest);
            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();

            GameState gs = JsonConvert.DeserializeObject <GameState>(json_r);

            tentativa.AtualizarDados(gs);


            while (!tentativa.JogoFinalizado && gs.RoundNumber < 50)
            {
                tentativa.ExecutarAlgoritmo();
                if ((tentativa.Nickname == "auto3" && gs.RoundNumber >= 3) || (tentativa.Nickname == "auto7" && gs.RoundNumber >= 7))
                {
                    tentativa.Acao = Playeraction.Quit;

                    tentativa.FinalizarTentativa();
                }
                path = "/api/Play";
                PlayRequest playRequest = new PlayRequest(tentativa.Id, tentativa.Acao);
                json = JsonConvert.SerializeObject(playRequest);

                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();

                gs = JsonConvert.DeserializeObject <GameState>(json_r);

                if (tentativa.Acao != Playeraction.Quit)
                {
                    tentativa.AtualizarDados(gs);
                    tentativa.ExecutarAcao();
                }


                RoundSummary rs = new RoundSummary();
                rs.AtualizarDados(tentativa.Acao, tentativa.Posicao, tentativa.InimigosDerrotados, tentativa.InimigosFintados, tentativa.ContadorItensEncontrados, tentativa.ChaveNoBolso, tentativa.PontosDeVida, tentativa.PontosDeForca, tentativa.PontosDeSorte, tentativa.PocoesDeVida, tentativa.Resultado, tentativa.Ronda);
                tentativa.FinalizarRonda(rs);
            }

            return(View("JogoFinalizadoAutonomo", tentativa));
        }