Tournament table: add the support of rounds.

This commit is contained in:
vonavi 2014-03-19 18:46:17 +02:00
parent 10f96915a0
commit 7244b0435a

View File

@ -5,7 +5,8 @@ module LORChess
require 'yaml' require 'yaml'
DATADIR='2014/1-tabiyas' DATADIR = '2014/1-tabiyas'
ROUNDS = 2
dir = File.dirname(__FILE__) dir = File.dirname(__FILE__)
players_yaml = File.expand_path("../../#{DATADIR}/players.yml", dir) players_yaml = File.expand_path("../../#{DATADIR}/players.yml", dir)
@ -14,6 +15,7 @@ module LORChess
@@db_results = YAML.load_file results_yaml @@db_results = YAML.load_file results_yaml
@@dim = @@db_players.length @@dim = @@db_players.length
@@rounds = ROUNDS || 1
# Sort players in numerical order # Sort players in numerical order
@@db_players.sort! { |x,y| x['number'] <=> y['number'] } @@db_players.sort! { |x,y| x['number'] <=> y['number'] }
@ -21,11 +23,13 @@ module LORChess
def initialize def initialize
@players = @@db_players.map { |player| player['lor'] } @players = @@db_players.map { |player| player['lor'] }
@elo_points = @@db_players.map { |player| player['elo'].to_s } @elo_points = @@db_players.map { |player| player['elo'].to_s }
@game_scores = Array.new(@@dim) { Array.new(@@dim) } @game_scores = Array.new(@@rounds) {
@player_games = [] Array.new(@@dim) { Array.new(@@dim) }
@total_scores = [] }
@player_places = [] @player_games = Array.new(@@dim, 0)
@berger_coefs = [] @total_scores = Array.new(@@dim, 0.0)
@player_places = Array.new(@@dim)
@berger_coefs = Array.new(@@dim, 0.0)
@buffer = '' @buffer = ''
# Players withdrew from tournament # Players withdrew from tournament
@ -50,23 +54,27 @@ module LORChess
def fill_results def fill_results
@@db_results.each do |tour| @@db_results.each do |tour|
# zero-based round
round = (tour['number'] - 1).div(@@dim)
tour['games'].each do |game| tour['games'].each do |game|
import game import game, round
end end
end end
end end
def import game def import game, round
num_white = @player_numbers[game['white']] num_white = @player_numbers[game['white']]
num_black = @player_numbers[game['black']] num_black = @player_numbers[game['black']]
score = game['result'].split ':' score = game['result'].split ':'
@game_scores[num_white][num_black] = score[0].to_f @game_scores[round][num_white][num_black] = score[0].to_f
@game_scores[num_black][num_white] = score[1].to_f @game_scores[round][num_black][num_white] = score[1].to_f
end end
def calculate def calculate
@game_scores.each do |row| @game_scores.each do |round|
round.each_with_index do |row, index|
games = 0 games = 0
sum = 0.0 sum = 0.0
row.each do |score| row.each do |score|
@ -75,8 +83,9 @@ module LORChess
sum += score sum += score
end end
end end
@player_games << games.to_s @player_games[index] += games
@total_scores << sum @total_scores[index] += sum
end
end end
calculate_berger calculate_berger
@ -110,17 +119,24 @@ module LORChess
end end
def calculate_berger def calculate_berger
@berger_coefs = @game_scores.map do |row| @game_scores.each do |round|
round.each_with_index do |row, index|
berger = 0.0 berger = 0.0
row.each_with_index do |score, i| row.each_with_index do |score, i|
berger += score * @total_scores[i] unless score.nil? berger += score * @total_scores[i] unless score.nil?
end end
berger @berger_coefs[index] += berger
end
end end
end end
def results_to_s def results_to_s
@game_scores.map! { |row| row.map { |cell| stylize_score cell } } @game_scores.map! do |round|
round.map do |row|
row.map { |cell| stylize_score cell }
end
end
@player_games.map! { |num| num.to_s }
@total_scores.map! { |score| stylize_score score } @total_scores.map! { |score| stylize_score score }
@berger_coefs.map! { |coef| coef.to_s } @berger_coefs.map! { |coef| coef.to_s }
end end
@ -148,7 +164,7 @@ module LORChess
@buffer << " <th>Участник</th>\n" @buffer << " <th>Участник</th>\n"
@buffer << " <th>elo*</th>\n" @buffer << " <th>elo*</th>\n"
@@dim.times do |i| (@@dim * @@rounds).times do |i|
@buffer << " <th class=\"{sorter: false}\">" << (i + 1).to_s << "</th>\n" @buffer << " <th class=\"{sorter: false}\">" << (i + 1).to_s << "</th>\n"
end end
@ -160,7 +176,7 @@ module LORChess
@buffer << " </thead>\n" @buffer << " </thead>\n"
@buffer << " <tfoot>\n" @buffer << " <tfoot>\n"
@buffer << " <tr>\n" @buffer << " <tr>\n"
@buffer << " <td colspan=\"" << (@@dim + 7).to_s << "\">* Средний elo на начало турнира</td>\n" @buffer << " <td colspan=\"" << (@@dim * @@rounds + 7).to_s << "\">* Средний elo на начало турнира</td>\n"
@buffer << " </tr>\n" @buffer << " </tr>\n"
@buffer << " </tfoot>\n" @buffer << " </tfoot>\n"
@buffer << " <tbody>\n" @buffer << " <tbody>\n"
@ -172,13 +188,15 @@ module LORChess
@buffer << " <td class=\"player\"><strong>" << @players[i] << "</strong></td>\n" @buffer << " <td class=\"player\"><strong>" << @players[i] << "</strong></td>\n"
@buffer << " <td class=\"elo\">" << @elo_points[i] << "</td>\n" @buffer << " <td class=\"elo\">" << @elo_points[i] << "</td>\n"
@@rounds.times do |round|
@@dim.times do |j| @@dim.times do |j|
unless j == i unless j == i
@buffer << " <td class=\"score\">" << @game_scores[i][j] << "</td>\n" @buffer << " <td class=\"score\">" << @game_scores[round][i][j] << "</td>\n"
else else
@buffer << " <td class=\"diagonal\"></td>\n" @buffer << " <td class=\"diagonal\"></td>\n"
end end
end end
end
@buffer << " <td class=\"games\">" << @player_games[i] << "</td>\n" @buffer << " <td class=\"games\">" << @player_games[i] << "</td>\n"
@buffer << " <td class=\"total\">" << @total_scores[i] << "</td>\n" @buffer << " <td class=\"total\">" << @total_scores[i] << "</td>\n"