Example #1
0
        public async Task <CompileDto> Compile(TaskDto task, string source, int language)
        {
            var hackerRankUrl = _configuration["HackerRankUrl"];

            var client = new RestClient(hackerRankUrl);

            var request = new RestRequest("/checker/submission.json", Method.POST);

            var keysList = _configuration.GetSection("HackerRankApiKeys").GetChildren().Select(c => c.Value).ToList();
            var random   = new Random();
            var element  = random.Next(keysList.Count);

            request.AddParameter("source", source);
            request.AddParameter("lang", language);
            request.AddParameter("testcases", JsonConvert.SerializeObject(task.Inputs));
            request.AddParameter("api_key", keysList[element]);
            request.AddParameter("wait", "true");
            request.AddParameter("format", "json");

            TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>();

            client.ExecuteAsync(request, r => taskCompletion.SetResult(r));

            RestResponse response = (RestResponse)await taskCompletion.Task;

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new ArgumentException("Error compiling task");
            }

            dynamic data = JsonConvert.DeserializeObject(response.Content);

            var compileResult = new CompileDto
            {
                Compiled    = data.result.result.Value.ToString() == "0",
                TotalInputs = task.Inputs.Count
            };

            if (compileResult.Compiled)
            {
                for (int i = 0; i < task.Outputs.Count; i++)
                {
                    var expected = string.Join('\n', task.Outputs[i].Split('\n', '\r').Where(s => s.Length != 0).Select(s => s.Trim('\r', '\n', ' ')));
                    var actual   = string.Join('\n', ((string)data.result.stdout[i].Value.ToString()).Split('\n', '\r').Where(s => s.Length != 0).Select(s => s.Trim('\r', '\n', ' ')));
                    if (expected != actual)
                    {
                        compileResult.FailedInput = i + 1;
                        // compileResult.Message = data.result.stderr[i].Value.ToString() == "False" ? (data.result.message[i].Value.ToString() != "Success" ? data.result.message[i].Value.ToString() : "") : data.result.stderr[i].Value.ToString();
                        break;
                    }
                }
            }
            else
            {
                var message = data.result.compilemessage.Value.ToString();
                compileResult.Message = Regex.Replace(message, @"[^\u0000-\u007F]+", string.Empty);
            }

            return(compileResult);
        }
        public async Task <CompileDto> Compile(TaskDto task, string source, int language)
        {
            var ideoneApiUrl = _configuration["IdeoneApiUrl"];
            var client       = new RestClient(ideoneApiUrl);

            var compileDto = new CompileDto
            {
                Compiled    = true,
                FailedInput = 0,
                TotalInputs = task.Inputs.Count
            };

            var languages    = GetLanguagesMap();
            var languageCode = languages[language];

            for (int i = 0; i < task.Outputs.Count; i++)
            {
                var request = new RestRequest("/api/submissions", Method.POST);

                request.AddParameter("sourceCode", source);
                request.AddParameter("langId", languageCode);
                request.AddParameter("stdin", task.Inputs[i]);

                TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>();
                client.ExecuteAsync(request, r => taskCompletion.SetResult(r));

                RestResponse response = (RestResponse)await taskCompletion.Task;

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new ArgumentException("Error compiling task");
                }

                dynamic data      = JsonConvert.DeserializeObject(response.Content);
                var     compileId = data.id;
                var     result    = await GetCompileResult(compileId.ToString(), task.Outputs[i], client);

                if (result.Compiled)
                {
                    if (!result.IsOutputCorrect)
                    {
                        compileDto.FailedInput = i + 1;
                        compileDto.Message     = result.Message;

                        return(compileDto);
                    }
                }
                else
                {
                    return(new CompileDto
                    {
                        Compiled = false,
                        Message = result.Message
                    });
                }
            }

            return(compileDto);
        }