2021-03-30 19:07:59 +02:00
|
|
|
import yaml
|
2021-03-25 17:22:32 +01:00
|
|
|
|
|
|
|
class LevelSelector:
|
2021-03-30 14:27:37 +02:00
|
|
|
"""Class handling selection of next level to play, in player's place.
|
|
|
|
|
|
|
|
Needs a file with level requirements, and a file with possibly mastered tools.
|
|
|
|
If a player knows a tool, and is under time limit, they are eligible for that level.
|
|
|
|
|
|
|
|
Levels with None requirements are a "default" that anyone can take."""
|
2021-03-25 18:18:56 +01:00
|
|
|
def __init__(self, tool_list_filename, level_requirements_filename):
|
|
|
|
self.read_level_requirements(level_requirements_filename)
|
|
|
|
self.read_tool_list(tool_list_filename)
|
2021-03-25 17:22:32 +01:00
|
|
|
self.known_tools = []
|
|
|
|
|
2021-03-30 14:27:37 +02:00
|
|
|
def read_level_requirements(self, filename):
|
|
|
|
"""Reads level requirements from file `filename`."""
|
2021-03-25 17:22:32 +01:00
|
|
|
with open(filename) as f:
|
|
|
|
self.level_requirements = yaml.load(f, Loader=yaml.FullLoader)
|
|
|
|
|
|
|
|
def read_tool_list(self, filename):
|
2021-03-30 14:27:37 +02:00
|
|
|
"""Reads list of tools from file `filename`."""
|
2021-03-25 17:22:32 +01:00
|
|
|
with open(filename) as f:
|
|
|
|
self.tool_list = yaml.load(f, Loader=yaml.FullLoader)
|
|
|
|
|
|
|
|
def add_known_tool(self, tool):
|
2021-03-30 14:27:37 +02:00
|
|
|
"""Add tool `tool` to list of known tools."""
|
2021-03-25 17:22:32 +01:00
|
|
|
if tool in self.known_tools:
|
|
|
|
return
|
|
|
|
self.known_tools.append(tool)
|
|
|
|
|
|
|
|
def next_level(self, current_level, current_time):
|
2021-03-30 14:27:37 +02:00
|
|
|
"""Select what the next level should be after `current_level`."""
|
2021-03-25 18:18:56 +01:00
|
|
|
next_levels = self.level_requirements["level" + str(current_level+1)]
|
2021-03-25 17:22:32 +01:00
|
|
|
if len(next_levels) == 1:
|
|
|
|
return next_levels[0][0]
|
|
|
|
candidate_level = None
|
2021-03-25 18:18:56 +01:00
|
|
|
for level, requirements in next_levels:
|
2021-03-25 17:22:32 +01:00
|
|
|
if requirements is None:
|
|
|
|
candidate_level = level
|
2021-03-25 18:18:56 +01:00
|
|
|
elif ((requirements[0] > current_time) and (requirements[1] in self.known_tools)):
|
2021-03-25 17:22:32 +01:00
|
|
|
candidate_level = level
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
return candidate_level
|
|
|
|
|