This repository has been archived on 2021-12-30. You can view files and clone it, but cannot push or open issues or pull requests.
ELOStar/lib/elo.py

56 lines
1.2 KiB
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : elo.py
@Author : liuyihui
@Email : liuyihui02@gmail.com
'''
# here put the import lib
import math
from .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 __add_history(P: Person):
P.count += 1
if P.count % 10 == 0:
P.hist += str('{:.2f}'.format(P.rate))
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)
__add_history(A)
__add_history(B)