Slight refactoring of 'tournament_table.rb' done.

This commit is contained in:
vonavi 2013-10-10 00:01:32 +03:00
parent fa62694aad
commit ddd008ce7f

View File

@ -17,8 +17,8 @@ module LORChess
@@db_players.sort! { |x,y| x['number'] <=> y['number'] } @@db_players.sort! { |x,y| x['number'] <=> y['number'] }
def initialize def initialize
@players = [] @players = @@db_players.map { |player| player['lor'] }
@elo_points = [] @elo_points = @@db_players.map { |player| player['elo'].to_s }
@game_scores = Array.new(@@dim) { Array.new(@@dim) } @game_scores = Array.new(@@dim) { Array.new(@@dim) }
@player_games = [] @player_games = []
@total_scores = [] @total_scores = []
@ -26,14 +26,9 @@ module LORChess
@berger_coefs = [] @berger_coefs = []
@buffer = '' @buffer = ''
@@db_players.each do |player|
@players << player['lor']
@elo_points << player['elo'].to_s
end
# Correlate the player with his number # Correlate the player with his number
@player_numbers = {} @player_numbers = {}
@players.each_with_index { |player, num| @player_numbers[player] = num } @players.each_with_index { |player, i| @player_numbers[player] = i }
fill_results fill_results
calculate calculate
@ -65,7 +60,7 @@ module LORChess
end end
def calculate def calculate
@game_scores.each_with_index do |row, num| @game_scores.each do |row|
games = 0 games = 0
sum = 0.0 sum = 0.0
row.each do |score| row.each do |score|
@ -81,10 +76,10 @@ module LORChess
calculate_berger calculate_berger
player_data = [] player_data = []
for num in 0..(@@dim - 1) @@dim.times do |i|
player_data << { :number => num, player_data << { :number => i,
:total => @total_scores[num], :total => @total_scores[i],
:berger => @berger_coefs[num] } :berger => @berger_coefs[i] }
end end
# Sort players in the reverse order to Berger coefficient # Sort players in the reverse order to Berger coefficient
@ -93,7 +88,7 @@ module LORChess
# Sort players in the reverse order to total score by the bubble # Sort players in the reverse order to total score by the bubble
# sorting, keeping the order of Berger coefficients for equal # sorting, keeping the order of Berger coefficients for equal
# total scores # total scores
0.upto(@@dim - 2) do |i| (@@dim - 1).times do |i|
(@@dim - 2).downto(i) do |j| (@@dim - 2).downto(i) do |j|
if player_data[j][:total] < player_data[j+1][:total] if player_data[j][:total] < player_data[j+1][:total]
data = player_data[j] data = player_data[j]
@ -103,36 +98,31 @@ module LORChess
end end
end end
player_data.each_with_index do |data, num| player_data.each_with_index do |data, i|
@player_places[data[:number]] = (num + 1).to_s @player_places[data[:number]] = (i + 1).to_s
end end
end end
def calculate_berger def calculate_berger
@game_scores.each do |row| @berger_coefs = @game_scores.map do |row|
berger = 0.0 berger = 0.0
row.each_with_index do |score, cell| row.each_with_index do |score, i|
berger += score * @total_scores[cell] unless score.nil? berger += score * @total_scores[i] unless score.nil?
end end
@berger_coefs << berger berger
end end
end end
def results_to_s def results_to_s
for row in 0..(@@dim - 1) @game_scores.map! { |row| row.map { |cell| stylize_score cell } }
for cell in 0..(@@dim - 1) @total_scores.map! { |score| stylize_score score }
@game_scores[row][cell] = stylize_score @game_scores[row][cell] @berger_coefs.map! { |coef| coef.to_s }
end
@total_scores[row] = stylize_score @total_scores[row]
@berger_coefs[row] = @berger_coefs[row].to_s
end
end end
# Replace the fractional part `0.5' by ½ # Replace the fractional part `0.5' by ½
def stylize_score score def stylize_score score
return '' if score.nil?
frac = score.to_s.split '.' frac = score.to_s.split '.'
return '' if frac[0].nil?
unless frac[0] == '0' unless frac[0] == '0'
str = frac[0] str = frac[0]
str += '½' if frac[1] == '5' str += '½' if frac[1] == '5'
@ -152,8 +142,8 @@ module LORChess
@buffer << " <th>Участник</th>\n" @buffer << " <th>Участник</th>\n"
@buffer << " <th>elo*</th>\n" @buffer << " <th>elo*</th>\n"
for cell in 0..(@@dim - 1) @@dim.times do |i|
@buffer << " <th>" << (cell + 1).to_s << "</th>\n" @buffer << " <th>" << (i + 1).to_s << "</th>\n"
end end
@buffer << " <th>Игры</th>\n" @buffer << " <th>Игры</th>\n"
@ -164,25 +154,25 @@ module LORChess
@buffer << " </thead>\n" @buffer << " </thead>\n"
@buffer << " <tbody>\n" @buffer << " <tbody>\n"
for row in 0..(@@dim - 1) @@dim.times do |i|
@buffer << " <tr class=\"place-" << @player_places[row] << "\">\n" @buffer << " <tr class=\"place-" << @player_places[i] << "\">\n"
@buffer << " <td class=\"number\">" << (row + 1).to_s << "</td>\n" @buffer << " <td class=\"number\">" << (i + 1).to_s << "</td>\n"
@buffer << " <td class=\"player\"><strong>" << @players[row] << "</strong></td>\n" @buffer << " <td class=\"player\"><strong>" << @players[i] << "</strong></td>\n"
@buffer << " <td class=\"elo\">" << @elo_points[row] << "</td>\n" @buffer << " <td class=\"elo\">" << @elo_points[i] << "</td>\n"
for cell in 0..(@@dim - 1) @@dim.times do |j|
unless cell == row unless j == i
@buffer << " <td class=\"score\">" << @game_scores[row][cell] << "</td>\n" @buffer << " <td class=\"score\">" << @game_scores[i][j] << "</td>\n"
else else
@buffer << " <td class=\"diagonal\"></td>\n" @buffer << " <td class=\"diagonal\"></td>\n"
end end
end end
@buffer << " <td class=\"games\">" << @player_games[row] << "</td>\n" @buffer << " <td class=\"games\">" << @player_games[i] << "</td>\n"
@buffer << " <td class=\"total\">" << @total_scores[row] << "</td>\n" @buffer << " <td class=\"total\">" << @total_scores[i] << "</td>\n"
@buffer << " <td class=\"place\">" << @player_places[row] << "</td>\n" @buffer << " <td class=\"place\">" << @player_places[i] << "</td>\n"
@buffer << " <td class=\"berger\">" << @berger_coefs[row] << "</td>\n" @buffer << " <td class=\"berger\">" << @berger_coefs[i] << "</td>\n"
@buffer << " </tr>\n" @buffer << " </tr>\n"
end end