From 199ddd82dcfd1a218bc49ef9d20b281b8aedef81 Mon Sep 17 00:00:00 2001 From: waldek Date: Thu, 14 Oct 2021 00:07:26 +0200 Subject: [PATCH] some improvments to the logic and startup --- ccpq/lib_ccpq.py | 7 +++-- standalone_tui.py | 78 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/ccpq/lib_ccpq.py b/ccpq/lib_ccpq.py index 8cd2f96..8e865fc 100644 --- a/ccpq/lib_ccpq.py +++ b/ccpq/lib_ccpq.py @@ -34,7 +34,7 @@ class Question(object): self._question = self._data[QUESTION].strip() self._answers = self._data[ANSWER].strip().split(" ") self._answers = [x for x in self._answers if x] - self._create_list_of_possibilities() + self._possibilities = self._create_list_of_possibilities() try: self._explication = self._data[EXPLICATION].strip() except: @@ -73,7 +73,7 @@ class Question(object): if key.isnumeric(): possibilities.append(self._data[key]) possibilities = [x for x in possibilities if x] # hack to remove empty objects - self._possibilities = possibilities + return possibilities def get_possibilities(self): return self._possibilities @@ -201,6 +201,9 @@ class Player(object): self.name = name self.stats = Stats() + def __str__(self): + return self.name + def get_stats(self): return self.name, self.stats.get_right(), self.stats.get_wrong() diff --git a/standalone_tui.py b/standalone_tui.py index 641f86e..92d59c3 100755 --- a/standalone_tui.py +++ b/standalone_tui.py @@ -30,7 +30,7 @@ DIFFICULTY = { "hard": "102-500", } -DIFFICULTY_SCALE = [ +LEVELS = [ "010-160", "101-400", "101-500", @@ -48,25 +48,25 @@ class Tui(object): os.system("clear") md = Markdown("# {}".format(question.get_question())) self._console.print(md) + md = Markdown("level: {}".format(question.get_level())) + self._console.print(md) md = "" for possibility in question.get_possibilities(): md += "1. {}\n".format(possibility) md = Markdown(md) self._console.print(md) - def prompt_for_answer(self): + def prompt_for_answer(self, name): #md = Markdown("What's your answer?") #self._console.print(md) - answer = self._parse_input() + answer = self._parse_input(name) return answer - def _parse_input(self): + def _parse_input(self, name): """ - TODO make it adapt to questions with multiple choices and fill the - blank """ answers = [] - results = input("\n What's your answer? (only numbers, separated by a SPACE) ") + results = input("\n {}, what's your answer? (only numbers, separated by a SPACE) ".format(name)) results = results.split() for result in results: if result.isdigit(): @@ -149,10 +149,11 @@ class Application(object): def run(self): while self._number > 0: player = self._session.get_random_player() - self._interface.signal_player(player) + if len(self._session.get_all_players()) > 1: + self._interface.signal_player(player) question = self._db.get_question() self._interface.ask_question(question) - answer = self._interface.prompt_for_answer() + answer = self._interface.prompt_for_answer(player) stat = question.verify(answer) player.stats.update_stats(stat) self._interface.show_success(stat) @@ -171,23 +172,64 @@ class Application(object): self._interface.goodbye() +def level_logic(level, difficulty): + if level is None and difficulty is None: + level = "lpic1_part1" + elif level is not None: + level = LEVELS[0:LEVELS.index(DIFFICULTY[level]) + 1] + elif difficulty is not None: + level = LEVELS[0:LEVELS.index(DIFFICULTY[difficulty]) + 1] + return level + + 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=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") + parser.add_argument( + "-f", + "--file", + required=False, + default="data", + help="path to specific CSV file or directory", + action="store" + ) + parser.add_argument( + "-p", + "--players", + required=False, + default="", + help="file with list of players", + action="store" + ) + parser.add_argument( + "-n", + "--number", + default=20, + help="number of questions to ask", + type=int, + action="store" + ) + group_level = parser.add_mutually_exclusive_group() + group_level.add_argument( + "-d", + "--difficulty", + choices=DIFFICULTY.keys(), + help="easy, medium or hard" + ) + group_level.add_argument( + "-l", + "--level", + choices=LEVELS, + help="LPI level to test" + ) args = parser.parse_args() + filepath_csv = pathlib.Path(args.file) filepath_players = pathlib.Path(args.players) if not filepath_csv.exists(): print("no such file or directory!") exit(1) - try: - level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY[args.difficulty]) + 1] - except Exception as e: - print(e) - level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY["easy"])] + + level = level_logic(args.level, args.difficulty) interface = Tui() app = Application(filepath_csv, filepath_players, interface, args.number, level) try: