예제 #1
0
        public static void AddNewCompetition(Models.CompetitionModel model)
        {
            Competition comp = new Competition();
            comp.CategoryId = model.CategoryId;
            comp.Start = model.Start;
            comp.End = model.End;
            comp.Duration = model.Duration;
            comp.Name = model.Name;
            comp.Description = model.Description;

            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                entityModel.AddToCompetitions(comp);
                entityModel.SaveChanges();
                if (model.SelectedProblems != null)
                {
                    foreach (var problem in model.SelectedProblems)
                    {
                        CompetetionsProblem cp = new CompetetionsProblem();
                        cp.CompetetionId = comp.ID;
                        cp.ProblemId = problem;
                        entityModel.CompetetionsProblems.AddObject(cp);
                    }
                    entityModel.SaveChanges();
                }
            }
        }
예제 #2
0
 public static void DeleteProblem(int id)
 {
     using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
     {
         var problems = entityModel.Problems.Where(x => x.ID == id).ToList();
         foreach (var problem in problems)
         {
             entityModel.Problems.DeleteObject(problem);
         }
         entityModel.SaveChanges();
     }
 }
예제 #3
0
        public static void AddNewProblem(ProblemModel model, int? id, ProblemType type)
        {
            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                Problem problem = new Problem();
                problem.Title = model.Title;
                problem.ProblemType = (int)type;

                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, model);
                problem.Data = ms.ToArray();

                entityModel.AddToProblems(problem);
                entityModel.SaveChanges();
            }
        }
예제 #4
0
        public static void EnrollUserForCompetition(string username, int competitionId)
        {
            using (TopCoderPrototypeEntities model = new TopCoderPrototypeEntities())
            {
                var user = GetUser(model, username);
                if (user != null)
                {
                    CompetitionsUser competitionUser = model.CompetitionsUsers
                        .Where(x => x.UserId == user.ID && x.CompetitionId == competitionId)
                        .FirstOrDefault();

                    // evade duplicates - mad evasion
                    if (competitionUser == null)
                    {
                        user.CompetitionsUsers.Add(new CompetitionsUser() { UserId = user.ID, CompetitionId = competitionId });
                        model.SaveChanges();
                    }
                }
            }
        }
예제 #5
0
        internal static bool SubmitSimpleQuestion(string username, int problemId, int answer)
        {
            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                CompetitionsUser cuser = entityModel.CompetitionsUsers.
                    Where(x => x.User.Username == username && x.Competition.CompetetionsProblems.Any(y => y.ProblemId == problemId)).FirstOrDefault();
                if(cuser == null || !cuser.Start.HasValue || cuser.Start.Value.AddMinutes(cuser.Competition.Duration) < DateTime.Now)
                {
                    return false;
                }

                SimpleProblemAnswer ans = new SimpleProblemAnswer();
                ans.Answer = answer;

                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, ans);

                Submission submission = new Submission();
                submission.UserId = cuser.UserId;
                submission.ProblemId = problemId;
                submission.Submitted = DateTime.Now;
                submission.Answer = ms.ToArray();

                entityModel.Submissions.Where(x => x.UserId == cuser.UserId && x.ProblemId == problemId).ToList()
                    .ForEach(entityModel.Submissions.DeleteObject);
                entityModel.AddToSubmissions(submission);
                entityModel.SaveChanges();
            }

            return true;
        }
예제 #6
0
        internal static bool SubmitComplexQuestion(string username, int problemId, string answer)
        {
            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                CompetitionsUser cuser = entityModel.CompetitionsUsers.
                    Where(x => x.User.Username == username && x.Competition.CompetetionsProblems.Any(y => y.ProblemId == problemId)).FirstOrDefault();
                if (cuser == null || !cuser.Start.HasValue || cuser.Start.Value.AddMinutes(cuser.Competition.Duration) < DateTime.Now)
                {
                    return false;
                }

                string[] tokens = answer.Split(',');
                List<int> ints = new List<int>();

                foreach (string token in tokens)
                {
                    int res = -1;
                    if (int.TryParse(token, out res))
                    {
                        ints.Add(res);
                    }
                }

                ComplexProblemAnswer ans = new ComplexProblemAnswer();
                ans.Answers = ints.ToArray();

                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, ans);

                Submission submission = new Submission();
                submission.UserId = cuser.UserId;
                submission.ProblemId = problemId;
                submission.Submitted = DateTime.Now;
                submission.Answer = ms.ToArray();

                entityModel.Submissions.Where(x => x.UserId == cuser.UserId && x.ProblemId == problemId).ToList()
                    .ForEach(entityModel.Submissions.DeleteObject);
                entityModel.AddToSubmissions(submission);
                entityModel.SaveChanges();
            }

            return true;
        }
예제 #7
0
 internal static void StartCompetitionForUser(string username, int competitionId)
 {
     using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
     {
         CompetitionsUser cuser = entityModel.CompetitionsUsers.
             Where(x => x.User.Username == username && x.CompetitionId == competitionId).FirstOrDefault();
         if (cuser != null && cuser.Start == null)
         {
             cuser.Start = DateTime.Now;
             entityModel.SaveChanges();
         }
     }
 }
예제 #8
0
        internal static DataTable GetCompetitionResults(int id)
        {
            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                Competition comp = entityModel.Competitions.Where(x => x.ID == id).First();

                foreach (CompetetionsProblem problem in comp.CompetetionsProblems)
                {
                    ProblemModel problemModel = GetTask(problem.ProblemId);
                    Submission submit = entityModel.Submissions
                        .Where(x => x.ProblemId == problem.ProblemId && !x.Score.HasValue)
                        .FirstOrDefault();
                    if (submit != null)
                    {
                        IProblemAnswer answer = DataHelper.GetAnswer(submit);
                        submit.Score = problemModel.Evaluate(answer);
                    }
                }

                entityModel.SaveChanges();
            }

            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                Competition comp = entityModel.Competitions.Where(x => x.ID == id).First();
                DataTable table = new DataTable();
                table.Columns.Add("Username");
                foreach (CompetetionsProblem problem in comp.CompetetionsProblems)
                {
                    table.Columns.Add(problem.Problem.Title);
                }

                table.Columns.Add("Total");

                foreach (CompetitionsUser user in comp.CompetitionsUsers)
                {
                    DataRow row = table.NewRow();
                    row["Username"] = user.User.Username;
                    int totalScore = 0;
                    foreach (CompetetionsProblem problem in comp.CompetetionsProblems)
                    {
                        Submission subm = entityModel.Submissions
                            .Where(x => x.UserId == user.UserId && x.ProblemId == problem.ProblemId)
                            .OrderByDescending(x => x.ID).FirstOrDefault();
                        int score = subm != null && subm.Score.HasValue ? subm.Score.Value : 0;
                        totalScore += score;
                        row[problem.Problem.Title] = score;
                    }

                    row["Total"] = totalScore;
                    table.Rows.Add(row);
                }

                return table;
            }
        }
예제 #9
0
 internal static void EditCompetition(int id, CompetitionModel model)
 {
     using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
     {
         Competition comp = entityModel.Competitions.Where(x => x.ID == id).FirstOrDefault();
         if (comp != null)
         {
             comp.CategoryId = model.CategoryId;
             comp.Start = model.Start;
             comp.End = model.End;
             comp.Duration = model.Duration;
             comp.Name = model.Name;
             comp.Description = model.Description;
         }
         entityModel.SaveChanges();
         entityModel.CompetetionsProblems.Where(x => x.CompetetionId == id).ToList()
             .ForEach(entityModel.CompetetionsProblems.DeleteObject);
         if (model.SelectedProblems != null)
         {
             foreach (var problem in model.SelectedProblems)
             {
                 CompetetionsProblem cp = new CompetetionsProblem();
                 cp.CompetetionId = comp.ID;
                 cp.ProblemId = problem;
                 entityModel.CompetetionsProblems.AddObject(cp);
             }
         }
         entityModel.SaveChanges();
     }
 }
예제 #10
0
 internal static void DeleteCompetition(int id)
 {
     using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
     {
         entityModel.CompetitionsUsers.Where(x => x.CompetitionId == id).ToList()
             .ForEach(entityModel.CompetitionsUsers.DeleteObject);
         entityModel.Competitions.Where(x => x.ID == id).ToList().ForEach(entityModel.Competitions.DeleteObject);
         entityModel.SaveChanges();
     }
 }
예제 #11
0
 public static void RegisterUser(string username, string password, string email)
 {
     using (TopCoderPrototypeEntities model = new TopCoderPrototypeEntities())
     {
         User user = new User();
         user.Username = username;
         user.Password = password;
         user.Email = email;
         user.Type = 1;
         model.Users.AddObject(user);
         model.SaveChanges();
     }
 }
예제 #12
0
        public static bool GradeSubmission(int submissionID, int score)
        {
            using (TopCoderPrototypeEntities entityModel = new TopCoderPrototypeEntities())
            {
                var submit = entityModel.Submissions.Where(x => x.ID == submissionID).FirstOrDefault();
                if (submit == null)
                {
                    return false;
                }
                var answer = DataHelper.GetAnswer(submit) as HumanGradableAnswer;
                answer.Score = score;

                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, answer);
                submit.Answer = ms.ToArray();
                submit.Score = null;

                entityModel.SaveChanges();
                return true;
            }
        }