From 667d106b27f266c1d9f7c7e205004a667c5cc00d Mon Sep 17 00:00:00 2001 From: vonavi Date: Sat, 5 Oct 2013 23:11:07 +0300 Subject: [PATCH] Add player's places in the tournament table. --- lib/lorchess/tournament_table.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/lorchess/tournament_table.rb b/lib/lorchess/tournament_table.rb index 2ebe9a2..625480f 100644 --- a/lib/lorchess/tournament_table.rb +++ b/lib/lorchess/tournament_table.rb @@ -20,6 +20,7 @@ module LORChess @dim = @@db_players.length @results = Array.new(@dim) { Array.new(@dim, '') } @player_score = [] + @player_place = [] @buffer = '' @@db_players.each do |player| @@ -61,11 +62,19 @@ module LORChess end def calculate - @results.each do |row| + score_data = [] + + @results.each_with_index do |row, i| sum = 0.0 row.each { |score| sum += score.to_f } @player_score << sum.to_s + score_data << [i, sum] end + + # Sort players in correspondence with the decrease of place + score_data.sort! { |x,y| y[1] <=> x[1] } + + score_data.each_with_index { |data,i| @player_place[data[0]] = (i+1).to_s } end def stylize_table @@ -120,14 +129,14 @@ module LORChess for cell in 0..(@dim-1) unless cell == row - @buffer << " " << @results[row][cell] << "\n" + @buffer << " " << @results[row][cell] << "\n" else - @buffer << " \n" + @buffer << " \n" end end @buffer << " " << @player_score[row] << "\n" - @buffer << " \n" + @buffer << " " << @player_place[row] << "\n" @buffer << " \n" end