Implemented UI #10
|
@ -51,6 +51,9 @@ class Game:
|
|||
self.__player.new_verse(text)
|
||||
elif packet_id == 3:
|
||||
self.__player.guess_incorrect()
|
||||
elif packet_id == 7:
|
||||
url = network_utilities.unpack_string(self.__server)
|
||||
self.__player.guess_partial_correct(url)
|
||||
elif packet_id == 4:
|
||||
self.__player.guess_correct()
|
||||
elif packet_id == 5:
|
||||
|
@ -125,6 +128,10 @@ class Player:
|
|||
if self.__game is not None:
|
||||
self.__ui.guess_incorrect()
|
||||
|
||||
def guess_partial_correct(self, url):
|
||||
if self.__game is not None:
|
||||
self.__ui.guess_partial_correct(url)
|
||||
|
||||
def guess_correct(self):
|
||||
if self.__game is not None:
|
||||
self.__ui.guess_correct()
|
||||
|
|
19
server.py
19
server.py
|
@ -17,6 +17,7 @@ class Game:
|
|||
|
||||
__library: Library
|
||||
__current_url: str
|
||||
__current_url_parts: list[str]
|
||||
__clients: list[Player]
|
||||
__round_points: list[int]
|
||||
__total_scores: list[int]
|
||||
|
@ -27,6 +28,7 @@ class Game:
|
|||
def __init__(self, library: Library):
|
||||
self.__library = library
|
||||
self.__current_url = ""
|
||||
self.__current_url_parts = []
|
||||
self.__clients = []
|
||||
self.__round_points = []
|
||||
self.__total_scores = []
|
||||
|
@ -58,18 +60,27 @@ class Game:
|
|||
def new_verse(self, url: str, text: str):
|
||||
for player in self.__clients:
|
||||
self.__current_url = url
|
||||
self.__current_url_parts = url.strip('/').split('/')
|
||||
player.new_verse(text)
|
||||
|
||||
def guess_reference(self, url: str, player: Player):
|
||||
if self.__active and not self.__finished:
|
||||
if url == self.__current_url:
|
||||
player.guess_correct()
|
||||
self.__round_points[self.__clients.index(player)] = 4
|
||||
for i, points in enumerate(self.__round_points):
|
||||
self.__total_scores[i] += points
|
||||
self.__clients[i].verse_guessed(
|
||||
points, self.__current_url, player.name)
|
||||
else:
|
||||
player.guess_incorrect()
|
||||
partially_correct = []
|
||||
for player_url, current_url in zip(url.strip('/').split('/'), self.__current_url_parts):
|
||||
if player_url == current_url:
|
||||
partially_correct.append(current_url)
|
||||
if partially_correct:
|
||||
player.guess_partial_correct(f"/{'/'.join(partially_correct)}")
|
||||
self.__round_points[self.__clients.index(player)] = len(partially_correct)
|
||||
else: player.guess_incorrect()
|
||||
|
||||
def end_game(self):
|
||||
self.__finished = True
|
||||
|
@ -121,6 +132,12 @@ class Player:
|
|||
data = network_utilities.pack_varint(3)
|
||||
self.__client.send(data)
|
||||
|
||||
def guess_partial_correct(self, url):
|
||||
print(f">> (7) guess_partial_correct({url})")
|
||||
data = network_utilities.pack_varint(7)
|
||||
data += network_utilities.pack_string(url)
|
||||
self.__client.send(data)
|
||||
|
||||
def guess_correct(self):
|
||||
print(">> (4) guess_correct()")
|
||||
data = network_utilities.pack_varint(4)
|
||||
|
|
14
ui.py
14
ui.py
|
@ -42,6 +42,10 @@ class UI:
|
|||
print(val, end='', flush=True)
|
||||
return None
|
||||
|
||||
def __reset(self):
|
||||
self.__buffer = ""
|
||||
print()
|
||||
|
||||
def loop(self):
|
||||
while not self.__game_over:
|
||||
self.__player.update()
|
||||
|
@ -84,6 +88,7 @@ class UI:
|
|||
print(f"{name} Joined the Game")
|
||||
|
||||
def new_verse(self, text: str):
|
||||
self.__reset()
|
||||
self.__in_game = True
|
||||
self.__verse = text
|
||||
print(self.__verse)
|
||||
|
@ -92,6 +97,11 @@ class UI:
|
|||
print("That guess was incorrect.")
|
||||
print(self.__verse)
|
||||
|
||||
def guess_partial_correct(self, url):
|
||||
try: ref = convert_url(url)
|
||||
except Exception: ref = url.upper().replace('/','.').strip('.')
|
||||
print(f"That guess was partially correct: {ref}")
|
||||
|
||||
def guess_correct(self):
|
||||
print("That guess was correct!")
|
||||
|
||||
|
@ -99,14 +109,14 @@ class UI:
|
|||
try: ref = convert_url(url)
|
||||
except Exception: ref = url.upper().replace('/','.').strip('.')
|
||||
print(
|
||||
f"The verse has been guessed by {player}.\n"
|
||||
f"\nThe verse has been guessed by {player}.\n"
|
||||
f"The reference is {ref}.\n"
|
||||
f"You have been awarded {points} points for your guess."
|
||||
)
|
||||
|
||||
def game_over(self, players: list[str], scores: list[int]):
|
||||
self.__game_over = True
|
||||
print("--- THANKS FOR PLAYING! ---")
|
||||
print("\n--- THANKS FOR PLAYING! ---")
|
||||
for player, score in players, scores:
|
||||
if player == self.__player.name:
|
||||
print(f" * {player}: {score} *")
|
||||
|
|
Loading…
Reference in New Issue