X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fgames%2Fquiz.rb;h=b139c66a2fad25eb2ec8c9c0e31900ce2f0307a3;hb=052217de30c59206d7025b582d4604557a747470;hp=a9ceddaf2af3529a16a4b3a391c119312fdd9565;hpb=e00ea339ca3baaa9cfe74f6cec85c36e83307b61;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/games/quiz.rb b/data/rbot/plugins/games/quiz.rb index a9ceddaf..b139c66a 100644 --- a/data/rbot/plugins/games/quiz.rb +++ b/data/rbot/plugins/games/quiz.rb @@ -33,9 +33,6 @@ define_structure :QuizBundle, :question, :answer define_structure :PlayerStats, :score, :jokers, :jokers_time # Why do we still need jokers_time? //Firetech -# Maximum number of jokers a player can gain -Max_Jokers = 3 - # Control codes Color = "\003" Bold = "\002" @@ -164,6 +161,11 @@ class QuizPlugin < Plugin :default => ['quiz.rbot'], :desc => "List of files and URLs that will be used to retrieve quiz questions") + + Config.register Config::IntegerValue.new('quiz.max_jokers', + :default => 3, + :desc => "Maximum number of jokers a player can gain") + def initialize() super @@ -173,6 +175,22 @@ class QuizPlugin < Plugin @ask_mutex = Mutex.new end + def cleanup + @ask_mutex.synchronize do + # purge all waiting timers + @waiting.each do |chan, t| + @bot.timer.remove t.first + @bot.say chan, _("stopped quiz timer") + end + @waiting.clear + end + chans = @quizzes.keys + @quizzes.clear + chans.each do |chan| + @bot.say chan, _("quiz stopped") + end + end + # Function that returns whether a char is a "separator", used for hints # def is_sep( ch ) @@ -184,10 +202,11 @@ class QuizPlugin < Plugin # (in quiz/) or web pages. # def fetch_data( m ) - # Read the winning messages file + # 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 @@ -212,13 +231,12 @@ 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 + data << "\n\n" << File.read(path) rescue m.reply "Failed to read from local database file #{p}, skipping." end @@ -256,13 +274,17 @@ class QuizPlugin < Plugin # Returns new Quiz instance for channel, or existing one + # Announce errors if a message is passed as second parameter # - def create_quiz( channel ) + def create_quiz(channel, m=nil) unless @quizzes.has_key?( channel ) @quizzes[channel] = Quiz.new( channel, @registry ) end if @quizzes[channel].has_errors + m.reply _("Sorry, the quiz database for %{chan} seems to be corrupt") % { + :chan => channel + } if m return nil else return @quizzes[channel] @@ -272,19 +294,18 @@ class QuizPlugin < Plugin def say_score( m, nick ) chan = m.channel - q = create_quiz( chan ) - if q.nil? - m.reply "Sorry, the quiz database for #{chan} seems to be corrupt" - return - end + q = create_quiz( chan, m ) + return unless q if q.registry.has_key?( nick ) score = q.registry[nick].score jokers = q.registry[nick].jokers rank = 0 - q.rank_table.each_index { |rank| break if nick.downcase == q.rank_table[rank][0].downcase } - rank += 1 + q.rank_table.each do |place| + rank += 1 + break if nick.downcase == place[0].downcase + end m.reply "#{nick}'s score is: #{score} Rank: #{rank} Jokers: #{jokers}" else @@ -295,7 +316,17 @@ class QuizPlugin < Plugin def help( plugin, topic="" ) if topic == "admin" - "Quiz game aministration commands (requires authentication): 'quiz autoask ' => enable/disable autoask mode. 'quiz autoask delay ' => delay next quiz by seconds when in autoask mode. 'quiz transfer [score] [jokers]' => transfer [score] points and [jokers] jokers from to (default is entire score and all jokers). 'quiz setscore ' => set 's score to . 'quiz setjokers ' => set 's number of jokers to . 'quiz deleteplayer ' => delete one player from the rank table (only works when score and jokers are set to 0). 'quiz cleanup' => remove players with no points and no jokers." + _("Quiz game aministration commands (requires authentication): ") + [ + _("'quiz autoask ' => enable/disable autoask mode"), + _("'quiz autoask delay