2013-10-05 00:16:33 +04:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
module LORChess
|
|
|
|
|
class TournamentTable
|
|
|
|
|
|
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
|
|
dir = File.dirname(__FILE__)
|
|
|
|
|
players_yaml = File.expand_path('../../autumn2013/players.yml', dir)
|
|
|
|
|
@@db_players = YAML.load_file players_yaml
|
|
|
|
|
results_yaml = File.expand_path('../../autumn2013/results.yml', dir)
|
|
|
|
|
@@db_results = YAML.load_file results_yaml
|
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@@dim = @@db_players.length
|
|
|
|
|
|
2013-10-05 00:16:33 +04:00
|
|
|
|
# Sort players in numerical order
|
|
|
|
|
@@db_players.sort! { |x,y| x['number'] <=> y['number'] }
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
@players = []
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@elo_points = []
|
|
|
|
|
@game_scores = Array.new(@@dim) { Array.new(@@dim) }
|
2013-10-09 13:43:50 +04:00
|
|
|
|
@player_games = []
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@total_scores = []
|
|
|
|
|
@player_places = []
|
|
|
|
|
@berger_coefs = []
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer = ''
|
|
|
|
|
|
|
|
|
|
@@db_players.each do |player|
|
|
|
|
|
@players << player['lor']
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@elo_points << player['elo'].to_s
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
# Correlate the player with his number
|
|
|
|
|
@player_numbers = {}
|
|
|
|
|
@players.each_with_index { |player, num| @player_numbers[player] = num }
|
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
|
|
|
|
|
|
|
|
|
# Clean the vacancy place
|
2013-10-09 22:29:58 +04:00
|
|
|
|
num = @player_numbers['Kasparov']
|
|
|
|
|
if num
|
|
|
|
|
@players[num] = '<em style="font-weight:normal">отсутствует</em>'
|
|
|
|
|
@elo_points[num] = '1200'
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
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|
|
|
|
|
|
tour['games'].each do |game|
|
|
|
|
|
import game
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def import game
|
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 ':'
|
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@game_scores[num_white][num_black] = score[0].to_f
|
|
|
|
|
@game_scores[num_black][num_white] = score[1].to_f
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def calculate
|
2013-10-09 22:29:58 +04:00
|
|
|
|
player_scores = []
|
2013-10-06 00:11:07 +04:00
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@game_scores.each_with_index do |row, num|
|
2013-10-09 13:43:50 +04:00
|
|
|
|
games = 0
|
2013-10-05 00:16:33 +04:00
|
|
|
|
sum = 0.0
|
2013-10-09 13:43:50 +04:00
|
|
|
|
row.each do |score|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
unless score.nil?
|
|
|
|
|
games += 1
|
|
|
|
|
sum += score
|
|
|
|
|
end
|
2013-10-09 13:43:50 +04:00
|
|
|
|
end
|
|
|
|
|
@player_games << games.to_s
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@total_scores << sum
|
|
|
|
|
player_scores << { :number => num, :total => sum }
|
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-06 02:47:40 +04:00
|
|
|
|
# Sort players in the reverse order to total score
|
2013-10-09 22:29:58 +04:00
|
|
|
|
player_scores.sort! { |x,y| y[:total] <=> x[:total] }
|
2013-10-06 00:11:07 +04:00
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
player_scores.each_with_index do |data, num|
|
|
|
|
|
@player_places[data[:number]] = (num + 1).to_s
|
|
|
|
|
end
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 22:51:19 +04:00
|
|
|
|
def calculate_berger
|
|
|
|
|
@game_scores.each do |row|
|
|
|
|
|
berger = 0.0
|
|
|
|
|
row.each_with_index do |score, cell|
|
|
|
|
|
berger += score * @total_scores[cell] unless score.nil?
|
|
|
|
|
end
|
|
|
|
|
@berger_coefs << berger
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def results_to_s
|
2013-10-09 22:29:58 +04:00
|
|
|
|
for row in 0..(@@dim - 1)
|
|
|
|
|
for cell in 0..(@@dim - 1)
|
|
|
|
|
@game_scores[row][cell] = stylize_score @game_scores[row][cell]
|
2013-10-05 21:32:33 +04:00
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@total_scores[row] = stylize_score @total_scores[row]
|
2013-10-09 22:51:19 +04:00
|
|
|
|
@berger_coefs[row] = @berger_coefs[row].to_s
|
2013-10-05 21:32:33 +04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Replace the fractional part `0.5' by ½
|
|
|
|
|
def stylize_score score
|
2013-10-09 22:29:58 +04:00
|
|
|
|
frac = score.to_s.split '.'
|
2013-10-05 21:32:33 +04:00
|
|
|
|
return '' if frac[0].nil?
|
|
|
|
|
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
|
|
|
|
|
|
2013-10-05 11:18:41 +04:00
|
|
|
|
@buffer << "<table class=\"table table-bordered tournament\">\n"
|
|
|
|
|
@buffer << " <caption><strong>LOR Chess : Осень-2013</strong><caption>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer << " <thead>\n"
|
|
|
|
|
@buffer << " <tr>\n"
|
2013-10-05 22:03:46 +04:00
|
|
|
|
@buffer << " <th>№</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer << " <th>Участник</th>\n"
|
2013-10-05 11:18:41 +04:00
|
|
|
|
@buffer << " <th>elo*</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
for cell in 0..(@@dim - 1)
|
|
|
|
|
@buffer << " <th>" << (cell + 1).to_s << "</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 13:43:50 +04:00
|
|
|
|
@buffer << " <th>Игры</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer << " <th>Очки</th>\n"
|
|
|
|
|
@buffer << " <th>Место</th>\n"
|
2013-10-09 22:51:19 +04:00
|
|
|
|
@buffer << " <th>Бергер</th>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer << " </tr>\n"
|
|
|
|
|
@buffer << " </thead>\n"
|
|
|
|
|
@buffer << " <tbody>\n"
|
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
for row in 0..(@@dim - 1)
|
2013-10-05 00:16:33 +04:00
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@buffer << " <tr class=\"place-" << @player_places[row] << "\">\n"
|
|
|
|
|
@buffer << " <td class=\"number\">" << (row + 1).to_s << "</td>\n"
|
2013-10-06 00:30:54 +04:00
|
|
|
|
@buffer << " <td class=\"player\"><strong>" << @players[row] << "</strong></td>\n"
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@buffer << " <td class=\"elo\">" << @elo_points[row] << "</td>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
|
2013-10-09 22:29:58 +04:00
|
|
|
|
for cell in 0..(@@dim - 1)
|
2013-10-05 00:16:33 +04:00
|
|
|
|
unless cell == row
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@buffer << " <td class=\"score\">" << @game_scores[row][cell] << "</td>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
else
|
2013-10-06 00:11:07 +04:00
|
|
|
|
@buffer << " <td class=\"diagonal\"></td>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-10-09 13:43:50 +04:00
|
|
|
|
@buffer << " <td class=\"games\">" << @player_games[row] << "</td>\n"
|
2013-10-09 22:29:58 +04:00
|
|
|
|
@buffer << " <td class=\"total\">" << @total_scores[row] << "</td>\n"
|
|
|
|
|
@buffer << " <td class=\"place\">" << @player_places[row] << "</td>\n"
|
2013-10-09 22:51:19 +04:00
|
|
|
|
@buffer << " <td class=\"berger\">" << @berger_coefs[row] << "</td>\n"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer << " </tr>\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@buffer << " </tbody>\n"
|
|
|
|
|
@buffer << "</table>\n"
|
2013-10-05 11:18:41 +04:00
|
|
|
|
@buffer << "* Средний elo на 13.09.2013 3.00 МСК"
|
2013-10-05 00:16:33 +04:00
|
|
|
|
@buffer
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|