Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AnswerQuestion
- include Interactor
- def call
- if not game
- context.fail!(message: 'Game is not started', code: 403)
- elsif question_in_round_session?
- context.fail!(message: 'Question answered', code: 403)
- else
- questions << question
- if update_game_attributes
- context.right = answer_right?
- context.comment = question.comment
- context.answers = question.answers
- else
- context.fail!(message: game.errors, code: 422)
- end
- end
- end
- private
- def question_in_round_session?
- questions.include?(question)
- end
- def answer_right?
- answers.sort == question.answers.sort
- end
- def update_game_attributes
- if answer_right?
- game.increment!(:right_answers)
- else
- game.increment!(:wrong_answers)
- end
- game.increment!(:questions_number)
- game.score += question.category.cost
- game.save
- end
- def game
- @game ||= context.user.games.where(ended_at: nil).first
- end
- def answers
- _answers = context.answers
- @answers ||= if _answers.is_a?(String) then JSON.parse _answers else _answers end
- end
- def questions
- @questions ||= game.round_sessions.last.questions
- end
- def question
- @question ||= Question.find(context.question_id)
- end
- end
Add Comment
Please, Sign In to add comment