1
Fork 0

Tournament table: add the support of rounds.

master
vonavi 2014-03-19 18:46:17 +02:00
parent 10f96915a0
commit 7244b0435a
1 changed files with 50 additions and 32 deletions

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,33 +54,38 @@ 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|
games = 0 round.each_with_index do |row, index|
sum = 0.0 games = 0
row.each do |score| sum = 0.0
unless score.nil? row.each do |score|
games += 1 unless score.nil?
sum += score games += 1
sum += score
end
end end
@player_games[index] += games
@total_scores[index] += sum
end end
@player_games << games.to_s
@total_scores << sum
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|
berger = 0.0 round.each_with_index do |row, index|
row.each_with_index do |score, i| berger = 0.0
berger += score * @total_scores[i] unless score.nil? row.each_with_index do |score, i|
berger += score * @total_scores[i] unless score.nil?
end
@berger_coefs[index] += berger
end end
berger
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,11 +188,13 @@ 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"
@@dim.times do |j| @@rounds.times do |round|
unless j == i @@dim.times do |j|
@buffer << " <td class=\"score\">" << @game_scores[i][j] << "</td>\n" unless j == i
else @buffer << " <td class=\"score\">" << @game_scores[round][i][j] << "</td>\n"
@buffer << " <td class=\"diagonal\"></td>\n" else
@buffer << " <td class=\"diagonal\"></td>\n"
end
end end
end end