Esempio n. 1
0
        private static User Create(IChatbotContext context, string username, bool deferSave)
        {
            var user = new User
            {
                Username                  = username,
                UsedVipRequests           = 0,
                UsedSuperVipRequests      = 0,
                SentGiftVipRequests       = 0,
                ModGivenVipRequests       = 0,
                FollowVipRequest          = 0,
                SubVipRequests            = 0,
                DonationOrBitsVipRequests = 0,
                ReceivedGiftVipRequests   = 0,
                TokenVipRequests          = 0,
                TokenBytes                = 0,
                TotalBitsDropped          = 0,
                TotalDonated              = 0,
                TimeLastInChat            = DateTime.UtcNow
            };

            context.Users.Add(user);

            if (!deferSave)
            {
                context.SaveChanges();
            }

            return(user);
        }
        private User AddUser(IChatbotContext context, string username, bool deferSave)
        {
            var userModel = new User
            {
                Username                  = username.ToLower(),
                UsedVipRequests           = 0,
                ModGivenVipRequests       = 0,
                FollowVipRequest          = 0,
                SubVipRequests            = 0,
                DonationOrBitsVipRequests = 0,
                TokenBytes                = 0,
                ReceivedGiftVipRequests   = 0,
                SentGiftVipRequests       = 0
            };

            try
            {
                context.Users.Add(userModel);
                if (!deferSave)
                {
                    context.SaveChanges();
                }
            }
            catch (Exception)
            {
                return(null);
            }

            return(userModel);
        }
        private static void TransferChatCommands(IChatbotContext context, string oldUsername, string newUsername)
        {
            var commands = context.InfoCommands.Where(ic => ic.AddedByUser == oldUsername);

            foreach (var command in commands)
            {
                command.AddedByUser = newUsername;
            }
        }
        private static void TransferSongRequests(IChatbotContext context, string oldUsername, string newUsername)
        {
            var songRequests = context.SongRequests.Where(sr => sr.RequestUsername == oldUsername);

            foreach (var songRequest in songRequests)
            {
                songRequest.RequestUsername = newUsername;
            }
        }
        private static void TransferGuessingGameRecords(IChatbotContext context, string oldUsername, string newUsername)
        {
            var guessingGameRecords = context.SongPercentageGuesses.Where(spg => spg.Username == oldUsername);

            foreach (var guess in guessingGameRecords)
            {
                guess.Username = newUsername;
            }
        }
        private static void TransferSearchSynonymRequests(IChatbotContext context, string oldUsername, string newUsername)
        {
            var searchSynonymRequests = context.SearchSynonymRequests.Where(ssr => ssr.Username == oldUsername);

            foreach (var searchSynonymRequest in searchSynonymRequests)
            {
                searchSynonymRequest.Username = newUsername;
            }
        }
        private static void TransferQuotes(IChatbotContext context, string oldUsername, string newUsername)
        {
            var quotes = context.Quotes.Where(q => q.CreatedBy == oldUsername);

            foreach (var quote in quotes)
            {
                quote.CreatedBy = newUsername;
            }
        }
        private static void LogTransfer(IChatbotContext context, string moderatorUsername, string oldUsername, string newUsername)
        {
            var logRecord = new ModerationLog
            {
                Username         = moderatorUsername,
                Action           = ModerationAction.UsernameTransfer,
                ActionTakenTime  = DateTime.UtcNow,
                ExtraInformation = $"{moderatorUsername} has transferred {oldUsername}'s account to {newUsername}"
            };

            context.ModerationLogs.Add(logRecord);
        }
Esempio n. 9
0
        private void CloseExistingGames(IChatbotContext context)
        {
            // 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;
            }

            context.SaveChanges();
        }
        public static void TransferUser(this IChatbotContext context, string moderatorUsername, string oldUsername,
                                        string newUsername)
        {
            var modUser = context.Users.Find(moderatorUsername);

            var oldUser = context.Users.Find(oldUsername);
            var newUser = context.Users.Find(newUsername);

            if (modUser == null || oldUser == null || newUser == null)
            {
                throw new Exception("One of the provided usernames does not exist");
            }

            TransferQuotes(context, oldUsername, newUsername);
            TransferSearchSynonymRequests(context, oldUsername, newUsername);
            TransferGuessingGameRecords(context, oldUsername, newUsername);
            TransferSongRequests(context, oldUsername, newUsername);
            TransferChatCommands(context, oldUsername, newUsername);
            TransferVipsAndBytes(context, oldUser, newUser);
            LogTransfer(context, moderatorUsername, oldUsername, newUsername);
        }
        private static void TransferVipsAndBytes(IChatbotContext context, User oldUsername, User newUsername)
        {
            newUsername.UsedVipRequests += oldUsername.UsedVipRequests;
            oldUsername.UsedVipRequests  = 0;

            newUsername.UsedSuperVipRequests += oldUsername.UsedSuperVipRequests;
            oldUsername.UsedSuperVipRequests  = 0;

            newUsername.SentGiftVipRequests += oldUsername.SentGiftVipRequests;
            oldUsername.SentGiftVipRequests  = 0;

            newUsername.ModGivenVipRequests += oldUsername.ModGivenVipRequests;
            oldUsername.ModGivenVipRequests  = 0;

            newUsername.FollowVipRequest = oldUsername.FollowVipRequest;
            oldUsername.FollowVipRequest = 0;

            newUsername.SubVipRequests += oldUsername.SubVipRequests;
            oldUsername.SubVipRequests  = 0;

            newUsername.DonationOrBitsVipRequests = oldUsername.DonationOrBitsVipRequests;
            oldUsername.DonationOrBitsVipRequests = 0;

            newUsername.ReceivedGiftVipRequests += oldUsername.ReceivedGiftVipRequests;
            oldUsername.ReceivedGiftVipRequests  = 0;

            newUsername.TokenVipRequests += oldUsername.TokenVipRequests;
            oldUsername.TokenVipRequests  = 0;

            newUsername.TokenBytes += oldUsername.TokenBytes;
            oldUsername.TokenBytes  = 0;

            newUsername.TotalBitsDropped = oldUsername.TotalBitsDropped;
            oldUsername.TotalBitsDropped = 0;

            newUsername.TotalDonated += oldUsername.TotalDonated;
            oldUsername.TotalDonated  = 0;
        }
Esempio n. 12
0
 public static User GetOrCreateUser(this IChatbotContext context, string username, bool deferSaveIfCreate = false)
 {
     return(Get(context, username) ?? Create(context, username, deferSaveIfCreate));
 }
Esempio n. 13
0
        private static User Get(IChatbotContext context, string username)
        {
            var user = context.Users.SingleOrDefault(u => u.Username.ToLower() == username.ToLower());

            return(user);
        }
Esempio n. 14
0
 private int GetRunningGameId(IChatbotContext context)
 {
     return(context.SongGuessingRecords?.SingleOrDefault(x => x.UsersCanGuess)?.SongGuessingRecordId ?? 0);
 }
Esempio n. 15
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");
        }