-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
110 lines (90 loc) · 3.43 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import socket
import time
import threading
import queue
import sys
import random
from termcolor import colored
from cards import show_my_cards, place
from drawer import drawScreen
HOST = "127.0.0.1" # The server's hostname or IP address
PORT = 65432 # The port used by the server
cmd_queue = queue.Queue() # Queue to hold commands
names = ["Per", "Pól", "Hans", "Jakki", "Stólur", "Top hat", "Tróndur", "Jóhannus", "Telda"]
myname = random.choice(names)
playerId = -1
keep_running = True # Control flag for threads
def send(tosend):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(tosend.encode('utf-8'))
return s.recv(1024).decode('utf-8')
interval = 0
def get_updates():
global keep_running, interval, myname, playerId
while keep_running:
# Check if there are commands in the queue to send
try:
tosend = cmd_queue.get_nowait()
except queue.Empty:
tosend = f"GU{playerId}" # Default message to get updates
response = send(f"P{playerId} {tosend}")
if response != "No new updates.":
if 'hand: ' in response:
tmp = show_my_cards(response)
print(tmp)
elif 'Current Players:' in response:
if "Turn" in response:
playerTurn = response.split("\n")[0][6:]
todraw = drawScreen(myname, playerId)
for i, player in enumerate(response.split('\n')[1:-1]):
thisPlayername = player.split(',')[0][6:]
if "ID" in player:
if float(player.split(',')[1][14:-5]) > 0.5:
symbol = colored("✖", 'red')
else:
symbol = colored("✔", 'green')
playerNameForPrint = thisPlayername
if "Turn" in response:
if int(playerTurn) == i:
playerNameForPrint = colored(thisPlayername, 'red')
playerNameForPrint = playerNameForPrint + (" " * (10 - len(thisPlayername)))
tmp = playerNameForPrint + "│ " + symbol + " │"
todraw = place(tmp, todraw, 61, 3+int(player[3]))
print(todraw)
#print(response)
#if "Turn" in response:
# print(playerTurn)
else:
print(response)
interval += 1
if interval == 10:
interval = 0
cmd_queue.put("list players")
time.sleep(0.1) # Polling interval
def handle_user_input():
global keep_running
while keep_running:
user_input = input()
cmd_queue.put(user_input)
if user_input.lower() == 'exit':
break
if __name__ == "__main__":
print(sys.argv)
if len(sys.argv) > 1:
myname = sys.argv[1]
# Start user input handling in a separate thread
input_thread = threading.Thread(target=handle_user_input)
input_thread.start()
response = send(f'Hallo, Eg eri {myname}')
print(f"Received {response}")
print(response[0])
if response[0] == 'P':
print('yey!')
time.sleep(0.5)
playerId = response[1]
update_thread = threading.Thread(target=get_updates)
update_thread.start()
else:
print("😭")
keep_running = False