Exemple #1
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;
        }
Exemple #2
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;
        }