Changeset - dfc18f1d128e
[Not reviewed]
default
0 1 0
Laman - 3 years ago 2021-10-14 17:04:08

implemented the below 5k drop cushion
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rank_progress.py
Show inline comments
 
import re
 
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):
 
		self.source = source
 

	
 
		self.pin = int(tokens[0])
 
		self.country_code = tokens[2]
 
		self.rating_before = int(tokens[8])
 
		self.rounded_before = round_rating(self.rating_before)
 
		self.rating_after = int(tokens[9])
 
		self.rounded_after = round_rating(self.rating_after)
 

	
 
		tournament_code = tokens[5]
 
		self.date = datetime.strptime(tournament_code[1:7], "%y%m%d")
 

	
 
		self.rank_change = (rating_to_rank(self.rating_before), rating_to_rank(self.rating_after))
 

	
 
	@classmethod
 
	def parse(cls, line):
 
		tokens = re.split(r" {2,}", line.strip())
 

	
 
		if len(tokens) != 10:
 
			return None
 

	
 
		return cls(tokens, line)
 

	
 
	def __str__(self):
 
		s = self.source + "  {} -> {}".format(*self.rank_change)
 
		return s
 

	
 

	
 
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")]
 
	return tuple(columns)
 

	
 

	
 
def round_rating(r):
 
	return (r+50)//100*100
 

	
 

	
 
def rating_to_rank(rating):
 
	rank_list = [str(i)+"k" for i in range(30, 0, -1)] + [str(i)+"d" for i in range(1, 8)]
 
	key = round_rating(rating)//100
 
	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()
 

	
 
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 (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:
 
		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 = [r for r in steps if since <= r.date <= to]
 
	if steps:
 
		print("\n".join(map(str, steps)))
 
		print()
0 comments (0 inline, 0 general)