diff --git a/components/stats.html b/components/stats.html index 23205b7..671805b 100644 --- a/components/stats.html +++ b/components/stats.html @@ -25,6 +25,7 @@ Player + Rating Games Played Games Won Games Lost @@ -36,6 +37,7 @@ {{range .}} {{.Username}} + {{.Rating}} {{.GamesPlayed}} {{.GamesWon}} {{.GamesLost}} diff --git a/migrations/002_add_stats_elo.down.sql b/migrations/002_add_stats_elo.down.sql new file mode 100644 index 0000000..365ba0b --- /dev/null +++ b/migrations/002_add_stats_elo.down.sql @@ -0,0 +1,3 @@ +DROP TRIGGER IF EXISTS update_player_rating; + +ALTER TABLE DROP COLUMN rating; diff --git a/migrations/002_add_stats_elo.up.sql b/migrations/002_add_stats_elo.up.sql new file mode 100644 index 0000000..c2b9346 --- /dev/null +++ b/migrations/002_add_stats_elo.up.sql @@ -0,0 +1,20 @@ +ALTER TABLE player_stats +ADD COLUMN rating REAL NOT NULL DEFAULT 1000.0; + +CREATE TRIGGER update_player_rating +BEFORE UPDATE OF games_played, games_won ON player_stats +WHEN NEW.games_played = OLD.games_played + 1 +BEGIN + UPDATE player_stats + SET rating = OLD.rating + + 32.0 * ( + CASE + WHEN NEW.games_won = OLD.games_won + 1 + THEN 1.0 - 0.5 -- Win term: 0.5 + ELSE 0.0 - 0.5 -- Loss term: -0.5 + END + ) + + 0.05 * (1000.0 - OLD.rating) + WHERE id = OLD.id; +END; + diff --git a/models/main.go b/models/main.go index 42a3194..6adaf9a 100644 --- a/models/main.go +++ b/models/main.go @@ -152,6 +152,7 @@ type PlayerStats struct { GuesserWinrate float64 `db:"guesser_winrate"` PlayedAsMime int `db:"played_as_mime"` PlayedAsGuesser int `db:"played_as_guesser"` + Rating float32 `db:"rating"` } type Room struct { diff --git a/repos/player_stats.go b/repos/player_stats.go index fbb0495..4e66890 100644 --- a/repos/player_stats.go +++ b/repos/player_stats.go @@ -38,6 +38,7 @@ func (p *RepoProvider) UpdatePlayerStats(ctx context.Context, stats *models.Play guesser_winrate = :guesser_winrate, played_as_mime = :played_as_mime, played_as_guesser = :played_as_guesser + rating = :rating WHERE username = :username`, stats) return err }