Example #1
0
 /// <summary>
 /// Evaluates if the word given for a category answer is correct, based on stored files.
 /// </summary>
 /// <param name="categoryPlayerAnswer">Category answer to evaluate</param>
 /// <returns>
 ///     True if category is not a default one, or file doesn't exist, or file contains the word;
 ///     False if answer is empty, or file exists and doesn't contain the word
 /// </returns>
 private bool EvaluateCategoryPlayerAnswer(CategoryPlayerAnswer categoryPlayerAnswer)
 {
     if (string.IsNullOrWhiteSpace(categoryPlayerAnswer.Answer))
     {
         return(false);
     }
     else if (!Engine.CategoriesList.Any(list => list.Value.Contains(categoryPlayerAnswer.GameCategory.Name)))
     {
         return(true);
     }
     try {
         var categoryFileWords = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\\Categories\\" + categoryPlayerAnswer.GameCategory.Name + ".txt").ToList();
         if (!categoryFileWords.Exists(word => word.ToLower() == categoryPlayerAnswer.Answer.ToLower()))
         {
             return(false);
         }
     } catch (FileNotFoundException fileNotFoundException) {
         Console.WriteLine("FileNotFoundException (EvaluateCategoryPlayerAnswer) -> " + fileNotFoundException.Message);
     } catch (IOException ioException) {
         Console.WriteLine("IOException (EvaluateCategoryPlayerAnswer) -> " + ioException.Message);
     } catch (Exception exception) {
         Console.WriteLine("Exception (EvaluateCategoryPlayerAnswer) -> " + exception.Message);
     }
     return(true);
 }
Example #2
0
        /// <summary>
        /// Sends the player category answers for an active game.
        /// </summary>
        /// <param name="guidGame">Game global unique identifier</param>
        /// <param name="guidPlayer">Player global unique identifier</param>
        /// <param name="categoryPlayerAnswers">List of category answers</param>
        /// <returns>True if category answers was sent successfully; False if not</returns>
        public bool SendCategoryAnswers(string guidGame, string guidPlayer, List <CategoryPlayerAnswer> categoryPlayerAnswers)
        {
            var findGame = activeGames.Find(game => game.ActiveGameGuid == guidGame);

            if (findGame == null)
            {
                return(false);
            }
            var findPlayer = findGame.Players.Find(player => player.ActivePlayerGuid == guidPlayer);

            if (findPlayer == null)
            {
                return(false);
            }
            foreach (var categoryPlayerAnswer in categoryPlayerAnswers)
            {
                var category = findGame.GameCategories.Find(gameCategory => gameCategory.Name.Equals(categoryPlayerAnswer.GameCategory.Name));
                if (category == null)
                {
                    continue;
                }
                var playerAnswerToAdd = new CategoryPlayerAnswer {
                    GameCategory = new GameCategory(0, null, category.Name),
                    Answer       = categoryPlayerAnswer.Answer,
                    Round        = categoryPlayerAnswer.Round,
                    Player       = findPlayer
                };
                playerAnswerToAdd.HasCorrectAnswer = EvaluateCategoryPlayerAnswer(playerAnswerToAdd);
                if (playerAnswerToAdd.HasCorrectAnswer)
                {
                    if (category.CategoryPlayerAnswer.Exists(answer => answer.Answer == playerAnswerToAdd.Answer))
                    {
                        findPlayer.Score += SCORE_POINTS_FOR_CORRECT_ANSWER / 2;
                    }
                    else
                    {
                        findPlayer.Score += SCORE_POINTS_FOR_CORRECT_ANSWER;
                    }
                }
                category.CategoryPlayerAnswer.Add(playerAnswerToAdd);
            }
            return(true);
        }