コード例 #1
0
        private void setHintAndBlank(ReviewViewModel model)
        {
            string[] words = Regex.Split(model.ViewCard.Question, @"\s+");
            string longestWord = words.OrderByDescending(w => w.Length).ToArray()[0];
            var sbHint = new StringBuilder();
            var sbBlank = new StringBuilder();
            var rand = new Random();
            char? blankAns = null;

            foreach (var w in words) {
                var hintAry = new char[w.Length];
                var blankAry = Regex.Replace(w, "[a-zA-Z]", "*").ToCharArray();
                Array.Copy(sourceArray: blankAry, destinationArray: hintAry, length: blankAry.Length);
                hintAry[0] = w[0];
                hintAry[w.Length - 1] = w[w.Length - 1];
                sbHint.Append(new string(hintAry)).Append(" ");
                if (blankAns == null && w == longestWord) {
                    var min = w.Length < 3 ? 0 : 1;
                    var max = w.Length < 3 ? w.Length - 1 : w.Length - 2;
                    var r = rand.Next(min, max); // [min, max)
                    blankAns = w[r];
                    blankAry[r] = '_';
                }
                sbBlank.Append(new string(blankAry)).Append(" ");
            }

            model.Hint = sbHint.ToString().Trim();
            model.Blank = sbBlank.ToString().Trim();
            model.BlankAnswer = blankAns;
        }
コード例 #2
0
        //
        // GET: /Review/
        public ActionResult Index(int page = 0, string reviewMode = "Fast")
        {
            #if DEBUG
            DateTime dt0 = DateTime.Now.AddMilliseconds(-6),
                           dt1 = DateTime.Now.AddMilliseconds(-12),
                           dt2 = DateTime.Now.AddSeconds(-3),
                           dt3 = DateTime.Now.AddSeconds(-7),
                           dt4 = DateTime.Now.AddSeconds(-14);
            #else
            DateTime dt0 = DateTime.Now.AddHours(-6),
                           dt1 = DateTime.Now.AddHours(-12),
                           dt2 = DateTime.Now.AddDays(-3),
                           dt3 = DateTime.Now.AddDays(-7),
                           dt4 = DateTime.Now.AddDays(-14);
            #endif

            int userId = (int)(Session["UserId"] ?? this.GetUserId());

            ViewBag.ReviewModeList = createReviewModeList();

            var cards = repository.Cards.Where(c => c.UserId == userId);

            cards = cards.Where(c =>
                (c.ReviewLevel == 0 && c.ReviewedAt < dt0) ||
                (c.ReviewLevel == 1 && c.ReviewedAt < dt1) ||
                (c.ReviewLevel == 2 && c.ReviewedAt < dt2) ||
                (c.ReviewLevel == 3 && c.ReviewedAt < dt3) ||
                (c.ReviewLevel == 4 && c.ReviewedAt < dt4)
                );

            cards = cards.OrderBy(c => c.ReviewLevel).ThenBy(c => c.ReviewedAt);

            var viewModel = new ReviewViewModel(itemsPerPage: 1, pageSkip: 5) {
                Cards = cards,
                Page = page,
                QuestionedAt = DateTime.Now,
                ReviewMode = reviewMode
            };
            if (page > viewModel.LastPage) {
                viewModel.Page = viewModel.LastPage;
            }
            viewModel.ViewCard = viewModel.GetCardsInPage().SingleOrDefault();

            if (reviewMode == "Typing" || reviewMode == "Blank") {
                setHintAndBlank(viewModel);
            }

            return View(viewModel);
        }