Skip to content

Commit

Permalink
Merge pull request #1007 from hackclub/malted/rating-hotfix
Browse files Browse the repository at this point in the history
Validate rating data
  • Loading branch information
maxwofford authored Dec 20, 2024
2 parents 8a44574 + 9f3a728 commit 4758cfa
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/battles/airtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ export const submitVote = async (
throw new Error('User not found')
}

const [winner, loser] = await Promise.all([
base('ships').find(voteData.winner),
base('ships').find(voteData.loser),
])

const K = 30
const expectedScoreWinner =
1 / (1 + Math.pow(10, (voteData.loserRating - voteData.winnerRating) / 400))
1 / (1 + Math.pow(10, (loser.fields.rating - winner.fields.rating) / 400))
const expectedScoreLoser = 1 - expectedScoreWinner

const newWinnerRating = Math.round(
voteData.winnerRating + K * (1 - expectedScoreWinner),
winner.fields.rating + K * (1 - expectedScoreWinner),
)
const newLoserRating = Math.round(
voteData.loserRating + K * (0 - expectedScoreLoser),
loser.fields.rating + K * (0 - expectedScoreLoser),
)

const record = await base('battles').create({
Expand All @@ -126,10 +131,10 @@ export const submitVote = async (
ships: [voteData.winner, voteData.loser],
winner: [voteData.winner],
loser: [voteData.loser],
winner_rating: voteData.winnerRating,
winner_adjustment: newWinnerRating - voteData.winnerRating,
loser_rating: voteData.loserRating,
loser_adjustment: newLoserRating - voteData.loserRating,
winner_rating: winner.fields.rating,
winner_adjustment: newWinnerRating - winner.fields.rating,
loser_rating: loser.fields.rating,
loser_adjustment: newLoserRating - loser.fields.rating,
is_tutorial_vote: !person.user_has_graduated,
generated_at: voteData.ts,
winner_readme_opened: voteData.winner_readme_opened,
Expand Down

0 comments on commit 4758cfa

Please sign in to comment.