Ejemplo n.º 1
0
        public async Task <IActionResult> New(NewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(Unauthorized());
            }

            var problem = new Problem
            {
                Title       = model.Title,
                Description = model.Description,
                AuthorId    = user.Id,
                PublishTime = DateTime.UtcNow,
            };

            var exampleData = new TestData
            {
                Input  = model.ExampleData.Input,
                Output = model.ExampleData.Output
            };

            problem.SetExampleData(exampleData);

            var judgeProfile = new JudgeProfile
            {
                MemoryLimit = model.MemoryLimit,
                TimeLimit   = model.TimeLimit,
            };

            problem.SetJudgeProfile(judgeProfile);

            problem.SetPassRate(new PassRate
            {
                Submit = 0,
                Pass   = 0
            });

            _context.Problems.Add(problem);
            await _context.SaveChangesAsync();

            // Save test data to local disk
            var judgeDataStorageDirectory = Path.Combine(Directory.GetCurrentDirectory(), "JudgeDataStorage");

            if (!Directory.Exists(judgeDataStorageDirectory))
            {
                Directory.CreateDirectory(judgeDataStorageDirectory);
            }

            // Copy zip file to target directory
            var problemDirectory = Path.Combine(judgeDataStorageDirectory, _context.Problems.LongCount().ToString());

            Directory.CreateDirectory(problemDirectory);
            var zipFilePath = Path.Combine(problemDirectory, "judge.zip");

            var stream = new FileStream(zipFilePath, FileMode.Create);

            model.TestDatas.CopyTo(stream);
            stream.Close();

            // Unzip file
            var targetDirectory = Path.Combine(problemDirectory, "data");
            var fastZip         = new FastZip();

            fastZip.ExtractZip(zipFilePath, targetDirectory, null);

            // _logger.Log(LogLevel.Information, $"[{DateTime.UtcNow}] User (Id: {user.Id}) created a new problem", problem);

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 2
0
        public IActionResult New(NewViewModel model, int[] ProblemLabels, int[] RecommendedValue)
        {
            var user = HttpContext.Session.Get <UserProfileModel>("CurrentUser");

            if (user == null)
            {
                TempData["Message"] = "请先登录";
                return(RedirectToAction("Login", "Auth"));
            }
            //创建新的Problem
            var problem = new Problem
            {
                OrderNumber = (1000 + _context.Problems.LongCount()).ToString(),
                Title       = model.Title,
                Description = model.Description,
                AuthorId    = user.Id.ToString(),
                PublishTime = DateTime.UtcNow,
                IsDelete    = false
            };

            var exampleData = new TestData
            {
                Input  = model.ExampleData.Input,
                Output = model.ExampleData.Output
            };

            problem.SetExampleData(exampleData);

            var judgeProfile = new JudgeProfile
            {
                MemoryLimit = model.MemoryLimit,
                TimeLimit   = model.TimeLimit,
            };

            problem.SetJudgeProfile(judgeProfile);

            problem.SetPassRate(new PassRate
            {
                Submit = 0,
                Pass   = 0
            });

            _context.Problems.Add(problem);
            _context.SaveChanges();


            //增加题目标签

            long ProblemId = _context.Problems.OrderByDescending(x => x.Id).FirstOrDefault().Id;

            int count = 0;

            foreach (var item in _context.LabelProfiles.ToList())
            {
                if (ProblemLabels.Length == count)
                {
                    break;
                }
                if (ProblemLabels[count] == item.Id)
                {
                    ProblemLabel problemLabel = new ProblemLabel();
                    problemLabel.LabelId   = item.Id.ToString();
                    problemLabel.ProblemId = ProblemId.ToString();
                    problemLabel.Weight    = RecommendedValue[count];
                    _context.ProblemLabels.Add(problemLabel);
                    _context.SaveChanges();
                    count++;
                }
            }



            //创建文件路径
            var judgeDataStorageDirectory = Path.Combine(Directory.GetCurrentDirectory(), "JudgeDataStorage");

            if (!Directory.Exists(judgeDataStorageDirectory))
            {
                Directory.CreateDirectory(judgeDataStorageDirectory);
            }

            //复制zip到指定路径
            var problemDirectory = Path.Combine(judgeDataStorageDirectory, _context.Problems.LongCount().ToString());

            Directory.CreateDirectory(problemDirectory);
            var zipFilePath = Path.Combine(problemDirectory, "judge.zip");

            var stream = new FileStream(zipFilePath, FileMode.Create);

            model.TestDatas.CopyTo(stream);
            stream.Close();

            //解压zip
            var targetDirectory = Path.Combine(problemDirectory, "data");
            var fastZip         = new FastZip();

            fastZip.ExtractZip(zipFilePath, targetDirectory, null);

            return(RedirectToAction(nameof(Index)));
        }