All pastes #97482 Raw Edit

alex

public text v1 · immutable
#97482 ·published 2006-07-23 19:50 UTC
rendered paste body
--- quiz.rb.orig	2006-07-23 21:10:45.000000000 +0200
+++ quiz.rb	2006-07-23 17:40:23.000000000 +0200
@@ -203,22 +203,34 @@
     # Updates the per-channel rank table, which is kept for performance reasons
     #
     def calculate_ranks( m, q, nick )
+        debug "before resort:"
+        debug q.rank_table.inspect
+
         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
@@ -231,14 +243,17 @@
                 i += 1
             end
 
-            if i < old_rank
+            unless old_rank.nil?
+              if i < old_rank
                 m.reply "#{nick} ascends to rank #{i + 1}. Congratulations :)"
-            elsif i > old_rank
+              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 "after resort:"
         debug q.rank_table.inspect
     end