Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program by: Jarrod Servilla
- //June 3rd, 2018
- def calc_e(rate_a: int, rate_b: int) -> float:
- return 1 / (1 + 10 ** ( (rate_b - rate_a) / 400) )
- def calc_rating(old_rating: int, e: float, is_win: bool) -> int:
- s = 0
- if is_win:
- s = 1
- return int(old_rating + 32 * (s - e))
- def import_ratings() -> dict:
- file = open("ratings.txt", "r")
- d = {}
- for line in file:
- if line != '':
- L = line.split(" ")
- d[L[0]] = int(L[1][:4])
- file.close()
- return(d)
- def interpret(d: dict, tournament: str) -> dict:
- file = open(tournament, "r")
- for line in file:
- if line != '' :
- L = line.split(" ")
- is_win = False
- if L[2] == 'T\n' or L[2] == 'T':
- is_win = True
- old_a = d.get(L[0], 1700)
- old_b = d.get(L[1], 1700)
- d[L[0]] = calc_rating(old_a, calc_e(old_a, old_b), is_win)
- d[L[1]] = calc_rating(old_b, calc_e(old_b, old_a), not is_win)
- file.close()
- return d
- def save_to_file(file_name: str, d: dict):
- file = open(file_name, 'w')
- for (key, value) in d.items():
- file.write(key + ' ' + str(value) + '\n')
- file.close()
- def update(tournament: str):
- d = import_ratings()
- d = interpret(d, tournament)
- save_to_file('ratings.txt', d)
- save_to_file(tournament, d)
- def record_elos(file_name: str, d: dict):
- file = open(file_name, 'r')
- for line in file:
- if line != '':
- L = line.split(" ")
- if L[0] not in d:
- d[L[0]] = [int(L[1][:4])]
- else:
- d[L[0]].append(int(L[1][:4]))
- file.close()
- file2 = open('graph.txt', 'w')
- for (key, value) in d.items():
- file2.write(key + ' ' + str(value) + '\n')
- file2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement