@@ -49,6 +49,11 @@ DECLARE
4949 _team_avg_kda FLOAT;
5050 _player_damage_percent FLOAT;
5151 match_type text ;
52+
53+ -- Series (best-of) scaling
54+ _player_map_wins INT := 0 ;
55+ _player_map_losses INT := 0 ;
56+ _series_multiplier INT := 1 ;
5257BEGIN
5358 SELECT " type" INTO match_type FROM match_options WHERE id = match_record .match_options_id ;
5459
7984 _opponent_lineup_id := match_record .lineup_1_id ;
8085 END IF;
8186
87+ -- Series multiplier: scale ELO by the net map differential for this player's team.
88+ -- BO1 win gives 1x, BO3 2-0 gives 2x, BO3 2-1 gives 1x, BO5 3-0 gives 3x, etc.
89+ -- GREATEST(..., 1) keeps ELO moving for ties / zero-recorded-winner forfeits.
90+ SELECT
91+ COUNT (* ) FILTER (WHERE mm .winning_lineup_id = _player_lineup_id),
92+ COUNT (* ) FILTER (WHERE mm .winning_lineup_id = _opponent_lineup_id)
93+ INTO _player_map_wins, _player_map_losses
94+ FROM match_maps mm
95+ WHERE mm .match_id = match_record .id
96+ AND mm .winning_lineup_id IS NOT NULL ;
97+
98+ _series_multiplier := GREATEST(ABS(_player_map_wins - _player_map_losses), 1 );
99+
82100 -- Calculate average ELO for player's team
83101 -- First get the sum of all previous ELO changes for each player in the team
84102 SELECT
@@ -215,8 +233,8 @@ BEGIN
215233 END IF;
216234
217235 -- Calculate the elo change (round to nearest integer)
218- -- ELO change formula: New Rating = Old Rating + K * (Actual Score - Expected Score) * Performance Multiplier
219- _elo_change := ROUND(_k_factor * (_actual_score - _expected_score) * _performance_multiplier);
236+ -- ELO change formula: New Rating = Old Rating + K * (Actual Score - Expected Score) * Performance Multiplier * Series Multiplier
237+ _elo_change := ROUND(_k_factor * (_actual_score - _expected_score) * _performance_multiplier * _series_multiplier );
220238
221239 -- Return the elo change as JSON with detailed information
222240 RETURN jsonb_build_object(
@@ -234,7 +252,10 @@ BEGIN
234252 ' kda' , _player_kda::FLOAT,
235253 ' team_avg_kda' , _team_avg_kda::FLOAT,
236254 ' damage_percent' , _player_damage_percent,
237- ' performance_multiplier' , _performance_multiplier
255+ ' performance_multiplier' , _performance_multiplier,
256+ ' map_wins' , _player_map_wins,
257+ ' map_losses' , _player_map_losses,
258+ ' series_multiplier' , _series_multiplier
238259 );
239260END;
240261$$ LANGUAGE plpgsql;
0 commit comments