コード例 #1
0
ファイル: DistractorService.cs プロジェクト: LeszekR/EduBot
        // MOCK
        // =============================================================================================
        public DistractorDTO MockDistractor(int userId, DistractorType distrType)
        {
            // backing time of last use for all distractors of the user
            var userDistractors = _userService.GetUserEntity(userId).user_distractor.ToList();

            foreach (var ud in userDistractors)
            {
                ud.time_last_used = ud.time_last_used.AddMinutes(-10);
            }

            // picking next distractor of required type
            var distractor = NextDistractor(userId, distrType);

            return(DistractorMapper.GetDTO(distractor));
        }
コード例 #2
0
ファイル: DistractorService.cs プロジェクト: LeszekR/EduBot
        // ---------------------------------------------------------------------------------------------
        public distractor NextDistractor(int userId, DistractorType type)
        {
            user user = _userService.GetUserEntity(userId);
            List <user_distractor> userDistractsAll = user.user_distractor.ToList();


            // nie wysyłamy dystraktora
            if (type == DistractorType.NO_DISTRACTOR)
            {
                return(null);
            }


            // ostatni dystraktor był wysłany zbyt niedawno, trzeba jeszcze poczekać z następnym
            if (!TimeForDistractor(ref userDistractsAll))
            {
                _logger.Debug("User: "******"|" + "Not providing a distractor as user had one quiet recently.");
                return(null);
            }

            // już czas na nastepny dystraktor
            string typeStr = "";

            if (type == DistractorType.KICK)
            {
                typeStr = KICK;
            }
            else if (type == DistractorType.REWARD)
            {
                typeStr = REWARD;
            }


            // pobranie wszystkich dystraktorów wymaganego typu
            var distractsAll       = _distractorRepository.All().ToList();
            var distractsOfTypeAll = distractsAll.Where(d => d.type == typeStr).ToList();


            // wybranie tylko tych, których użytkownik jeszcze nie widział
            var userDistractsOfType = userDistractsAll
                                      .Select(ud => ud.distractor)
                                      .Where(d => d.type == typeStr)
                                      .ToList();
            var unseenDistracts = distractsOfTypeAll.Except(userDistractsOfType).ToList();
            int nDistracts      = unseenDistracts.Count();


            // jeżeli widział już wszystkie - pobranie najstarszej połowy
            if (nDistracts == 0)
            {
                nDistracts = userDistractsOfType.Count();
                nDistracts = nDistracts == 1 ? 1 : nDistracts / 2;

                unseenDistracts = userDistractsAll
                                  .OrderBy(a => a.time_last_used)
                                  .Select(ud => ud.distractor)
                                  .Where(d => d.type == typeStr)
                                  .Take(nDistracts)
                                  .ToList();
            }

            // w bazie nie ma jeszcze dystraktorów
            if (nDistracts == 0)
            {
                return(null);
            }

            // losowanie jednego dystraktora
            var index         = (Int32)(new Random().Next(0, nDistracts));
            var newDistractor = unseenDistracts[index];


            // Dopisanie kolejnego dystraktora do listy wysłanych użytkownikowi
            // lub zaktualizowanie timestamp z chwili jego wysłania, jesli jest wysyłany po raz kolejny.
            UpsertUserDistractor(user, newDistractor);


            _logger.Info("User: "******"|" + "Distractor of type \"" + newDistractor.type + "\" was drawn with content: " + newDistractor.distr_content);
            return(newDistractor);
        }