Changeset - 4ade6373edba
[Not reviewed]
default
0 1 0
Laman - 3 years ago 2021-10-20 14:26:47

created a main() function
1 file changed with 18 insertions and 14 deletions:
0 comments (0 inline, 0 general)
rank_progress.py
Show inline comments
 
@@ -45,87 +45,91 @@ class RankTracker:
 
	@property
 
	def rounded_rating(self):
 
		return self._rounded_rating
 

	
 
	def update(self, rating):
 
		assert rating >= -900
 
		rounded_rating = round_rating(rating)
 
		old_rank = self.rank
 

	
 
		if rounded_rating == self._rounded_rating:
 
			pass
 
		elif rounded_rating > self._rounded_rating:  # promotion
 
			# when pushing the best by 2 and more ranks you have to cross the hundred, not just the fifty
 
			if rounded_rating >= self._best+200 and rating < rounded_rating:
 
				self._rounded_rating = rounded_rating-100
 
			else:
 
				self._rounded_rating = rounded_rating
 
		else:  # demotion
 
			# 100 points for 5k and better
 
			if self._rounded_rating >= 1600 and self._rounded_rating - rating > 100:
 
				self._rounded_rating = rounded_rating
 
			# 150 points for the others
 
			elif self._rounded_rating - rating > 150:
 
				self._rounded_rating = rounded_rating+100
 
			else:
 
				pass
 

	
 
		self._best = max(self._best, self._rounded_rating)
 
		new_rank = self.rank
 
		return new_rank if new_rank != old_rank else 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")]
 
	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)]
 

	
 

	
 
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")
 

	
 
	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()
 

	
 
	with open("/tmp/all.hst") as f:
 
		s = f.read()
 

	
 
def main(s, since, to, args):
 
	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)
 
		tracker = RankTracker(tourneys[0].rating_before)
 
		steps = []
 

	
 
		for r in tourneys:
 
			# omit reset ratings
 
			if tracker.rounded_rating != round_rating(r.rating_before):
 
				tracker = RankTracker(r.rating_before)
 

	
 
			old_rank = tracker.rank
 
			new_rank = tracker.update(r.rating_after)
 
			if new_rank is not False:
 
				r.rank_change = (old_rank, new_rank)
 
				steps.append(r)
 

	
 
		steps = [r for r in steps if since <= r.date <= to]
 
		if steps:
 
			print("\n".join(map(str, steps)))
 
			print()
 

	
 

	
 
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")
 

	
 
	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()
 

	
 
	with open("/tmp/all.hst") as f:
 
		s = f.read()
 

	
 
	main(s, since, to, args)
 
\ No newline at end of file
0 comments (0 inline, 0 general)