From e622b8a4b3a24f4af87a30c3d11f71f8bc967a0f Mon Sep 17 00:00:00 2001 From: vonavi Date: Fri, 4 Oct 2013 23:16:33 +0300 Subject: [PATCH] Add Ruby routines for generating the tournament table. --- bin/lorchess | 5 ++ lib/lorchess.rb | 4 ++ lib/lorchess/tournament_table.rb | 118 +++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100755 bin/lorchess create mode 100644 lib/lorchess.rb create mode 100644 lib/lorchess/tournament_table.rb diff --git a/bin/lorchess b/bin/lorchess new file mode 100755 index 0000000..ff2f4ae --- /dev/null +++ b/bin/lorchess @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +require File.expand_path('../lib/lorchess.rb', File.dirname(__FILE__)) + +table = LORChess::TournamentTable.new +puts table.to_html diff --git a/lib/lorchess.rb b/lib/lorchess.rb new file mode 100644 index 0000000..100c233 --- /dev/null +++ b/lib/lorchess.rb @@ -0,0 +1,4 @@ +$:.unshift File.dirname(__FILE__) # for use/testing when no gem is installed + +# Internal requires +require 'lorchess/tournament_table.rb' diff --git a/lib/lorchess/tournament_table.rb b/lib/lorchess/tournament_table.rb new file mode 100644 index 0000000..7778e48 --- /dev/null +++ b/lib/lorchess/tournament_table.rb @@ -0,0 +1,118 @@ +# -*- 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 + + # Sort players in numerical order + @@db_players.sort! { |x,y| x['number'] <=> y['number'] } + + def initialize + @players = [] + @elo_list = [] + @dim = @@db_players.length + @results = Array.new(@dim) { Array.new(@dim, '') } + @player_score = [] + @buffer = '' + + @@db_players.each do |player| + @players << player['lor'] + @elo_list << player['elo'] + end + + # Correlate the player with his position + @player_pos = {} + @players.each_with_index { |player, pos| @player_pos[player] = pos } + + fill + calculate + + # Clean the vacancy place + index = @player_pos['Kasparov'] + @players[index] = 'отсутствует' + @elo_list[index] = 1200 + for cell in 0..(@dim-1) + @results[index][cell] = '' + end + + end + + def fill + @@db_results.each do |tour| + tour['games'].each do |game| + import game + end + end + end + + def import game + pos_white = @player_pos[game['white']] + pos_black = @player_pos[game['black']] + score = game['result'].split ':' + + @results[pos_white][pos_black] = score[0] + @results[pos_black][pos_white] = score[1] + end + + def calculate + @results.each do |row| + sum = 0.0 + row.each { |score| sum += score.to_f } + @player_score << sum + end + end + + def to_html + + @buffer << "\n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + + for cell in 0..(@dim-1) + @buffer << " \n" + end + + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + + for row in 0..(@dim-1) + + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + + for cell in 0..(@dim-1) + unless cell == row + @buffer << " \n" + else + @buffer << " \n" + end + end + + @buffer << " \n" + @buffer << " \n" + @buffer << " \n" + end + + @buffer << " \n" + @buffer << "
LOR Chess : Осень-2013\n" + @buffer << "
Участникelo" << (cell+1).to_s << "ОчкиМесто
" << (row+1).to_s << "" << @players[row] << "" << @elo_list[row].to_s << "" << @results[row][cell] << "" << @player_score[row].to_s << "
\n" + @buffer + end + + end +end