Feat: add rating
This commit is contained in:
		
							
								
								
									
										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; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder