adds multiple changes after in class testing
This commit is contained in:
parent
70b3ba7806
commit
d0ff612b5a
|
@ -55,6 +55,9 @@ class Question(object):
|
||||||
}
|
}
|
||||||
return json.dumps(data)
|
return json.dumps(data)
|
||||||
|
|
||||||
|
def get_level(self):
|
||||||
|
return self._level
|
||||||
|
|
||||||
def get_uuid(self):
|
def get_uuid(self):
|
||||||
return str(self._uuid)
|
return str(self._uuid)
|
||||||
|
|
||||||
|
@ -108,18 +111,34 @@ class Question(object):
|
||||||
class Database(object):
|
class Database(object):
|
||||||
"""holds all the Question objects and methods to get new questions"""
|
"""holds all the Question objects and methods to get new questions"""
|
||||||
|
|
||||||
def __init__(self, filepath):
|
def __init__(self, filepath, level):
|
||||||
self.filepath = filepath
|
self.filepath = filepath
|
||||||
|
self.level = level
|
||||||
self._db = []
|
self._db = []
|
||||||
|
self.shuffled_db = []
|
||||||
self.setup()
|
self.setup()
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
data = csv.DictReader(open(self.filepath, "r"))
|
if self.filepath.is_dir():
|
||||||
for row in data:
|
for filepath in self.filepath.glob("*.csv"):
|
||||||
self._db.append(Question(row))
|
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):
|
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):
|
def get_question_by_uuid(self, uuid):
|
||||||
question = [question for question in self._db if question.get_uuid() == 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._filepath = pathlib.Path(filepath)
|
||||||
self._players = []
|
self._players = []
|
||||||
self.setup_players()
|
self.setup_players()
|
||||||
|
self.shuffled_list = []
|
||||||
|
|
||||||
def setup_players(self):
|
def setup_players(self):
|
||||||
with open(self._filepath, "r") as fp:
|
with open(self._filepath, "r") as fp:
|
||||||
|
@ -144,7 +164,13 @@ class Game(object):
|
||||||
self.add_player(line.strip())
|
self.add_player(line.strip())
|
||||||
|
|
||||||
def get_random_player(self):
|
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):
|
def add_player(self, name):
|
||||||
player = Player(name)
|
player = Player(name)
|
||||||
|
|
|
@ -6,5 +6,4 @@ Hawai
|
||||||
Hugo
|
Hugo
|
||||||
Jonathan
|
Jonathan
|
||||||
Nicolas
|
Nicolas
|
||||||
Laurant
|
Laurent
|
||||||
Wouter
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import random
|
||||||
import time
|
import time
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.markdown import Markdown
|
from rich.markdown import Markdown
|
||||||
|
from rich.table import Table
|
||||||
from ccpq.lib_ccpq import Question, Database, Game
|
from ccpq.lib_ccpq import Question, Database, Game
|
||||||
|
|
||||||
MSG = {
|
MSG = {
|
||||||
|
@ -23,6 +24,12 @@ MSG = {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DIFFICULTY = {
|
||||||
|
"easy": "010-160",
|
||||||
|
"medium": "101-500",
|
||||||
|
"hard": "102-500",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Tui(object):
|
class Tui(object):
|
||||||
def __init__(self):
|
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 = "{}, you have {} out of {} right!\n".format(stats[0], stats[1], stats[1] + stats[2])
|
||||||
md = Markdown(md)
|
md = Markdown(md)
|
||||||
self._console.print(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)
|
md = Markdown(md)
|
||||||
self._console.print(md)
|
self._console.print(md)
|
||||||
input()
|
input()
|
||||||
|
|
||||||
def goodbye(self):
|
def goodbye(self):
|
||||||
md = Markdown("# Goodbye!")
|
md = Markdown("# Goodbye!")
|
||||||
self._console.print(md)
|
self._console.print(md)
|
||||||
|
|
||||||
|
|
||||||
class Application(object):
|
class Application(object):
|
||||||
def __init__(self, filepath_csv, filepath_players, interface, number):
|
def __init__(self, filepath_csv, filepath_players, interface, number, level):
|
||||||
self._db = Database(filepath_csv)
|
self._db = Database(filepath_csv, level)
|
||||||
self._number = number
|
self._number = number
|
||||||
self._session = Game(filepath_players)
|
self._session = Game(filepath_players)
|
||||||
self._interface = interface
|
self._interface = interface
|
||||||
|
@ -128,11 +151,16 @@ class Application(object):
|
||||||
self._interface.show_success(stat)
|
self._interface.show_success(stat)
|
||||||
self._interface.show_response(question)
|
self._interface.show_response(question)
|
||||||
self._interface.show_explanation(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._number -= 1
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
|
players = self._session.get_all_players()
|
||||||
|
self._interface.show_game_stats(players)
|
||||||
self._interface.goodbye()
|
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("-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=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("-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()
|
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() or not filepath_players.exists():
|
if not filepath_csv.exists() or not filepath_players.exists():
|
||||||
print("no such file!")
|
print("no such file!")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
try:
|
||||||
|
level = DIFFICULTY[args.difficulty]
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
level = DIFFICULTY["easy"]
|
||||||
interface = Tui()
|
interface = Tui()
|
||||||
app = Application(filepath_csv, filepath_players, interface, args.number)
|
app = Application(filepath_csv, filepath_players, interface, args.number, level)
|
||||||
try:
|
try:
|
||||||
app.run()
|
app.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
Loading…
Reference in New Issue