예제 #1
0
        public async Task Create(string title, string description, ChallengeDifficulty difficulty, string url)
        {
            if (!Uri.TryCreate(url, UriKind.Absolute, out var result))
            {
                await ReplyAsync("Invalid URL format");

                return;
            }

            if (result.Host != "gist.githubusercontent.com")
            {
                await ReplyAsync("URL must begin with `gist.githubusercontent.com`");

                return;
            }

            Data?data;

            try
            {
                using var wc = new WebClient();
                var json = wc.DownloadString(result);
                data = JsonConvert.DeserializeObject <Data>(json, new JsonSerializerSettings {
                    Converters = new JsonConverter[] {
                        new YololValueConverter()
                    },
                    FloatParseHandling = FloatParseHandling.Decimal
                });
            }
            catch (Exception e)
            {
                await ReplyAsync("Failed: " + e.Message.Substring(0, Math.Min(1000, e.Message.Length)));

                return;
            }

            if (data == null)
            {
                await ReplyAsync("Test cases cannot be null");

                return;
            }

            if (data.In == null)
            {
                await ReplyAsync("Input values cannot be null");

                return;
            }

            if (data.Out == null)
            {
                await ReplyAsync("Output values cannot be null");

                return;
            }

            var c = new Challenge(0, title, "done", data.In, data.Out, null, difficulty, description, data.Shuffle ?? true, data.Mode ?? ScoreMode.BasicScoring);
            await _challenges.Create(c);

            await ReplyAsync("Challenge added to queue");
        }
예제 #2
0
        public async Task Create()
        {
            await ReplyAsync("What is the challenge title?");

            var title = (await NextMessageAsync(timeout: TimeSpan.FromMilliseconds(-1))).Content;

            await ReplyAsync("What is the challenge description?");

            var desc = (await NextMessageAsync(timeout: TimeSpan.FromMilliseconds(-1))).Content;

            var levels = string.Join(',', Enum.GetNames(typeof(ChallengeDifficulty)));

            await ReplyAsync($"What is the challenge difficulty ({levels})?");

            var difficulty = Enum.Parse <ChallengeDifficulty>((await NextMessageAsync(timeout: TimeSpan.FromMilliseconds(-1))).Content);

            await ReplyAsync("What is the challenge URL (raw JSON)?");

            var url = (await NextMessageAsync(timeout: TimeSpan.FromMilliseconds(-1))).Content;

            if (!Uri.TryCreate(url, UriKind.Absolute, out var result))
            {
                await ReplyAsync("Invalid URL format");

                return;
            }

            if (result.Host != "gist.githubusercontent.com")
            {
                await ReplyAsync("URL must begin with `gist.githubusercontent.com`");

                return;
            }

            Data?data;

            try
            {
                using var wc = new WebClient();
                var json = wc.DownloadString(result);
                data = JsonConvert.DeserializeObject <Data>(json, new JsonSerializerSettings {
                    Converters = new JsonConverter[] {
                        new YololValueConverter()
                    },
                    FloatParseHandling = FloatParseHandling.Decimal
                });
            }
            catch (Exception e)
            {
                await ReplyAsync("Failed: " + e.Message.Substring(0, Math.Min(1000, e.Message.Length)));

                return;
            }

            if (data == null)
            {
                await ReplyAsync("Test cases cannot be null");

                return;
            }

            if (data.In == null)
            {
                await ReplyAsync("Input values cannot be null");

                return;
            }

            if (data.Out == null)
            {
                await ReplyAsync("Output values cannot be null");

                return;
            }

            var program = await Parse(data.Code ?? "");

            if (program == null)
            {
                await ReplyAsync("Invalid Program. Cancelling creation.");

                return;
            }

            await ReplyAsync("Do you want to create this challenge (yes/no)?");

            var confirm = (await NextMessageAsync(timeout: TimeSpan.FromMilliseconds(-1))).Content;

            if (!confirm.Equals("yes", StringComparison.OrdinalIgnoreCase))
            {
                await ReplyAsync("Cancelled creating challenge!");

                return;
            }

            var c = new Challenge(0, title, "done", data.In, data.Out, null, difficulty, desc, data.Shuffle ?? true, data.Mode ?? ScoreMode.BasicScoring, data.Chip ?? YololChip.Professional, program);
            await _challenges.Create(c);

            await ReplyAsync("Challenge added to queue");
        }