public void JudgeFinished(int record_id) { using (DB db = new DB()) { Record record = (from r in db.Records where r.ID == record_id select r).FirstOrDefault(); var Type = record.Problem.Contest.Type; if (Type == ContestType.OI) return; var dtl = (record.Problem.Contest.Type == ContestType.ACM && record.Status != RecordStatus.CompileError ? "" : record.Detail); Result re = new Result() { StatusID = record.ID, Status = record.Status, TimeUsage = record.TimeUsage, MemoryUsage = record.MemoryUsage, UserID = record.UserID, SubmissionTime = record.SubmissionTime, UserNickName = record.UserNickName, Detail = dtl, Language = record.Language, ProblemTitle = record.Problem.Title }; System.Threading.Tasks.Task.Factory.StartNew(() => { LocalCenaServer client; if (App.Clients.TryGetValue(record.UserID, out client)) { client.Callback.JudgeFinished(re); } }); // Push to remote management servers foreach (var s in App.Clients.Values.Where(s => s.SessionMode == LocalCenaServer.SessionType.Server)) { System.Threading.Tasks.Task.Factory.StartNew(() => s.Callback.JudgeFinished(re)); } if (StandingsCache.Standings[record.Problem.ContestID] == null) { StandingsCache.Rebuild(record.Problem.ContestID); } var userindex = (StandingsCache.Standings[record.Problem.ContestID] as List<Entity.StandingItem>).FindIndex(x => x.UserID == record.UserID); if (userindex == -1) { StandingsCache.UpdateSingleUser(record.UserID, record.Problem.ContestID, (from p in db.Problems where p.ContestID == record.Problem.ContestID select p.ID).ToList()); } else { StandingsCache.UpdateSingleDetail(record.UserID, record.ProblemID, record.Problem.ContestID, Type); } foreach (var c in App.Clients.Values) { System.Threading.Tasks.Task.Factory.StartNew(() => { c.Callback.StandingsPush(record.Problem.ContestID, (StandingsCache.Standings[record.Problem.ContestID] as List<Entity.StandingItem>).Find(x => x.UserID == record.UserID)); }); } } }
public byte[] GetOutputHash(int testCaseID) { using (DB db = new DB()) { // Use linq to load only hash return (from t in db.TestCases where t.ID == testCaseID select t.OutputHash).SingleOrDefault(); } }
public void ScheduleAll() { using (DB db = new DB()) { var ids = db.Contests.Where(c => c.EndTime > DateTime.Now).Select(c => c.ID); foreach (var id in ids) { Reschedule(id); } } }
/// <summary> /// 获取有效记录个数 /// </summary> /// <param name="user_id"></param> /// <param name="problem_id"></param> /// <returns></returns> public static int GetEffectiveCount(int user_id, int problem_id) { using (DB db = new DB()) { int count = (from r in db.Records where r.UserID == user_id && r.ProblemID == problem_id && EffectiveStatus.Contains((Entity.RecordStatus)r.StatusAsInt) select r.ID).Count(); return count; } }
/// <summary> /// 获取第一个AC状态的记录 /// </summary> /// <param name="user_id"></param> /// <param name="problem_id"></param> /// <returns></returns> public static Record GetFirstAcceptedRecord(int user_id, int problem_id) { using (DB db = new DB()) { var record = (from r in db.Records where r.UserID == user_id && r.ProblemID == problem_id && r.StatusAsInt == (int)RecordStatus.Accepted orderby r.SubmissionTime ascending select r).FirstOrDefault(); return record; } }
public void StartJudgeAllPending() { List<int> pendingRecordIDs; using (DB db = new DB()) { pendingRecordIDs = (from r in db.Records where r.StatusAsInt == (int)RecordStatus.Pending select r.ID).ToList(); } foreach (var id in pendingRecordIDs) { int _id = id; System.Threading.Tasks.Task.Factory.StartNew(() => JudgeRecord(_id)); } }
public Entity.TestCase GetTestCase(int id) { using (DB db = new DB()) { var testCase = db.TestCases.Find(id); if (testCase == null) return null; return new Entity.TestCase { ID = testCase.ID, Input = testCase.Input, InputHash = testCase.InputHash, Output = testCase.Output, OutputHash = testCase.OutputHash, ProblemID = testCase.ProblemID, ProblemTitle = testCase.Problem.Title, Type = testCase.Type, }; } }
public void Reschedule(int contestID) { using (DB db = new DB()) { Contest contest = db.Contests.Find(contestID); lock (timers) { List<Timer> previousTimers; if (timers.TryGetValue(contestID, out previousTimers)) { foreach (var timer in previousTimers) timer.Stop(); timers.Remove(contestID); } if (contest == null) return; if (contest.EndTime <= DateTime.Now) return; List<Timer> newTimers = new List<Timer>(); if (contest.StartTime > DateTime.Now) { var whenStart = new Timer((contest.StartTime - DateTime.Now).TotalMilliseconds); whenStart.AutoReset = false; whenStart.Elapsed += (o, e) => WhenContestStart(contestID); whenStart.Start(); newTimers.Add(whenStart); } if (contest.EndTime > DateTime.Now) { var whenEnd = new Timer((contest.EndTime - DateTime.Now).TotalMilliseconds); whenEnd.AutoReset = false; whenEnd.Elapsed += (o, e) => WhenContestEnd(contestID); whenEnd.Start(); newTimers.Add(whenEnd); } timers.Add(contestID, newTimers); } } }
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; } }
public Record GetRecord(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var record = db.Records.Find(id); if (record == null) return null; var contest = record.Problem.Contest; var problem = record.Problem; // Managers can view all the details // All records should be published when the contest ends. if (CurrentUser.Role >= UserRole.Manager || contest.EndTime < DateTime.Now) return new Record { ID = record.ID, Code = record.Code, Detail = record.Detail, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; // If not started, competitors cannot view any records if (contest.StartTime > DateTime.Now) return null; // until now, we've got sure that the user is a competitor // and the contest is in progress. switch (contest.Type) { case ContestType.OI: if (CurrentUser.ID == record.UserID) return new Record { ID = record.ID, Code = record.Code, Detail = record.Status == RecordStatus.CompileError ? record.Detail : null, Language = record.Language, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status == RecordStatus.CompileError ? RecordStatus.CompileError : RecordStatus.Unknown, SubmissionTime = record.SubmissionTime, UserID = record.UserID, UserNickName = record.User.NickName }; else return new Record { ID = record.ID, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = RecordStatus.Unknown, SubmissionTime = record.SubmissionTime, UserID = record.UserID, UserNickName = record.User.NickName }; case ContestType.ACM: if (CurrentUser.ID == record.UserID) return new Record { ID = record.ID, Code = record.Code, Detail = record.Status == RecordStatus.CompileError ? record.Detail : null, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; else return new Record { ID = record.ID, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; case ContestType.Codeforces: if (CurrentUser.ID == record.ID || problem.LockedUsers.Contains(CurrentUser)) return new Record { ID = record.ID, Code = record.Code, Detail = record.Detail, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; else return new Record { ID = record.ID, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; case ContestType.TopCoder: if (CurrentUser.ID == record.ID || contest.HackStartTime < DateTime.Now) return new Record { ID = record.ID, Code = record.Code, Detail = record.Detail, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; else return new Record { ID = record.ID, Language = record.Language, MemoryUsage = record.MemoryUsage, ProblemID = record.ProblemID, ProblemTitle = record.Problem.Title, Status = record.Status, SubmissionTime = record.SubmissionTime, TimeUsage = record.TimeUsage, UserID = record.UserID, UserNickName = record.User.NickName }; default: throw new NotImplementedException(); } } }
public List<int> GetRecordList(int contestID) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var contest = db.Contests.Find(contestID); if (contest == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = contestID, Type = "Contest" }); var problemIDs = (from p in db.Problems where p.ContestID == contestID select p.ID).ToList(); var recordIDs = from r in db.Records where problemIDs.Contains(r.ProblemID) where CurrentUser.Role >= UserRole.Manager || r.UserID == CurrentUser.ID orderby r.ID descending select r.ID; return recordIDs.ToList(); } }
public Question GetQuestion(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); Question question = db.Questions.Find(id); if (question == null) return null; if (CurrentUser.Role >= UserRole.Manager || question.Status == QuestionStatus.Public || question.AskerID == CurrentUser.ID) { return 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 }; } else return null; } }
public List<int> GetQuestionList(int contestID) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); return (from q in db.Questions where q.ContestID == contestID where CurrentUser.RoleAsInt >= (int)UserRole.Manager || q.AskerID == CurrentUser.ID || q.StatusAsInt == (int)QuestionStatus.Public select q.ID).ToList(); } }
public Contest GetContest(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var contest = db.Contests.Find(id); if (contest == null) return null; return new Contest { ID = contest.ID, Description = contest.Description, EndTime = contest.EndTime, StartTime = contest.StartTime, RestTime = contest.RestTime, HackStartTime = contest.HackStartTime, Title = contest.Title, Type = contest.Type, PrintingEnabled = contest.PrintingEnabled }; } }
public bool GetLockStatus(int problem_id) { using (DB db = new DB()) { var p = db.Problems.Find(problem_id); if (p.LockedUsers.Contains(CurrentUser)) return true; else return false; } }
public Hack GetHack(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); Hack hack = db.Hacks.Find(id); if (hack == null) return null; return new Hack { DatamakerLanguage = hack.DatamakerLanguage, DataOrDatamaker = hack.DataOrDatamaker, Detail = hack.Detail, HackerID = hack.HackerID, HackerNickName = hack.Hacker.NickName, HackeeNickName = hack.Record.User.NickName, HackeeID = hack.Record.UserID, ID = hack.ID, RecordID = hack.RecordID, Status = hack.Status }; } }
public ProblemStatistics GetProblemStatistics(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" }); if (problem.Contest.Type == ContestType.OI && CurrentUser.Role == UserRole.Competitor) throw new FaultException<AccessDeniedError>(new AccessDeniedError()); var records = from r in db.Records where r.ProblemID == problem.ID select r; return new ProblemStatistics { ProblemTitle = problem.Title, AC = records.Where(r => r.StatusAsInt == (int)RecordStatus.Accepted).Count(), CE = records.Where(r => r.StatusAsInt == (int)RecordStatus.CompileError).Count(), MLE = records.Where(r => r.StatusAsInt == (int)RecordStatus.MemoryLimitExceeded).Count(), RE = records.Where(r => r.StatusAsInt == (int)RecordStatus.RuntimeError).Count(), SE = records.Where(r => r.StatusAsInt == (int)RecordStatus.SystemError).Count(), TLE = records.Where(r => r.StatusAsInt == (int)RecordStatus.TimeLimitExceeded).Count(), VE = records.Where(r => r.StatusAsInt == (int)RecordStatus.ValidatorError).Count(), WA = records.Where(r => r.StatusAsInt == (int)RecordStatus.WrongAnswer).Count(), }; } }
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 }; } }
public HackResult GetHackGeneral(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); Hack hack = db.Hacks.Find(id); if (hack == null) return null; return new HackResult { ProblemTitle = hack.Record.Problem.Title, HackerUserNickName = hack.Hacker.NickName, DefenderUserNickName = hack.Record.User.NickName, HackID = hack.ID, RecordID = hack.RecordID, Status = hack.Status, Time = hack.Time }; } }
public List<int> GetPrintRequestList(int contestID) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); return (from r in db.PrintRequests where r.ContestID == contestID && (CurrentUser.Role >= UserRole.Manager ? true : r.UserID == CurrentUser.ID) orderby r.StatusAsInt ascending select r.ID).ToList(); } }
public PrintRequest GetPrintRequest(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); PrintRequest request = db.PrintRequests.Find(id); if (request == null) return null; if (CurrentUser.Role >= UserRole.Manager || CurrentUser.ID == request.ID) { return new PrintRequest { ID = request.ID, Content = request.Content, ContestID = request.ContestID, ContestTitle = request.Contest.Title, Copies = request.Copies, Status = request.Status, Time = request.Time, UserID = request.UserID, UserNickName = request.User.NickName }; } else { return null; } } }
public List<int> GetOnlineList() { using (DB db = new DB()) { CheckRole(db, UserRole.Manager); } lock (App.Clients) { return (from c in App.Clients where c.Value.CurrentUser != null && c.Value.CurrentUser.Role != UserRole.System select c.Value.CurrentUser.ID).ToList(); } }
/// <summary> /// 获取最后一条记录 /// </summary> /// <param name="user_id"></param> /// <param name="problem_id"></param> /// <returns></returns> public static Record GetLastRecord(int user_id, int problem_id) { using (DB db = new DB()) { var record = (from r in db.Records where r.UserID == user_id && r.ProblemID == problem_id orderby r.SubmissionTime descending select r).FirstOrDefault(); return record; } }
public Entity.ProblemGeneral GetProblemTitle(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var problem = db.Problems.Find(id); if (problem == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = id, Type = "Problem" }); if (CurrentUser.Role == UserRole.Competitor && problem.Contest.StartTime > DateTime.Now) throw new FaultException<AccessDeniedError>(new AccessDeniedError()); Entity.ProblemGeneral problem_general = new ProblemGeneral(); problem_general.Title = problem.Title; problem_general.TimeLimit = problem.TimeLimit; problem_general.MemoryLimit = Convert.ToInt32(problem.MemoryLimit / 1024 / 1024); problem_general.ProblemID = problem.ID; problem_general.SpecialJudge = problem.Spj == null ? false : true; if (problem.Contest.Type == ContestType.Codeforces || problem.Contest.Type == ContestType.TopCoder) problem_general.Points = problem.Score; var contest = db.Contests.Find(problem.ContestID); var last_record = RecordHelper.GetLastRecord(CurrentUser.ID, problem.ID); switch (contest.Type) { case ContestType.OI: { if (last_record != null) { problem_general.Status = ProblemGeneralStatus.Submitted; problem_general.Time = last_record.SubmissionTime; } break; } case ContestType.ACM: { if (last_record != null) { problem_general.Status = ProblemGeneralStatus.Pending; problem_general.Time = last_record.SubmissionTime; } else problem_general.Status = null; if (RecordHelper.GetFirstAcceptedRecord(CurrentUser.ID, problem.ID) != null) problem_general.Status = ProblemGeneralStatus.Accepted; break; } case ContestType.Codeforces: { if (last_record == null) problem_general.Status = null; else { problem_general.Time = last_record.SubmissionTime; if (last_record.Status == RecordStatus.Hacked) problem_general.Status = ProblemGeneralStatus.Hacked; else if (last_record.Status == RecordStatus.Accepted) problem_general.Status = ProblemGeneralStatus.Accepted; else problem_general.Status = ProblemGeneralStatus.Pending; } break; } } return problem_general; } }
public List<int> GetContestList() { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); IQueryable<Contest> contests = db.Contests; var ids = (from c in db.Contests let status = (c.StartTime <= DateTime.Now && DateTime.Now < c.EndTime) ? 0 : (DateTime.Now < c.StartTime ? 1 : 2) orderby status ascending, c.StartTime descending select c.ID).ToList(); return ids; } }
public User GetProfile() { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); return new User { ID = CurrentUser.ID, Name = CurrentUser.Name, NickName = CurrentUser.NickName, Role = CurrentUser.Role }; } }
public List<int> GetProblemList(int contestID) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var contest = db.Contests.Find(contestID); if (contest == null) throw new FaultException<NotFoundError>(new NotFoundError { ID = contestID, Type = "Contest" }); if (CurrentUser.Role == UserRole.Competitor && contest.StartTime > DateTime.Now) return new List<int>(); return (from p in db.Problems where p.ContestID == contestID orderby p.Score ascending select p.ID).ToList(); } }
public List<int> GetHackList(int contestID) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); return (from h in db.Hacks where h.Record.Problem.ContestID == contestID select h.ID).ToList(); } }
private void WhenContestEnd(int contestID) { using (DB db = new DB()) { var contest = db.Contests.Find(contestID); switch (contest.Type) { case ContestType.Codeforces: //TODO: rejudge all records including system test. break; case ContestType.TopCoder: //TODO: rejudge all records including system test. break; } } }
public int GetProblemRelatedContest(int id) { using (DB db = new DB()) { CheckRole(db, UserRole.Competitor); var cid = (from p in db.Problems where p.ID == id select p.ContestID).FirstOrDefault(); return cid; } }