implements player logic
This commit is contained in:
parent
088ce7e055
commit
70b3ba7806
|
@ -132,6 +132,27 @@ class Database(object):
|
|||
|
||||
|
||||
class Game(object):
|
||||
def __init__(self, filepath):
|
||||
self._filepath = pathlib.Path(filepath)
|
||||
self._players = []
|
||||
self.setup_players()
|
||||
|
||||
def setup_players(self):
|
||||
with open(self._filepath, "r") as fp:
|
||||
lines = fp.readlines()
|
||||
for line in lines:
|
||||
self.add_player(line.strip())
|
||||
|
||||
def get_random_player(self):
|
||||
return random.choice(self._players)
|
||||
|
||||
def add_player(self, name):
|
||||
player = Player(name)
|
||||
self._players.append(player)
|
||||
|
||||
|
||||
class Stats(object):
|
||||
"""TODO placeholder for the stats class"""
|
||||
def __init__(self):
|
||||
self._stats = []
|
||||
|
||||
|
@ -139,20 +160,22 @@ class Game(object):
|
|||
self._stats.append(stat)
|
||||
|
||||
def get_stats(self):
|
||||
right = self._stats.count(True)
|
||||
wrong = self._stats.count(False)
|
||||
total = len(self._stats)
|
||||
return (right, wrong, total)
|
||||
return self.get_right(), self.get_wrong(), len(self._stats)
|
||||
|
||||
def get_wrong(self):
|
||||
return self._stats.count(False)
|
||||
|
||||
class Stats(object):
|
||||
"""TODO placeholder for the stats class"""
|
||||
pass
|
||||
def get_right(self):
|
||||
return self._stats.count(True)
|
||||
|
||||
|
||||
class Player(object):
|
||||
"""TODO placeholder for the player class"""
|
||||
pass
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.stats = Stats()
|
||||
|
||||
def get_stats(self):
|
||||
return self.name, self.stats.get_right(), self.stats.get_wrong()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
Abdellah
|
||||
David
|
||||
Sarah
|
||||
Selçuk
|
||||
Hawai
|
||||
Hugo
|
||||
Jonathan
|
||||
Nicolas
|
||||
Laurant
|
||||
Wouter
|
|
@ -4,6 +4,7 @@ import pathlib
|
|||
import argparse
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
from rich.console import Console
|
||||
from rich.markdown import Markdown
|
||||
from ccpq.lib_ccpq import Question, Database, Game
|
||||
|
@ -60,6 +61,14 @@ class Tui(object):
|
|||
self._console.print(md)
|
||||
return answers
|
||||
|
||||
def signal_player(self, player):
|
||||
os.system("clear")
|
||||
md = "# {}".format(player.name)
|
||||
md = Markdown(md)
|
||||
self._console.print(md)
|
||||
time.sleep(2)
|
||||
os.system("clear")
|
||||
|
||||
def show_response(self, question):
|
||||
answers = question.get_right_answers()
|
||||
if len(answers) == 1:
|
||||
|
@ -83,7 +92,7 @@ class Tui(object):
|
|||
self._console.print(md)
|
||||
|
||||
def show_stats(self, stats):
|
||||
md = "### you have {} out of {} right!".format(stats[0], stats[2])
|
||||
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"
|
||||
|
@ -97,10 +106,10 @@ class Tui(object):
|
|||
|
||||
|
||||
class Application(object):
|
||||
def __init__(self, filepath, interface, number):
|
||||
self._db = Database(filepath)
|
||||
def __init__(self, filepath_csv, filepath_players, interface, number):
|
||||
self._db = Database(filepath_csv)
|
||||
self._number = number
|
||||
self._session = Game()
|
||||
self._session = Game(filepath_players)
|
||||
self._interface = interface
|
||||
|
||||
def start(self):
|
||||
|
@ -109,15 +118,17 @@ class Application(object):
|
|||
|
||||
def run(self):
|
||||
while self._number > 0:
|
||||
player = self._session.get_random_player()
|
||||
self._interface.signal_player(player)
|
||||
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)
|
||||
player.stats.update_stats(stat)
|
||||
self._interface.show_success(stat)
|
||||
self._interface.show_response(question)
|
||||
self._interface.show_explanation(question) # will work once issue 5 is addressed
|
||||
self._interface.show_stats(self._session.get_stats())
|
||||
self._interface.show_explanation(question)
|
||||
self._interface.show_stats(player.get_stats())
|
||||
self._number -= 1
|
||||
self.quit()
|
||||
|
||||
|
@ -128,14 +139,16 @@ class Application(object):
|
|||
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=True, help="list of players", action="store")
|
||||
parser.add_argument("-n", "--number", default=10, help="number of questions to ask", type=int, action="store")
|
||||
args = parser.parse_args()
|
||||
filepath = pathlib.Path(args.file)
|
||||
if not filepath.exists():
|
||||
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)
|
||||
interface = Tui()
|
||||
app = Application(filepath, interface, args.number)
|
||||
app = Application(filepath_csv, filepath_players, interface, args.number)
|
||||
try:
|
||||
app.run()
|
||||
except KeyboardInterrupt:
|
||||
|
|
Loading…
Reference in New Issue