2021-05-23 16:04:09 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import csv
|
|
|
|
import random
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.markdown import Markdown
|
|
|
|
|
|
|
|
from ccpq.lib_ccpq import Question, Database, Game
|
|
|
|
|
2021-05-24 15:38:11 +02:00
|
|
|
MSG = {
|
|
|
|
True: [
|
|
|
|
"Yes!",
|
|
|
|
"Good one!",
|
|
|
|
"Super!",
|
|
|
|
"Excellent job!"
|
|
|
|
],
|
|
|
|
False: [
|
|
|
|
"Sorry, that's wrong",
|
|
|
|
"No, that's not right",
|
|
|
|
"Damn it! That's not the right answer",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-23 16:04:09 +02:00
|
|
|
class Tui(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._console = Console()
|
|
|
|
self._stats = []
|
|
|
|
|
|
|
|
def ask_question(self, question):
|
2021-05-24 15:38:11 +02:00
|
|
|
os.system("clear")
|
2021-05-23 16:04:09 +02:00
|
|
|
md = Markdown("# {}".format(question.get_question()))
|
|
|
|
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):
|
2021-05-24 15:38:11 +02:00
|
|
|
#md = Markdown("What's your answer?")
|
|
|
|
#self._console.print(md)
|
2021-05-23 16:04:09 +02:00
|
|
|
answer = self._parse_input()
|
|
|
|
return answer
|
|
|
|
|
|
|
|
def _parse_input(self):
|
|
|
|
"""
|
|
|
|
TODO make it adapt to questions with multiple choices and fill the
|
|
|
|
blank
|
|
|
|
"""
|
2021-05-24 15:38:11 +02:00
|
|
|
answers = []
|
2021-05-24 16:44:18 +02:00
|
|
|
results = input("\n What's your answer? (only numbers, separated by a SPACE) ")
|
2021-05-24 15:38:11 +02:00
|
|
|
results = results.split()
|
|
|
|
for result in results:
|
2021-05-23 16:04:09 +02:00
|
|
|
if result.isdigit():
|
2021-05-24 15:38:11 +02:00
|
|
|
answers.append(result)
|
|
|
|
else:
|
|
|
|
md = Markdown("**only digits please**")
|
|
|
|
self._console.print(md)
|
|
|
|
return answers
|
2021-05-23 16:04:09 +02:00
|
|
|
|
|
|
|
def show_response(self, question):
|
|
|
|
answers = question.get_right_answers()
|
|
|
|
if len(answers) == 1:
|
|
|
|
answer = answers[0]
|
|
|
|
md = Markdown("### The right answer is: {}".format(answer))
|
|
|
|
else:
|
|
|
|
md = "### The right answers are:\n"
|
|
|
|
for answer in answers:
|
|
|
|
md += "* {}\n".format(answer)
|
|
|
|
md = Markdown(md)
|
|
|
|
self._console.print(md)
|
|
|
|
|
2021-05-24 15:38:11 +02:00
|
|
|
def show_success(self, success):
|
|
|
|
md = "# {}".format(random.choice(MSG[success]))
|
|
|
|
md = Markdown(md)
|
|
|
|
self._console.print(md)
|
|
|
|
|
2021-05-23 16:04:09 +02:00
|
|
|
def show_stats(self, stats):
|
|
|
|
md = "### you have {} out of {} right!".format(stats[0], stats[2])
|
|
|
|
md = Markdown(md)
|
|
|
|
self._console.print(md)
|
|
|
|
md = "### press **enter** to get a new question or **CTRL-C** to quit"
|
|
|
|
md = Markdown(md)
|
|
|
|
self._console.print(md)
|
|
|
|
input()
|
|
|
|
|
|
|
|
def goodbye(self):
|
|
|
|
md = Markdown("# Goodbye!")
|
|
|
|
self._console.print(md)
|
|
|
|
|
|
|
|
|
|
|
|
class Application(object):
|
|
|
|
def __init__(self, filepath, interface):
|
|
|
|
self._db = Database(filepath)
|
|
|
|
self._session = Game()
|
|
|
|
self._interface = interface
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
question = self._db.get_question()
|
|
|
|
self._interface.ask_question(question)
|
|
|
|
answer = self._interface.prompt_for_answer()
|
|
|
|
stat = question.verify(answer)
|
|
|
|
self._session.update_stats(stat)
|
2021-05-24 15:38:11 +02:00
|
|
|
self._interface.show_success(stat)
|
2021-05-23 16:04:09 +02:00
|
|
|
self._interface.show_response(question)
|
|
|
|
self._interface.show_stats(self._session.get_stats())
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
self._interface.goodbye()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-24 16:44:18 +02:00
|
|
|
filepath = pathlib.Path("./data/list_book1.csv")
|
2021-05-23 16:04:09 +02:00
|
|
|
interface = Tui()
|
|
|
|
app = Application(filepath, interface)
|
|
|
|
try:
|
|
|
|
app.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
app.quit()
|