aboutsummaryrefslogtreecommitdiff
path: root/src/strategy.py
blob: 1d30a55ff138e13062bd5b5adb65442caf2918f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

from typing import TYPE_CHECKING, Protocol

if TYPE_CHECKING:
    from src.game import Game
    from src.player import Player


class GameEloChangeStrategy(Protocol):
    def proccess_game(
        self,
        game: Game,
        current_elo: dict[Player, int],
    ) -> dict[Player, float]:
        ...


class SimpleEloChangeStrategy(GameEloChangeStrategy):
    D = 400
    K = 32

    def proccess_game(
        self,
        game: Game,
        current_elo: dict[Player, int],
    ) -> dict[Player, float]:
        players_elo_updates = {player: 0.0 for player in game.scores}

        for player_1, score_1 in game.scores.items():
            for player_2, score_2 in game.scores.items():
                if player_1 != player_2:
                    expected_score = 1 / (
                        1
                        + 10
                        ** ((current_elo[player_2] - current_elo[player_1]) / self.D)
                    )
                    result = (
                        1.0 if score_1 > score_2 else 0.0 if score_1 < score_2 else 0.5
                    )
                    players_elo_updates[player_1] += self.K * (result - expected_score)

        return players_elo_updates