Beispiel #1
0
        public void OnMouseUpOnObject(AObject lAnswer, AObject lField)
        {
            if (lAnswer is AnswerToken)
            {
                AnswerToken answerToken = lAnswer as AnswerToken;

                // test field token
                if (lField != null)
                {
                    FieldToken fieldToken = lField as FieldToken;

                    fieldToken.AssociatedToken = answerToken;

                    fieldToken.ChangeDisplayText(answerToken.Text);

                    this.NotifyGameStateChanged(this.CurrentLevel.LevelName, new GameEvent(EventType.INSERT_WORD, string.Empty));
                }
                else
                {
                    this.NotifyGameStateChanged(this.CurrentLevel.LevelName, new GameEvent(EventType.DROP_WORD, string.Empty));
                }

                answerToken.IsFocused = false;
                answerToken.SetKinematicParameters(answerToken.InitialPosition, new Vector2f(0, 0));
            }
            else if (lField is TimerObject)
            {
                this.NotifyInternalGameEvent(lAnswer, null, "timerPassed");
            }
        }
Beispiel #2
0
        public bool PostRepresentativeQuestion(AskRepresentativeQuestionModel model, string userID)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var representative = context.Representatives
                                     .Where(x => x.RepresentativeID == model.RepresentativeID)
                                     .Include(x => x.Party)
                                     .FirstOrDefault();

                if (representative == null)
                {
                    return(false);
                }

                var user = context.Users.Where(x => x.Id == userID).FirstOrDefault();
                if (user == null)
                {
                    return(false);
                }

                var customQuestion = new Question
                {
                    CreateTimeUtc = DateTime.UtcNow,
                    Text          = model.Text,
                    IsSuggested   = false,
                    Verified      = false
                };
                context.Questions.Add(customQuestion);

                var answerToken = new AnswerToken
                {
                    Representative = representative,
                    Question       = customQuestion,
                    Token          = Guid.NewGuid(),
                    CreateTimeUtc  = DateTime.UtcNow
                };
                context.AnswerTokens.Add(answerToken);

                var question = new UserRepresentativeQuestion
                {
                    ApplicationUserID = userID,
                    CreateTimeUtc     = DateTime.UtcNow,
                    Question          = customQuestion,
                    AnswerToken       = answerToken,
                    Processed         = false,
                    Representative    = representative
                };

                context.UserRepresentativeQuestions.Add(question);
                context.SaveChanges();
                return(true);
            }
        }
Beispiel #3
0
        public void PostQuestion(AskLawQuestionModel model, string userID)
        {
            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var user = context.Users.Where(x => x.Id == userID).FirstOrDefault();
                if (user == null)
                {
                    return;
                }

                var law = context.Laws.Where(x => x.LawID == model.LawID).FirstOrDefault();
                if (law == null)
                {
                    return;
                }

                List <int> selectedPredefinedQuestionIDs = new List <int>();
                if (model.Questions != null)
                {
                    selectedPredefinedQuestionIDs = model.Questions.Where(x => x.IsSelected).Select(x => x.ID).ToList();
                }

                var predefinedQuestions = context.Questions.Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID));

                var selectedRepresentativeIDs = model.SuggestedRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList();
                selectedRepresentativeIDs.AddRange(model.OtherRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList());

                if (selectedRepresentativeIDs.Count == 0)
                {
                    return;
                }

                // If user asked any of given questions
                var existingUserQuestions = context.UserRepresentativeQuestions
                                            .Where(x => x.ApplicationUserID == user.Id &&
                                                   selectedPredefinedQuestionIDs.Contains(x.QuestionID) &&
                                                   selectedRepresentativeIDs.Contains(x.RepresentativeID))
                                            .ToList();

                // should check if any of users asked same question to skip creating token
                var existingQuestions = context.UserRepresentativeQuestions
                                        .Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID) &&
                                               selectedRepresentativeIDs.Contains(x.RepresentativeID))
                                        .ToList();

                // Prepare custom question if needed
                DomainModels.Question customQuestion = null;

                if (!string.IsNullOrWhiteSpace(model.Text))
                {
                    customQuestion = new DomainModels.Question
                    {
                        LawID         = law.LawID,
                        Text          = model.Text,
                        IsSuggested   = false,
                        CreateTimeUtc = DateTime.UtcNow
                    };
                    context.Questions.Add(customQuestion);
                }

                var         newQuestions = new List <DomainModels.UserRepresentativeQuestion>();
                AnswerToken answerToken  = null;
                foreach (var rep in selectedRepresentativeIDs.Distinct())
                {
                    foreach (var question in selectedPredefinedQuestionIDs)
                    {
                        answerToken = null;
                        if (existingUserQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any())
                        {
                            continue;
                        }

                        if (!existingQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any())
                        {
                            answerToken = new AnswerToken
                            {
                                RepresentativeID = rep,
                                QuestionID       = question,
                                Token            = Guid.NewGuid(),
                                CreateTimeUtc    = DateTime.UtcNow
                            };
                            context.AnswerTokens.Add(answerToken);
                        }

                        var newPredefinedQuestion = new DomainModels.UserRepresentativeQuestion
                        {
                            ApplicationUserID = user.Id,
                            QuestionID        = question,
                            RepresentativeID  = rep,
                            CreateTimeUtc     = DateTime.UtcNow,
                            AnswerToken       = answerToken
                        };
                        context.UserRepresentativeQuestions.Add(newPredefinedQuestion);
                        // Notifications are being processed only for predefined questions at this point.
                        if (answerToken != null)
                        {
                            newQuestions.Add(newPredefinedQuestion);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(model.Text))
                    {
                        answerToken = new AnswerToken
                        {
                            RepresentativeID = rep,
                            Question         = customQuestion,
                            Token            = Guid.NewGuid(),
                            CreateTimeUtc    = DateTime.UtcNow
                        };
                        context.AnswerTokens.Add(answerToken);

                        context.UserRepresentativeQuestions.Add(
                            new DomainModels.UserRepresentativeQuestion
                        {
                            ApplicationUserID = user.Id,
                            RepresentativeID  = rep,
                            CreateTimeUtc     = DateTime.UtcNow,
                            Question          = customQuestion,
                            AnswerToken       = answerToken
                        });
                    }
                }

                context.SaveChanges();
                new NotificationService().ProcessNewQuestions(newQuestions);
            }
        }