Class: Muzang::Plugins::NerdPursuit

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/muzang-plugins/muzang-nerdpursuit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ NerdPursuit

Returns a new instance of NerdPursuit.



10
11
12
13
14
15
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 10

def initialize(bot)
  @bot = bot
  @quiz_time = false
  @answers = {}
  create_database("nerd_pursuit.yml", Array.new, :questions)
end

Instance Attribute Details

#questionsObject

Returns the value of attribute questions.



8
9
10
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 8

def questions
  @questions
end

#quiz_timeObject

Returns the value of attribute quiz_time.



8
9
10
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 8

def quiz_time
  @quiz_time
end

Instance Method Details

#all_questionsObject



17
18
19
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 17

def all_questions
  names = Dir["#{File.dirname(__FILE__)}/muzang-nerdpursuit/questions/**/*.json"]
end

#call(connection, message) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 66

def call(connection, message)
  on_channel(message) do
    match(message, /^!quiz$/) do
      quiz!
      connection.msg(message.channel, "Quiz time!")
      EM.add_timer(period(1)) { connection.msg(message.channel, "Category: #{current_question["category"]}") }
      EM.add_timer(period(2)) { connection.msg(message.channel, "Question: #{current_question["text"]}") }
      4.times do |time|
        EM.add_timer(period(2+time+1)) { connection.msg(message.channel, "Answer #{time+1}: #{current_question["a#{time+1}"]}") }
      end
      EM.add_timer(period(40)) do
        connection.msg(message.channel, "Right answer: #{current_question["right_answer"][1..1]}")
        @winner = find_winner
        if @winner.first && @winner.first.first
          connection.msg(message.channel, "The winner is... #{@winner.first.first}")
        end
        @answers = {}
        @winner = nil
        end_quiz!
      end
    end

    match(message, /\d/) do |match|
      answer = match[0]
      if @quiz_time
        unless @answers[message.nick]
          @answers[message.nick] = { :answer => answer, :time => Time.now }
        end
      end
    end
  end
end

#current_questionObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 31

def current_question
  if @current_question
    @current_question
  else
    begin
      sample_question = (all_questions - questions).sample
      questions << sample_question
      save
      @current_question = ::JSON.parse(File.open(sample_question).read)["question"]
    end while(!valid?(@current_question))
  end

  @current_question
end

#end_quiz!Object



26
27
28
29
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 26

def end_quiz!
  @quiz_time = false
  @current_question = nil
end

#find_winnerObject



54
55
56
57
58
59
60
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 54

def find_winner
  @winner = @answers.reject do |_, answer|
    answer[:answer] != current_question["right_answer"][1..1]
  end.sort_by do |_,answer|
    answer[:time]
  end
end

#period(time) ⇒ Object



62
63
64
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 62

def period(time)
  time # need for speed up tests
end

#quiz!(&block) ⇒ Object



21
22
23
24
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 21

def quiz!(&block)
  @quiz_time = true
  current_question # load current question
end

#valid?(question) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 46

def valid?(question)
  if question
    question["category"] and question["text"] and question["right_answer"]
  else
    false
  end
end