Exemplo n.º 1
0
        private async Task GenerateSeed(string settingPath)
        {
            var now            = DateTime.UtcNow;
            var userSeedEntity = new UserSeedEntity
            {
                UserId          = Context.User.Id,
                LastSeedRequest = now
            };
            await UserSeedRepository.Save(userSeedEntity);

            var messageResult = await ReplyNoTagAsync("Generating seed...");

            new Thread(async() =>
            {
                try
                {
                    var stopwatch = new Stopwatch();
                    var(patchPath, hashIconPath, spoilerLogPath, version) = await _mmrService.GenerateSeed(now, settingPath, async(i) =>
                    {
                        if (i < 0)
                        {
                            stopwatch.Start();
                            await ModifyNoTagAsync(messageResult, mp => mp.Content = "Generating seed...");
                        }
                        else
                        {
                            await ModifyNoTagAsync(messageResult, mp => mp.Content = $"You are number {i + 1} in the queue.");
                        }
                    });
                    stopwatch.Stop();
                    var filesToSend = new List <FileAttachment>
                    {
                        new FileAttachment(patchPath),
                        new FileAttachment(hashIconPath)
                    };
                    await ModifyNoTagAsync(messageResult, mp =>
                    {
                        mp.Content     = $"Completed in {stopwatch.Elapsed}";
                        mp.Attachments = filesToSend;
                    });
                    File.Delete(patchPath);
                    File.Delete(hashIconPath);
                    userSeedEntity.Version = version;
                    await UserSeedRepository.Save(userSeedEntity);
                }
                catch
                {
                    await UserSeedRepository.DeleteById(Context.User.Id);
                    await ModifyNoTagAsync(messageResult, mp => mp.Content = "An error occured.");
                }
            }).Start();
        }
Exemplo n.º 2
0
        public async Task Seed([Remainder] string settingName = null)
        {
            if (_tournamentChannels.Contains(Context.Channel.Id))
            {
                // Tournament seed
                var mentionedUsers = Context.Message.MentionedUsers.DistinctBy(u => u.Id);
                if (mentionedUsers.Any(u => u.Id == Context.User.Id))
                {
                    await ReplyAsync("Cannot generate a seed for yourself.");

                    return;
                }
                if (mentionedUsers.Count() < 2)
                {
                    await ReplyAsync("Must mention at least two users.");

                    return;
                }
                var tournamentSeedReply = await ReplyAsync("Generating seed...");

                new Thread(async() =>
                {
                    try
                    {
                        var(patchPath, hashIconPath, spoilerLogPath, _) = await MMRService.GenerateSeed(DateTime.UtcNow, null);
                        if (File.Exists(patchPath) && File.Exists(hashIconPath) && File.Exists(spoilerLogPath))
                        {
                            foreach (var user in mentionedUsers)
                            {
                                await user.SendFileAsync(patchPath, "Here is your tournament match seed! Please be sure your Hash matches and let an organizer know if you have any issues before you begin.");
                                await user.SendFileAsync(hashIconPath);
                            }
                            await Context.User.SendFileAsync(spoilerLogPath);
                            await Context.User.SendFileAsync(hashIconPath);
                            File.Delete(spoilerLogPath);
                            File.Delete(patchPath);
                            File.Delete(hashIconPath);
                            await tournamentSeedReply.ModifyAsync(mp => mp.Content = "Success.");
                        }
                        else
                        {
                            throw new Exception("MMR.CLI succeeded, but output files not found.");
                        }
                    }
                    catch (Exception e)
                    {
                        // TODO log exception.
                        await tournamentSeedReply.ModifyAsync(mp => mp.Content = "An error occured.");
                    }
                }).Start();
                return;
            }
            var lastSeedRequest = (await UserSeedRepository.GetById(Context.User.Id))?.LastSeedRequest;

            if (lastSeedRequest.HasValue && (DateTime.UtcNow - lastSeedRequest.Value).TotalHours < 6)
            {
                await ReplyAsync("You may only request a seed once every 6 hours.");

                return;
            }
            if (!string.IsNullOrWhiteSpace(settingName))
            {
                settingName = MMRService.GetSettingsPath(Context.Guild.Id, settingName);
                if (!File.Exists(settingName))
                {
                    await ReplyAsync("Setting not found.");

                    return;
                }
            }
            var now            = DateTime.UtcNow;
            var userSeedEntity = new UserSeedEntity
            {
                UserId          = Context.User.Id,
                LastSeedRequest = now
            };
            await UserSeedRepository.Save(userSeedEntity);

            var messageResult = await ReplyAsync("Generating seed...");

            new Thread(async() =>
            {
                try
                {
                    var(patchPath, hashIconPath, spoilerLogPath, version) = await MMRService.GenerateSeed(now, settingName);
                    await Context.Channel.SendFileAsync(patchPath);
                    await Context.Channel.SendFileAsync(hashIconPath);
                    File.Delete(patchPath);
                    File.Delete(hashIconPath);
                    await messageResult.DeleteAsync();
                    userSeedEntity.Version = version;
                    await UserSeedRepository.Save(userSeedEntity);
                }
                catch
                {
                    await UserSeedRepository.DeleteById(Context.User.Id);
                    await messageResult.ModifyAsync(mp => mp.Content = "An error occured.");
                }
            }).Start();
        }