Пример #1
0
            /// <summary>
            /// Remove ActiveUser Entry
            /// and add TimeWatched values
            /// </summary>
            /// <param name="sender">todo: describe sender parameter on RemoveUserFromActiveUsers</param>
            /// <param name="e">todo: describe e parameter on RemoveUserFromActiveUsers</param>
            public async static void RemoveUserFromActiveUsers(object sender, OnUserLeftArgs e)
            {
                using (var context = new Storage.StorageEntities()) {
                    //var twitchID = TwitchApi.Users.GetUser(e.Username);
                    var twitchID = await TwitchAPI.Users.v5.GetUserByName(e.Username);

                    if (twitchID != null && twitchID.Total > 0)
                    {
                        foreach (var userMatch in twitchID.Matches)
                        {
                            if (String.Compare(userMatch.Name, e.Username, true) != 0)
                            {
                                continue;
                            }

                            var user = context.Users.SingleOrDefault(u => String.Compare(u.Id, userMatch.Id) == 0);

                            if (user != null)
                            {
                                var duration = DateTime.Now.Subtract(user.ActiveUsers.JoinedTime.Value);

                                user.TimeWatched.TimeWatched1 += duration.Ticks;

                                context.ActiveUsers.Remove(user.ActiveUsers);

                                context.SaveChanges();
                            }
                        }
                    }
                }
            }
Пример #2
0
            /// <summary>
            /// Add or Update User in Database
            /// </summary>
            /// <param name="User">todo: describe User parameter on AddUser</param>
            private static void AddUserToDatabase(TwitchLib.Models.API.v5.Users.User User)
            {
                if (User != null)
                {
                    using (var context = new Storage.StorageEntities()) {
                        var databaseUser = context.Users.SingleOrDefault(u => String.Compare(u.Id, User.Id) == 0);

                        // Update User
                        if (databaseUser != null)
                        {
                            databaseUser.Bio         = User.Bio;
                            databaseUser.CreatedAt   = User.CreatedAt;
                            databaseUser.DisplayName = User.DisplayName;
                            databaseUser.Logo        = User.Logo;
                            databaseUser.Name        = User.Name;
                            databaseUser.Type        = User.Type;
                            databaseUser.UpdatedAt   = User.UpdatedAt;

                            // Active users
                            if (databaseUser.ActiveUsers == null)
                            {
                                databaseUser.ActiveUsers = new Storage.ActiveUsers {
                                    ID         = databaseUser.Id,
                                    JoinedTime = DateTime.Now,
                                    Users      = databaseUser,
                                };
                            }
                        }
                        // Create User
                        else
                        {
                            var newUser = new Storage.Users {
                                Id          = User.Id,
                                Bio         = User.Bio,
                                CreatedAt   = User.CreatedAt,
                                DisplayName = User.DisplayName,
                                Logo        = User.Logo,
                                Name        = User.Name,
                                Type        = User.Type,
                                UpdatedAt   = User.UpdatedAt,
                                Currency    = new Storage.Currency {
                                    Value = 0,
                                },
                                ActiveUsers = new Storage.ActiveUsers {
                                    JoinedTime = DateTime.Now,
                                },
                                TimeWatched = new Storage.TimeWatched {
                                    TimeWatched1 = 0,
                                }
                            };

                            context.Users.Add(newUser);
                        }

                        context.SaveChanges();
                    }
                }
            }
Пример #3
0
        /// <summary>
        /// Check if User have written a BlacklistedWord and timeout him
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void BlacklistWordsChecker(object sender, OnMessageReceivedArgs e)
        {
            using (var context = new Storage.StorageEntities()) {
                var result = context.BlacklistedWords.Any(word => e.ChatMessage.Message.Contains(word.Word));

                if (result)
                {
                    AivaClient.Instance.AivaTwitchClient.TimeoutUser(e.ChatMessage.Username, new TimeSpan(0, 5, 0), Text.Instance.GetString("BlacklistedWordTimeoutText"));
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Add raw Message to Database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void AddReceivedMessageToDatabase(object sender, OnMessageReceivedArgs e)
        {
            using (var context = new Storage.StorageEntities()) {
                context.Chat.Add(new Storage.Chat {
                    TwitchID    = e.ChatMessage.UserId,
                    ChatMessage = e.ChatMessage.Message,
                    Timestamp   = DateTime.Now,
                });

                context.SaveChanges();
            }
        }
Пример #5
0
            /// <summary>
            /// Remove Currency from User
            /// </summary>
            /// <param name="twitchID"></param>
            /// <param name="value"></param>
            public static void RemoveCurrencyFromUser(string twitchID, int value)
            {
                using (var context = new Storage.StorageEntities()) {
                    var user = context.Users.SingleOrDefault(u => String.Compare(u.Id, twitchID) == 0);

                    if (user != null)
                    {
                        user.Currency.Value -= value;

                        context.SaveChanges();
                    }
                }
            }
Пример #6
0
                /// <summary>
                /// Timer tick
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private static void CurrencyTimerTick(object sender, System.Timers.ElapsedEventArgs e)
                {
                    using (var context = new Storage.StorageEntities()) {
                        var activeUsers = context.ActiveUsers.ToList();

                        foreach (var user in activeUsers)
                        {
                            user.Users.Currency.Value += Convert.ToInt64(Config.Config.Instance[nameof(Currency)]["CurrencyToAddFrequently"]);
                        }

                        context.SaveChanges();
                    }
                }
Пример #7
0
            /// <summary>
            /// Add list of Users Currency
            /// </summary>
            /// <param name="UserList">todo: describe UserList parameter on AddCurrencyToUserList</param>
            public async static void AddCurrencyToUserList(List <Models.DatabaseCurrencyModel.ListCurrencyUpdate> UserList)
            {
                using (var context = new Storage.StorageEntities()) {
                    foreach (var user in UserList)
                    {
                        var userDb = context.Users.SingleOrDefault(u => String.Compare(u.Id, user.TwitchID) == 0);

                        if (userDb != null)
                        {
                            userDb.Currency.Value += user.Value;
                        }
                    }

                    await context.SaveChangesAsync();
                }
            }
Пример #8
0
        /// <summary>
        /// Add User to Table Currency
        /// </summary>
        /// <param name="twitchID"></param>
        public async static void AddUserToCurrencyTable(string twitchID)
        {
            using (var context = new Storage.StorageEntities()) {
                var currencyEntry = context.Currency.SingleOrDefault(c => String.Compare(c.ID, twitchID) == 0);

                if (currencyEntry == null)
                {
                    context.Currency.Add(
                        new Storage.Currency {
                        ID    = twitchID,
                        Value = 0
                    });

                    await context.SaveChangesAsync();
                }
            }
        }
Пример #9
0
 /// <summary>
 /// Returns the existing Timers
 /// </summary>
 /// <returns></returns>
 public static List <Storage.Timers> GetExistingTimers()
 {
     using (var context = new Storage.StorageEntities()) {
         return(context.Timers.ToList());
     }
 }
Пример #10
0
 /// <summary>
 /// Check if the user is in the Database
 /// </summary>
 /// <param name="username"></param>
 /// <returns></returns>
 public static bool IsUserInDatabase(string username)
 {
     using (var context = new Storage.StorageEntities()) {
         return(context.Users.Any(u => String.Compare(u.Name, username, true) == 0));
     }
 }