Changeset - 2a3c04d47300
[Not reviewed]
default
0 2 0
Laman - 3 years ago 2021-10-20 11:50:32

limited promotion when pushing the personal best
2 files changed with 9 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rank_progress.py
Show inline comments
 
@@ -27,50 +27,58 @@ class Record:
 

	
 
		return cls(tokens, line)
 

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

	
 

	
 
class RankTracker:
 
	def __init__(self, rating):
 
		assert rating >= -900
 
		self._rounded_rating = round_rating(rating)
 
		self._best = self._rounded_rating
 

	
 
	@property
 
	def rank(self):
 
		return rating_to_rank(self._rounded_rating)
 

	
 
	@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
test.py
Show inline comments
 
@@ -9,25 +9,25 @@ class TestRankTracker(TestCase):
 
		for r in [1945, 1949, 1900, 1850]:
 
			self.assertFalse(t.update(r), r)
 

	
 
	def test_promotion(self):
 
		# basic
 
		t = RankTracker(1945)
 
		for (rating, rank) in [(1950, "1k"), (1945, False), (2000, False), (2049, False), (2051, "1d")]:
 
			self.assertEqual(t.update(rating), rank, (rating, rank))
 

	
 
		# 2+ ranks leaps
 

	
 
		# limited when pushing the personal best
 
		self.skipTest("not yet implemented")
 
		# self.skipTest("not yet implemented")
 
		t = RankTracker(1100)
 
		self.assertEqual(t.update(1299), "9k")
 
		self.assertEqual(t.update(1250), "8k")
 
		self.assertEqual(t.update(1500), "6k")
 

	
 
		# regular below the personal best
 
		t = RankTracker(1201)  # 9k
 
		t.update(1000)
 
		t.update(900)
 
		t.update(849)  # 11k
 
		self.assertEqual(t.update(1250), "8k")
 

	
0 comments (0 inline, 0 general)