From f8920c5709dfb1ed263fb21291678033cc34ba49 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Thu, 2 Jun 2016 22:11:23 -0700 Subject: [PATCH] took care of mixed tabs / spaces --- README.rdoc | 2 +- lib/elo.rb | 2 -- lib/elo/configuration.rb | 14 +++----- lib/elo/game.rb | 70 ++++++++++++++++++++-------------------- lib/elo/helper.rb | 10 ++---- lib/elo/player.rb | 68 ++++++++++++++++++-------------------- lib/elo/rating.rb | 47 ++++++++++++--------------- 7 files changed, 97 insertions(+), 116 deletions(-) diff --git a/README.rdoc b/README.rdoc index 728f2c3..28aa19b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -108,7 +108,7 @@ games, and 16 for everybody else. gem install elo == Note on Patches/Pull Requests - + * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a diff --git a/lib/elo.rb b/lib/elo.rb index b4de98d..98976d3 100644 --- a/lib/elo.rb +++ b/lib/elo.rb @@ -6,7 +6,6 @@ # See README.rdoc for general information about Elo. module Elo - # Accessor to the configuration object, which, # should be instantiated only once (and automatically). def self.config @@ -22,5 +21,4 @@ def self.config def self.configure(&block) yield(config) end - end diff --git a/lib/elo/configuration.rb b/lib/elo/configuration.rb index 4c10daa..2281ab2 100644 --- a/lib/elo/configuration.rb +++ b/lib/elo/configuration.rb @@ -1,7 +1,5 @@ module Elo - class Configuration - # This is the lower boundry of the rating you need to be a pro player. # This setting is used in the FIDE k-factor rules. (default = 2400) attr_accessor :pro_rating_boundry @@ -19,9 +17,9 @@ class Configuration # Use the settings that FIDE use for determening the K-factor. # This is the case when all settings are unaltered. (default = true) - # + # # In short: - # + # # * K-factor is 25 when a player is a starter (less than 30 games played) # * K-factor is 10 when a player is a pro (rating above 2400, now or in the past) # * K-factor is 15 when a player in other cases @@ -72,12 +70,10 @@ def k_factors def apply_fide_k_factors unless @applied_fide_k_factors - k_factor(10) { pro? or pro_rating? } - k_factor(25) { starter? } - @applied_fide_k_factors = true + k_factor(10) { pro? or pro_rating? } + k_factor(25) { starter? } + @applied_fide_k_factors = true end end - end - end diff --git a/lib/elo/game.rb b/lib/elo/game.rb index 0da44b2..f40e881 100644 --- a/lib/elo/game.rb +++ b/lib/elo/game.rb @@ -1,26 +1,24 @@ module Elo - - # A Game is a collection of two Elo::Player objects - # and a result. - # Once the result is known, it propagates the new - # ratings to the players. + # A Game is a collection of two Elo::Player objects + # and a result. + # Once the result is known, it propagates the new + # ratings to the players. class Game - include Helper - # The result is the result of the match. It's a nubmer - # from 0 to 1 from the perspective of player +:one+. + # The result is the result of the match. It's a nubmer + # from 0 to 1 from the perspective of player +:one+. attr_reader :result - # The first Elo::Player. The result is in perspecive of - # this player. + # The first Elo::Player. The result is in perspecive of + # this player. attr_reader :one - # The second Elo::Player. + # The second Elo::Player. attr_reader :two # Every time a result is set, it tells the Elo::Player - # objects to update their scores. + # objects to update their scores. def process_result(result) @result = result calculate @@ -36,41 +34,41 @@ def calculate self end - # Player +:one+ has won! - # This is a shortcut method for setting the score to 1 + # Player +:one+ has won! + # This is a shortcut method for setting the score to 1 def win process_result 1.0 end - # Player +:one+ has lost! - # This is a shortcut method for setting the score to 0 + # Player +:one+ has lost! + # This is a shortcut method for setting the score to 0 def lose process_result 0.0 end - # It was a draw. - # This is a shortcut method for setting the score to 0.5 + # It was a draw. + # This is a shortcut method for setting the score to 0.5 def draw process_result 0.5 end # You can override this method if you store each game - # in a database or something like that. - # This method will be called when a result is known. + # in a database or something like that. + # This method will be called when a result is known. def save end - # Set the winner. Provide it with a Elo::Player. + # Set the winner. Provide it with a Elo::Player. def winner=(player) process_result(player == one ? 1.0 : 0.0) end - # Set the loser. Provide it with a Elo::Player. + # Set the loser. Provide it with a Elo::Player. def loser=(player) process_result(player == one ? 0.0 : 1.0) end - # Access the Elo::Rating objects for both players. + # Access the Elo::Rating objects for both players. def ratings @ratings ||= { one => rating_one, two => rating_two } end @@ -81,22 +79,24 @@ def inspect private - # Create an Elo::Rating object for player one + # Create an Elo::Rating object for player one def rating_one - Rating.new(:result => result, - :old_rating => one.rating, - :other_rating => two.rating, - :k_factor => one.k_factor) + Rating.new( + result: result, + old_rating: one.rating, + other_rating: two.rating, + k_factor: one.k_factor + ) end - # Create an Elo::Rating object for player two + # Create an Elo::Rating object for player two def rating_two - Rating.new(:result => (1.0 - result), - :old_rating => two.rating, - :other_rating => one.rating, - :k_factor => two.k_factor) + Rating.new( + result: 1.0 - result, + old_rating: two.rating, + other_rating: one.rating, + k_factor: two.k_factor + ) end - end - end diff --git a/lib/elo/helper.rb b/lib/elo/helper.rb index b76ed9f..6b76811 100644 --- a/lib/elo/helper.rb +++ b/lib/elo/helper.rb @@ -1,15 +1,11 @@ module Elo - - module Helper - + module Helper # Every object can be initialized with a hash, - # almost, but not quite, entirely unlike ActiveRecord. + # almost, but not quite, entirely unlike ActiveRecord. def initialize(attributes = {}) attributes.each do |key, value| instance_variable_set("@#{key}", value) end end - - end - + end end diff --git a/lib/elo/player.rb b/lib/elo/player.rb index 4b80332..49e546c 100644 --- a/lib/elo/player.rb +++ b/lib/elo/player.rb @@ -1,8 +1,6 @@ module Elo - # A player. You need at least two play a Game. class Player - include Helper # The rating you provided, or the default rating from configuration @@ -10,54 +8,54 @@ def rating @rating ||= Elo.config.default_rating end - # The number of games played is needed for calculating the K-factor. + # The number of games played is needed for calculating the K-factor. def games_played @games_played ||= games.size end - # A list of games played by the player. + # A list of games played by the player. def games @games ||= [] end - # Is the player considered a pro, because his/her rating crossed - # the threshold configured? This is needed for calculating the K-factor. + # Is the player considered a pro, because his/her rating crossed + # the threshold configured? This is needed for calculating the K-factor. def pro_rating? rating >= Elo.config.pro_rating_boundry end - # Is the player just starting? Provide the boundry for - # the amount of games played in the configuration. - # This is needed for calculating the K-factor. + # Is the player just starting? Provide the boundry for + # the amount of games played in the configuration. + # This is needed for calculating the K-factor. def starter? games_played < Elo.config.starter_boundry end - # FIDE regulations specify that once you reach a pro status - # (see +pro_rating?+), you are considered a pro for life. - # - # You might need to specify it manually, when depending on - # external persistence of players. - # - # Elo::Player.new(:pro => true) + # FIDE regulations specify that once you reach a pro status + # (see +pro_rating?+), you are considered a pro for life. + # + # You might need to specify it manually, when depending on + # external persistence of players. + # + # Elo::Player.new(:pro => true) def pro? !!@pro end # You can override this method if you store each game - # in a database or something like that. - # This method will be called when a result is known. + # in a database or something like that. + # This method will be called when a result is known. def save end - # Calculates the K-factor for the player. - # Elo allows you specify custom Rules (see Elo::Configuration). - # - # You can set it manually, if you wish: - # - # Elo::Player.new(:k_factor => 10) - # - # This stops this player from using the K-factor rules. + # Calculates the K-factor for the player. + # Elo allows you specify custom Rules (see Elo::Configuration). + # + # You can set it manually, if you wish: + # + # Elo::Player.new(:k_factor => 10) + # + # This stops this player from using the K-factor rules. def k_factor return @k_factor if @k_factor Elo.config.applied_k_factors.each do |rule| @@ -66,26 +64,26 @@ def k_factor Elo.config.default_k_factor end - # Start a game with another player. At this point, no - # result is known and nothing really happens. + # Start a game with another player. At this point, no + # result is known and nothing really happens. def versus(other_player, options = {}) Game.new(options.merge(:one => self, :two => other_player)).calculate end - # Start a game with another player and set the score - # immediately. + # Start a game with another player and set the score + # immediately. def wins_from(other_player, options = {}) versus(other_player, options).win end - # Start a game with another player and set the score - # immediately. + # Start a game with another player and set the score + # immediately. def plays_draw(other_player, options = {}) versus(other_player, options).draw end - # Start a game with another player and set the score - # immediately. + # Start a game with another player and set the score + # immediately. def loses_from(other_player, options = {}) versus(other_player, options).lose end @@ -108,7 +106,5 @@ def played(game) @pro = true if pro_rating? save end - end - end diff --git a/lib/elo/rating.rb b/lib/elo/rating.rb index 0b954b4..6abc03d 100644 --- a/lib/elo/rating.rb +++ b/lib/elo/rating.rb @@ -1,59 +1,54 @@ module Elo - - # This class calculates the rating between two players, - # but only from one persons perspective. You need two Rating-instances - # to calculate ratings for both players. Luckily, Elo::Game handles - # this for you automatically. + # This class calculates the rating between two players, + # but only from one persons perspective. You need two Rating-instances + # to calculate ratings for both players. Luckily, Elo::Game handles + # this for you automatically. class Rating - include Helper - # The rating of the player you DON"T wish to calculate. + # The rating of the player you DON"T wish to calculate. attr_reader :other_rating - # The rating of the player you wish to calculate. + # The rating of the player you wish to calculate. attr_reader :old_rating - # The k-factor you wish to use for this calculation. + # The k-factor you wish to use for this calculation. attr_reader :k_factor - # The new rating is... wait for it... the new rating! + # The new rating is... wait for it... the new rating! def new_rating (old_rating.to_f + change).to_i end - private + private - # The result of the match. 1 means that the player won, 0 means that the - # player lost and 0.5 means that it was a draw. + # The result of the match. 1 means that the player won, 0 means that the + # player lost and 0.5 means that it was a draw. def result raise "Invalid result: #{@result.inspect}" unless valid_result? @result.to_f end - # Only values between 0 and 1 are considered to be valid scores. + # Only values between 0 and 1 are considered to be valid scores. def valid_result? (0..1).include? @result end - # The expected score is the probably outcome of the match, depending - # on the difference in rating between the two players. - # - # For more information visit - # {Wikipedia}[http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details] + # The expected score is the probably outcome of the match, depending + # on the difference in rating between the two players. + # + # For more information visit + # {Wikipedia}[http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details] def expected 1.0 / ( 1.0 + ( 10.0 ** ((other_rating.to_f - old_rating.to_f) / 400.0) ) ) end - # The change is the points you earn or lose. - # - # For more information visit - # {Wikipedia}[http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details] + # The change is the points you earn or lose. + # + # For more information visit + # {Wikipedia}[http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details] def change k_factor.to_f * ( result.to_f - expected ) end - - end - end