public async Task <IActionResult> NovoJogo(string playerName, string playerClass)
        {
            HttpClient client = MyGameHTTPClient.Client;
            string     path   = "/api/NewGame";

            GameApiRequest req  = new GameApiRequest(playerName, playerClass);
            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("NovoJogo"));
            }

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

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

            Jogo novoJogo = new Jogo(playerName, playerClass);

            novoJogo.AtualizarJogo(gr);
            Repositorio.AdicionarJogo(novoJogo);

            return(View("Jogo", novoJogo));
        }
Example #2
0
        public async Task <GameApiResponse> GetGamesByNameAsync(Dictionary <string, string> searchFilters, string gameName, int page)
        {
            GameApiResponse games                 = null;
            var             requestUri            = $"{urlApi}/games?search={gameName}&page_size=10&page={page}&ordering=-rating";
            var             requestUriWithFilters = AddFiltersToUri(searchFilters, requestUri);

            var json = await _httpClient.GetStringAsync(requestUriWithFilters);

            if (json != null)
            {
                games = JsonConvert.DeserializeObject <GameApiResponse>(json);
            }
            return(games);
        }
        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));
            }
        }
Example #4
0
        public async Task <GameApiResponse> GetAllNewReleasedGamesForLast30DaysAsync(Dictionary <string, string> searchFilters, int page)
        {
            GameApiResponse games                    = null;
            var             currentDate              = $"{DateTime.Now.Year}-{DateTime.Now.Month:D2}-{DateTime.Now.Day:D2}";
            var             thirtyDaysBefore         = -30;
            var             thirtyDaysBeforeDateTime = DateTime.Today.AddDays(thirtyDaysBefore);
            var             thirtyDaysBeforeToday    = $"{thirtyDaysBeforeDateTime.Year}-{thirtyDaysBeforeDateTime.Month:D2}-{thirtyDaysBeforeDateTime.Day:D2}";

            var requestUri = $"{urlApi}/games?dates={thirtyDaysBeforeToday},{currentDate}&ordering=released&ordering=-rating&page_size=10&page={page}";

            var requestUriWithFilters = AddFiltersToUri(searchFilters, requestUri);

            var json = await _httpClient.GetStringAsync(requestUriWithFilters);

            if (json != null)
            {
                games = JsonConvert.DeserializeObject <GameApiResponse>(json);
            }
            return(games);
        }
Example #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));
        }