From 4aa585349ca0305bd512c18111a43c86fd5c5533 Mon Sep 17 00:00:00 2001 From: Mark Kretschmann Date: Wed, 20 Sep 2006 09:18:25 +0000 Subject: [PATCH] Bugfix for the quiz plugin: With newly created games (no scores yet known) the rank table handling would not work correctly. Effectively no player would be able to surpass the first player. This is now fixed. --- data/rbot/plugins/quiz.rb | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/data/rbot/plugins/quiz.rb b/data/rbot/plugins/quiz.rb index 83e6ff92..4cd26f15 100644 --- a/data/rbot/plugins/quiz.rb +++ b/data/rbot/plugins/quiz.rb @@ -199,46 +199,58 @@ class QuizPlugin < Plugin end - # Updates the per-channel rank table, which is kept for performance reasons + # Updates the per-channel rank table, which is kept for performance reasons. + # This table contains all players sorted by rank. # def calculate_ranks( m, q, nick ) if q.registry.has_key?( nick ) stats = q.registry[nick] # Find player in table + found_player = false i = 0 q.rank_table.each_index do |i| - break if nick == q.rank_table[i][0] + if nick == q.rank_table[i][0] + found_player = true + break + end end - old_rank = i - q.rank_table.delete_at( i ) + # Remove player from old position + if found_player + old_rank = i + q.rank_table.delete_at( i ) + else + old_rank = nil + end # Insert player at new position inserted = false q.rank_table.each_index do |i| - if stats.score >= q.rank_table[i][1].score + if stats.score > q.rank_table[i][1].score q.rank_table[i,0] = [[nick, stats]] inserted = true break end end - # If less than all other players' scores, append at the end + # If less than all other players' scores, append to table unless inserted + i += 1 unless q.rank_table.empty? q.rank_table << [nick, stats] - i += 1 end - if i < old_rank - m.reply "#{nick} ascends to rank #{i + 1}. Congratulations :)" - elsif i > old_rank - m.reply "#{nick} slides down to rank #{i + 1}. So Sorry! NOT. :p" + # Print congratulations/condolences if the player's rank has changed + unless old_rank.nil? + if i < old_rank + m.reply "#{nick} ascends to rank #{i + 1}. Congratulations :)" + elsif i > old_rank + m.reply "#{nick} slides down to rank #{i + 1}. So Sorry! NOT. :p" + end end else q.rank_table << [[nick, PlayerStats.new( 1 )]] end - debug q.rank_table.inspect end -- 2.39.2