Пример #1
0
        private void AnnounceCurrentGameWinner(SongGuessingRecord currentGuessingRecord, IChatbotContext context)
        {
            var potentialWinnerModels = context.SongPercentageGuesses
                                        .Where(g => g.SongGuessingRecord.SongGuessingRecordId == currentGuessingRecord.SongGuessingRecordId).ToList()
                                        .Select(pg =>
                                                (Math.Floor(Math.Abs(currentGuessingRecord.FinalPercentage - pg.Guess) * 10) / 10, pg)).ToList();

            // No-one guessed?
            if (!potentialWinnerModels.Any())
            {
                Client.SendMessage(string.IsNullOrEmpty(DevelopmentRoomId) ? _streamerChannel : DevelopmentRoomId, "Nobody guessed! Good luck next time :)");
            }

            var winners = GuessingGameWinner.Create(potentialWinnerModels);

            // TODO: URGENT -> Refactor this to own service when bytes service is brought over to library project.
            GiveBytes(winners.Where(w => w.Difference <= 20).ToList());

            Client.SendMessage(string.IsNullOrEmpty(DevelopmentRoomId) ? _streamerChannel : DevelopmentRoomId,
                               winners[0].Difference > 20 ?
                               $"@{string.Join(", @", winners.Select(w=> w.Username))} has won... nothing!" +
                               $" {string.Join(", ", winners.Select(w => $"{w.Username} guessed {w.Guess}%"))} " +
                               $"You were {winners[0].Difference} away from the actual score. Do you even know {_streamerChannel}?"
                    : winners[0].Difference == 0
                        ? $"@{string.Join(", @", winners.Select(w => w.Username))} has won! You guessed {winners[0].Guess}. You were spot on! You've received {winners[0].BytesWon} bytes"
                        : $"@{string.Join(", @", winners.Select(w => w.Username))} has won! " +
                               $"{string.Join(", ", winners.Select(w => $"{w.Username} guessed {w.Guess}% "))}" +
                               $"You were {winners[0].Difference} away from the actual score. You've received {winners[0].BytesWon} bytes");
        }
Пример #2
0
        private bool OpenGuessingGame(string songName)
        {
            try
            {
                using (var context = contextFactory.Create())
                {
                    CloseExistingGames(context);

                    var newGuessRecord = new SongGuessingRecord
                    {
                        SongDetails   = songName,
                        UsersCanGuess = true,
                        IsInProgress  = true
                    };

                    context.SongGuessingRecords.Add(newGuessRecord);
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Encountered an error, returning false. SongName: {songName}");
                return(false);
            }
        }
        public bool Open(string songName)
        {
            try
            {
                using (var context = _chatbotContextFactory.Create())
                {
                    // Unlikely, but ensure that other open games are closed
                    var unclosedGames = context.SongGuessingRecords.Where(x => x.UsersCanGuess || x.IsInProgress);

                    foreach (var unclosedGame in unclosedGames)
                    {
                        unclosedGame.UsersCanGuess = false;
                        unclosedGame.IsInProgress  = false;
                    }

                    var newGuessRecord = new SongGuessingRecord
                    {
                        SongDetails   = songName,
                        UsersCanGuess = true,
                        IsInProgress  = true
                    };

                    context.SongGuessingRecords.Add(newGuessRecord);
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Encountered an error, returning false. SongName: {songName}");
                return(false);
            }
        }