#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @File : elo.py @Author : liuyihui @Email : liuyihui02@gmail.com ''' # here put the import lib import math from typing_extensions import Self class Person(object): '''personal information Attributes ---------- rate : elo rate name : name sex : male or female link : baike link Methods ------- compute_K : compute the constant K ''' def __init__(self, name: str, rate: float = 1400, *, sex: str = None, link: str = None) -> None: self.name = name self.rate = rate self.link = link if sex and sex not in ['male', 'female']: raise ValueError("Sex must be male or female") self.sex = sex def compute_wre(self, B: Self) -> float: '''compute win rate expectation (wre for short) Parameters ---------- B : another person Returns ------- wre : win rate expectation ''' wre = 1 / (1 + math.pow(10, (B.rate-self.rate)/400)) return wre def compute_K(self) -> float: if self.rate > 2400: return 12 elif self.rate > 2000: return 24 else: return 36 def set_result(self, wre: float, res: float) -> None: '''set result of this game Parameters ---------- wre : win rate expectation res : the result, 0 for lose, 1 for win and 0.5 for draw ''' K = self.compute_K() self.rate += K * (res - wre)