Skip to content

Instantly share code, notes, and snippets.

@mmiliaus
Created July 24, 2011 20:41
Show Gist options
  • Save mmiliaus/1103081 to your computer and use it in GitHub Desktop.
Save mmiliaus/1103081 to your computer and use it in GitHub Desktop.
def match! battle, winner, loser
r_a = winner.rating
r_b = loser.rating
max = battle.max.to_i
min = battle.min.to_i
th = ( max - min ) / 2
e_a = 1.0 / ( 1 + 10 ** ( (r_b - r_a) / 400 ))
e_b = 1.0 / ( 1 + 10 ** ( (r_a - r_b) / 400 ))
w_a = 1
w_b = 0
winner.won += 1
loser.lost += 1
k_a = r_a >= th ? 16 : 32
k_b = r_b >= th ? 16 : 32
r_a_new = ( r_a + k_a * ( w_a - e_a ) ).round
r_b_new = ( r_b + k_b * ( w_b - e_b ) ).round
winner.rating = r_a_new
loser.rating = r_b_new
winner.save!
loser.save!
battle.max = [max, r_a_new, r_b_new].max
battle.min = [min, r_a_new, r_b_new].min
battle.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment