-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
142 lines (132 loc) · 5.34 KB
/
Server.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import socket
import threading
from time import sleep
import Player
import Dealer
HOST = '127.0.0.1'
PORT = 12345
dealer = Dealer.Dealer()
players = []
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen()
print("Server is listening for incoming connections...")
# Function to handle client connections
def handle_client(client_socket, client_address):
print(f"Connection from {client_address} has been established.")
curr_player = None
for player in players:
if player.socket == client_socket:
curr_player = player
client_data = str(curr_player.money)
client_socket.sendall(client_data.encode())
break
while True:
# Receive data from the client
try:
data = client_socket.recv(1024).decode()
except:
break
if not data:
break
if data == "bet50 button clicked":
if curr_player.bet(50) is False:
client_data = "incorrect bet"
client_socket.sendall(client_data.encode())
else:
client_data = str(curr_player.money)
client_socket.sendall(client_data.encode())
start_game(curr_player)
if data == "bet100 button clicked":
if curr_player.bet(100) is False:
client_data = "incorrect bet"
client_socket.sendall(client_data.encode())
else:
client_data = str(curr_player.money)
client_socket.sendall(client_data.encode())
start_game(curr_player)
if data == "betall button clicked":
if curr_player.bet(curr_player.money) is False:
client_data = "incorrect bet"
client_socket.sendall(client_data.encode())
else:
client_data = str(curr_player.money)
client_socket.sendall(client_data.encode())
start_game(curr_player)
if data == "hit button clicked":
dealer.deal_one_card(curr_player)
card_data = make_card_data()
client_socket.sendall(card_data.encode())
if curr_player.hit_option() is False:
curr_player.stay_option()
client_socket.sendall("no".encode())
end_game(curr_player)
else:
client_socket.sendall("yes".encode())
if data == "stay button clicked":
curr_player.stay_option()
card_data = make_card_data()
client_socket.sendall(card_data.encode())
end_game(curr_player)
if data == "double button clicked":
if curr_player.double_option() is True:
dealer.deal_one_card(curr_player)
curr_player.stay_option()
card_data = make_card_data()
client_socket.sendall(card_data.encode())
end_game(curr_player)
else:
client_socket.sendall("you are brokie".encode())
print(f"Received from client {client_address}: {data}")
# Close the connection
players.remove(curr_player)
client_socket.close()
print(f"Connection from {client_address} has been closed.")
def start_game(curr_player):
if players[0] == curr_player:
dealer.new_deck()
dealer.players = players
dealer.deal_cards()
if all(player.bet_value is not None for player in players):
data = make_card_data()
for player in dealer.players:
player.socket.sendall(data.encode())
def end_game(curr_player):
if players[0] == curr_player:
dealer.hit()
dealer.pay_player()
if all(player.is_ready is not False for player in players):
card_data = make_card_data()
for player in players:
data = card_data + "|" + str(player.money)
player.socket.sendall(data.encode())
dealer.clear_dealer()
for player in players:
player.clear_player()
def make_card_data():
data = ""
for card in dealer.cards:
data += " " + card.name
for player in dealer.players:
data += ","
for card in player.cards:
data += " " + card.name
return data
# Function to accept incoming connections and create threads for each client
def accept_connections():
while True:
# Accept incoming connection
client_socket, client_address = server_socket.accept()
if len(players) >= 3:
print("Maximum connections reached. Rejecting new connection.")
client_socket.sendall("server is full".encode())
client_socket.close()
continue
players.append(Player.Player(1000, client_socket))
client_socket.sendall(str(len(players)).encode())
# Create a new thread to handle the client connection
client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address))
client_thread.start()
#Main
if __name__ == "__main__":
accept_connections()