ALTER TABLE player_stats 
ADD COLUMN rating REAL NOT NULL DEFAULT 1000.0;

CREATE TRIGGER update_player_rating 
AFTER 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;

