public async Task <IActionResult> Submit(
            int pid, CodeSubmitModel model,
            [FromServices] SubmissionManager subMgr)
        {
            if (model.ProblemId != pid)
            {
                return(BadRequest());
            }

            // check user blocking
            if (User.IsInRole("Blocked"))
            {
                ModelState.AddModelError("xys::blocked",
                                         "You are not permitted to submit code.");
            }

            // check problem submit
            var probQuery =
                from a in DbContext.Archives
                where a.PublicId == pid
                join p in DbContext.Problems on a.ProblemId equals p.ProblemId
                select new { p.Title, p.Source, p.ProblemId, p.AllowSubmit };
            var prob = await probQuery.SingleOrDefaultAsync();

            if (prob == null)
            {
                return(NotFound());
            }

            if (!prob.AllowSubmit)
            {
                StatusMessage = $"Problem {pid} is not allowed for submitting.";
                return(RedirectToAction(nameof(View)));
            }

            // check language blocking
            var lang = await DbContext.Languages
                       .Where(l => l.LangId == model.Language)
                       .SingleOrDefaultAsync();

            if (lang == null)
            {
                ModelState.AddModelError("lang::notfound",
                                         "Language is not found.");
            }
            else if (!lang.AllowSubmit)
            {
                ModelState.AddModelError("lang::notallow",
                                         "You can't submit solutions with this language.");
            }

            if (ModelState.ErrorCount > 0)
            {
                ViewBag.ProblemTitle = prob.Title;
                ViewBag.Language     = await LanguagesAsync();

                return(View(model));
            }
            else
            {
                var sub = await subMgr.CreateAsync(
                    code : model.Code,
                    langid : model.Language,
                    probid : prob.ProblemId,
                    cid : 0,
                    uid : int.Parse(UserManager.GetUserId(User)),
                    ipAddr : HttpContext.Connection.RemoteIpAddress,
                    via : "problem-list",
                    username : UserManager.GetUserName(User));

                int id = sub.SubmissionId;

                return(RedirectToAction(nameof(View)));
            }
        }
Beispiel #2
0
        private async Task LoadSubmissionsAsync(ZipArchive zip)
        {
            var prefix = "submissions/";
            var files  = zip.Entries
                         .Where(z => z.FullName.StartsWith(prefix) && !z.FullName.EndsWith('/'))
                         .ToList();
            var langs = await dbContext.Languages.ToListAsync();

            foreach (var file in files)
            {
                if (file.Length > 65536)
                {
                    Log($"Too big for jury solution '{file.FullName}'");
                    continue;
                }

                var lang = langs.FirstOrDefault(l =>
                                                "." + l.FileExtension == Path.GetExtension(file.FullName));

                if (lang == null)
                {
                    Log($"No language found for jury solution '{file.FullName}'.");
                }
                else
                {
                    var expected = Verdict.Unknown;
                    if (file.FullName.StartsWith("submissions/accepted/"))
                    {
                        expected = Verdict.Accepted;
                    }
                    if (file.FullName.StartsWith("submissions/wrong_answer/"))
                    {
                        expected = Verdict.WrongAnswer;
                    }
                    if (file.FullName.StartsWith("submissions/time_limit_exceeded/"))
                    {
                        expected = Verdict.TimeLimitExceeded;
                    }
                    if (file.FullName.StartsWith("submissions/run_time_error/"))
                    {
                        expected = Verdict.RuntimeError;
                    }

                    using (var stream = file.Open())
                        using (var reader = new StreamReader(stream))
                        {
                            var content = await reader.ReadToEndAsync();

                            var sub = await submissionManager.CreateAsync(
                                code : content,
                                langid : lang.LangId,
                                probid : Problem.ProblemId,
                                cid : 0,
                                uid : 0,
                                ipAddr : System.Net.IPAddress.Parse("127.0.0.1"),
                                via : "polygon-upload",
                                username : "******",
                                expected : expected);

                            Log($"Jury solution '{file.FullName}' saved s{sub.SubmissionId}.");
                        }
                }
            }
        }