Implemented UI #10
46
main.py
46
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):
|
def server(host: str='', port: int=7788):
|
||||||
from library import Library
|
from library import Library
|
||||||
|
@ -11,7 +53,7 @@ def client(playername: str, host: str='localhost', port: int=7788):
|
||||||
ui.loop()
|
ui.loop()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
client("TestPlayer")
|
client(name_gen())
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
58
server.py
58
server.py
|
@ -44,7 +44,10 @@ class Game:
|
||||||
|
|
||||||
def add_player(self, name: str, conn: socket):
|
def add_player(self, name: str, conn: socket):
|
||||||
if not self.__active:
|
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)
|
self.__total_scores.append(0)
|
||||||
for player in self.__clients:
|
for player in self.__clients:
|
||||||
player.player_joined(name)
|
player.player_joined(name)
|
||||||
|
@ -72,6 +75,7 @@ class Game:
|
||||||
self.__total_scores[i] += points
|
self.__total_scores[i] += points
|
||||||
self.__clients[i].verse_guessed(
|
self.__clients[i].verse_guessed(
|
||||||
points, self.__current_url, player.name)
|
points, self.__current_url, player.name)
|
||||||
|
self.end_game()
|
||||||
else:
|
else:
|
||||||
partially_correct = []
|
partially_correct = []
|
||||||
for player_url, current_url in zip(url.strip('/').split('/'), self.__current_url_parts):
|
for player_url, current_url in zip(url.strip('/').split('/'), self.__current_url_parts):
|
||||||
|
@ -106,45 +110,47 @@ class Player:
|
||||||
__name: str
|
__name: str
|
||||||
__game: Game
|
__game: Game
|
||||||
__client: socket
|
__client: socket
|
||||||
|
__connected: bool
|
||||||
|
|
||||||
def __init__(self, name: str, game: Game, conn: socket):
|
def __init__(self, name: str, game: Game, conn: socket):
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__game = game
|
self.__game = game
|
||||||
self.__client = conn
|
self.__client = conn
|
||||||
|
self.__connected = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str: return self.__name
|
def name(self) -> str: return self.__name
|
||||||
|
|
||||||
def player_joined(self, name: str):
|
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_varint(1)
|
||||||
data += network_utilities.pack_string(name)
|
data += network_utilities.pack_string(name)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def new_verse(self, text: str):
|
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_varint(2)
|
||||||
data += network_utilities.pack_string(text)
|
data += network_utilities.pack_string(text)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def guess_incorrect(self):
|
def guess_incorrect(self):
|
||||||
print(">> (3) guess_incorrect()")
|
print(f">> (3, {self.name}) guess_incorrect()")
|
||||||
data = network_utilities.pack_varint(3)
|
data = network_utilities.pack_varint(3)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def guess_partial_correct(self, url):
|
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_varint(7)
|
||||||
data += network_utilities.pack_string(url)
|
data += network_utilities.pack_string(url)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def guess_correct(self):
|
def guess_correct(self):
|
||||||
print(">> (4) guess_correct()")
|
print(">> (4, {self.name}) guess_correct()")
|
||||||
data = network_utilities.pack_varint(4)
|
data = network_utilities.pack_varint(4)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def verse_guessed(self, points: int, url: str, player: str):
|
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(5)
|
||||||
data += network_utilities.pack_varint(points)
|
data += network_utilities.pack_varint(points)
|
||||||
data += network_utilities.pack_string(url)
|
data += network_utilities.pack_string(url)
|
||||||
|
@ -152,29 +158,31 @@ class Player:
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
|
|
||||||
def game_over(self, players: list[str], scores: list[int]):
|
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_varint(6)
|
||||||
data += network_utilities.pack_string_array(players)
|
data += network_utilities.pack_string_array(players)
|
||||||
data += network_utilities.pack_varint_array(scores)
|
data += network_utilities.pack_varint_array(scores)
|
||||||
self.__client.send(data)
|
self.__client.send(data)
|
||||||
self.__client.close()
|
self.__client.close()
|
||||||
|
self.__connected = False
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
ready_to_read, _, _ = select.select([self.__client], [], [], 0)
|
if self.__connected:
|
||||||
if ready_to_read:
|
ready_to_read, _, _ = select.select([self.__client], [], [], 0)
|
||||||
packet_id = network_utilities.unpack_varint(self.__client)
|
if ready_to_read:
|
||||||
if packet_id == 2:
|
packet_id = network_utilities.unpack_varint(self.__client)
|
||||||
print("<< (2) start_game()")
|
if packet_id == 2:
|
||||||
self.__game.start_game()
|
print(f"<< (2, {self.name}) start_game()")
|
||||||
elif packet_id == 3:
|
self.__game.start_game()
|
||||||
difficulty = network_utilities.unpack_varint(self.__client)
|
elif packet_id == 3:
|
||||||
print(f"<< (3) start_round({difficulty})")
|
difficulty = network_utilities.unpack_varint(self.__client)
|
||||||
self.__game.start_round(difficulty)
|
print(f"<< (3, {self.name}) start_round({difficulty})")
|
||||||
elif packet_id == 4:
|
self.__game.start_round(difficulty)
|
||||||
url = network_utilities.unpack_string(self.__client)
|
elif packet_id == 4:
|
||||||
print(f"<< (4) guess_reference({url}, {self.name})")
|
url = network_utilities.unpack_string(self.__client)
|
||||||
self.__game.guess_reference(url, self)
|
print(f"<< (4, {self.name}) guess_reference({url}, {self.name})")
|
||||||
elif packet_id == 5:
|
self.__game.guess_reference(url, self)
|
||||||
print("<< (5) end_game()")
|
elif packet_id == 5:
|
||||||
self.__game.end_game()
|
print(f"<< (5, {self.name}) end_game()")
|
||||||
|
self.__game.end_game()
|
||||||
|
|
||||||
|
|
9
ui.py
9
ui.py
|
@ -64,6 +64,9 @@ class UI:
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if url:
|
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)
|
self.__player.guess_reference(url)
|
||||||
return
|
return
|
||||||
elif possible:
|
elif possible:
|
||||||
|
@ -85,7 +88,8 @@ class UI:
|
||||||
self.__player.new_round(difficulty)
|
self.__player.new_round(difficulty)
|
||||||
|
|
||||||
def player_joined(self, name: str):
|
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):
|
def new_verse(self, text: str):
|
||||||
self.__reset()
|
self.__reset()
|
||||||
|
@ -101,6 +105,7 @@ class UI:
|
||||||
try: ref = convert_url(url)
|
try: ref = convert_url(url)
|
||||||
except Exception: ref = url.upper().replace('/','.').strip('.')
|
except Exception: ref = url.upper().replace('/','.').strip('.')
|
||||||
print(f"That guess was partially correct: {ref}")
|
print(f"That guess was partially correct: {ref}")
|
||||||
|
print(self.__verse)
|
||||||
|
|
||||||
def guess_correct(self):
|
def guess_correct(self):
|
||||||
print("That guess was correct!")
|
print("That guess was correct!")
|
||||||
|
@ -117,7 +122,7 @@ class UI:
|
||||||
def game_over(self, players: list[str], scores: list[int]):
|
def game_over(self, players: list[str], scores: list[int]):
|
||||||
self.__game_over = True
|
self.__game_over = True
|
||||||
print("\n--- THANKS FOR PLAYING! ---")
|
print("\n--- THANKS FOR PLAYING! ---")
|
||||||
for player, score in players, scores:
|
for player, score in zip(players, scores):
|
||||||
if player == self.__player.name:
|
if player == self.__player.name:
|
||||||
print(f" * {player}: {score} *")
|
print(f" * {player}: {score} *")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue