Files
PointsBot/pointsbot/level.py
Collin R 5b3a3d8f1f Clean up
2020-02-01 00:48:14 -08:00

45 lines
1.1 KiB
Python

from collections import namedtuple
### Data Structures ###
# A (string, int) tuple
Level = namedtuple('Level', 'name points')
# A ([Level], Level, Level) tuple;
# previous can be empty, and exactly one of current and next can be None
LevelInfo = namedtuple('LevelInfo', 'previous current next')
### Functions ###
def user_level_info(points, levels):
'''Return a tuple the user's previous (plural), current, and next levels.
If the user has yet to reach the first level, return ([], None, <first
level>).
If the user has reached the max level, return ([previous], <max level>,
None).
Assume levels is sorted in ascending order by points.
'''
past_levels, cur_level, next_level = [], None, None
for level in levels:
lvlname, lvlpoints = level
if points < lvlpoints:
next_level = level
break
if cur_level:
past_levels.append(cur_level)
cur_level = level
else:
next_level = None
return LevelInfo(past_levels, cur_level, next_level)
def is_max_level(level_info):
return not level_info.next