Feat: add rating
This commit is contained in:
		| @@ -25,6 +25,7 @@ | |||||||
|             <thead class="bg-gray-800 text-white"> |             <thead class="bg-gray-800 text-white"> | ||||||
|                 <tr> |                 <tr> | ||||||
|                     <th class="py-2 px-4">Player</th> |                     <th class="py-2 px-4">Player</th> | ||||||
|  |                     <th class="py-2 px-4">Rating</th> | ||||||
|                     <th class="py-2 px-4">Games Played</th> |                     <th class="py-2 px-4">Games Played</th> | ||||||
|                     <th class="py-2 px-4">Games Won</th> |                     <th class="py-2 px-4">Games Won</th> | ||||||
|                     <th class="py-2 px-4">Games Lost</th> |                     <th class="py-2 px-4">Games Lost</th> | ||||||
| @@ -36,6 +37,7 @@ | |||||||
|                 {{range .}} |                 {{range .}} | ||||||
|                 <tr> |                 <tr> | ||||||
|                     <td class="py-2 px-4 border">{{.Username}}</td> |                     <td class="py-2 px-4 border">{{.Username}}</td> | ||||||
|  |                     <td class="py-2 px-4 border">{{.Rating}}</td> | ||||||
|                     <td class="py-2 px-4 border">{{.GamesPlayed}}</td> |                     <td class="py-2 px-4 border">{{.GamesPlayed}}</td> | ||||||
|                     <td class="py-2 px-4 border">{{.GamesWon}}</td> |                     <td class="py-2 px-4 border">{{.GamesWon}}</td> | ||||||
|                     <td class="py-2 px-4 border">{{.GamesLost}}</td> |                     <td class="py-2 px-4 border">{{.GamesLost}}</td> | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								migrations/002_add_stats_elo.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								migrations/002_add_stats_elo.down.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | DROP TRIGGER IF EXISTS update_player_rating; | ||||||
|  |  | ||||||
|  | ALTER TABLE DROP COLUMN rating; | ||||||
							
								
								
									
										20
									
								
								migrations/002_add_stats_elo.up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								migrations/002_add_stats_elo.up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |  | ||||||
| @@ -152,6 +152,7 @@ type PlayerStats struct { | |||||||
| 	GuesserWinrate      float64 `db:"guesser_winrate"` | 	GuesserWinrate      float64 `db:"guesser_winrate"` | ||||||
| 	PlayedAsMime        int     `db:"played_as_mime"` | 	PlayedAsMime        int     `db:"played_as_mime"` | ||||||
| 	PlayedAsGuesser     int     `db:"played_as_guesser"` | 	PlayedAsGuesser     int     `db:"played_as_guesser"` | ||||||
|  | 	Rating              float32 `db:"rating"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type Room struct { | type Room struct { | ||||||
|   | |||||||
| @@ -38,6 +38,7 @@ func (p *RepoProvider) UpdatePlayerStats(ctx context.Context, stats *models.Play | |||||||
| 		guesser_winrate = :guesser_winrate, | 		guesser_winrate = :guesser_winrate, | ||||||
| 		played_as_mime = :played_as_mime, | 		played_as_mime = :played_as_mime, | ||||||
| 		played_as_guesser = :played_as_guesser | 		played_as_guesser = :played_as_guesser | ||||||
|  | 		rating = :rating | ||||||
| 	WHERE username = :username`, stats) | 	WHERE username = :username`, stats) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder