예제 #1
0
 /// <summary>
 /// 增加一组测试数据
 /// </summary>
 /// <param name="testcase"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 public Guid AddTestCase(TestCase testCase)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (!contest.Owner.Contains(User.CurrentUser.name) &&
         !User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         var result = db.TESTDATAs.Add(new TESTDATA()
         {
             ID = Guid.NewGuid(),
             Input = testCase._Input,
             Data = testCase._Data,
             TimeLimit = testCase.TimeLimit,
             MemoryLimit = testCase.MemoryLimit,
             PROBLEM1 = (from p in db.PROBLEMs
                         where p.ID == ID
                         select p).Single(),
             Available = testCase.Available
         });
         db.SaveChanges();
         return result.ID;
     }
 }
 protected override int Run()
 {
     bool flg = false;
     var testers = (from t in Framework.tester
                    where t.HuntList.Count < 3 && t.RecList.Count < 3
                    select t).ToArray();
     for (int i = 0; i < testers.Length; i++)
     {
         var x = testers[i];
         int p = rand.Next(testers.Length);
         testers[i] = testers[p];
         testers[p] = x;
     }
     using (var db = new CHDB())
     {
         foreach (var tester in testers)
         {
             var rec = (from r in db.RECORDs
                        where r.Status == (int)Record.StatusType.Pending
                        select r).FirstOrDefault();
             if (null != rec)
             {
                 flg = true;
                 rec.Status = (int)Record.StatusType.Running;
                 db.SaveChanges();
                 tester.RecList.Add(rec.ID);
             }
             var hu = (from h in db.HUNTs
                       where h.Status == (int)Hunt.StatusType.Pending
                       select h).FirstOrDefault();
             if (null != hu)
             {
                 flg = true;
                 hu.Status = (int)Hunt.StatusType.Running;
                 db.SaveChanges();
                 tester.HuntList.Add(hu.ID);
             }
         }
     }
     if (flg)
         return 0;
     return 3000;
 }
예제 #3
0
 /// <summary>
 /// 增加用户组 需要填充 Name
 /// </summary>
 /// <param name="name"></param>
 /// <exception cref="PermissionDeniedException"></exception>
 public static void Add(Group group)
 {
     CheckPriviledge();
     using (var db = new CHDB())
     {
         db.GROUPs.Add(new GROUP()
         {
             ID = Guid.NewGuid(),
             Name =group.Name
         });
         db.SaveChanges();
     }
 }
예제 #4
0
 public static void DomainInstallation()
 {
     using (var db = new CHDB())
     {
         db.GROUPs.Add(new GROUP()
         {
             ID = Guid.NewGuid(),
             Name = "Administrators"
         });
         using (var sha = SHA256.Create())
         {
             db.USERs.Add(new USER()
             {
                 ID = Guid.NewGuid(),
                 Name = "Administrator",
                 Email = "*****@*****.**",
                 Password = sha.ComputeHash(Encoding.Unicode.GetBytes("07070078899"))
             });
         }
         db.SaveChanges();
         (from u in db.USERs
          where u.Name == "Administrator"
          select u).Single().GROUPs.Add
         (
         (from g in db.GROUPs
             where g.Name == "Administrators"
             select g).Single()
         );
         db.SESSIONs.Add(new SESSION()
         {
             ID = Guid.Empty,
             Name = "公共版聊",
             Type = (int)Chat.SessionType.CommonChat
         });
         db.SaveChanges();
     }
 }
예제 #5
0
 /// <summary>
 /// 发送消息至公共聊天区 只需要填充 Content
 /// </summary>
 /// <param name="Msg"></param>
 public static void PostMessage(Session Session, Message Msg)
 {
     using (var db = new CHDB())
     {
         db.MESSAGEs.Add(new MESSAGE()
         {
             Session = Session.ID,
             Content = Msg.Content,
             User = User.CurrentUser.ID,
             Time = DateTime.Now,
             ID = Guid.NewGuid()
         });
         db.SaveChanges();
     }
 }
예제 #6
0
 /// <summary>
 /// 修改测试数据时间/内存限制
 /// </summary>
 /// <param name="ID"></param>
 /// <param name="newTimeLimit"></param>
 /// <param name="newMemoryLimit"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 public static void Change(Guid ID, int newTimeLimit,long newMemoryLimit,bool newAvailable)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     using (var db = new CHDB())
     {
         var test = (from t in db.TESTDATAs
                     where t.ID == ID
                     select t).Single();
         if (!User.CurrentUser.IsAdmin && !(from u in test.PROBLEM1.CONTEST1.OWNERs
                                                                      select u.Name).Contains(User.CurrentUser.name))
             throw new PermissionDeniedException();
         test.MemoryLimit = newMemoryLimit;
         test.TimeLimit = newTimeLimit;
         test.Available = newAvailable;
         db.SaveChanges();
     }
 }
예제 #7
0
 /// <summary>
 /// 重新测评Hunt
 /// </summary>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedExcetpion"></exception>
 public void ReJudge()
 {
     if (null == Domain.User.CurrentUser)
         throw new UserNotLoginException();
     using (var db = new CHDB())
     {
         var curHunt = (from h in db.HUNTs
                          where h.ID == ID
                          select h).Single();
         var curContest = Domain.Contest.ByName(curHunt.RECORD1.PROBLEM1.CONTEST1.Name);
         if (!Domain.User.CurrentUser.IsAdmin && !curContest.Owners.Contains(Domain.User.CurrentUser.Name))
             throw new PermissionDeniedException();
         (from h in db.HUNTs
          where h.ID == ID
          select h).Single().Status = (int)StatusType.Pending;
         db.SaveChanges();
     }
 }
예제 #8
0
 /// <summary>
 /// 为指定用户组增加用户
 /// </summary>
 /// <param name="grp"></param>
 /// <param name="user"></param>
 /// <exception cref="PermissionDeniedException"></exception>
 public void AddUser(User user)
 {
     CheckPriviledge();
     using (var db = new CHDB())
     {
         (from u in db.USERs
          where u.ID==user.ID
          select u).Single().GROUPs.Add
          (
             (from g in db.GROUPs
             where g.ID==ID
             select g).Single()
             );
         db.SaveChanges();
     }
 }
예제 #9
0
 /// <summary>
 /// Hunt本记录
 /// </summary>
 /// <param name="Data"></param>
 /// <exception cref="RecordStatusMismatchException"></exception>
 /// <exception cref="ContestTypeMismatchException"></exception>
 /// <exception cref="ContestEndedException"></exception>
 /// <exception cref="ProblemNotLockedException"></exception>
 /// <exception cref="ProblemNotPassedException"></exception>
 /// <exception cref="HuntSelfException"></exception>
 public Guid Hunt(string Data, LanguageType Type)
 {
     using (var db = new CHDB())
     {
         var curRecord = (from r in db.RECORDs
                          where r.ID == ID
                          select r).Single();
         if (curRecord.USER1.ID == Domain.User.CurrentUser.ID)
             throw new HuntSelfException();
         if (curRecord.Status != (int)Record.StatusType.Accept || (from r in db.RECORDs
                                                                   where r.USER1.ID == curRecord.USER1.ID
                                                                   && r.PROBLEM1.ID == curRecord.PROBLEM1.ID
                                                                   && (r.Status == (int)Record.StatusType.Accept || r.Status == (int)Record.StatusType.Hacked)
                                                                   orderby r.VirtualSubmitTime descending
                                                                   select r).First().ID != curRecord.ID)
             throw new RecordStatusMismatchException();
         var curContest = Domain.Contest.ByName(curRecord.PROBLEM1.CONTEST1.Name);
         if (curContest.Type != Domain.Contest.ContestType.CF)
             throw new ContestTypeMismatchException();
         if (DateTime.Now > curContest.AbsoluteEndTime)
             throw new ContestEndedException();
         var curProbelm = curContest.ProblemByName(curRecord.PROBLEM1.Name);
         if (!curProbelm.IsLock())
             throw new ProblemNotLockedException();
         /*
         if (!(from r in db.RECORDs
               where r.USER1.ID == Domain.User.CurrentUser.ID
               && r.PROBLEM1.ID == curProbelm.ID
               && r.Status == (int)Record.StatusType.Accept
               select r).Any())
             throw new ProblemNotPassedException();
          * */
         Guid newid = Guid.NewGuid();
         db.HUNTs.Add(new HUNT()
         {
             ID = newid,
             HuntData = Data,
             DataType = (int)Type,
             RECORD1 = curRecord,
             User = Domain.User.CurrentUser.ID,
             Status = (int)Domain.Hunt.StatusType.Pending,
             Time = DateTime.Now
         });
         db.SaveChanges();
         return newid;
     }
 }
예제 #10
0
        /// <summary>
        /// 重新计算Rating
        /// </summary>
        /// <exception cref="UserNotLoginException"></exception>
        /// <exception cref="PermissionDeniedException"></exception>
        public void ReCalcRating()
        {
            if (null == User.CurrentUser)
                throw new UserNotLoginException();
            if (!User.CurrentUser.IsAdmin && !Owner.Contains(User.CurrentUserName))
                throw new PermissionDeniedException();
            using (var db = new CHDB())
            {

                foreach (var r in (from r in db.RATINGs
                                   where r.CONTEST1.ID == ID
                                   select r))
                    db.RATINGs.Remove(r);
                (from c in db.CONTESTs
                 where c.ID == ID
                 select c).Single().Status = (int)StatusType.BeforeFinalTest;
                db.SaveChanges();
            }
        }
예제 #11
0
        /// <summary>
        /// 删除指定名称的题目
        /// </summary>
        /// <param name="name"></param>
        /// <exception cref="ContestNotStartedException"></exception>
        /// <exception cref="UserNotLoginException"></exception>
        /// <exception cref="ProblemNotFoundException"></exception>
        public void RemoveProblem(string name)
        {
            if (null == User.CurrentUser)
                throw new UserNotLoginException();
            if (!Owner.Contains(User.CurrentUser.name)
                && !User.CurrentUser.IsAdmin)
                throw new PermissionDeniedException();

            using (var db = new CHDB())
            {
                var prob = (from p in db.PROBLEMs
                            where p.Name == name && p.CONTEST1.ID==ID
                            select p).SingleOrDefault();
                if (null == prob)
                    throw new ProblemNotFoundException();
                db.PROBLEMs.Remove(prob);
                db.SaveChanges();
            }
        }
예제 #12
0
        /// <summary>
        /// 添加比赛
        /// </summary>
        /// <param name="contest"></param>
        /// <exception cref="UserNotLoginException"></exception>
        /// <exception cref="PermissionDeniedException"></exception>
        /// <exception cref="ContestNameExistedException"></exception>
        public static void Add(Contest contest)
        {
            if (null == User.CurrentUser)
                throw new UserNotLoginException();
            if (!contest.Owners.Contains(User.CurrentUser.name))
                throw new PermissionDeniedException();
            using (var db = new CHDB())
            {
                if (contest.IsOfficial && !User.CurrentUser.IsAdmin
                    && User.ByName(User.CurrentUser.name).Rating < 2100)
                    throw new PermissionDeniedException();
                contest.Name = Helper.GetLegalName(contest.Name);
                if ((from c in db.CONTESTs
                     where c.Name == contest.Name
                     select c).Any())
                    throw new ContestNameExistedException();
                var curContest = db.CONTESTs.Add(new CONTEST()
                {
                    ID = Guid.NewGuid(),
                    Name = Helper.GetLegalName(contest.Name),
                    StartTime = contest.AbsoluteStartTime,
                    EndTime = contest.AbsoluteEndTime,
                    Description = contest.Description,
                    Type = (int)contest.Type,
                    IsOfficial = contest.IsOfficial,
                    Weight = User.CurrentUser.IsAdmin ? contest.Weight : 16
                });

                foreach (string name in contest.Owners)
                {
                    curContest.OWNERs.Add(
                        (from u in db.USERs
                         where u.Name == name
                         select u).Single()
                        );
                }

                db.SaveChanges();
            }
        }
예제 #13
0
 /// <summary>
 /// 修改Contest信息
 /// </summary>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 /// <exception cref="ContestNameExistedException"></exception>
 public void Change()
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (!Owner.Contains(User.CurrentUser.name) &&
         !User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         var con = (from c in db.CONTESTs
                    where c.ID==ID
                    select c).Single();
         if (IsOfficial != con.IsOfficial && !User.CurrentUser.IsAdmin &&
             User.ByName(User.CurrentUser.name).Rating < 2100)
             throw new PermissionDeniedException();
         Name = Helper.GetLegalName(Name);
         if (con.Name != Helper.GetLegalName(Name))
         {
             if ((from c in db.CONTESTs
                  where c.Name == Name
                  select c).Any())
                 throw new ContestNameExistedException();
             con.Name = Name;
         }
         con.Description = Description;
         if (Owner != Owners)
         {
             con.OWNERs.Clear();
             foreach (var name in Owners)
             {
                 con.OWNERs.Add((from u in db.USERs
                                 where u.Name == name
                                 select u).Single());
             }
         }
         con.IsOfficial = IsOfficial;
         con.StartTime = AbsoluteStartTime;
         con.EndTime = AbsoluteEndTime;
         con.Type = (int)Type;
         if (User.CurrentUser.IsAdmin)
             con.Weight = Weight;
         db.SaveChanges();
     }
 }
예제 #14
0
 static void RecordBadWating(CHDB db)
 {
     foreach (var h in (from h in db.RECORDs
                        where h.Status == (int)Record.StatusType.Running
                        select h).ToArray())
         h.Status = (int)Record.StatusType.Pending;
     int x=db.SaveChanges();
 }
예제 #15
0
 protected override int Run()
 {
     using (var db = new CHDB())
     {
         var con = (from c in db.CONTESTs
                    where c.EndTime < DateTime.Now && c.Status != (int)Contest.StatusType.Done
                    select c).FirstOrDefault();
         if (null == con)
             return 300000;
         switch ((Contest.ContestType)con.Type)
         {
             case Contest.ContestType.ACM:
                 if (con.Status == (int)Contest.StatusType.BeforeFinalTest)
                 {
                     con.Status = (int)Contest.StatusType.FinalTesting;
                 }
                 else
                 {
                     if (!(from r in db.RECORDs
                           where r.PROBLEM1.CONTEST1.ID == con.ID &&
                           r.SubmitTime <= con.EndTime &&
                           (r.Status == (int)Record.StatusType.Pending || r.Status == (int)Record.StatusType.Running)
                           select r).Any())
                     {
                         if (con.IsOfficial)
                             CalcRating(con, db);
                         con.Status = (int)Contest.StatusType.Done;
                     }
                 }
                 break;
             case Contest.ContestType.CF:
                 if (con.Status == (int)Contest.StatusType.BeforeFinalTest)
                 {
                     if ((from h in db.HUNTs
                          where h.RECORD1.PROBLEM1.CONTEST1.ID == con.ID
                          && (h.Status == (int)Hunt.StatusType.Pending || h.Status == (int)Hunt.StatusType.Running)
                          select h).Any())
                         break;
                     foreach (TESTDATA test in (from t in db.TESTDATAs
                                                where t.PROBLEM1.CONTEST1.ID == con.ID && t.Available == false
                                                select t))
                     {
                         test.Available = true;
                     }
                     foreach (RECORD rec in (from r in db.RECORDs
                                             where r.PROBLEM1.CONTEST1.ID == con.ID
                                             select r))
                     {
                         rec.Status = (int)Record.StatusType.Pending;
                     }
                     con.Status = (int)Contest.StatusType.FinalTesting;
                     string[] probs = (from p in con.PROBLEMs
                                       select p.ID.ToString()).ToArray();
                     lock (HuntLst)
                     {
                         foreach (string key in HuntLst.Keys.ToArray())
                         {
                             bool flg = false;
                             foreach (string prob in probs)
                                 if (key.EndsWith(prob))
                                 {
                                     flg = true;
                                     break;
                                 }
                             if (flg)
                             {
                                 HuntLst.Remove(key);
                             }
                         }
                     }
                 }
                 else
                 {
                     if (!(from r in db.RECORDs
                           where r.PROBLEM1.CONTEST1.ID == con.ID &&
                           r.SubmitTime <= con.EndTime &&
                           (r.Status == (int)Record.StatusType.Pending || r.Status == (int)Record.StatusType.Running)
                           select r).Any())
                     {
                         if (con.IsOfficial)
                             CalcRating(con, db);
                         con.Status = (int)Contest.StatusType.Done;
                     }
                 }
                 break;
             case Contest.ContestType.OI:
                 if (con.Status == (int)Contest.StatusType.BeforeFinalTest)
                 {
                     con.Status = (int)Contest.StatusType.FinalTesting;
                 }
                 else
                 {
                     if (!(from r in db.RECORDs
                           where r.PROBLEM1.CONTEST1.ID == con.ID &&
                           r.SubmitTime <= con.EndTime &&
                           (r.Status == (int)Record.StatusType.Pending || r.Status == (int)Record.StatusType.Running)
                           select r).Any())
                     {
                         if (con.IsOfficial)
                             CalcRating(con, db);
                         con.Status = (int)Contest.StatusType.Done;
                     }
                 }
                 break;
         }
         db.SaveChanges();
     }
     return 60000;
 }
예제 #16
0
 static void HuntBadWating(CHDB db)
 {
     foreach (var h in (from h in db.HUNTs
                        where h.Status == (int)Hunt.StatusType.Running
                        select h))
         h.Status = (int)Hunt.StatusType.Pending;
     db.SaveChanges();
 }
예제 #17
0
 /// <summary>
 /// 修改Problem内容 把 Name OriginRating Content Comparer DataChecker Owner DataCheckerLanguage ComparerLanguage 更新
 /// </summary>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 /// <exception cref="UserNotFoundException"></exception>
 /// <exception cref="ProblemNameExistedException"></exception>
 public void Change()
 {
     if(null==User.CurrentUser)
         throw new UserNotLoginException();
     if (!contest.Owner.Contains(User.CurrentUser.name) &&
         !User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         Name = Helper.GetLegalName(Name);
         var pro = (from p in db.PROBLEMs
                    where p.ID==ID
                    select p).Single();
         var owner = (from u in db.USERs
                      where u.Name == Owner
                      select u).SingleOrDefault();
         if (null == owner)
             throw new UserNotFoundException();
         if (pro.Name != Name && (from p in db.PROBLEMs
                                  where p.Name == Name && p.CONTEST1.ID == contest.ID
                                  select p).Any())
             throw new ProblemNameExistedException();
         pro.Name = Name;
         pro.OriginRating = OriginRating;
         pro.Content = Content;
         pro.Comparer = Comparer;
         pro.DataChecker = DataChecker;
         pro.OWNER = owner;
         pro.DataCheckerLanguage = (int)DataCheckerLanguage;
         pro.ComparerLanguage = (int)ComparerLanguage;
         db.SaveChanges();
     }
 }
예제 #18
0
 /// <summary>
 /// 锁定题目
 /// </summary>
 /// <exception cref="AttendedNotNormalException"></exception>
 /// <exception cref="ContestTypeMismatchException"></exception>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="NotAttendedContestException"></exception>
 /// <exception cref="ProblemNotPassedException"></exception>
 public void Lock()
 {
     if (contest.GetAttendType() != Domain.Contest.AttendType.Normal)
         throw new AttendedNotNormalException();
     if (contest.Type != Domain.Contest.ContestType.CF)
         throw new ContestTypeMismatchException();
     if (contest.RelativeNow > contest.RelativeEndTime)
         throw new ContestEndedException();
     using (var db = new CHDB())
     {
         if (!(from r in db.RECORDs
               where r.USER1.ID == User.CurrentUser.ID
               && r.Status == (int)Record.StatusType.Accept
               && r.PROBLEM1.ID == ID
               select r).Any())
             throw new ProblemNotPassedException();
         (from u in db.USERs
          where u.ID==User.CurrentUser.ID
          select u).Single().LOCKs.Add((from p in db.PROBLEMs
                                        where p.ID==ID
                                        select p).Single());
         db.SaveChanges();
     }
 }
예제 #19
0
 /// <summary>
 /// 提交题目 record只需要填充 Code 和 Language
 /// </summary>
 /// <param name="record"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="ContestNotStartedException"></exception>
 /// <exception cref="NotAttendedContestException"></exception>
 /// <exception cref="ProblemLockedException"></exception>
 public Guid Submit(Record record)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     using (var db = new CHDB())
     {
         (from u in db.USERs
          where u.ID == User.CurrentUser.ID
          select u).Single().PreferLanguage = (int)record.Language;
         var currpro = (from p in db.PROBLEMs
                        where p.ID == ID
                        select p).Single();
         if (!contest.Owner.Contains(User.CurrentUser.name) && !User.CurrentUser.IsAdmin)
         {
             if (DateTime.Now < contest.RelativeStartTime)
                 throw new ContestNotStartedException();
             if (!contest.IsAttended())
                 throw new NotAttendedContestException();
         }
         if (contest.Type == Domain.Contest.ContestType.CF && IsLock() && contest.RelativeNow < contest.RelativeEndTime)
             throw new ProblemLockedException();
         Guid ret;
         db.RECORDs.Add(new RECORD()
         {
             Code = record.Code,
             CodeLength = record.Code.Length,
             Detail = null,
             ExecutedTime = null,
             ID = ret = Guid.NewGuid(),
             Language = (int)record.Language,
             MemoryUsed = null,
             PROBLEM1 = (from p in db.PROBLEMs
                         where p.ID == ID
                         select p).Single(),
             USER1 = (from u in db.USERs
                      where u.Name == User.CurrentUser.name
                      select u).Single(),
             Status = (int)Record.StatusType.Pending,
             SubmitTime = DateTime.Now,
             VirtualSubmitTime = GetVirtualSubmitTime(currpro)
         });
         db.SaveChanges();
         return ret;
     }
 }
예제 #20
0
 /// <summary>
 /// 删除指定测试数据
 /// </summary>
 /// <param name="testCase"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 public void RemoveTestCase(Guid testCase)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (!contest.Owner.Contains(User.CurrentUser.name) &&
         !User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         db.TESTDATAs.Remove((from t in db.TESTDATAs
                              where t.ID == testCase
                              select t).Single()
                              );
         db.SaveChanges();
     }
 }
 protected override int Run()
 {
     bool flg=false;
     Guid Record,Hunt;
     flg |= RecList.TryTake(out Record);
     flg |= HuntList.TryTake(out Hunt);
     if (!flg)
     {
         return 3000;
     }
     using (var db = new CHDB())
     {
         try
         {
             using (var tester = new NativeRunner(_ak.ip, _ak.port))
             {
                 DealRecord(tester, db, Record, _ak.desp);
                 db.SaveChanges();
                 DealHunt(tester, db, Hunt, _ak.desp);
                 db.SaveChanges();
             }
         }
         catch
         {
             if (Record != Guid.Empty)
                 (from h in db.RECORDs
                  where h.ID == Record
                  select h).Single().Status = (int)Domain.Record.StatusType.Pending;
             if (Hunt != Guid.Empty)
                 (from h in db.HUNTs
                  where h.ID == Hunt
                  select h).Single().Status = (int)Domain.Hunt.StatusType.Pending;
             db.SaveChanges();
             throw;
         }
     }
     return 0;
 }
예제 #22
0
 /// <summary>
 /// 获取私聊会话
 /// </summary>
 /// <param name="name">要私聊用户名称</param>
 /// <returns></returns>
 public static Session GetPrivateSession(string name)
 {
     using (var db = new CHDB())
     {
         var u1 = (from u in db.USERs
                   where u.Name == name
                   select u).Single();
         var u2 = (from u in db.USERs
                   where u.ID == User.CurrentUser.ID
                   select u).Single();
         var ret = (from s in u2.SESSIONs
                    where s.USERs.Select(x => x.Name).Contains(name)
                    && s.Type == (int)SessionType.PrivateChat
                    select new Session()
                              {
                                  ID = s.ID,
                                  Name = s.Name,
                                  Type = SessionType.PrivateChat
                              }).FirstOrDefault();
         if (null == ret)
         {
             var session = db.SESSIONs.Add(new SESSION()
             {
                 ID = Guid.NewGuid(),
                 Name = "",
                 Type = (int)SessionType.PrivateChat
             });
             session.USERs.Add(u1);
             session.USERs.Add(u2);
             db.SaveChanges();
             return new Session()
             {
                 ID = session.ID,
                 Type = SessionType.PrivateChat
             };
         }
         return ret;
     }
 }
예제 #23
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();
     }
 }
예제 #24
0
 /// <summary>
 /// 登陆帐户
 /// </summary>
 /// <param name="name"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 /// <exception cref="UserNotFoundException"></exception>
 /// <exception cref="PasswordMismatchException"></exception>
 public static string Login(string name, string password, string ip)
 {
     using (var sha = SHA256.Create())
     {
         byte[] pwdInBytes = sha.ComputeHash(Encoding.Unicode.GetBytes(password));
         using (var db = new CHDB())
         {
             var currentUser = (from u in db.USERs
                                where u.Name == name
                                select u).SingleOrDefault();
             if (null == currentUser)
                 throw new UserNotFoundException();
             if (!Enumerable.SequenceEqual<byte>(pwdInBytes, currentUser.Password))
                 throw new PasswordMismatchException();
             currentUser.LastLoginIP = ip;
             currentUser.LastLoginTime = DateTime.Now;
             var newToken = new OnlineUser()
                 {
                     ID = currentUser.ID,
                     Token = Guid.NewGuid(),
                     name = currentUser.Name,
                     email = currentUser.Email,
                     groups = (from g in currentUser.GROUPs
                               select g.Name).ToList(),
                     ip = ip,
                     IsAdmin = currentUser.GROUPs.Where(x => x.Name == "Administrators").Any()
                 };
             OnlineUsers.AddOrUpdate(currentUser.ID, newToken, (Guid id, OnlineUser oldv) => newToken);
             db.SaveChanges();
             return currentUser.ID.ToString() + "|" + newToken.Token.ToString();
         }
     }
     throw new UndefinedException();
 }
예제 #25
0
 /// <summary>
 /// 取消参加比赛
 /// </summary>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="NotAttendedContestException"></exception>
 /// <exception cref="ContestStartedException"></exception>
 public void Disattended()
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (!IsAttended())
         throw new NotAttendedContestException();
     using (var db = new CHDB())
     {
         var con = (from c in db.CONTESTs
                    where c.ID==ID
                    select c).Single();
         var con_att = (from u in con.CONTEST_ATTEND
                        where u.USER1.Name == User.CurrentUser.name
                        select u).Single();
         if (DateTime.Now > RelativeStartTime)
             throw new ContestStartedException();
         con.CONTEST_ATTEND.Remove(con_att);
         db.SaveChanges();
     }
 }
예제 #26
0
        /// <summary>
        /// 验证密码并添加用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="originalPassword"></param>
        /// <param name="encryptedPassword"></param>
        /// <param name="email"></param>
        /// <exception cref="PasswordMismatchException"></exception>
        /// <exception cref="EmailMismatchException"></exception>
        public static void Register(string name, string originalPassword, string encryptedPassword, string email, string emailHash,
            string country, string province, string city, string school, string motto, string realName)
        {
            email = email.Trim().ToLower();
            if (originalPassword != Encoding.UTF8.GetString(DESHelper.Decrypt(Convert.FromBase64String(encryptedPassword))))
                throw new PasswordMismatchException();
            if (email != Encoding.UTF8.GetString(DESHelper.Decrypt(Convert.FromBase64String(emailHash))))
                throw new EmailMismatchException();

            using (var db = new CHDB())
            {
                using (SHA256 hash = SHA256.Create())
                {
                    db.USERs.Add(new USER()
                    {
                        ID = Guid.NewGuid(),
                        Name = Helper.GetLegalName(name),
                        Password = hash.ComputeHash(Encoding.Unicode.GetBytes(originalPassword)),
                        Email = email,
                        Country = country,
                        Province = province,
                        City = city,
                        School = school,
                        Motto = motto,
                        RealName = realName,
                        AcceptEmail = true
                    });
                }
                db.SaveChanges();
            }
        }
예제 #27
0
 /// <summary>
 /// 删除本比赛,只有 Administrators 组有此权限
 /// </summary>
 /// <exception cref="PermissionDeniedException"></exception>
 public void Remove()
 {
     if (!User.CurrentUser.IsAdmin)
         throw new PermissionDeniedException();
     using (var db = new CHDB())
     {
         db.CONTESTs.Remove((from c in db.CONTESTs
                             where c.ID == ID
                             select c).Single());
         db.SaveChanges();
     }
 }
예제 #28
0
 /// <summary>
 /// 修改用户信息,必须验证当前密码
 /// </summary>
 /// <param name="oriPassword"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="PermissionDeniedException"></exception>
 /// <exception cref="PasswordMismatchException"></exception>
 public void Change(string oriPassword)
 {
     if (null == CurrentUser)
         throw new UserNotLoginException();
     if (CurrentUser.ID != ID)
         throw new PermissionDeniedException();
     using (SHA256 hash = SHA256.Create())
     {
         using (var db = new CHDB())
         {
             var usr = (from u in db.USERs
                        where u.ID == ID
                        select u).Single();
             if (!hash.ComputeHash(Encoding.Unicode.GetBytes(oriPassword)).SequenceEqual(usr.Password))
                 throw new PasswordMismatchException();
             if (null != Password)
                 usr.Password = hash.ComputeHash(Encoding.Unicode.GetBytes(Password));
             usr.Country = Country;
             usr.Province = Province;
             usr.City = City;
             usr.School = School;
             usr.Email = Email;
             usr.RealName = RealName;
             usr.Motto = Motto;
             usr.AcceptEmail = AcceptEmail;
             db.SaveChanges();
         }
     }
 }
예제 #29
0
 /// <summary>
 /// 虚拟报名比赛
 /// </summary>
 /// <param name="startTime"></param>
 /// <exception cref="UserNotLoginException"></exception>
 /// <exception cref="AlreadyAttendedContestException"></exception>
 /// <exception cref="ContestNotStartedException"></exception>
 /// <exception cref="VirtualStartTooEarlyException"></exception>
 public void VirtualAttend(DateTime startTime)
 {
     if (null == User.CurrentUser)
         throw new UserNotLoginException();
     if (IsAttended())
         throw new AlreadyAttendedContestException();
     if (DateTime.Now <= RelativeStartTime)
         throw new ContestNotStartedException();
     if (DateTime.Now > startTime)
         throw new VirtualStartTooEarlyException();
     using (var db = new CHDB())
     {
         var con = (from c in db.CONTESTs
                    where c.ID==ID
                    select c).Single();
         con.CONTEST_ATTEND.Add(new CONTEST_ATTEND()
         {
             USER1 = (from u in db.USERs
                      where u.ID == User.CurrentUser.ID
                      select u).Single(),
             Type = (int)AttendType.Virtual,
             Time = startTime
         });
         db.SaveChanges();
     }
 }
예제 #30
0
 /// <summary>
 /// 移除用户组
 /// </summary>
 /// <param name="name"></param>
 /// <exception cref="PermissionDeniedException"></exception>
 public void Remove()
 {
     CheckPriviledge();
     using (var db = new CHDB())
     {
         db.GROUPs.Remove(
             (from g in db.GROUPs
              where g.ID==ID
              select g).Single()
             );
         db.SaveChanges();
     }
 }