From 109efe66c590f70474e163390684ead2b638c020 Mon Sep 17 00:00:00 2001 From: Kyler Date: Thu, 24 Apr 2025 22:08:33 -0600 Subject: [PATCH] Implement name generation for players and enhance UI feedback on player actions --- main.py | 46 +++++++++++++++++++++++++++++++++++++++++-- server.py | 58 +++++++++++++++++++++++++++++++------------------------ ui.py | 9 +++++++-- 3 files changed, 84 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 84aee3c..10fbcf6 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,46 @@ -import argparse + +import random + +def name_gen(): + adjectives = [ + "able", "active", "adaptable", "adventurous", "agreeable", "alert", + "amazing", "amiable", "ample", "artistic", "attractive", "balanced", + "beautiful", "blissful", "bold", "brave", "bright", "brilliant", + "bubbly", "calm", "capable", "careful", "charming", "cheerful", "clean", + "clear", "clever", "colorful", "comfortable", "compassionate", + "confident", "considerate", "cool", "cooperative", "courageous", + "creative", "cultured", "cute", "daring", "decent", "delightful", + "detailed", "determined", "dignified", "disciplined", "dynamic", + "eager", "easygoing", "elegant", "energetic", "engaging", + "enthusiastic", "excellent", "exciting", "expressive", "fair", + "faithful", "fancy", "fascinating", "flexible", "focused", "friendly", + "fun", "funny", "generous", "gentle", "genuine", "gifted", "glad", + "gleaming", "good", "graceful", "gracious", "great", "handsome", + "happy", "harmonious", "helpful", "honest", "hopeful", "humble", + "imaginative", "impressive", "independent", "innocent", "inspiring", + "intelligent", "interesting", "intuitive", "jolly", "jovial", "joyful", + "kind", "lively", "logical", "lovely", "loyal", "lucky", "mature", + "mindful", "modest", + ] + nouns = [ + "Cello", "Badger", "Fish", "Apple", "Mountain", "River", "Teacher", + "Book", "Car", "Tree", "Dog", "House", "Chair", "Phone", "Computer", + "City", "Ocean", "Guitar", "Desk", "Flower", "Star", "Sky", "Window", + "Road", "Train", "Plane", "School", "Garden", "Table", "Bottle", + "Shirt", "Door", "Bridge", "Watch", "Camera", "Bag", "Pencil", "Cup", + "Hat", "Wall", "Cloud", "Island", "Forest", "Room", "Engine", "Shoe", + "Candle", "Bed", "Lamp", "Mirror", "Clock", "Keyboard", "Mouse", + "Blanket", "Pillow", "Soap", "Towel", "Toothbrush", "Backpack", + "Basket", "Fan", "Television", "Magazine", "Newspaper", "Statue", + "Painting", "Ladder", "Fence", "Rope", "Ball", "Drum", "Violin", + "Microphone", "Box", "Shelf", "Ring", "Necklace", "Coin", "Wallet", + "Purse", "Ticket", "Key", "Lock", "Brush", "Comb", "Notebook", + "Envelope", "Stamp", "Hammer", "Screwdriver", "Nail", "Saw", "Plank", + "Brick", "Tile", "Carpet", "Curtain", "Apron", "Oven", "Refrigerator", + "Blender", "Pot", "Pan", + ] + + return random.choice(adjectives).capitalize() + random.choice(nouns) def server(host: str='', port: int=7788): from library import Library @@ -11,7 +53,7 @@ def client(playername: str, host: str='localhost', port: int=7788): ui.loop() def main(): - client("TestPlayer") + client(name_gen()) if __name__ == "__main__": main() diff --git a/server.py b/server.py index 733f6a8..f576ddb 100644 --- a/server.py +++ b/server.py @@ -44,7 +44,10 @@ class Game: def add_player(self, name: str, conn: socket): if not self.__active: - self.__clients.append(Player(name, self, conn)) + new_player = Player(name, self, conn) + for player in self.__clients: + new_player.player_joined(player.name) + self.__clients.append(new_player) self.__total_scores.append(0) for player in self.__clients: player.player_joined(name) @@ -72,6 +75,7 @@ class Game: self.__total_scores[i] += points self.__clients[i].verse_guessed( points, self.__current_url, player.name) + self.end_game() else: partially_correct = [] for player_url, current_url in zip(url.strip('/').split('/'), self.__current_url_parts): @@ -106,45 +110,47 @@ class Player: __name: str __game: Game __client: socket + __connected: bool def __init__(self, name: str, game: Game, conn: socket): self.__name = name self.__game = game self.__client = conn + self.__connected = True @property def name(self) -> str: return self.__name def player_joined(self, name: str): - print(f">> (1) player_joined({name})") + print(f">> (1, {self.name}) player_joined({name})") data = network_utilities.pack_varint(1) data += network_utilities.pack_string(name) self.__client.send(data) def new_verse(self, text: str): - print(f">> (2) new_verse({text})") + print(f">> (2, {self.name}) new_verse({text})") data = network_utilities.pack_varint(2) data += network_utilities.pack_string(text) self.__client.send(data) def guess_incorrect(self): - print(">> (3) guess_incorrect()") + print(f">> (3, {self.name}) guess_incorrect()") data = network_utilities.pack_varint(3) self.__client.send(data) def guess_partial_correct(self, url): - print(f">> (7) guess_partial_correct({url})") + print(f">> (7, {self.name}) 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()") + print(">> (4, {self.name}) guess_correct()") data = network_utilities.pack_varint(4) self.__client.send(data) def verse_guessed(self, points: int, url: str, player: str): - print(f">> (5) verse_guessed({points}, {url})") + print(f">> (5, {self.name}) verse_guessed({points}, {url})") data = network_utilities.pack_varint(5) data += network_utilities.pack_varint(points) data += network_utilities.pack_string(url) @@ -152,29 +158,31 @@ class Player: self.__client.send(data) def game_over(self, players: list[str], scores: list[int]): - print(f">> (6) game_over({len(players)}, {len(scores)})") + print(f">> (6, {self.name}) game_over({len(players)}, {len(scores)})") data = network_utilities.pack_varint(6) data += network_utilities.pack_string_array(players) data += network_utilities.pack_varint_array(scores) self.__client.send(data) self.__client.close() + self.__connected = False def update(self): - ready_to_read, _, _ = select.select([self.__client], [], [], 0) - if ready_to_read: - packet_id = network_utilities.unpack_varint(self.__client) - if packet_id == 2: - print("<< (2) start_game()") - self.__game.start_game() - elif packet_id == 3: - difficulty = network_utilities.unpack_varint(self.__client) - print(f"<< (3) start_round({difficulty})") - self.__game.start_round(difficulty) - elif packet_id == 4: - url = network_utilities.unpack_string(self.__client) - print(f"<< (4) guess_reference({url}, {self.name})") - self.__game.guess_reference(url, self) - elif packet_id == 5: - print("<< (5) end_game()") - self.__game.end_game() + if self.__connected: + ready_to_read, _, _ = select.select([self.__client], [], [], 0) + if ready_to_read: + packet_id = network_utilities.unpack_varint(self.__client) + if packet_id == 2: + print(f"<< (2, {self.name}) start_game()") + self.__game.start_game() + elif packet_id == 3: + difficulty = network_utilities.unpack_varint(self.__client) + print(f"<< (3, {self.name}) start_round({difficulty})") + self.__game.start_round(difficulty) + elif packet_id == 4: + url = network_utilities.unpack_string(self.__client) + print(f"<< (4, {self.name}) guess_reference({url}, {self.name})") + self.__game.guess_reference(url, self) + elif packet_id == 5: + print(f"<< (5, {self.name}) end_game()") + self.__game.end_game() diff --git a/ui.py b/ui.py index 23779c1..223f970 100644 --- a/ui.py +++ b/ui.py @@ -64,6 +64,9 @@ class UI: ) else: if url: + try: ref = convert_url(url) + except Exception: ref = url.upper().replace('/','.').strip('.') + print(f"Your input was interpreted as: {ref}") self.__player.guess_reference(url) return elif possible: @@ -85,7 +88,8 @@ class UI: self.__player.new_round(difficulty) def player_joined(self, name: str): - print(f"{name} Joined the Game") + if name == self.__player.name: print(f"* {name} Joined the Game *") + else: print(f"{name} Joined the Game") def new_verse(self, text: str): self.__reset() @@ -101,6 +105,7 @@ class UI: try: ref = convert_url(url) except Exception: ref = url.upper().replace('/','.').strip('.') print(f"That guess was partially correct: {ref}") + print(self.__verse) def guess_correct(self): print("That guess was correct!") @@ -117,7 +122,7 @@ class UI: def game_over(self, players: list[str], scores: list[int]): self.__game_over = True print("\n--- THANKS FOR PLAYING! ---") - for player, score in players, scores: + for player, score in zip(players, scores): if player == self.__player.name: print(f" * {player}: {score} *") else: