diff --git a/rank_progress.py b/rank_progress.py --- a/rank_progress.py +++ b/rank_progress.py @@ -3,13 +3,6 @@ from itertools import groupby from datetime import datetime import argparse -parser = argparse.ArgumentParser() -parser.add_argument("since", help="a date in YYYYMMDD format") -parser.add_argument("to", nargs="?", help="a date in YYYYMMDD format") -parser.add_argument("-c", "--country-code", default="CZ", help="a two letter country code, default=CZ") - -args = parser.parse_args() - class Record: def __init__(self, tokens, source): @@ -41,6 +34,33 @@ class Record: return s +class RankTracker: + def __init__(self, rating): + self._rounded_rating = round_rating(rating) + + @property + def rank(self): + return rating_to_rank(self._rounded_rating) + + def update(self, rating): + rounded_rating = round_rating(rating) + + if rounded_rating == self._rounded_rating: + return False + elif rounded_rating > self._rounded_rating: # promotion + self._rounded_rating = rounded_rating + return self.rank + else: # demotion + if rounded_rating >= 1500 and self._rounded_rating - rating > 100: + self._rounded_rating = rounded_rating + return self.rank + elif self._rounded_rating - rating > 150: + self._rounded_rating = rounded_rating+100 + return self.rank + else: + return False + + def parse_record(record): types = [int, str, str, str, str, str, int, int, int, int] columns = [f(token) for (f, token) in zip(types, record)] + [datetime.strptime(record[5][1:7], "%y%m%d")] @@ -57,38 +77,46 @@ def rating_to_rank(rating): return rank_list[min(key+9, 36)] -since = datetime.strptime(args.since, "%Y%m%d") -to = datetime.strptime(args.to, "%Y%m%d") if args.to else datetime.now() - -with open("/tmp/all.hst") as f: - s = f.read() +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("since", help="a date in YYYYMMDD format") + parser.add_argument("to", nargs="?", help="a date in YYYYMMDD format") + parser.add_argument("-c", "--country-code", default="CZ", help="a two letter country code, default=CZ") -records = [Record.parse(line) for line in s.splitlines()] -records = filter(lambda rec: rec is not None, records) -national_records = filter(lambda rec: rec.country_code == args.country_code, records) -player_records = groupby(national_records, lambda rec: rec.pin) + args = parser.parse_args() + + since = datetime.strptime(args.since, "%Y%m%d") + to = datetime.strptime(args.to, "%Y%m%d") if args.to else datetime.now() -for (pin, recs) in player_records: - tourneys = list(recs) - rounded_rating = tourneys[0].rounded_before + with open("/tmp/all.hst") as f: + s = f.read() - steps = [] + records = [Record.parse(line) for line in s.splitlines()] + records = filter(lambda rec: rec is not None, records) + national_records = filter(lambda rec: rec.country_code == args.country_code, records) + player_records = groupby(national_records, lambda rec: rec.pin) - for r in tourneys: - # omit reset ratings - if rounded_rating != r.rounded_before: - rounded_rating = r.rounded_before + for (pin, recs) in player_records: + tourneys = list(recs) + rounded_rating = tourneys[0].rounded_before + + steps = [] + + for r in tourneys: + # omit reset ratings + if rounded_rating != r.rounded_before: + rounded_rating = r.rounded_before - if r.rating_after-rounded_rating >= 50 or \ - (rounded_rating-r.rating_after > 100 and rounded_rating >= 1600): - steps.append(r) - rounded_rating = r.rounded_after - elif rounded_rating-r.rating_after > 150: + if r.rating_after-rounded_rating >= 50 or \ + (rounded_rating-r.rating_after > 100 and rounded_rating >= 1600): + steps.append(r) + rounded_rating = r.rounded_after + elif rounded_rating-r.rating_after > 150: - steps.append(r) - rounded_rating = r.rounded_after + 100 + steps.append(r) + rounded_rating = r.rounded_after + 100 - steps = [r for r in steps if since <= r.date <= to] - if steps: - print("\n".join(map(str, steps))) - print() + steps = [r for r in steps if since <= r.date <= to] + if steps: + print("\n".join(map(str, steps))) + print()