]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/quiz.rb
+ @bot.path and datafile methods
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / quiz.rb
index 6338326223964176eb8479c3a7963fec15a7b358..9159a4c4fef333b5fca3dd252288d8ec313b4e75 100644 (file)
 # TODO:: when Ruby 2.0 gets out, fix the FIXME 2.0 UTF-8 workarounds
 
 # Class for storing question/answer pairs
-QuizBundle = Struct.new( "QuizBundle", :question, :answer )
+define_structure :QuizBundle, :question, :answer
 
 # Class for storing player stats
-PlayerStats = Struct.new( "PlayerStats", :score, :jokers, :jokers_time )
+define_structure :PlayerStats, :score, :jokers, :jokers_time
 # Why do we still need jokers_time? //Firetech
 
 # Maximum number of jokers a player can gain
@@ -156,11 +156,11 @@ end
 # CLASS QuizPlugin
 #######################################################################
 class QuizPlugin < Plugin
-  BotConfig.register BotConfigBooleanValue.new('quiz.dotted_nicks',
+  Config.register Config::BooleanValue.new('quiz.dotted_nicks',
     :default => true,
     :desc => "When true, nicks in the top X scores will be camouflaged to prevent IRC hilighting")
 
-  BotConfig.register BotConfigArrayValue.new('quiz.sources',
+  Config.register Config::ArrayValue.new('quiz.sources',
     :default => ['quiz.rbot'],
     :desc => "List of files and URLs that will be used to retrieve quiz questions")
 
@@ -186,8 +186,9 @@ class QuizPlugin < Plugin
   def fetch_data( m )
     # Read the winning messages file 
     @win_messages = Array.new
-    if File.exists? "#{@bot.botclass}/quiz/win_messages"
-      IO.foreach("#{@bot.botclass}/quiz/win_messages") { |line| @win_messages << line.chomp }
+    winfile = datafile 'win_messages'
+    if File.exists? winfile
+      IO.foreach(winfile) { |line| @win_messages << line.chomp }
     else
       warning( "win_messages file not found!" )
       # Fill the array with a least one message or code accessing it would fail
@@ -203,7 +204,7 @@ class QuizPlugin < Plugin
       if p =~ /^https?:\/\//
         # Wiki data
         begin
-          serverdata = @bot.httputil.get_cached( URI.parse( p ) ) # "http://amarok.kde.org/amarokwiki/index.php/Rbot_Quiz"
+          serverdata = @bot.httputil.get(p) # "http://amarok.kde.org/amarokwiki/index.php/Rbot_Quiz"
           serverdata = serverdata.split( "QUIZ DATA START\n" )[1]
           serverdata = serverdata.split( "\nQUIZ DATA END" )[0]
           serverdata = serverdata.gsub( /&nbsp;/, " " ).gsub( /&amp;/, "&" ).gsub( /&quot;/, "\"" )
@@ -212,13 +213,13 @@ class QuizPlugin < Plugin
           m.reply "Failed to download questions from #{p}, ignoring sources"
         end
       else
-        path = "#{@bot.botclass}/quiz/#{p}"
+        path = datafile p
         debug "Fetching from #{path}"
 
         # Local data
         begin
-          datafile = File.new( path, File::RDONLY )
-          data << "\n\n" << datafile.read
+          file = File.new( path, File::RDONLY )
+          data << "\n\n" << file.read
         rescue
           m.reply "Failed to read from local database file #{p}, skipping."
         end
@@ -360,9 +361,7 @@ class QuizPlugin < Plugin
 
   # Reimplemented from Plugin
   #
-  def listen( m )
-    return unless m.kind_of?(PrivMessage)
-
+  def message(m)
     chan = m.channel
     return unless @quizzes.has_key?( chan )
     q = @quizzes[chan]
@@ -651,9 +650,9 @@ class QuizPlugin < Plugin
 
 
   def cmd_refresh( m, params )
-    q = create_quiz ( m.channel )
+    q = create_quiz(m.channel)
     q.questions.clear
-    fetch_data ( m )
+    fetch_data(m)
     cmd_quiz( m, params )
   end
 
@@ -706,7 +705,7 @@ class QuizPlugin < Plugin
       score = player[1].score
       ar << "#{i + 1}. #{unhilight_nick( nick )} (#{score})"
     end
-    m.reply ar.join(" | ")
+    m.reply ar.join(" | "), :split_at => /\s+\|\s+/
   end
 
 
@@ -932,9 +931,23 @@ class QuizPlugin < Plugin
 
   end
 
-end
-
+  def stop(m, params)
+    unless m.public?
+      m.reply 'you must be on some channel to use this command'
+      return
+    end
+    if @quizzes.delete m.channel
+      @ask_mutex.synchronize do
+        t = @waiting.delete(m.channel)
+        @bot.timer.remove t if t
+      end
+      m.okay
+    else
+      m.reply(_("there is no active quiz on #{m.channel}"))
+    end
+  end
 
+end
 
 plugin = QuizPlugin.new
 plugin.default_auth( 'edit', false )
@@ -952,6 +965,7 @@ plugin.map 'quiz refresh',          :action => 'cmd_refresh'
 plugin.map 'quiz top5',             :action => 'cmd_top5'
 plugin.map 'quiz top :number',      :action => 'cmd_top_number'
 plugin.map 'quiz stats',            :action => 'cmd_stats'
+plugin.map 'quiz stop', :action => :stop
 
 # Admin commands
 plugin.map 'quiz autoask :enable',  :action => 'cmd_autoask', :auth_path => 'edit'