Class: NerdPursuit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Muzang::Plugins::Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ NerdPursuit

Returns a new instance of NerdPursuit.



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

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.



6
7
8
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 6

def questions
  @questions
end

#quiz_timeObject

Returns the value of attribute quiz_time.



6
7
8
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 6

def quiz_time
  @quiz_time
end

Instance Method Details

#all_questionsObject



15
16
17
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 15

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

#call(connection, message) ⇒ Object



64
65
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
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 64

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



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

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



24
25
26
27
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 24

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

#find_winnerObject



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

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



60
61
62
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 60

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

#quiz!(&block) ⇒ Object



19
20
21
22
# File 'lib/muzang-plugins/muzang-nerdpursuit.rb', line 19

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

#valid?(question) ⇒ Boolean

Returns:

  • (Boolean)


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

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