From 8662634ff5987cb9ce53dc050c04e7f281688e4b Mon Sep 17 00:00:00 2001 From: waldek Date: Mon, 11 Oct 2021 23:21:57 +0200 Subject: [PATCH] fixes some minor bugs --- ccpq/lib_ccpq.py | 15 ++++++++------- standalone_tui.py | 21 ++++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ccpq/lib_ccpq.py b/ccpq/lib_ccpq.py index 5452e55..8cd2f96 100644 --- a/ccpq/lib_ccpq.py +++ b/ccpq/lib_ccpq.py @@ -29,7 +29,6 @@ class Question(object): def _clean_data(self): """ TODO needs quite bit of actual cleanup to make the parsing more robust - TODO needs a 'private' variable for issue 5 """ self._level = self._data[LEVEL].strip() self._question = self._data[QUESTION].strip() @@ -44,7 +43,6 @@ class Question(object): def dump_json(self): """ dumps all data to JSON for the REST API - TODO needs a key to include the date for issue 5 """ data = { UUID: self.get_uuid(), @@ -125,7 +123,7 @@ class Database(object): for row in data: try: question = Question(row) - if question.get_level() == self.level: + if question.get_level() in self.level: self._db.append(question) except Exception as e: print(e) @@ -158,10 +156,13 @@ class Game(object): self.shuffled_list = [] def setup_players(self): - with open(self._filepath, "r") as fp: - lines = fp.readlines() - for line in lines: - self.add_player(line.strip()) + if not self._filepath.is_file(): + self.add_player("Hans Solo") + else: + with open(self._filepath, "r") as fp: + lines = fp.readlines() + for line in lines: + self.add_player(line.strip()) def get_random_player(self): if len(self.shuffled_list) == 0: diff --git a/standalone_tui.py b/standalone_tui.py index 4c8cb16..641f86e 100755 --- a/standalone_tui.py +++ b/standalone_tui.py @@ -30,6 +30,14 @@ DIFFICULTY = { "hard": "102-500", } +DIFFICULTY_SCALE = [ + "010-160", + "101-400", + "101-500", + "102-400", + "102-500", + ] + class Tui(object): def __init__(self): @@ -131,14 +139,13 @@ class Tui(object): class Application(object): def __init__(self, filepath_csv, filepath_players, interface, number, level): self._db = Database(filepath_csv, level) - self._number = number self._session = Game(filepath_players) + self._number = number * len(self._session.get_all_players()) self._interface = interface def start(self): pass - def run(self): while self._number > 0: player = self._session.get_random_player() @@ -167,20 +174,20 @@ class Application(object): if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-f", "--file", required=True, help="file to use as database", action="store") - parser.add_argument("-p", "--players", required=True, help="list of players", action="store") + parser.add_argument("-p", "--players", required=False, default="", help="list of players", action="store") parser.add_argument("-n", "--number", default=10, help="number of questions to ask", type=int, action="store") parser.add_argument("-d", "--difficulty", default="easy", help="easy, medium or hard", type=str, action="store") args = parser.parse_args() filepath_csv = pathlib.Path(args.file) filepath_players = pathlib.Path(args.players) - if not filepath_csv.exists() or not filepath_players.exists(): - print("no such file!") + if not filepath_csv.exists(): + print("no such file or directory!") exit(1) try: - level = DIFFICULTY[args.difficulty] + level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY[args.difficulty]) + 1] except Exception as e: print(e) - level = DIFFICULTY["easy"] + level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY["easy"])] interface = Tui() app = Application(filepath_csv, filepath_players, interface, args.number, level) try: