adds multiple changes after in class testing

This commit is contained in:
waldek 2021-06-18 15:42:02 +02:00
parent 70b3ba7806
commit d0ff612b5a
3 changed files with 73 additions and 14 deletions

View File

@ -55,6 +55,9 @@ class Question(object):
}
return json.dumps(data)
def get_level(self):
return self._level
def get_uuid(self):
return str(self._uuid)
@ -108,18 +111,34 @@ class Question(object):
class Database(object):
"""holds all the Question objects and methods to get new questions"""
def __init__(self, filepath):
def __init__(self, filepath, level):
self.filepath = filepath
self.level = level
self._db = []
self.shuffled_db = []
self.setup()
def setup(self):
data = csv.DictReader(open(self.filepath, "r"))
for row in data:
self._db.append(Question(row))
if self.filepath.is_dir():
for filepath in self.filepath.glob("*.csv"):
data = csv.DictReader(open(filepath, "r"))
for row in data:
try:
question = Question(row)
if question.get_level() == self.level:
self._db.append(question)
except Exception as e:
print(e)
else:
data = csv.DictReader(open(self.filepath, "r"))
for row in data:
self._db.append(Question(row))
def get_question(self):
return random.choice(self._db)
if len(self.shuffled_db) == 0:
self.shuffled_db = list(self._db)
random.shuffle(self.shuffled_db)
return self.shuffled_db.pop()
def get_question_by_uuid(self, uuid):
question = [question for question in self._db if question.get_uuid() == uuid]
@ -136,6 +155,7 @@ class Game(object):
self._filepath = pathlib.Path(filepath)
self._players = []
self.setup_players()
self.shuffled_list = []
def setup_players(self):
with open(self._filepath, "r") as fp:
@ -144,7 +164,13 @@ class Game(object):
self.add_player(line.strip())
def get_random_player(self):
return random.choice(self._players)
if len(self.shuffled_list) == 0:
self.shuffled_list = list(self._players)
random.shuffle(self.shuffled_list)
return self.shuffled_list.pop()
def get_all_players(self):
return self._players
def add_player(self, name):
player = Player(name)

View File

@ -6,5 +6,4 @@ Hawai
Hugo
Jonathan
Nicolas
Laurant
Wouter
Laurent

View File

@ -7,6 +7,7 @@ import random
import time
from rich.console import Console
from rich.markdown import Markdown
from rich.table import Table
from ccpq.lib_ccpq import Question, Database, Game
MSG = {
@ -23,6 +24,12 @@ MSG = {
]
}
DIFFICULTY = {
"easy": "010-160",
"medium": "101-500",
"hard": "102-500",
}
class Tui(object):
def __init__(self):
@ -95,19 +102,35 @@ class Tui(object):
md = "{}, you have {} out of {} right!\n".format(stats[0], stats[1], stats[1] + stats[2])
md = Markdown(md)
self._console.print(md)
md = "### press **enter** to get a new question or **CTRL-C** to quit"
md = "### press **enter** to get a new question, s for stats and **CTRL-C** to quit"
md = Markdown(md)
self._console.print(md)
result = input()
return result
def show_game_stats(self, players):
os.system("clear")
table = Table("Player")
table.add_column("Right")
table.add_column("Wrong")
table.add_column("Total")
for player in players:
name, right, wrong = player.get_stats()
table.add_row(str(name), str(right), str(wrong), str(right + wrong))
self._console.print(table)
md = "### press **enter** to continue"
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_csv, filepath_players, interface, number):
self._db = Database(filepath_csv)
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._interface = interface
@ -128,11 +151,16 @@ class Application(object):
self._interface.show_success(stat)
self._interface.show_response(question)
self._interface.show_explanation(question)
self._interface.show_stats(player.get_stats())
result = self._interface.show_stats(player.get_stats())
if result.startswith("s"):
players = self._session.get_all_players()
self._interface.show_game_stats(players)
self._number -= 1
self.quit()
def quit(self):
players = self._session.get_all_players()
self._interface.show_game_stats(players)
self._interface.goodbye()
@ -141,14 +169,20 @@ if __name__ == "__main__":
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("-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!")
exit(1)
try:
level = DIFFICULTY[args.difficulty]
except Exception as e:
print(e)
level = DIFFICULTY["easy"]
interface = Tui()
app = Application(filepath_csv, filepath_players, interface, args.number)
app = Application(filepath_csv, filepath_players, interface, args.number, level)
try:
app.run()
except KeyboardInterrupt: