some improvments to the logic and startup

This commit is contained in:
waldek 2021-10-14 00:07:26 +02:00
parent 71177c1aa3
commit 199ddd82dc
2 changed files with 65 additions and 20 deletions

View File

@ -34,7 +34,7 @@ class Question(object):
self._question = self._data[QUESTION].strip() self._question = self._data[QUESTION].strip()
self._answers = self._data[ANSWER].strip().split(" ") self._answers = self._data[ANSWER].strip().split(" ")
self._answers = [x for x in self._answers if x] self._answers = [x for x in self._answers if x]
self._create_list_of_possibilities() self._possibilities = self._create_list_of_possibilities()
try: try:
self._explication = self._data[EXPLICATION].strip() self._explication = self._data[EXPLICATION].strip()
except: except:
@ -73,7 +73,7 @@ class Question(object):
if key.isnumeric(): if key.isnumeric():
possibilities.append(self._data[key]) possibilities.append(self._data[key])
possibilities = [x for x in possibilities if x] # hack to remove empty objects possibilities = [x for x in possibilities if x] # hack to remove empty objects
self._possibilities = possibilities return possibilities
def get_possibilities(self): def get_possibilities(self):
return self._possibilities return self._possibilities
@ -201,6 +201,9 @@ class Player(object):
self.name = name self.name = name
self.stats = Stats() self.stats = Stats()
def __str__(self):
return self.name
def get_stats(self): def get_stats(self):
return self.name, self.stats.get_right(), self.stats.get_wrong() return self.name, self.stats.get_right(), self.stats.get_wrong()

View File

@ -30,7 +30,7 @@ DIFFICULTY = {
"hard": "102-500", "hard": "102-500",
} }
DIFFICULTY_SCALE = [ LEVELS = [
"010-160", "010-160",
"101-400", "101-400",
"101-500", "101-500",
@ -48,25 +48,25 @@ class Tui(object):
os.system("clear") os.system("clear")
md = Markdown("# {}".format(question.get_question())) md = Markdown("# {}".format(question.get_question()))
self._console.print(md) self._console.print(md)
md = Markdown("level: {}".format(question.get_level()))
self._console.print(md)
md = "" md = ""
for possibility in question.get_possibilities(): for possibility in question.get_possibilities():
md += "1. {}\n".format(possibility) md += "1. {}\n".format(possibility)
md = Markdown(md) md = Markdown(md)
self._console.print(md) self._console.print(md)
def prompt_for_answer(self): def prompt_for_answer(self, name):
#md = Markdown("What's your answer?") #md = Markdown("What's your answer?")
#self._console.print(md) #self._console.print(md)
answer = self._parse_input() answer = self._parse_input(name)
return answer 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 = [] 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() results = results.split()
for result in results: for result in results:
if result.isdigit(): if result.isdigit():
@ -149,10 +149,11 @@ class Application(object):
def run(self): def run(self):
while self._number > 0: while self._number > 0:
player = self._session.get_random_player() player = self._session.get_random_player()
if len(self._session.get_all_players()) > 1:
self._interface.signal_player(player) self._interface.signal_player(player)
question = self._db.get_question() question = self._db.get_question()
self._interface.ask_question(question) self._interface.ask_question(question)
answer = self._interface.prompt_for_answer() answer = self._interface.prompt_for_answer(player)
stat = question.verify(answer) stat = question.verify(answer)
player.stats.update_stats(stat) player.stats.update_stats(stat)
self._interface.show_success(stat) self._interface.show_success(stat)
@ -171,23 +172,64 @@ class Application(object):
self._interface.goodbye() 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__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", required=True, help="file to use as database", action="store") parser.add_argument(
parser.add_argument("-p", "--players", required=False, default="", help="list of players", action="store") "-f",
parser.add_argument("-n", "--number", default=10, help="number of questions to ask", type=int, action="store") "--file",
parser.add_argument("-d", "--difficulty", default="easy", help="easy, medium or hard", type=str, action="store") 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() args = parser.parse_args()
filepath_csv = pathlib.Path(args.file) filepath_csv = pathlib.Path(args.file)
filepath_players = pathlib.Path(args.players) filepath_players = pathlib.Path(args.players)
if not filepath_csv.exists(): if not filepath_csv.exists():
print("no such file or directory!") print("no such file or directory!")
exit(1) exit(1)
try:
level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY[args.difficulty]) + 1] level = level_logic(args.level, args.difficulty)
except Exception as e:
print(e)
level = DIFFICULTY_SCALE[0:DIFFICULTY_SCALE.index(DIFFICULTY["easy"])]
interface = Tui() interface = Tui()
app = Application(filepath_csv, filepath_players, interface, args.number, level) app = Application(filepath_csv, filepath_players, interface, args.number, level)
try: try: