1
0
Fork 0
This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
adaptive-game-assistant/adaptive_game_module/flag_checker.py

31 lines
1.0 KiB
Python
Raw Normal View History

2021-03-30 19:08:09 +02:00
import yaml
class FlagChecker:
2021-05-28 12:09:46 +02:00
"""Class that checks if flags are correct, and also remembers wrong flags.
Needs a file with the flags to levels written in."""
2021-03-30 19:08:09 +02:00
def __init__(self, filename):
self.wrong_flags = {}
self.read_keys(filename)
def read_keys(self, filename):
2021-05-28 12:09:46 +02:00
"""Reads levels and flags from file `filename`"""
2021-03-30 19:08:09 +02:00
with open(filename) as f:
self.flags = yaml.load(f, Loader=yaml.FullLoader)
def check_flag(self, level, flag):
2021-05-28 12:09:46 +02:00
"""Checks if `flag` is a correct flag for `level`.
If it isn't, it adds `flag` to the list of incorrect flags."""
2021-03-30 19:08:09 +02:00
if self.flags[level] == flag:
return True
else:
if not level in self.wrong_flags:
self.wrong_flags[level] = []
self.wrong_flags[level].append(flag)
return False
def log_to_file(self, filename):
2021-05-28 12:09:46 +02:00
"""Logs everything to a file."""
2021-03-30 19:08:09 +02:00
with open(filename, 'a') as f:
yaml.dump(self.wrong_flags, f)