Beispiel #1
0
 /// <summary>
 /// 添加题目 所有赛制必须填充 Name,Content,Comparer,Owner,DataChecker CF赛制还须填充OriginRating
 /// </summary>
 /// <param name="problem"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 /// <exception cref="ProblemNameExistedException"></exception>
 /// <exception cref="UserNotFoundException"></exception>
 public void AddProblem(Problem problem)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (!Owner.Contains(User.CurrentUser.name)
         && !User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         problem.Name = Helper.GetLegalName(problem.Name);
         if ((from p in db.PROBLEMs
              where p.Name == problem.Name && p.CONTEST1.ID==ID
              select p).Any())
             throw new ProblemNameExistedException();
         var owner = (from u in db.USERs
                      where u.Name == problem.Owner
                      select u).SingleOrDefault();
         if (null == owner)
             throw new UserNotFoundException();
         db.PROBLEMs.Add(new PROBLEM()
         {
             ID = Guid.NewGuid(),
             Name = problem.Name,
             Content = problem.Content,
             Comparer = problem.Comparer,
             ComparerLanguage = (int)problem.ComparerLanguage,
             OriginRating = problem.OriginRating,
             DataChecker = problem.DataChecker,
             DataCheckerLanguage = (int)problem.DataCheckerLanguage,
             CONTEST1 = (from c in db.CONTESTs
                         where c.ID == ID
                         select c).Single(),
             OWNER = owner
         });
         db.SaveChanges();
     }
 }
        List<TestCase> AddTestCase(Contest contest, Problem problem, HttpPostedFileBase file)
        {
            if (file == null)
            {
                ModelState.AddModelError("File", "请选择文件");
                return null;
            }
            Dictionary<int, byte[]> inputFiles = new Dictionary<int, byte[]>();
            Dictionary<int, byte[]> outputFiles = new Dictionary<int, byte[]>();

            if (new[] { "application/zip", "application/x-zip-compressed", "application/x-zip" }.Contains(file.ContentType)
                || file.ContentType == "application/octet-stream" && file.FileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
            {
                using (ZipInputStream stream = new ZipInputStream(file.InputStream))
                {
                    ZipEntry entry;
                    while ((entry = stream.GetNextEntry()) != null)
                    {
                        byte[] bytes;
                        using (MemoryStream mem = new MemoryStream())
                        {
                            stream.CopyTo(mem);
                            bytes = mem.ToArray();
                        }
                        if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles))
                        {
                            return null;
                        }
                    }
                }
            }
            else if (file.FileName.EndsWith(".tgz") || file.FileName.EndsWith(".tar.gz"))
            {
                using (GZipStream stream = new GZipStream(file.InputStream, CompressionMode.Decompress))
                {
                    using (TarInputStream tar = new TarInputStream(stream))
                    {
                        TarEntry entry;
                        while ((entry = tar.GetNextEntry()) != null)
                        {
                            byte[] bytes;
                            using (MemoryStream mem = new MemoryStream())
                            {
                                tar.CopyTo(mem);
                                bytes = mem.ToArray();
                            }
                            if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles))
                            {
                                return null;
                            }
                        }
                    }
                }
            }
            else if (file.FileName.EndsWith(".tar.bz2"))
            {
                using (BZip2InputStream stream = new BZip2InputStream(file.InputStream))
                {
                    using (TarInputStream tar = new TarInputStream(stream))
                    {
                        TarEntry entry;
                        while ((entry = tar.GetNextEntry()) != null)
                        {
                            byte[] bytes;
                            using (MemoryStream mem = new MemoryStream())
                            {
                                tar.CopyTo(mem);
                                bytes = mem.ToArray();
                            }
                            if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles))
                            {
                                return null;
                            }
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("File", "不支持的压缩文件类型");
                return null;
            }

            if (!inputFiles.Keys.OrderBy(x => x).SequenceEqual(outputFiles.Keys.OrderBy(x => x)))
            {
                ModelState.AddModelError("File", "输入与输出文件没有一一对应");
                return null;
            }

            var testCases = inputFiles.Keys.Select(id => new TestCase
                {
                    Input = inputFiles[id],
                    Data = outputFiles[id],
                    MemoryLimit = DEFAULT_TEST_CASE_MEMORY_LIMIT,
                    TimeLimit = DEFAULT_TEST_CASE_TIME_LIMIT,
                    Available = contest.Type == Contest.ContestType.CF ? false : true
                }).ToList();
            foreach (var t in testCases)
            {
                t.ID = problem.AddTestCase(t);
            }
            return testCases;
        }