Beispiel #1
0
        /// <summary>
        /// Add a Command to the Database
        /// </summary>
        /// <param name="AddModel"></param>
        public async void AddCommandAsync(Models.Commands.AddModel AddModel)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                var commandEntry = context.Commands.SingleOrDefault(c => String.Compare(c.Command, AddModel.Command, true) == 0);

                // Command doesnt exist, create new
                if (commandEntry == null)
                {
                    commandEntry = new Core.Storage.Commands {
                        Command        = AddModel.Command,
                        CreatedAt      = DateTime.Now,
                        ModifiedAt     = DateTime.Now,
                        ExecutionRight = (int)AddModel.SelectedUserRight,
                        Text           = AddModel.Text,
                        Cooldown       = AddModel.Cooldown,
                        Count          = 0,
                    };

                    context.Commands.Add(commandEntry);
                }
                else
                {
                    // Command exists; update
                    commandEntry.ExecutionRight = (int)AddModel.SelectedUserRight;
                    commandEntry.ModifiedAt     = DateTime.Now;
                    commandEntry.Text           = AddModel.Text;
                    commandEntry.Cooldown       = AddModel.Cooldown;
                }

                await context.SaveChangesAsync();
            }

            // Load new CommandList
            LoadCommandList();
        }
Beispiel #2
0
 public Users()
 {
     using (var context = new Core.Storage.StorageEntities()) {
         UsersList = new ObservableCollection <Core.Storage.Users>(
             context.Users.ToList());
     }
 }
Beispiel #3
0
        /// <summary>
        /// Remove the selected command command from list and database
        /// </summary>
        public static void RemoveTimer(Storage.Timers timer)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                context.Timers.Remove(timer);

                context.SaveChanges();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Remove the selected command command from list and database
        /// </summary>
        public void RemoveCommand()
        {
            using (var context = new Core.Storage.StorageEntities()) {
                context.Commands.Remove(SelectedCommand);

                context.SaveChanges();
            }

            LoadCommandList();
        }
Beispiel #5
0
        /// <summary>
        /// Increate the stack from the command
        /// </summary>
        /// <param name="commandName"></param>
        public static void IncreaseCommandCount(string commandName)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                var command = context.Commands.SingleOrDefault(c => String.Compare(commandName, c.Command, true) == 0);

                if (command != null)
                {
                    command.Count++;

                    context.SaveChanges();
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Store Message in Database
        /// </summary>
        /// <param name="AddModel"></param>
        private static void StoreIndatabase(Models.Chat.Messages AddModel)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                context.Chat.Add(
                    new Core.Storage.Chat {
                    TwitchID    = AddModel.TwitchID,
                    ChatMessage = AddModel.Message,
                    Timestamp   = AddModel.TimeStamp,
                });

                context.SaveChanges();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Get Currency from a user
        /// </summary>
        /// <param name="twitchID">todo: describe twitchID parameter on GetCurrencyFromUser</param>
        /// <returns></returns>
        public static long?GetCurrencyFromUser(string twitchID)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                var user = context.Users.SingleOrDefault(u => String.Compare(u.Id, twitchID) == 0);

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

            return(null);
        }
Beispiel #8
0
        /// <summary>
        /// Check if the User has enough Currency
        /// </summary>
        /// <param name="bet"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        private bool UserHasEnoughCurrency(long bet, string userID)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                var currency = context.Currency.SingleOrDefault(c => String.Compare(c.ID, userID) == 0);

                if (currency != null)
                {
                    if (currency.Value >= bet)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #9
0
        public async void AddTimerAsync(Models.Timers.AddModel model)
        {
            var timer = new Core.Storage.Timers {
                Active    = model.Active,
                Autoreset = model.Autoreset,
                CreatedAt = DateTime.Now,
                Interval  = model.Interval,
                Text      = model.Text,
                Timer     = model.Name
            };

            using (var context = new Core.Storage.StorageEntities()) {
                context.Timers.Add(timer);

                await context.SaveChangesAsync();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Fires when a chat command is received
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommandReceived(object sender, OnChatCommandReceivedArgs e)
        {
            using (var context = new Core.Storage.StorageEntities()) {
                var command = context.Commands.SingleOrDefault(c => String.Compare(e.Command.Command, c.Command, true) == 0);

                if (command != null)
                {
                    // can user execute command?
                    if (CanUserExecuteCommand(e.Command.ChatMessage.UserType, command.ExecutionRight))
                    {
                        // check if cooldown is over 0
                        if (command.Cooldown.HasValue && command.Cooldown > 0)
                        {
                            if (command.LastExecution.HasValue)
                            {
                                // last execution is not empty
                                var nextTime = command.LastExecution.Value.AddMinutes(command.Cooldown.Value);

                                // if next execution time <= now
                                if (nextTime <= DateTime.Now)
                                {
                                    ExecuteCommand(command, e);
                                    command.LastExecution = DateTime.Now;

                                    context.SaveChanges();
                                }
                            }
                            else
                            {
                                // last execution is empty - start command
                                ExecuteCommand(command, e);
                                command.LastExecution = DateTime.Now;
                            }
                        }
                        else
                        {
                            // cooldown has no value or is < 0
                            ExecuteCommand(command, e);
                            command.LastExecution = DateTime.Now;
                        }
                    }
                }
            }
        }
Beispiel #11
0
        public UsersInfoVM(string name = null, string id = null)
        {
            if (name == null && id == null)
            {
                return;
            }
            if (id == null)
            {
                using (var context = new Core.Storage.StorageEntities()) {
                    var user = context.Users.SingleOrDefault(x => (String.Compare(x.Name, name, true)) == 0);

                    if (user != null)
                    {
                        Username = user.DisplayName;
                        ID       = user.Id.ToString();
                        Currency = user.Currency.Value.ToString();
                    }
                }
            }
        }
Beispiel #12
0
 /// <summary>
 /// Load the list from the Database
 /// </summary>
 private void LoadCommandList()
 {
     using (var context = new Core.Storage.StorageEntities()) {
         CommandList = context.Commands.ToList();
     }
 }