Class: Robut::Plugin::Quiz

Inherits:
Object
  • Object
show all
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

Instance Method Details

#create_the_question_based_on_type(question_type, sender, request) ⇒ Object

Parameters:

  • question_type (String)

    the name of the question type which will be converted from a String to the Class name within the current namespace.

  • sender (String)

    the sender of the question

  • request (String)

    the entire message that initiated the question



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

Returns:

  • (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

Parameters:

  • time (Time)

    at which the message has arrived

  • sender_nick (String)

    the sender

  • message (String)

    the message that was sent



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, message)
  
  # check to see if the user is asking the bot a question
  request = words(message).join(" ")
  
  if sent_to_me? message 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? message 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

Returns:

  • (Boolean)


45
46
47
# File 'lib/quiz.rb', line 45

def is_a_valid_question? message
  QUESTION_REGEX =~ message
end

#is_a_valid_response?(message) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/quiz.rb', line 51

def is_a_valid_response? message
  ANSWER_REGEX =~ message
end

#pop_the_questionObject



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

Parameters:

  • sender_nick (String)

    the name of the person that is responding to the question.

  • response (String)

    is the answer to the question proposed.



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

Parameters:

  • sender (String)

    the user proposing the question

  • request (String)

    the data related to the asking and the question data itself.



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

#quizmasterObject

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_popObject



76
77
78
# File 'lib/quiz.rb', line 76

def there_are_no_more_questions_to_pop
  (store["quiz::question::queue"] ||= []).empty?
end

#usageArray<String>

Returns contains the various types of responses that are valid usage examples that would be returned by the Help Plugin.

Returns:

  • (Array<String>)

    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