Exemple #1
0
        public void UpdateUser(int id, string name, string nickname, string password, UserRole? role)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                var user = db.Users.Find(id);
                if (user == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "User" });

                if (name != null)
                    user.Name = name;
                if (user.NickName != null)
                    user.NickName = nickname;
                if (password != null)
                    user.Password = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
                if (role != null)
                    user.Role = role.Value;

                db.SaveChanges();
            }
        }
Exemple #2
0
        public int CreateContest(string title, string description, DateTime startTime, DateTime? restTime, DateTime? hackStartTime, DateTime endTime, ContestType type, bool printingEnabled)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                if (startTime > endTime)
                    throw new FaultException<ValidationError>(new ValidationError());
                if (restTime != null && (restTime < startTime || restTime > endTime))
                    throw new FaultException<ValidationError>(new ValidationError());
                if (hackStartTime != null && (hackStartTime < startTime || hackStartTime > endTime))
                    throw new FaultException<ValidationError>(new ValidationError());
                if (restTime != null && hackStartTime != null && restTime > hackStartTime)
                    throw new FaultException<ValidationError>(new ValidationError());

                Contest contest = new Contest
                {
                    Description = description,
                    EndTime = endTime,
                    PrintingEnabled = printingEnabled,
                    StartTime = startTime,
                    RestTime = restTime,
                    HackStartTime = hackStartTime,
                    Title = title,
                    Type = type
                };

                db.Contests.Add(contest);
                db.SaveChanges();
                if (NewContest != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => NewContest(contest.ID));
                return contest.ID;
            }
        }
Exemple #3
0
        public Problem GetProblem(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                var problem = db.Problems.Find(id);
                if (problem == null) return null;

                if (CurrentUser.Role == UserRole.Competitor && problem.Contest.StartTime > DateTime.Now)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError());

                if (problem.Contest.Type == ContestType.TopCoder)
                {
                    var view = db.ProblemViews.Find(id, CurrentUser.ID);
                    if (view == null)
                    {
                        view = new ProblemView
                        {
                            ProblemID = id,
                            UserID = CurrentUser.ID,
                            Time = DateTime.Now
                        };
                        db.ProblemViews.Add(view);
                        db.SaveChanges();
                    }
                }

                return new Problem
                {
                    ID = problem.ID,
                    Content = problem.Content,
                    ContestID = problem.ContestID,
                    Score = problem.Score,
                    ContestTitle = problem.Contest.Title,
                    Title = problem.Title,
                    TestCasesCount = problem.TestCases.Count,
                    MemoryLimit = problem.MemoryLimit,
                    TimeLimit = problem.TimeLimit,
                    Spj = CurrentUser.Role >= UserRole.Manager ? problem.Spj : null,
                    Std = CurrentUser.Role >= UserRole.Manager ? problem.Std : null,
                    Validator = CurrentUser.Role >= UserRole.Manager ? problem.Validator : null,
                    SpjLanguage = CurrentUser.Role >= UserRole.Manager ? problem.SpjLanguage : null,
                    StdLanguage = CurrentUser.Role >= UserRole.Manager ? problem.StdLanguage : null,
                    ValidatorLanguage = CurrentUser.Role >= UserRole.Manager ? problem.ValidatorLanguage : null,
                    ForbiddenLanguages = problem.ForbiddenLanguages
                };
            }
        }
Exemple #4
0
        private void DoJudgeHack(int hackID)
        {
            using (DB db = new DB())
            {
                Contest contest;
                Problem problem;
                Record record;
                Hack hack;

                var h = db.Hacks.Find(hackID);
                if (h == null) return;

                if (h.Status != HackStatus.Pending) return;
                h.Status = Entity.HackStatus.Running;
                db.SaveChanges();

                hack = new Hack
                {
                    ID = h.ID,
                    DatamakerLanguage = h.DatamakerLanguage,
                    DataOrDatamaker = h.DataOrDatamaker,
                    HackeeID = h.Record.UserID,
                    HackeeNickName = h.Record.User.NickName,
                    HackerID = h.HackerID,
                    HackerNickName = h.Hacker.NickName,
                    RecordID = h.RecordID,
                    Status = HackStatus.Running
                };

                var r = h.Record;
                record = new Record
                {
                    ID = r.ID,
                    Code = r.Code,
                    Language = r.Language,
                    ProblemID = r.ProblemID,
                    ProblemTitle = r.Problem.Title,
                    Status = RecordStatus.Running,
                    SubmissionTime = r.SubmissionTime,
                    UserID = r.UserID,
                    UserNickName = r.UserNickName
                };

                var p = r.Problem;
                problem = new Problem
                {
                    ID = p.ID,
                    ContestID = p.ContestID,
                    ContestTitle = p.Contest.Title,
                    ForbiddenLanguages = p.ForbiddenLanguages,
                    MemoryLimit = p.MemoryLimit,
                    Score = p.Score,
                    Spj = p.Spj,
                    SpjLanguage = p.SpjLanguage,
                    Std = p.Std,
                    StdLanguage = p.StdLanguage,
                    TestCasesCount = p.TestCases.Count,
                    TimeLimit = p.TimeLimit,
                    Title = p.Title,
                    Validator = p.Validator,
                    ValidatorLanguage = p.ValidatorLanguage
                };

                using (var conn = GetFreestNode())
                {
                    var ret = conn.Hack(problem, record, hack);
                    h.Status = ret.HackStatus;

                    if (ret.HackStatus == HackStatus.Success)
                    {
                        h.Record.Status = RecordStatus.Hacked;
                        var inputHash = MD5.Create().ComputeHash(ret.HackData.Input);
                        var outputHash = MD5.Create().ComputeHash(ret.HackData.Output);

                        bool existed = (from t in db.TestCases
                                        where t.ProblemID == p.ID
                                         && t.InputHash == inputHash
                                        select t).Any();
                        if (!existed)
                        {
                            var hackTestCase = new TestCase
                            {
                                Input = ret.HackData.Input,
                                InputHash = inputHash,
                                Output = ret.HackData.Output,
                                OutputHash = outputHash,
                                ProblemID = p.ID,
                                Type = TestCaseType.Systemtest
                            };
                            db.TestCases.Add(hackTestCase);
                            db.SaveChanges();
                            hack.GeneratedTestCaseID = hackTestCase.ID;
                        }
                    }
                    else
                    {
                        h.Detail = ret.CompilerOutput;
                    }
                    db.SaveChanges();
                }
            }
        }
Exemple #5
0
        public void DeletePrintRequest(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                PrintRequest request = db.PrintRequests.Find(id);
                if (request == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "PrintRequest" });

                if (CurrentUser.Role >= UserRole.Manager || request.UserID == CurrentUser.ID)
                {
                    if (request.UserID == CurrentUser.ID && CurrentUser.Role == UserRole.Competitor && request.Status != PrintRequestStatus.Pending)
                        return;
                    db.PrintRequests.Remove(request);
                    db.SaveChanges();
                    if (PrintRequestDeleted != null)
                    {
                        System.Threading.Tasks.Task.Factory.StartNew(() => PrintRequestDeleted(id));
                    }
                    foreach (var s in App.Clients.Values.Where(s => s.SessionMode == LocalCenaServer.SessionType.Server))
                    {
                        System.Threading.Tasks.Task.Factory.StartNew(() => s.Callback.PrintDeleted(request.ID));
                    }
                }
                else
                {
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError());
                }
            }
        }
Exemple #6
0
        public void DeleteTestCase(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                TestCase testCase = db.TestCases.Find(id);
                if (testCase == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "TestCase" });

                db.TestCases.Remove(testCase);
                db.SaveChanges();
            }
        }
Exemple #7
0
        public int Submit(int problemID, string code, ProgrammingLanguage language)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                var problem = db.Problems.Find(problemID);
                if (problem == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = problemID, Type = "Problem" });

                // Out of contest time
                if (CurrentUser.Role == UserRole.Competitor && (problem.Contest.StartTime > DateTime.Now || problem.Contest.EndTime < DateTime.Now))
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError());

                // After TC rest time
                if (problem.Contest.Type == ContestType.TopCoder && CurrentUser.Role == UserRole.Competitor && problem.Contest.RestTime < DateTime.Now)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError(), "After tc rest time");

                // CF Locked
                if (problem.Contest.Type == ContestType.Codeforces && CurrentUser.Role == UserRole.Competitor && problem.LockedUsers.Contains(CurrentUser))
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError(), "problem locked");

                var record = new Record
                {
                    Code = code,
                    Language = language,
                    ProblemID = problemID,
                    Status = RecordStatus.Pending,
                    SubmissionTime = DateTime.Now,
                    UserID = CurrentUser.ID,
                    UserNickName = CurrentUser.NickName,
                    Score = 0
                };

                db.Records.Add(record);
                db.SaveChanges();
                if (NewRecord != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => NewRecord(record.ID));
                return record.ID;
            }
        }
Exemple #8
0
        public void CreateUser(string name, string nickname, string password, UserRole role)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                if (db.Users.Where(u => u.Name == name).Any())
                    throw new FaultException<ConflictError>(new ConflictError());

                db.Users.Add(new User
                {
                    Name = name,
                    NickName = nickname,
                    Password = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(password)),
                    Role = role
                });

                db.SaveChanges();
            }
        }
Exemple #9
0
        public int RequestPrinting(int contestID, string content, int copies)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                Contest contest = db.Contests.Find(contestID);
                if (contest == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = contestID, Type = "Contest" });

                if (!contest.PrintingEnabled)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError(), "printing is not enabled");

                if (contest.StartTime > DateTime.Now || contest.EndTime < DateTime.Now)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError(), "Out of contest time");

                PrintRequest request = new PrintRequest
                {
                    Content = content,
                    ContestID = contestID,
                    Copies = copies,
                    Status = PrintRequestStatus.Pending,
                    Time = DateTime.Now,
                    UserID = CurrentUser.ID
                };
                db.PrintRequests.Add(request);
                db.SaveChanges();
                if (NewPrintRequest != null)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => NewPrintRequest(request.ID));
                }
                foreach (var s in App.Clients.Values.Where(s => s.SessionMode == LocalCenaServer.SessionType.Server))
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => s.Callback.NewPrint(request.ID));
                }
                return request.ID;
            }
        }
Exemple #10
0
        public void SetConfig(string key, string value)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Config config = db.Configs.Find(key);
                if (config == null)
                {
                    config = new Config { Key = key, Value = value };
                    db.Configs.Add(config);
                    db.SaveChanges();
                }
                else
                {
                    config.Value = value;
                    db.SaveChanges();
                }
            }
        }
Exemple #11
0
        public void Rejudge(int recordID)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Record record = db.Records.Find(recordID);
                if (record == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = recordID, Type = "Record" });

                record.Status = RecordStatus.Pending;
                record.TimeUsage = null;
                record.MemoryUsage = null;
                record.Detail = null;
                db.SaveChanges();

                if (RecordRejudged != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => RecordRejudged(recordID));
            }
        }
Exemple #12
0
        public void LockProblem(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                Problem problem = db.Problems.Find(id);
                if (problem == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Problem" });

                Contest contest = problem.Contest;
                if (contest.TypeAsInt != (int)ContestType.Codeforces)
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Not codeforces");

                bool accepted = (from r in db.Records
                                 where r.ProblemID == id && r.UserID == CurrentUser.ID
                                 where r.StatusAsInt == (int)RecordStatus.Accepted
                                 select r).Any();
                if (!accepted)
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Not accepted");

                problem.LockedUsers.Add(CurrentUser);
                db.SaveChanges();
            }
        }
Exemple #13
0
        public int HackRecord(int recordID, string dataOrDatamaker, ProgrammingLanguage? datamakerLanguage)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                Record record = db.Records.Find(recordID);
                if (record == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = recordID, Type = "Record" });

                if (record.Status != RecordStatus.Accepted)
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Only accepted records can be hacked");

                Contest contest = record.Problem.Contest;
                Problem problem = record.Problem;

                if (contest.StartTime > DateTime.Now)
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Contest not started");
                if (contest.EndTime < DateTime.Now)
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Contest has finished");

                if (contest.Type == ContestType.Codeforces)
                {
                    if (!problem.LockedUsers.Contains(CurrentUser))
                        throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Please lock the problem first");
                }
                else if (contest.Type == ContestType.TopCoder)
                {
                    if (contest.HackStartTime > DateTime.Now)
                        throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "Hack before hackStartTime");
                }
                else
                    throw new FaultException<InvalidOperationError>(new InvalidOperationError(), "This contest does not support hacking.");

                Hack hack = new Hack
                {
                    DatamakerLanguage = datamakerLanguage,
                    DataOrDatamaker = dataOrDatamaker,
                    HackerID = CurrentUser.ID,
                    RecordID = record.ID,
                    Status = HackStatus.Pending,
                    Time = DateTime.Now
                };

                db.Hacks.Add(hack);
                db.SaveChanges();

                if (NewHack != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => NewHack(hack.ID));
                return hack.ID;
            }
        }
Exemple #14
0
        public int CreateProblem(int contestID, string title, string content, int score, int timeLimit, long memoryLimit,
            string std, string spj, string validator, ProgrammingLanguage? stdLanguage, ProgrammingLanguage? spjLanguage, ProgrammingLanguage? validatorLanguage, IEnumerable<ProgrammingLanguage> forbiddenLanguages)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                if (db.Contests.Find(contestID) == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = contestID, Type = "Contest" });

                Problem problem = new Problem
                {
                    Title = title,
                    Content = content,
                    Score = score,
                    ContestID = contestID,
                    TimeLimit = timeLimit,
                    MemoryLimit = memoryLimit,
                    Std = std,
                    Spj = spj,
                    Validator = validator,
                    StdLanguage = stdLanguage,
                    SpjLanguage = spjLanguage,
                    ValidatorLanguage = validatorLanguage,
                    ForbiddenLanguages = forbiddenLanguages
                };

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

                Bll.StandingsCache.Rebuild(problem.ContestID);//添加或删除题目后需要重新构建排名,这里必须是一个同步事件
                foreach (var client in App.Clients.Values)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => client.Callback.RebuildStandings(problem.ContestID, StandingsCache.Standings[contestID] as List<Entity.StandingItem>));
                }
                return problem.ID;
            }
        }
Exemple #15
0
        public void UpdateContest(int id, string title, string description, DateTime? startTime, DateTime? restTime, DateTime? hackStartTime, DateTime? endTime, ContestType? type, bool? printingEnabled)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Contest contest = db.Contests.Find(id);
                if (contest == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Contest" });

                if (title != null)
                    contest.Title = title;
                if (startTime != null)
                    contest.StartTime = startTime.Value;
                if (endTime != null)
                    contest.EndTime = endTime.Value;
                if (restTime != null)
                    contest.RestTime = restTime.Value;
                if (hackStartTime != null)
                    contest.HackStartTime = hackStartTime.Value;

                if (contest.StartTime > contest.EndTime)
                    throw new FaultException<ValidationError>(new ValidationError());
                if (contest.RestTime != null && (contest.RestTime < contest.StartTime || contest.RestTime > contest.EndTime))
                    throw new FaultException<ValidationError>(new ValidationError());
                if (contest.HackStartTime != null && (contest.HackStartTime < contest.StartTime || contest.HackStartTime > contest.EndTime))
                    throw new FaultException<ValidationError>(new ValidationError());
                if (contest.RestTime != null && contest.HackStartTime != null && contest.RestTime > contest.HackStartTime)
                    throw new FaultException<ValidationError>(new ValidationError());

                if (description != null)
                    contest.Description = description;
                if (type != null)
                    contest.Type = type.Value;
                if (printingEnabled != null)
                    contest.PrintingEnabled = printingEnabled.Value;

                db.SaveChanges();
                if (ContestModified != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => ContestModified(contest.ID));
            }
        }
Exemple #16
0
        public int CreateTestCase(int problemID, byte[] input, byte[] output, TestCaseType type)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                TestCase testCase = new TestCase
                {
                    Input = input,
                    InputHash = MD5.Create().ComputeHash(input),
                    Output = output,
                    OutputHash = MD5.Create().ComputeHash(output),
                    ProblemID = problemID,
                    Type = type
                };
                db.TestCases.Add(testCase);
                db.SaveChanges();
                return testCase.ID;
            }
        }
Exemple #17
0
        public void UpdatePrintRequest(int id, string content, int? copies, PrintRequestStatus? status)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                PrintRequest request = db.PrintRequests.Find(id);
                if (request == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "PrintRequest" });
                if (CurrentUser.Role < UserRole.Manager && request.Status > PrintRequestStatus.Pending)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError());

                if (content != null)
                    request.Content = content;
                if (copies != null)
                    request.Copies = copies.Value;
                if (CurrentUser.Role >= UserRole.Manager && status != null)
                    request.Status = status.Value;
                db.SaveChanges();
                if (PrintRequestUpdated != null)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => PrintRequestUpdated(request.ID));
                }
                foreach (var s in App.Clients.Values.Where(s => s.SessionMode == LocalCenaServer.SessionType.Server))
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => s.Callback.PrintUpdated(request.ID));
                }
            }
        }
Exemple #18
0
        public void DeleteContest(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Contest contest = db.Contests.Find(id);
                if (contest == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Contest" });

                db.Contests.Remove(contest);
                db.SaveChanges();
                if (ContestDeleted != null)
                    System.Threading.Tasks.Task.Factory.StartNew(() => ContestDeleted(id));
            }
        }
Exemple #19
0
        public void UpdateProblem(int id, string title, string content, int? score, int? timeLimit, long? memoryLimit,
            string std, string spj, string validator, ProgrammingLanguage? stdLanguage, ProgrammingLanguage? spjLanguage, ProgrammingLanguage? validatorLanguage, IEnumerable<ProgrammingLanguage> forbiddenLanguages)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Problem problem = db.Problems.Find(id);
                if (problem == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Problem" });

                if (title != null)
                    problem.Title = title;
                if (content != null)
                    problem.Content = content;
                if (score != null)
                    problem.Score = score.Value;
                if (memoryLimit != null)
                    problem.MemoryLimit = memoryLimit.Value;
                if (std != null)
                    problem.Std = std;
                if (spj != null)
                    problem.Spj = spj;
                if (validator != null)
                    problem.Validator = validator;
                if (stdLanguage != null)
                    problem.StdLanguage = stdLanguage;
                if (spjLanguage != null)
                    problem.SpjLanguage = spjLanguage;
                if (validatorLanguage != null)
                    problem.ValidatorLanguage = validatorLanguage;
                if (forbiddenLanguages != null)
                    problem.ForbiddenLanguages = forbiddenLanguages;

                db.SaveChanges();

                foreach (var client in App.Clients.Values)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => client.Callback.RebuildStandings(problem.ContestID, StandingsCache.Standings[problem.ContestID] as List<Entity.StandingItem>));
                }
            }
        }
Exemple #20
0
        public void DeleteProblem(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                Problem problem = db.Problems.Find(id);
                if (problem == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Problem" });
                var contest_id = problem.ContestID;
                db.Problems.Remove(problem);
                db.SaveChanges();
                foreach (var client in App.Clients.Values)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => client.Callback.RebuildStandings(contest_id, StandingsCache.Standings[problem.ContestID] as List<Entity.StandingItem>));
                }
            }
        }
Exemple #21
0
        public void UpdateQuestion(int id, string description, string answer, QuestionStatus? status)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                Question question = db.Questions.Find(id);
                if (question == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Question" });

                if (CurrentUser.Role == UserRole.Competitor && question.AskerID != CurrentUser.ID)
                    throw new FaultException<AccessDeniedError>(new AccessDeniedError(), "The question is not owned by you!");

                if (description != null)
                    question.Description = description;
                if (CurrentUser.Role >= UserRole.Manager && answer != null)
                    question.Answer = answer;
                if (CurrentUser.Role >= UserRole.Manager && status != null)
                    question.Status = status.Value;

                db.SaveChanges();

                Question q = new Question
                {
                    Answer = question.Answer,
                    AskerID = question.AskerID,
                    AskerNickName = question.Asker.NickName,
                    ContestID = question.ContestID,
                    ContestName = question.ContestName,
                    Description = question.Description,
                    ID = question.ID,
                    Status = question.Status,
                    Time = question.Time
                };
                if (question.Status == QuestionStatus.Public)
                {
                    foreach (var client in App.Clients.Values)
                    {
                        System.Threading.Tasks.Task.Factory.StartNew(() => client.Callback.QuestionUpdated(q));
                    }
                }
                else if (question.Status == QuestionStatus.Private)
                {
                    App.Clients[question.AskerID].Callback.QuestionUpdated(q);
                }
                if (QuestionUpdated != null)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => QuestionUpdated(q.ID));
                }
            }
        }
Exemple #22
0
        public void DeleteUser(int id)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                User user = db.Users.Find(id);
                if (user == null)
                    throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "User" });

                db.Users.Remove(user);
                db.SaveChanges();
            }
        }
Exemple #23
0
        public void UpdateTestCase(int id, byte[] input, byte[] output, TestCaseType? type)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Manager);

                TestCase testCase = db.TestCases.Find(id);
                if (testCase == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "TestCase" });

                if (input != null)
                {
                    testCase.Input = input;
                    testCase.InputHash = MD5.Create().ComputeHash(input);
                }
                if (output != null)
                {
                    testCase.Output = output;
                    testCase.OutputHash = MD5.Create().ComputeHash(output);
                }
                if (type != null)
                    testCase.Type = type.Value;

                db.SaveChanges();
            }
        }
Exemple #24
0
        public int AskQuestion(int contestID, string description)
        {
            using (DB db = new DB())
            {
                CheckRole(db, UserRole.Competitor);

                Question question = new Question
                {
                    AskerID = CurrentUser.ID,
                    ContestID = contestID,
                    Description = description,
                    Status = QuestionStatus.Pending,
                    Time = DateTime.Now
                };

                db.Questions.Add(question);
                db.SaveChanges();
                if (NewQuestion != null)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => NewQuestion(question.ID));
                }
                foreach (var s in App.Clients.Values.Where(s => s.SessionMode == LocalCenaServer.SessionType.Server))
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() => s.Callback.NewQuestion(question.ID));
                }
                return question.ID;
            }
        }
Exemple #25
0
        private void DoJudgeRecord(int recordID)
        {
            using (DB db = new DB())
            {
                Contest contest;
                Problem problem;
                Record record;
                StringBuilder detail = new StringBuilder();

                var r = db.Records.Find(recordID);
                if (r == null) return;

                if (r.Status != RecordStatus.Pending) return;
                r.Status = Entity.RecordStatus.Running;
                db.SaveChanges();

                record = new Record
                {
                    ID = r.ID,
                    Code = r.Code,
                    Language = r.Language,
                    ProblemID = r.ProblemID,
                    ProblemTitle = r.Problem.Title,
                    Status = RecordStatus.Running,
                    SubmissionTime = r.SubmissionTime,
                    UserID = r.UserID,
                    UserNickName = r.UserNickName
                };

                var p = r.Problem;
                problem = new Problem
                {
                    ID = p.ID,
                    ContestID = p.ContestID,
                    ContestTitle = p.Contest.Title,
                    ForbiddenLanguages = p.ForbiddenLanguages,
                    MemoryLimit = p.MemoryLimit,
                    Score = p.Score,
                    Spj = p.Spj,
                    SpjLanguage = p.SpjLanguage,
                    Std = p.Std,
                    StdLanguage = p.StdLanguage,
                    TestCasesCount = p.TestCases.Count,
                    TimeLimit = p.TimeLimit,
                    Title = p.Title,
                    Validator = p.Validator,
                    ValidatorLanguage = p.ValidatorLanguage
                };

                contest = p.Contest;

                using (var node = GetFreestNode())
                {
                    var ret = node.Compile(problem, record);
                    if (ret.RecordStatus == RecordStatus.SystemError)
                    {
                        System.Threading.Thread.Sleep(3000);
                        App.Server.Rejudge(record.ID);
                        return;
                    }
                    if (ret.RecordStatus != RecordStatus.Accepted)
                    {
                        r.Status = ret.RecordStatus;
                        r.Detail = ret.CompilerOutput;
                        db.SaveChanges();
                        return;
                    }

                    detail.AppendLine(ret.CompilerOutput);

                    var testCases = from t in db.TestCases
                                    where t.ProblemID == problem.ID
                                    select t;

                    if ((contest.Type == ContestType.Codeforces || contest.Type == ContestType.TopCoder) && contest.EndTime > DateTime.Now)
                    {
                        testCases = testCases.Where(t => t.TypeAsInt == (int)TestCaseType.Pretest);
                        testCases = testCases.Union(from h in db.Hacks
                                                    where h.Record.UserID == record.UserID
                                                        && h.StatusAsInt == (int)HackStatus.Success
                                                        && h.GeneratedTestCase != null
                                                        && h.GeneratedTestCase.ProblemID == problem.ID
                                                    select h.GeneratedTestCase);

                    }

                    var testCaseIDs = testCases.Select(t => t.ID);
                    List<Task<TaskFeedback_Run>> runs = new List<Task<TaskFeedback_Run>>();
                    foreach (var id in testCaseIDs)
                    {
                        var _id = id;
                        var run = System.Threading.Tasks.Task.Factory.StartNew(() =>
                        {
                            return node.Run(problem, record, _id);
                        });
                        runs.Add(run);
                    }

                    System.Threading.Tasks.Task.WaitAll(runs.ToArray());

                    RecordStatus finalStatus = RecordStatus.Accepted;
                    int totalTime = 0;
                    long maxMemory = 0;
                    int account = 0;
                    for (int i = 0; i < runs.Count; i++)
                    {
                        var result = runs[i].Result;
                        if (result.RecordStatus == RecordStatus.SystemError)
                        {
                            System.Threading.Thread.Sleep(3000);
                            App.Server.Rejudge(result.RecordID);
                            return;
                        }
                        detail.AppendFormat("#{0} {1} ({2} ms, {3} KiB)\r\n", i, result.RecordStatus, result.TimeUsage, result.MemUsage / 1024);
                        if (result.RecordStatus > finalStatus)
                        {
                            finalStatus = result.RecordStatus;
                        }
                        if (result.RecordStatus == RecordStatus.Accepted)
                            account++;
                        totalTime += result.TimeUsage;
                        maxMemory = Math.Max((long)result.MemUsage, maxMemory);
                    }
                    r.Status = finalStatus;
                    if (finalStatus == RecordStatus.Accepted)
                    {
                        r.Score = 100;
                    }
                    else
                    {
                        if (testCases.Count() == 0) r.Score = 0;
                        else r.Score = 100 * account / testCases.Count();
                    }
                    r.TimeUsage = totalTime;
                    r.MemoryUsage = maxMemory;
                    r.Detail = detail.ToString();
                    db.SaveChanges();
                }
            }
        }