Class: Robut::Plugin::Quiz
- Inherits:
-
Object
- Object
- Robut::Plugin::Quiz
- Includes:
- Robut::Plugin
- Defined in:
- lib/robut_quiz.rb,
lib/quiz.rb
Constant Summary collapse
- VERSION =
"0.2.1"
- QUESTION_REGEX =
/^ask ?(choice|polar|range)? (?:question )?(?:(?:for )?(\d+) minutes?)?(.+)$/
- ANSWER_REGEX =
/^answer .+$/
Instance Method Summary collapse
- #create_the_question_based_on_type(question_type, sender, request) ⇒ Object
- #currently_asking_a_question? ⇒ Boolean
- #enqueue_the_question(sender, question) ⇒ Object
- #handle(time, sender_nick, message) ⇒ Object
- #is_a_valid_question?(message) ⇒ Boolean
- #is_a_valid_response?(message) ⇒ Boolean
- #pop_the_question ⇒ Object
- #process_response_for_active_question(sender_nick, response) ⇒ Object
- #process_the_question(sender, request) ⇒ Object
-
#quizmaster ⇒ Object
Return a new thread which will pop questions in the queue of questions.
- #set_current_question(question, length_of_time) ⇒ Object
- #start_accepting_responses_for_this_question(question) ⇒ Object
- #stop_accepting_responses_for_this_question(question) ⇒ Object
- #there_are_no_more_questions_to_pop ⇒ Object
-
#usage ⇒ Array<String>
Contains the various types of responses that are valid usage examples that would be returned by the Help Plugin.
Instance Method Details
#create_the_question_based_on_type(question_type, sender, request) ⇒ Object
103 104 105 |
# File 'lib/quiz.rb', line 103 def create_the_question_based_on_type(question_type,sender,request) self.class.const_get(question_type.capitalize).new sender, request end |
#currently_asking_a_question? ⇒ Boolean
80 81 82 |
# File 'lib/quiz.rb', line 80 def currently_asking_a_question? defined? @@current_question and @@current_question end |
#enqueue_the_question(sender, question) ⇒ Object
55 56 57 |
# File 'lib/quiz.rb', line 55 def enqueue_the_question(sender,question) (store["quiz::question::queue"] ||= []) << [sender,question] end |
#handle(time, sender_nick, message) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/quiz.rb', line 24 def handle(time, sender_nick, ) # check to see if the user is asking the bot a question request = words().join(" ") if sent_to_me? and is_a_valid_question? request enqueue_the_question sender_nick, request reply "@#{sender_nick}, I have added your question to the list." quizmaster end if sent_to_me? and currently_asking_a_question? and is_a_valid_response? request process_response_for_active_question sender_nick, request end end |
#is_a_valid_question?(message) ⇒ Boolean
45 46 47 |
# File 'lib/quiz.rb', line 45 def is_a_valid_question? QUESTION_REGEX =~ end |
#is_a_valid_response?(message) ⇒ Boolean
51 52 53 |
# File 'lib/quiz.rb', line 51 def is_a_valid_response? ANSWER_REGEX =~ end |
#pop_the_question ⇒ Object
70 71 72 73 74 |
# File 'lib/quiz.rb', line 70 def pop_the_question if popped_question = (store["quiz::question::queue"] ||= []).pop process_the_question *popped_question end end |
#process_response_for_active_question(sender_nick, response) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/quiz.rb', line 138 def process_response_for_active_question(sender_nick, response) if @@current_question.handle_response sender_nick, response[/^answer (.+)$/,1] reply "Thank you, @#{sender_nick}, I have recorded your response." else reply "Sorry, @#{sender_nick}, I was unable to record that answer" end end |
#process_the_question(sender, request) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/quiz.rb', line 88 def process_the_question(sender,request) request =~ QUESTION_REGEX type = Regexp.last_match(1) || 'polar' question_length = Regexp.last_match(2) || '2' question_data = Regexp.last_match(3) set_current_question create_the_question_based_on_type(type,sender,question_data), question_length end |
#quizmaster ⇒ Object
Return a new thread which will pop questions in the queue of questions
62 63 64 65 66 67 68 |
# File 'lib/quiz.rb', line 62 def quizmaster if not defined?(@@quizmaster) or @@quizmaster.nil? or !@@quizmaster.alive? @@quizmaster = Thread.new { pop_the_question until there_are_no_more_questions_to_pop } end @@quizmaster end |
#set_current_question(question, length_of_time) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/quiz.rb', line 107 def set_current_question(question,length_of_time) start_accepting_responses_for_this_question question reply question.ask sleep length_of_time.to_i * 60 stop_accepting_responses_for_this_question question reply "The results are in for '#{question}':" reply question.results # allow some time between the results and asking the next question sleep 10 end |
#start_accepting_responses_for_this_question(question) ⇒ Object
125 126 127 |
# File 'lib/quiz.rb', line 125 def start_accepting_responses_for_this_question(question) @@current_question = question end |
#stop_accepting_responses_for_this_question(question) ⇒ Object
129 130 131 |
# File 'lib/quiz.rb', line 129 def stop_accepting_responses_for_this_question(question) @@current_question = nil end |
#there_are_no_more_questions_to_pop ⇒ Object
76 77 78 |
# File 'lib/quiz.rb', line 76 def there_are_no_more_questions_to_pop (store["quiz::question::queue"] ||= []).empty? end |
#usage ⇒ Array<String>
Returns contains the various types of responses that are valid usage examples that would be returned by the Help Plugin.
11 12 13 14 15 16 17 |
# File 'lib/quiz.rb', line 11 def usage [ "#{at_nick} ask 'Should we break for lunch?'", "#{at_nick} ask for 3 minutes 'Should I continue the presentation?'", "#{at_nick} answer yes" ] end |