#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @File : elo.py @Author : liuyihui @Email : liuyihui02@gmail.com ''' # here put the import lib import math from lib.db_init import Person def __compute_wre(A: Person, B: Person) -> float: '''compute win rate expectation (wre for short) Parameters ---------- A, B : two persons Returns ------- wre : win rate expectation of A ''' wre = 1 / (1 + math.pow(10, (B.rate-A.rate)/400)) return wre def __compute_K(P: Person) -> float: if P.rate > 2400: return 12 elif P.rate > 1900: return 24 else: return 36 def set_result(A: Person, B: Person, res: float) -> None: '''set result of this game Parameters ---------- A, B : two persons res : the result, 0 for A win, 1 for B win and -1 for draw ''' wre1 = __compute_wre(A, B) wre2 = 1 - wre1 if res >= 0: A.rate += __compute_K(A) * (1 - res - wre1) B.rate += __compute_K(B) * (res - wre2)