2013-10-05 00:16:33 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
module LORChess
|
2013-11-23 12:53:38 +04:00
|
|
|
class RoundRobinTable
|
2013-10-05 00:16:33 +04:00
|
|
|
|
|
|
|
require 'yaml'
|
|
|
|
|
2014-05-10 15:19:46 +04:00
|
|
|
DATADIR = '2014/2-summertime'
|
2014-03-19 20:46:17 +04:00
|
|
|
ROUNDS = 2
|
2013-11-21 21:39:38 +04:00
|
|
|
|
2013-10-05 00:16:33 +04:00
|
|
|
dir = File.dirname(__FILE__)
|
2013-11-21 21:39:38 +04:00
|
|
|
players_yaml = File.expand_path("../../#{DATADIR}/players.yml", dir)
|
2013-10-05 00:16:33 +04:00
|
|
|
@@db_players = YAML.load_file players_yaml
|
2013-11-21 21:39:38 +04:00
|
|
|
results_yaml = File.expand_path("../../#{DATADIR}/results.yml", dir)
|
2013-10-05 00:16:33 +04:00
|
|
|
@@db_results = YAML.load_file results_yaml
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
@@dim = @@db_players.length
|
2014-03-19 20:46:17 +04:00
|
|
|
@@rounds = ROUNDS || 1
|
2013-10-09 22:29:58 +04:00
|
|
|
|
2013-10-05 00:16:33 +04:00
|
|
|
# Sort players in numerical order
|
|
|
|
@@db_players.sort! { |x,y| x['number'] <=> y['number'] }
|
|
|
|
|
|
|
|
def initialize
|
2013-10-10 01:01:32 +04:00
|
|
|
@players = @@db_players.map { |player| player['lor'] }
|
|
|
|
@elo_points = @@db_players.map { |player| player['elo'].to_s }
|
2014-03-19 20:46:17 +04:00
|
|
|
@game_scores = Array.new(@@rounds) {
|
|
|
|
Array.new(@@dim) { Array.new(@@dim) }
|
|
|
|
}
|
|
|
|
@player_games = Array.new(@@dim, 0)
|
|
|
|
@total_scores = Array.new(@@dim, 0.0)
|
|
|
|
@player_places = Array.new(@@dim)
|
|
|
|
@berger_coefs = Array.new(@@dim, 0.0)
|
2013-10-05 00:16:33 +04:00
|
|
|
@buffer = ''
|
|
|
|
|
2013-11-02 02:27:03 +04:00
|
|
|
# Players withdrew from tournament
|
|
|
|
@players_withdrew = @@db_players
|
|
|
|
.select { |player| player['status'] == 'withdrew' }
|
|
|
|
.map { |player| player['lor'] }
|
2013-10-31 13:22:05 +04:00
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
# Correlate the player with his number
|
|
|
|
@player_numbers = {}
|
2013-10-10 01:01:32 +04:00
|
|
|
@players.each_with_index { |player, i| @player_numbers[player] = i }
|
2013-10-05 00:16:33 +04:00
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
fill_results
|
2013-10-05 00:16:33 +04:00
|
|
|
calculate
|
2013-10-09 22:51:19 +04:00
|
|
|
results_to_s
|
2013-10-05 00:16:33 +04:00
|
|
|
|
2013-11-02 02:27:03 +04:00
|
|
|
# Withdraw players from tournament (seppuku)
|
|
|
|
@players_withdrew.each do |player|
|
2013-10-31 13:22:05 +04:00
|
|
|
num = @player_numbers[player]
|
|
|
|
@players[num] = "<del>#{player}</del>"
|
|
|
|
end
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
def fill_results
|
2013-10-05 00:16:33 +04:00
|
|
|
@@db_results.each do |tour|
|
2014-03-25 17:15:47 +04:00
|
|
|
if tour['games']
|
|
|
|
# zero-based round
|
|
|
|
round = (tour['number'] - 1).div(@@dim - 1)
|
2014-03-19 20:46:17 +04:00
|
|
|
|
2014-03-25 17:15:47 +04:00
|
|
|
tour['games'].each do |game|
|
|
|
|
import game, round
|
|
|
|
end
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-19 20:46:17 +04:00
|
|
|
def import game, round
|
2013-10-09 22:29:58 +04:00
|
|
|
num_white = @player_numbers[game['white']]
|
|
|
|
num_black = @player_numbers[game['black']]
|
2013-10-05 00:16:33 +04:00
|
|
|
score = game['result'].split ':'
|
|
|
|
|
2014-03-19 20:46:17 +04:00
|
|
|
@game_scores[round][num_white][num_black] = score[0].to_f
|
|
|
|
@game_scores[round][num_black][num_white] = score[1].to_f
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def calculate
|
2014-03-19 20:46:17 +04:00
|
|
|
@game_scores.each do |round|
|
|
|
|
round.each_with_index do |row, index|
|
|
|
|
games = 0
|
|
|
|
sum = 0.0
|
|
|
|
row.each do |score|
|
|
|
|
unless score.nil?
|
|
|
|
games += 1
|
|
|
|
sum += score
|
|
|
|
end
|
2013-10-09 22:29:58 +04:00
|
|
|
end
|
2014-03-19 20:46:17 +04:00
|
|
|
@player_games[index] += games
|
|
|
|
@total_scores[index] += sum
|
2013-10-09 13:43:50 +04:00
|
|
|
end
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
2013-10-06 00:11:07 +04:00
|
|
|
|
2013-10-09 22:51:19 +04:00
|
|
|
calculate_berger
|
|
|
|
|
2013-10-09 23:55:55 +04:00
|
|
|
player_data = []
|
2013-10-10 01:01:32 +04:00
|
|
|
@@dim.times do |i|
|
|
|
|
player_data << { :number => i,
|
|
|
|
:total => @total_scores[i],
|
|
|
|
:berger => @berger_coefs[i] }
|
2013-10-09 23:55:55 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sort players in the reverse order to Berger coefficient
|
|
|
|
player_data.sort! { |x,y| y[:berger] <=> x[:berger] }
|
|
|
|
|
|
|
|
# Sort players in the reverse order to total score by the bubble
|
|
|
|
# sorting, keeping the order of Berger coefficients for equal
|
|
|
|
# total scores
|
2013-10-10 01:01:32 +04:00
|
|
|
(@@dim - 1).times do |i|
|
2013-10-09 23:55:55 +04:00
|
|
|
(@@dim - 2).downto(i) do |j|
|
|
|
|
if player_data[j][:total] < player_data[j+1][:total]
|
|
|
|
data = player_data[j]
|
|
|
|
player_data[j] = player_data[j+1]
|
|
|
|
player_data[j+1] = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-06 00:11:07 +04:00
|
|
|
|
2013-10-10 01:01:32 +04:00
|
|
|
player_data.each_with_index do |data, i|
|
|
|
|
@player_places[data[:number]] = (i + 1).to_s
|
2013-10-09 22:29:58 +04:00
|
|
|
end
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
|
2013-10-09 22:51:19 +04:00
|
|
|
def calculate_berger
|
2014-03-19 20:46:17 +04:00
|
|
|
@game_scores.each do |round|
|
|
|
|
round.each_with_index do |row, index|
|
|
|
|
berger = 0.0
|
|
|
|
row.each_with_index do |score, i|
|
|
|
|
berger += score * @total_scores[i] unless score.nil?
|
|
|
|
end
|
|
|
|
@berger_coefs[index] += berger
|
2013-10-09 22:51:19 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def results_to_s
|
2014-03-19 20:46:17 +04:00
|
|
|
@game_scores.map! do |round|
|
|
|
|
round.map do |row|
|
|
|
|
row.map { |cell| stylize_score cell }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@player_games.map! { |num| num.to_s }
|
2013-10-10 01:01:32 +04:00
|
|
|
@total_scores.map! { |score| stylize_score score }
|
|
|
|
@berger_coefs.map! { |coef| coef.to_s }
|
2013-10-05 21:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Replace the fractional part `0.5' by ½
|
|
|
|
def stylize_score score
|
2013-10-10 01:01:32 +04:00
|
|
|
return '' if score.nil?
|
2013-10-09 22:29:58 +04:00
|
|
|
frac = score.to_s.split '.'
|
2013-10-05 21:32:33 +04:00
|
|
|
unless frac[0] == '0'
|
2013-10-09 22:29:58 +04:00
|
|
|
str = frac[0]
|
|
|
|
str += '½' if frac[1] == '5'
|
2013-10-05 21:32:33 +04:00
|
|
|
else
|
2013-10-09 22:29:58 +04:00
|
|
|
str = (frac[1] == '5') ? '½' : '0'
|
2013-10-05 21:32:33 +04:00
|
|
|
end
|
2013-10-09 22:29:58 +04:00
|
|
|
str
|
2013-10-05 21:32:33 +04:00
|
|
|
end
|
|
|
|
|
2013-10-05 00:16:33 +04:00
|
|
|
def to_html
|
|
|
|
|
2014-03-04 23:23:15 +04:00
|
|
|
@buffer << "<table class=\"table tournament tablesorter\">\n"
|
|
|
|
@buffer << " <caption><strong>Таблица результатов</strong><caption>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
@buffer << " <thead>\n"
|
|
|
|
@buffer << " <tr>\n"
|
2014-05-21 18:05:43 +04:00
|
|
|
@buffer << " <th class=\"number\">№</th>\n"
|
|
|
|
@buffer << " <th class=\"player\">Участник</th>\n"
|
|
|
|
@buffer << " <th class=\"elo\">elo*</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
2014-03-22 12:29:47 +04:00
|
|
|
@@dim.times do |i|
|
2014-05-21 18:05:43 +04:00
|
|
|
@buffer << " <th class=\"score\" colspan=\"#{@@rounds.to_s}\">#{(i + 1).to_s}</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
|
2014-05-21 18:05:43 +04:00
|
|
|
@buffer << " <th class=\"games\">Игры</th>\n"
|
|
|
|
@buffer << " <th class=\"total\">Очки</th>\n"
|
|
|
|
@buffer << " <th class=\"place\">Место</th>\n"
|
|
|
|
@buffer << " <th class=\"berger\">Бергер</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
@buffer << " </tr>\n"
|
|
|
|
@buffer << " </thead>\n"
|
2014-03-04 23:23:15 +04:00
|
|
|
@buffer << " <tfoot>\n"
|
|
|
|
@buffer << " <tr>\n"
|
2014-03-22 12:29:47 +04:00
|
|
|
@buffer << " <td colspan=\"#{(@@dim * @@rounds + 7).to_s}\">* Средний elo на начало турнира</td>\n"
|
2014-03-04 23:23:15 +04:00
|
|
|
@buffer << " </tr>\n"
|
|
|
|
@buffer << " </tfoot>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
@buffer << " <tbody>\n"
|
|
|
|
|
2013-10-10 01:01:32 +04:00
|
|
|
@@dim.times do |i|
|
2013-10-05 00:16:33 +04:00
|
|
|
|
2014-03-22 12:29:47 +04:00
|
|
|
@buffer << " <tr class=\"place-#{@player_places[i]}\">\n"
|
|
|
|
@buffer << " <td class=\"number\">#{(i + 1).to_s}</td>\n"
|
|
|
|
@buffer << " <td class=\"player\"><strong>#{@players[i]}</strong></td>\n"
|
|
|
|
@buffer << " <td class=\"elo\">#{@elo_points[i]}</td>\n"
|
|
|
|
|
|
|
|
@@dim.times do |j|
|
|
|
|
unless j == i
|
|
|
|
@@rounds.times do |round|
|
|
|
|
@buffer << " <td class=\"score\">#{@game_scores[round][i][j]}</td>\n"
|
2014-03-19 20:46:17 +04:00
|
|
|
end
|
2014-03-22 12:29:47 +04:00
|
|
|
else
|
2014-03-22 15:22:10 +04:00
|
|
|
@buffer << " <td class=\"diagonal\"></td>\n" * @@rounds
|
2013-10-05 00:16:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-22 12:29:47 +04:00
|
|
|
@buffer << " <td class=\"games\">#{@player_games[i]}</td>\n"
|
|
|
|
@buffer << " <td class=\"total\">#{@total_scores[i]}</td>\n"
|
|
|
|
@buffer << " <td class=\"place\">#{@player_places[i]}</td>\n"
|
|
|
|
@buffer << " <td class=\"berger\">#{@berger_coefs[i]}</td>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
@buffer << " </tr>\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
@buffer << " </tbody>\n"
|
|
|
|
@buffer << "</table>\n"
|
|
|
|
@buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|