Exemplo n.º 1
0
        public async Task <Game> NewGame(WordDifficulty wordDifficulty)
        {
            try
            {
                // Get the response from the server url and REST path for the data
                var response = await m_HttpClient.PostAsync(new Uri(baseAddress + "/NewGame"),
                                                            new StringContent(JsonConvert.SerializeObject(wordDifficulty), Encoding.UTF8, "application/json"));

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException("Access Denied");
                }

                if (response.IsSuccessStatusCode)
                {
                    return(JsonConvert.DeserializeObject <Game>(await response.Content.ReadAsStringAsync()));
                }

                throw new WebException(response.ReasonPhrase);
            }
            catch (Exception ex)
            {
                // TODO:
                throw ex;
            }
        }
Exemplo n.º 2
0
        public async Task <Game> NewGame(WordDifficulty wordDifficulty, string userName)
        {
            var user = await db.Set <User>().Include(u => u.Player).FirstOrDefaultAsync(u => u.UserName == userName);

            if (user == null)
            {
                return(null);
            }

            var words = await db.Set <Word>().Where(w => w.Difficulty == wordDifficulty && w.CreatedBy != userName).ToListAsync();

            var rand = new Random();
            var word = words[rand.Next(words.Count)];

            if (word == null)
            {
                return(null);
            }

            var game = new Game()
            {
                Player = user.Player,
                Word   = word,
            };

            var serverGame = AttachEntity(game, state: EntityState.Added);

            await db.SaveChangesAsync();

            return(serverGame);
        }
Exemplo n.º 3
0
        public void RandomWordDifficulty()
        {
            Array          values = Enum.GetValues(typeof(WordDifficulty));
            Random         random = new Random();
            WordDifficulty randomWordDifficulty = (WordDifficulty)values.GetValue(random.Next(values.Length));

            this.wordDifficulty = randomWordDifficulty;
        }
Exemplo n.º 4
0
        /*public void CreateWords()
         * {
         *  List<string> easyWords = new List<string>() { "note", "drums", "music" };
         *  List<string> mediumWords = new List<string>() { "guitar", "rhythm", "chord" };
         *  List<string> hardWords = new List<string>() { "saxophone", "performance", "orchestra" };
         *  if (context.Words.Any())
         *  {
         *      for (int i = 0; i < easyWords.Count; i++)
         *      {
         *          context.Words.Add(new Word { Name = easyWords[i], WordDifficulty = WordDifficulty.Easy, CategoryId = 3 });
         *          context.SaveChanges();
         *      }
         *      for (int i = 0; i < mediumWords.Count; i++)
         *      {
         *          context.Words.Add(new Word { Name = mediumWords[i], WordDifficulty = WordDifficulty.Medium, CategoryId = 3 });
         *          context.SaveChanges();
         *      }
         *      for (int i = 0; i < hardWords.Count; i++)
         *      {
         *          context.Words.Add(new Word { Name = hardWords[i], WordDifficulty = WordDifficulty.Hard, CategoryId = 3 });
         *          context.SaveChanges();
         *      }
         *  }
         * }*/

        public string GetRandomWord(WordDifficulty wordDifficulty, int categoryId)
        {
            int countOfWords = context.Words.Count(x => x.WordDifficulty == wordDifficulty && x.CategoryId == categoryId);

            if (!context.Words.Any())
            {
                throw new ArgumentException("There are no words in the database!");
            }
            int skippedWords = new Random().Next(countOfWords);
            var word         = context.Words.Where(x => x.WordDifficulty == wordDifficulty && x.CategoryId == categoryId)
                               .Skip(skippedWords)
                               .First().Name;

            return(word);
        }
Exemplo n.º 5
0
        private async Task StartGame(WordDifficulty wordDifficulty)
        {
            viewModel.IsBusy = true;

            try
            {
                gameService = new GameService(await new Token().GetToken());
                var game = await gameService.NewGame(wordDifficulty);

                var gameViewModel = new GameViewModel(game);

                await Navigation.PushAsync(new GamePage(gameViewModel));
            }
            catch (System.Exception)
            {
                var notificationService = DependencyService.Get <INotificationService>();
                notificationService.Notify("No se pudo iniciar el juego.");
            }
        }
Exemplo n.º 6
0
        public async Task AddPoints(int userId, WordDifficulty wordDifficulty)
        {
            int points = 0;

            switch (wordDifficulty.ToString())
            {
            case "Easy":
                points = 2;
                break;

            case "Medium":
                points = 5;
                break;

            case "Hard":
                points = 8;
                break;

            default:
                break;
            }
            await ApiClient.UpdateUserPointsWithGivenUserIdAndPoints(userId, points);
        }
Exemplo n.º 7
0
 public ActionResult <List <string> > GetAllWordsWithGivenUserIdDifficultyAndCategory(int userId, WordDifficulty wordDifficulty, int categoryId)
 {
     return(userGuessedService.GetAllWordsWithGivenUserIdDifficultyAndCategory(userId, wordDifficulty, categoryId));
 }
Exemplo n.º 8
0
 private void StartGame(WordDifficulty difficulty)
 {
 }
Exemplo n.º 9
0
        public List <string> GetAllWordsWithGivenUserIdDifficultyAndCategory(int userId, WordDifficulty wordDifficulty, int categoryId)
        {
            var list         = new List <string>();
            var wordsGuessed = context.UsersGuessed.Where(x => x.UserId == userId).ToList();
            var words        = context.Words.Where(x => x.WordDifficulty == wordDifficulty && x.CategoryId == categoryId);

            foreach (var wordG in wordsGuessed)
            {
                foreach (var word in words)
                {
                    if (word.Id == wordG.WordId)
                    {
                        list.Add(word.Name);
                    }
                }
            }
            return(list);
        }
Exemplo n.º 10
0
        public ActionResult <string> GetRandomWord(WordDifficulty wordDifficulty, int categoryId)
        {
            var word = wordService.GetRandomWord(wordDifficulty, categoryId);

            return(word);
        }
Exemplo n.º 11
0
 public async Task <IHttpActionResult> NewGame(WordDifficulty wordDifficulty)
 {
     return(Ok(await gameService.NewGame(wordDifficulty, HttpContext.Current.User.Identity.Name)));
 }
Exemplo n.º 12
0
 public async Task <List <string> > GetAllWordsWithGivenUserIdDifficultyAndCategory(int userId, WordDifficulty wordDifficulty, int categoryId)
 {
     return(await this.httpClient.GetJsonAsync <List <string> >($"https://localhost:44382/api/userguessed/{userId}/{wordDifficulty}/{categoryId}"));
 }