Example #1
0
        public void Submit(int gameId, string username, decimal percentageGuess)
        {
            using (var context = _chatbotContextFactory.Create())
            {
                var existingGuess = context.SongPercentageGuesses.SingleOrDefault(g =>
                                                                                  g.SongGuessingRecordId == gameId && g.Username == username);


                if (existingGuess == null)
                {
                    existingGuess = new SongPercentageGuess
                    {
                        Guess = percentageGuess,
                        SongGuessingRecordId = gameId,
                        Username             = username
                    };
                    context.SongPercentageGuesses.Add(existingGuess);
                }
                else
                {
                    existingGuess.Guess = percentageGuess;
                }

                context.SaveChanges();
            }
        }
Example #2
0
        public bool UserGuess(string username, decimal percentageGuess)
        {
            try
            {
                using (var context = contextFactory.Create())
                {
                    var currentGameId = GetRunningGameId(context);

                    if (currentGameId == 0)
                    {
                        return(false);
                    }

                    var existingGuess = context.SongPercentageGuesses.SingleOrDefault(g =>
                                                                                      g.SongGuessingRecordId == currentGameId && g.Username == username);


                    if (existingGuess == null)
                    {
                        existingGuess = new SongPercentageGuess
                        {
                            Guess = percentageGuess,
                            SongGuessingRecordId = currentGameId,
                            Username             = username
                        };
                        context.SongPercentageGuesses.Add(existingGuess);
                    }
                    else
                    {
                        existingGuess.Guess = percentageGuess;
                    }
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.LogInformation(e,
                                       $"Encountered an error while trying to record a user's guess for the guessing game. username: {username}, percentageguess: {percentageGuess}");
                return(false);
            }
        }