I am programming a database to automatically calculate a player's Elo rating (http://en.wikipedia.org/wiki/Elo_rating_system) for a foosball league. A user completes a form that asks who the two players were and which of them won. The change in each user's rating is calculated by a formula that uses both of their current ratings:
Code:
ExpectedA = 1/(1+10^((RatingA-RatingB)/400))
RatingDiff = 20(W-ExpectedA)
RatingNewA = RatingA+RatingDiff
RatingNewB = RatingB-RatingDiff
W is 1 if A wins, 0 if B wins.
Currently, my database has tables:
[Players] with fields PlayerID, FirstName, LastName, Rating
[Games] with fields GameID, PlayerGold, PlayerBlack, Winner
There are two sides to the foosball table, Gold team and Black team; the Winner field is allowed to contain either 'Black' or 'Gold'.
PlayerBlack and PlayerGold are the PlayerIDs of the two competing players.
How would the code look that, upon submitting a form that contains the two players names and which of them won, selects the Rating value for each player, performs the calculation, and then updates the Rating value for each player with their new rating?