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
Raw Normal View History

2021-12-29 16:53:57 +08:00
#!/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
2021-12-29 16:53:57 +08:00
def __compute_wre(A: Person, B: Person) -> float:
'''compute win rate expectation (wre for short)
2021-12-29 16:53:57 +08:00
Parameters
2021-12-29 16:53:57 +08:00
----------
A, B : two persons
2021-12-29 16:53:57 +08:00
Returns
2021-12-29 16:53:57 +08:00
-------
wre : win rate expectation of A
2021-12-29 16:53:57 +08:00
'''
wre = 1 / (1 + math.pow(10, (B.rate-A.rate)/400))
return wre
2021-12-29 16:53:57 +08:00
def __compute_K(P: Person) -> float:
if P.rate > 2400:
return 12
elif P.rate > 1900:
return 24
else:
return 36
2021-12-29 16:53:57 +08:00
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
2021-12-29 16:53:57 +08:00
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)