private static void BackUpBank()
 {
     if (Registry.Instance.BackupFrequency > 0 && Registry.Instance.SyncProfileWithBank && Profile.GetProfile().RankPoints >= 1000)
     {
         try
         {
             Log.Info("Begin Cloning Bank File.");
             BankSaver.CopyBankFile();
             Log.Info("Finished Cloning bank.");
         }
         catch (Exception ex)
         {
             Log.Error("Failed to clone bank file", ex);
         }
     }
 }
Exemplo n.º 2
0
        protected async override Task <bool> ExecuteCommand(IDiscordBot bot, SocketMessage message, TOption option)
        {
            var userId = message.Author.Id.ToString();

            using (var context = _contextFactory.Create())
            {
                var user = await context.Users.AsQueryable()
                           .Include(t => t.Items)
                           .Include(t => t.Equipment)
                           .Include(t => t.CurrentTask)
                           .Include(t => t.SkillSet)
                           .ThenInclude(t => t.Skills)
                           .FirstOrDefaultAsync(t => t.Id == userId);

                if (user == null)
                {
                    await message.Channel.SendMessageAsync($"{message.Author} is not registered, please type +register.");

                    return(false);
                }

                user.Bank = new Inventory(BankConstants.Size, Inventory.StackMode.STACK_ALWAYS);
                user.Bank.CopyTo(user.Items.Select(t => t.Item));
                bool res = await ExecuteCommand(bot, message, user, context, option);

                if (user.Bank.UpdateRequired)
                {
                    await BankSaver.SaveBank(context, user);

                    SaveChanges = true;
                }

                if (SaveChanges)
                {
                    await context.SaveChangesAsync();

                    SaveChanges = false;
                }

                return(res);
            }
        }
Exemplo n.º 3
0
        public async Task Process()
        {
            while (!_quit)
            {
                List <CurrentTask> tasks = null;
                using (var context = _contextFactory.Create())
                {
                    var now = DateTime.Now;
                    tasks = context.CurrentTasks.AsQueryable()
                            .Include(t => t.ExpGains)
                            .Include(t => t.Items)
                            .Where(t => !t.Notified && now > t.UnlockTime)
                            .ToList();

                    if (tasks.Any())
                    {
                        var userIds = tasks.Select(t => t.UserId).ToList();

                        var users = context.Users.AsQueryable()
                                    .Include(t => t.Items)
                                    .Include(t => t.Equipment)
                                    .Include(t => t.CurrentTask)
                                    .Include(t => t.SkillSet)
                                    .ThenInclude(t => t.Skills)
                                    .Where(t => userIds.Contains(t.Id)).ToList();

                        users.ForEach(user =>
                        {
                            var bankUpdateRequired = tasks.Where(t => t.UserId == user.Id && t.Items != null && t.Items.Any()).Any();
                            if (bankUpdateRequired)
                            {
                                context.RemoveRange(user.Items);
                                user.Bank = new Inventory(BankConstants.Size, Inventory.StackMode.STACK_ALWAYS);
                                user.Bank.CopyTo(user.Items.Select(t => t.Item));
                            }
                        });

                        await context.SaveChangesAsync();

                        foreach (var task in tasks)
                        {
                            var user = users.FirstOrDefault(t => t.Id == task.UserId);
                            if (user != null)
                            {
                                if (task.Items != null && task.Items.Any())
                                {
                                    foreach (var item in task.Items)
                                    {
                                        user.Bank.Add(item.Item);
                                    }
                                    await BankSaver.SaveBank(context, user, false);
                                }
                                if (task.ExpGains.Any())
                                {
                                    foreach (var xpGain in task.ExpGains)
                                    {
                                        user.SkillSet.AddExperience(xpGain.Skill, xpGain.Amount);
                                        context.Update(user.SkillSet);
                                    }
                                }
                            }

                            var repeatReply = _replyAwaiter.CreateReply(task);
                            var channel     = _discordBot.Client.GetChannel(task.ChannelId) as IMessageChannel;
                            var xpStr       = string.Join('\n', task.ExpGains.Select(xpGain => $"{_discordBot.GetEmote(Skill.GetName(xpGain.Skill))} +{xpGain.Amount} XP Gained, Level: {user.SkillSet.GetLevel(xpGain.Skill)}, Total XP: {user.SkillSet.GetExp(xpGain.Skill)}"));
                            xpStr += $"\n Press `+{repeatReply.ConfirmChar}` to repeat";
                            await _replyAwaiter.Add(repeatReply);

                            await channel.SendMessageAsync(string.Format(task.CompletionMessage, xpStr));

                            context.RemoveRange(task.Items);
                            context.RemoveRange(task.ExpGains);
                            task.Notified = true;
                        }

                        context.UpdateRange(tasks);
                        await context.SaveChangesAsync();
                    }
                }
                await Task.Delay(TimeSpan.FromMinutes(1));
            }
        }