public void AttendContest(string id) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { Contest contest = (from c in db.Contests where c.ID == iid select c).SingleOrDefault<Contest>(); if (contest == null) throw new ArgumentException("无此比赛"); Access.Required(db, contest, Function.AttendContest); if (contest.EndTime < DateTime.Now) { throw new InvalidOperationException("比赛已结束"); } User currentUser = Security.CurrentUser.GetDBUser(db); if (contest.User.Contains(currentUser)) { throw new InvalidOperationException("早已参加比赛"); } contest.User.Add(currentUser); db.SaveChanges(); } }
public static void Install() { using (MooDB db = new MooDB()) { Install(db); } }
public void ProcessRequest(HttpContext context) { Match match = URL_PATTERN.Match(context.Request.Path); if (match.Success) { int id = int.Parse(match.Groups[1].ToString()); using (MooDB db = new MooDB()) { UploadedFile file = (from f in db.UploadedFiles where f.ID == id select f).SingleOrDefault<UploadedFile>(); if (file == null) { context.Response.StatusCode = 404; return; } context.Server.TransferRequest("/upload/" + file.FileName + "?filename=" + HttpUtility.UrlEncode(file.Name)); } } else { context.Response.StatusCode = 404; } }
public static void AddTestData() { using (MooDB db = new MooDB()) { AddTestData(db); } }
static void AddRoles(MooDB db) { db.Roles.AddObject(Organizer); db.Roles.AddObject(Worker); db.Roles.AddObject(NormalUser); db.Roles.AddObject(Reader); db.SaveChanges(); }
public static BriefRecord ToBriefRecord(this Record record, MooDB db) { return new BriefRecord() { ID = record.ID, CreateTime = record.CreateTime, PublicCode = record.PublicCode, JudgeInfo = record.JudgeInfo == null ? null : (int?)record.JudgeInfo.ID, Language = record.Language, Problem = record.Problem.ID, User = record.User.ID }; }
protected override int Run() { using (MooDB db = new MooDB()) { Contest contest = (from c in db.Contests where c.Status == "Before" && c.StartTime <= DateTime.Now select c).FirstOrDefault<Contest>(); if (contest != null) { contest.Status = "During"; foreach (Problem problem in contest.Problem) { problem.EnableTesting = contest.EnableTestingOnStart; problem.TestCaseHidden = contest.HideTestCaseOnStart; problem.PostLocked = contest.LockPostOnStart; problem.TestCaseLocked = contest.LockTestCaseOnStart; problem.Locked = contest.LockProblemOnStart; problem.Hidden = contest.HideProblemOnStart; problem.RecordLocked = contest.LockRecordOnStart; problem.JudgeInfoHidden = contest.HideJudgeInfoOnStart; problem.ArticleLocked = contest.LockArticleOnStart; } db.SaveChanges(); return 0; } contest = (from c in db.Contests where c.Status == "During" && c.EndTime <= DateTime.Now select c).FirstOrDefault<Contest>(); if (contest != null) { contest.Status = "After"; foreach (Problem problem in contest.Problem) { problem.EnableTesting = contest.EnableTestingOnEnd; problem.TestCaseHidden = contest.HideTestCaseOnEnd; problem.PostLocked = contest.LockPostOnEnd; problem.TestCaseLocked = contest.LockTestCaseOnEnd; problem.Locked = contest.LockProblemOnEnd; problem.Hidden = contest.HideProblemOnEnd; problem.RecordLocked = contest.LockRecordOnEnd; problem.JudgeInfoHidden = contest.HideJudgeInfoOnEnd; problem.ArticleLocked = contest.LockArticleOnEnd; } db.SaveChanges(); return 0; } return 30 * 1000; } }
void Test(MooDB db, Record record) { TestResult result; switch (record.Problem.Type) { case "Traditional": result = TestTraditional(db, record); break; case "SpecialJudged": result = TestSpecialJudged(db, record); break; case "Interactive": result = TestInteractive(db, record); break; case "AnswerOnly": result = TestAnswerOnly(db, record); break; default: result = new TestResult() { Score = 0, Info = "<color:red>*未知的题目类型*</color>" }; break; } int oldScore = (from r in db.Records where r.User.ID == record.User.ID && r.Problem.ID == record.Problem.ID && r.JudgeInfo != null && r.JudgeInfo.Score >= 0 select r.JudgeInfo.Score).DefaultIfEmpty().Max(); int currentScore = Math.Max(oldScore, result.Score); record.User.Score -= oldScore; record.Problem.ScoreSum -= oldScore; record.User.Score += currentScore; record.Problem.ScoreSum += currentScore; if (record.Problem.MaximumScore == null) { record.Problem.MaximumScore = result.Score; } else { record.Problem.MaximumScore = Math.Max(result.Score, (int)record.Problem.MaximumScore); } record.JudgeInfo.Score = result.Score; record.JudgeInfo.Info = result.Info; }
static void FixDatabase(MooDB db) { db.ExecuteStoreCommand("CREATE UNIQUE INDEX IX_Users_Name ON [dbo].[Users] ([Name])"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_TraditionalTestCase] DROP CONSTRAINT [FK_TraditionalTestCase_inherits_TestCase];"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_TraditionalTestCase] ADD CONSTRAINT [FK_TraditionalTestCase_inherits_TestCase] FOREIGN KEY ([ID]) REFERENCES [dbo].[TestCases]([ID]) ON DELETE CASCADE;"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_SpecialJudgedTestCase] DROP CONSTRAINT [FK_SpecialJudgedTestCase_inherits_TestCase];"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_SpecialJudgedTestCase] ADD CONSTRAINT [FK_SpecialJudgedTestCase_inherits_TestCase] FOREIGN KEY ([ID]) REFERENCES [dbo].[TestCases]([ID]) ON DELETE CASCADE;"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_InteractiveTestCase] DROP CONSTRAINT [FK_InteractiveTestCase_inherits_TestCase];"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_InteractiveTestCase] ADD CONSTRAINT [FK_InteractiveTestCase_inherits_TestCase] FOREIGN KEY ([ID]) REFERENCES [dbo].[TestCases]([ID]) ON DELETE CASCADE;"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_AnswerOnlyTestCase] DROP CONSTRAINT [FK_AnswerOnlyTestCase_inherits_TestCase];"); db.ExecuteStoreCommand("ALTER TABLE [dbo].[TestCases_AnswerOnlyTestCase] ADD CONSTRAINT [FK_AnswerOnlyTestCase_inherits_TestCase] FOREIGN KEY ([ID]) REFERENCES [dbo].[TestCases]([ID]) ON DELETE CASCADE;"); }
IndexItem NextArticle() { using (MooDB db = new MooDB()) { Article article = (from a in db.Articles orderby a.ID select a).Skip(passedArticleNumber++).FirstOrDefault(); if (article == null) return null; return new IndexItem { ID = article.ID, Content = article.LatestRevision.Content, Keywords = article.Tag.Select(t => t.Name).ToList(), Title = article.Name }; } }
protected void Application_Start(object sender, EventArgs e) { using (MooDB db = new MooDB()) { if (!db.DatabaseExists()) { DatabaseInstaller.Install(db); MooTestData.AddTestData(db); } } TestDaemon.Instance.TestComplete += (o, record) => { WebSockets.WebSocketsAPIHandler.NotifyTestComplete(record); }; Daemon.StartAll(); }
public static string Login(int userID, string password) { password = Converter.ToSHA256Hash(password); using (MooDB db = new MooDB()) { User user = (from u in db.Users where u.ID==userID && u.Password == password select u).SingleOrDefault<User>(); if (user == null) return null; int token = Rand.RAND.Next(); if (!SiteUsers.ByID.ContainsKey(user.ID)) { SiteUsers.ByID.Add(user.ID, new SiteUser()); } SiteUsers.ByID[user.ID].Initialize(user); SiteUsers.ByID[user.ID].Token = token; return user.ID + "," + token; } }
public void AddContestProblem(string id, int problemID) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { Contest contest = (from c in db.Contests where c.ID == iid select c).SingleOrDefault<Contest>(); if (contest == null) throw new ArgumentException("无此比赛"); Problem problem = (from p in db.Problems where p.ID == problemID select p).SingleOrDefault<Problem>(); if (problem == null) throw new ArgumentException("无此题目"); Access.Required(db, contest, Function.ModifyContest); contest.Problem.Add(problem); db.SaveChanges(); } }
protected override int Run() { using (MooDB db = new MooDB()) { Record record = (from r in db.Records where r.JudgeInfo == null && r.Problem.EnableTesting select r).FirstOrDefault<Record>(); var a = (from r in db.Records where r.JudgeInfo == null select r); if (record == null) { return 5 * 1000; } else { record.JudgeInfo = new JudgeInfo() { Record = record, Score = -1, Info = "<color:blue>*正在评测*</color>" }; db.SaveChanges(); if (TestStart != null) { TestStart(this, record); } Test(db, record); db.SaveChanges(); if (TestComplete != null) { TestComplete(this, record); } return 0; } } }
static void AddRequiredData(MooDB db) { AddRoles(db); }
public static void Install(MooDB db) { db.CreateDatabase(); FixDatabase(db); AddRequiredData(db); }
public int CreateTestCase(string problemID, CreateTestCaseData testCase) { int iproblemID = int.Parse(problemID); using (MooDB db = new MooDB()) { Problem problem = (from p in db.Problems where p.ID == iproblemID select p).SingleOrDefault<Problem>(); if (problem == null) throw new ArgumentException("无此题目"); TestCase newTestCase = testCase.ToTestCase(db); newTestCase.CreatedBy = Security.CurrentUser.GetDBUser(db); newTestCase.Problem = problem; newTestCase.CreateTime = DateTime.Now; Access.Required(db, newTestCase, Function.CreateTestCase); db.TestCases.AddObject(newTestCase); db.SaveChanges(); return newTestCase.ID; } }
IndexItem NextContest() { using (MooDB db = new MooDB()) { Contest contest = (from c in db.Contests orderby c.ID select c).Skip(passedContestNumber++).FirstOrDefault(); if (contest == null) return null; return new IndexItem { ID = contest.ID, Content = contest.Description, Keywords = new List<string>(), Title = contest.Name }; } }
public int CreateUser(CreateUserData user) { using (MooDB db = new MooDB()) { User newUser = new User() { BriefDescription = "我很懒,什么都没留下~", Description = "我真的很懒,真的什么也没留下。", Email = user.Email, Name = user.Name, Password = Converter.ToSHA256Hash(user.Password), CreateTime = DateTime.Now, Role = new SiteRoles(db).NormalUser, Score = 0, }; if (Security.Authenticated) { Access.Required(db, newUser, Function.CreateUser); } db.Users.AddObject(newUser); db.SaveChanges(); return newUser.ID; } }
public void DeleteArticleTag(string articleID, string id) { int iarticleID = int.Parse(articleID); int iid = int.Parse(id); using (MooDB db = new MooDB()) { Article article = (from a in db.Articles where a.ID == iarticleID select a).SingleOrDefault<Article>(); if (article == null) throw new ArgumentException("无此文章"); Tag tag = (from t in article.Tag where t.ID == iid select t).SingleOrDefault<Tag>(); if (tag == null) throw new ArgumentException("无此标签"); Access.Required(db, article, Function.ModifyArticle); article.Tag.Remove(tag); db.SaveChanges(); } }
IndexItem NextTag() { using (MooDB db = new MooDB()) { Tag tag = (from t in db.Tags orderby t.ID select t).Skip(passedTagNumber++).FirstOrDefault(); if (tag == null) return null; return new IndexItem { ID = tag.ID, Keywords = new List<string>(), Title = tag.Name, Content = "" }; } }
IndexItem NextProblem() { using (MooDB db = new MooDB()) { Problem problem = (from p in db.Problems where !p.Hidden orderby p.ID select p).Skip(passedProblemNumber++).FirstOrDefault(); if (problem == null) return null; return new IndexItem { ID = problem.ID, Content = problem.LatestRevision == null ? null : problem.LatestRevision.Content, Keywords = problem.Tag.Select(t => t.Name).ToList(), Title = problem.Name }; } }
IndexItem NextFile() { using (MooDB db = new MooDB()) { UploadedFile file = (from f in db.UploadedFiles orderby f.ID select f).Skip(passedFileNumber++).FirstOrDefault(); if (file == null) return null; return new IndexItem { ID = file.ID, Keywords = new List<string>(), Title = file.Name, Content = file.Description }; } }
public void DeleteMessage() { int? with = OptionalIntParameter("with"); using (MooDB db = new MooDB()) { if (with == null) { Access.Required(db, new Message(), Function.DeletePublicMessage); var toDelete = from m in db.Messages where m.To == null select m; foreach (Message msg in toDelete) { db.Messages.DeleteObject(msg); } } else { Access.Required(db, new Message(), Function.DeletePrivateMessage); var fromMe = from m in db.Messages where m.From.ID == Security.CurrentUser.ID && m.To.ID == with select m; foreach (Message msg in fromMe) { msg.DeletedByFrom = true; if (msg.DeletedByTo) { db.Messages.DeleteObject(msg); } } var toMe = from m in db.Messages where m.To.ID == Security.CurrentUser.ID && m.From.ID == with select m; foreach (Message msg in toMe) { msg.DeletedByTo = true; if (msg.DeletedByFrom) { db.Messages.DeleteObject(msg); } } } db.SaveChanges(); } }
public void DeleteJudgeInfo(string recordID) { int irecordID = int.Parse(recordID); using (MooDB db = new MooDB()) { Record record = (from r in db.Records where r.ID == irecordID select r).SingleOrDefault<Record>(); if (record == null) throw new ArgumentException("无此记录"); //Omit if (record.JudgeInfo == null) return; JudgeInfo info = record.JudgeInfo; Access.Required(db, info, Function.DeleteJudgeInfo); CreateMessage(new CreateMessageData { ToID = record.User.ID, Content = "我对您的[Record:" + record.ID + "]进行了重测。\r\n" + "原始得分为*" + info.Score + "*。\r\n" + "原始测评信息为\r\n" + info.Info }); record.JudgeInfo = null; db.JudgeInfos.DeleteObject(info); db.SaveChanges(); } }
public void DeleteArticle(string id) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { Article article = (from a in db.Articles where a.ID == iid select a).SingleOrDefault<Article>(); if (article == null) throw new ArgumentException("无此文章"); Access.Required(db, article, Function.DeleteArticle); article.Tag.Clear(); db.Articles.DeleteObject(article); db.SaveChanges(); } }
public void DeleteContest(string id) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { Contest contest = (from c in db.Contests where c.ID == iid select c).SingleOrDefault<Contest>(); if (contest == null) throw new ArgumentException("无此比赛"); Access.Required(db, contest, Function.DeleteContest); contest.Problem.Clear(); contest.User.Clear(); db.Contests.DeleteObject(contest); db.SaveChanges(); } }
IndexItem NextUser() { using (MooDB db = new MooDB()) { User user = (from u in db.Users orderby u.ID select u).Skip(passedUserNumber++).FirstOrDefault(); if (user == null) return null; return new IndexItem { ID = user.ID, Content = user.BriefDescription + "\r\n" + user.Description, Keywords = new List<string>(), Title = user.Name, }; } }
public void DeleteFile(string id) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { UploadedFile file = (from f in db.UploadedFiles where f.ID == iid select f).SingleOrDefault<UploadedFile>(); if (file == null) throw new ArgumentException("无此文件"); var spj = from t in db.TestCases.OfType<SpecialJudgedTestCase>() where t.Judger.ID == file.ID select t; var interactive = from t in db.TestCases.OfType<InteractiveTestCase>() where t.Invoker.ID == file.ID select t; var answerOnly = from t in db.TestCases.OfType<AnswerOnlyTestCase>() where t.Judger.ID == file.ID select t; if (spj.Any() || interactive.Any() || answerOnly.Any()) { throw new ArgumentException("尚有测试点使用此文件"); } Access.Required(db, file, Function.DeleteFile); File.Delete(Config.UploadFileDirectory + file.FileName); db.UploadedFiles.DeleteObject(file); db.SaveChanges(); } }
public void DeleteArticleRevision(string articleID, string id) { int iid = int.Parse(id); using (MooDB db = new MooDB()) { ArticleRevision revision = (from r in db.ArticleRevisions where r.ID == iid select r).SingleOrDefault<ArticleRevision>(); if (revision == null) throw new ArgumentException("无此文章版本"); Access.Required(db, revision, Function.DeleteArticleRevision); Article article = revision.Article; if (article.LatestRevision.ID == revision.ID) { throw new InvalidOperationException("不可删除最新版本"); } db.ArticleRevisions.DeleteObject(revision); db.SaveChanges(); } }