locked
redirect task<iactionresult> to another task<iactionresult> RRS feed

  • Question

  • User1043400518 posted

    Hello,

    I'm having trouble to redirect a task<iactionresult> to another task<iactionresult>

    [HttpPost]
    public async Task<IActionResult> CriarJogoAutonomo(string Nome)
    {
    JogoAutonomo JogoNovo = new JogoAutonomo(Nome);
    
    HttpClient client = NewGameHttpClient.Client;
    string path = "/api/NewGame";
    
    NovoJogoApiRequest req = new NovoJogoApiRequest(JogoNovo.Nome, JogoNovo.PerfilTipo);
    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();
    GameStateApi gs = JsonConvert.DeserializeObject<GameStateApi>(json_r);
    
    
    if (JogoNovo.Nome == "teste3")
    {
    JogoNovo.Rondas = 3;
    }
    if (JogoNovo.Nome == "teste7")
    {
    JogoNovo.Rondas = 7;
    }
    if (JogoNovo.Nome == "teste0")
    {
    JogoNovo.Rondas = 40;
    }
    
    JogoNovo.AtualizarJogoAutonomo(gs);
    
    RepositorioJogosAutonomos.AdicionarJogo(JogoNovo);
    
    return RedirectToAction("AccaoJogoAutonomo", new { gameid = JogoNovo.GameID, action = JogoNovo.TomarAccao });
    }


    Until here, i check in debug and everything is fine.  the last gameid gets the value of Jogo.NovoGameID, and the last action gets the value of JogoNovo.TomarAccao

    The other task<iactionresult> is this:

    [HttpGet]
            public async Task<IActionResult> AccaoJogoAutonomo(int gameid, PlayerAction action)
            {
                JogoAutonomo JogoAtual = RepositorioJogosAutonomos.GetJogo(gameid);
                HiScores ScoreAtual = RepositorioHiScoresdbContext.GetScore(gameid);
                
                if (JogoAtual.TomarAccao != PlayerAction.Quit && JogoAtual.Rondas > 0)
                {
                    HttpClient client = NewGameHttpClient.Client;
                    string path = "/api/Play";
    
                    AtualizarJogoApiRequest aj = new AtualizarJogoApiRequest(gameid, action);
                    string json = JsonConvert.SerializeObject(aj);
    
                    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();
                    GameStateApi gs1 = JsonConvert.DeserializeObject<GameStateApi>(json_r);
    
                    JogoAtual.AtualizarJogoAutonomo(gs1);
                    JogoAtual.Rondas = JogoAtual.Rondas - 1; 
                    return RedirectToAction("AccaoJogoAutonomo", new { JogoAtual.GameID, JogoAtual.TomarAccao });
                }
                else
                {
                    JogoAtual.CalcularBonus();
                    JogoAtual.Desistiu = true;
                    JogoAtual.ResultadoJogo = ResultadoJogo.Desistiu;
                    JogoAtual.Terminado = true;
                    HiScores NovoScore = new HiScores();
                    NovoScore.AtualizarScores(JogoAtual);
                    RepositorioHiScoresdbContext.AdicionarScore(NovoScore);
                    return View("DadosJogoAutonomo", JogoAtual);
                }
                
            }

    In the debug it jumps from the last "}" of the first Code, to the "if (!response.IsSuccessStatusCode) { return Redirect("/"); }" of the second one.
    I'm not sure why it doesnt run the "Task<IActionResult> AccaoJogoAutonomo" from the start.

    Anyone can help?

    I tried with [HttpPost] but doesn't work either.

    Sunday, June 9, 2019 8:14 AM

Answers

  • User475983607 posted

    The design does not make sense.  There's no logical reason for the browser redirects.  I recommend writing a JavaScript application, jQuery/AJAX or fetch, within a loop (or timer) to execute the API URLs.

    I also recommend that you meet with your teacher for assistance.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 9, 2019 2:36 PM

All replies

  • User475983607 posted

    Can you explain the design?  A redirect causes the browser to make a second request to a URL.  Why redirect when you can simply execute whatever logic you like in CriarJogoAutonomo?  

    The AccaoJogoAutonomo can redirect to itself which does not make sense.

    Sunday, June 9, 2019 2:06 PM
  • User1043400518 posted

    I'm doing a project for school that consists of a Game in html. The teacher has an API that gives us some variables and we need to send actions to the API, and then the API returns us variables, and so long... untill we win the game or we lose.

    To play the game, we have buttons in the VIEW that have the value of the action that we want to take, after clicking the button, action goes to API, API returns new values, we show new values on VIEW and wait for another action.

    Now... the teacher want us to make a way that the Game (Model) can play alone, without us having to click buttons to take the actions. So, in this mode, after we click "Create Game" the next view we see is just the data saying if we lose, win, and how many points we did. So the game has to play alone till the end, meaning, sending API requests alone, then receiving API values, process info and chose the next action and send to API, and so on...

    The thing is, the API has 2 steps, one to create game, which requires some parameters like (name of the player, and hero type) and it generates a gameid. second step is when the game is already created the api require us just to send it the game id and the action.

    So i have this to Task<iactionresult> one to create the game where it sends the name and hero type to the api, and the other "AccaoJogoAutonomo" is to play the game, just sending gameid and playeraction untill the game ends, thats why it needs to redirect to itself untill one of the values breakes the cicle so it can go to the view DadosJogoAutonomo with the score of the game.

    I don't know if I explained myself clrearly... hope I did.

    Sunday, June 9, 2019 2:22 PM
  • User475983607 posted

    The design does not make sense.  There's no logical reason for the browser redirects.  I recommend writing a JavaScript application, jQuery/AJAX or fetch, within a loop (or timer) to execute the API URLs.

    I also recommend that you meet with your teacher for assistance.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 9, 2019 2:36 PM
  • User1043400518 posted

    Ye you'r probably right. This must not make any sense. I will meet with him in 3 days probably, was just trying to get things done asap, but I guess i'll have to wait.

    Thanks for your inputs!
    Regards!

    Sunday, June 9, 2019 2:38 PM