Forked from JonathonMA/pingpongninja_expected_results.rb
Created
February 18, 2016 05:20
-
-
Save bernardeli/efdb785b2c8949eb044c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# pingpongninja_expected_results -- simulate a series of games on pingpongninja | |
# | |
# algorithm extracted from | |
# https://github.com/jdennes/pingpongapp/blob/master/pingpong/rankings.py | |
def new_ranks(rank1, rank2, points1, points2) | |
decay_factor = 10 | |
game_ranking_points = (rank1 + rank2) / 2 | |
ranking_change1 = game_ranking_points + (points1 - points2) * 100 / [points1, points2].max | |
ranking_change2 = game_ranking_points + (points2 - points1) * 100 / [points1, points2].max | |
new_rank1 = rank1 / decay_factor * (decay_factor - 1) + ranking_change1 / decay_factor | |
new_rank2 = rank2 / decay_factor * (decay_factor - 1) + ranking_change2 / decay_factor | |
[new_rank1, new_rank2] | |
end | |
def run_game(player1, rank1, player2, rank2, score1, score2) | |
new_rank1, new_rank2 = new_ranks(rank1, rank2, score1, score2) | |
delta = new_rank1 - rank1 | |
puts "#{score1} - #{score2}: #{delta.round(2)}" | |
end | |
def simulate(player1, rank1, player2, rank2) | |
rank1 = rank1.to_f | |
rank2 = rank2.to_f | |
puts "#{player1} beats #{player2} and gains points" | |
0.upto(9).each do |points| | |
run_game(player1, rank1, player2, rank2, 11, points) | |
end | |
10.upto(14) do |point1| | |
run_game(player1, rank1, player2, rank2, point1+2, point1) | |
end | |
puts "#{player1} loses to #{player2} and gains points" | |
0.upto(9).each do |points| | |
run_game(player1, rank1, player2, rank2, points, 11) | |
end | |
10.upto(14) do |point1| | |
run_game(player1, rank1, player2, rank2, point1, point1+2) | |
end | |
end | |
if ARGV.size != 4 | |
puts "Usage: #{File.basename($0)} <p1_name> <p1_rank> <p2_name> <p2_rank>" | |
exit 1 | |
end | |
simulate(*ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment