Exemplo n.º 1
0
        public async Task <bool> GameStatusAsync()
        {
            using (HttpClient client = CreateClient())
            {
                HttpResponseMessage response = await client.GetAsync("/BoggleService.svc/games/" + state.gameID);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();

                    dynamic gamestate = JsonConvert.DeserializeObject(result);
                    if (state.state == "pending" && gamestate.GameState == "active")
                    {
                        state.status = "Player found!";
                    }
                    state.state = gamestate.GameState;
                    if (state.state == "pending")
                    {
                        state.status = "Waiting for players...";
                        return(false);
                    }
                    state.timeLeft  = gamestate.TimeLeft;
                    state.board     = gamestate.Board;
                    state.timeLimit = gamestate.TimeLimit;
                    dynamic player1 = gamestate.Player1;
                    dynamic player2 = gamestate.Player2;
                    Player  p1      = new Player((string)player1.Nickname, Convert.ToInt32(player1.Score));
                    Player  p2      = new Player((string)player2.Nickname, Convert.ToInt32(player2.Score));
                    if (state.state == "completed")
                    {
                        foreach (dynamic foo in gamestate.Player1.WordsPlayed)
                        {
                            p1.addWord((string)foo.Word, Convert.ToInt32(foo.Score));
                        }
                        foreach (dynamic foo in gamestate.Player2.WordsPlayed)
                        {
                            p2.addWord((string)foo.Word, Convert.ToInt32(foo.Score));
                        }
                    }
                    state.players = new List <Player>();
                    state.players.Add(p1);
                    state.players.Add(p2);
                    return(true);
                }
                state.status = "Error getting game status: " + response.StatusCode + ", " + response.ReasonPhrase;
                return(false);
            }
        }