-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgame_server.py
161 lines (121 loc) · 4.75 KB
/
game_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import tkinter as tk
import socket
import threading
from time import sleep
window = tk.Tk()
window.title("Sever")
# Top frame consisting of two buttons widgets (i.e. btnStart, btnStop)
topFrame = tk.Frame(window)
btnStart = tk.Button(topFrame, text="Start", command=lambda : start_server())
btnStart.pack(side=tk.LEFT)
btnStop = tk.Button(topFrame, text="Stop", command=lambda : stop_server(), state=tk.DISABLED)
btnStop.pack(side=tk.LEFT)
topFrame.pack(side=tk.TOP, pady=(5, 0))
# Middle frame consisting of two labels for displaying the host and port info
middleFrame = tk.Frame(window)
lblHost = tk.Label(middleFrame, text = "Address: X.X.X.X")
lblHost.pack(side=tk.LEFT)
lblPort = tk.Label(middleFrame, text = "Port:XXXX")
lblPort.pack(side=tk.LEFT)
middleFrame.pack(side=tk.TOP, pady=(5, 0))
# The client frame shows the client area
clientFrame = tk.Frame(window)
lblLine = tk.Label(clientFrame, text="**********Client List**********").pack()
scrollBar = tk.Scrollbar(clientFrame)
scrollBar.pack(side=tk.RIGHT, fill=tk.Y)
tkDisplay = tk.Text(clientFrame, height=10, width=30)
tkDisplay.pack(side=tk.LEFT, fill=tk.Y, padx=(5, 0))
scrollBar.config(command=tkDisplay.yview)
tkDisplay.config(yscrollcommand=scrollBar.set, background="#F4F6F7", highlightbackground="grey", state="disabled")
clientFrame.pack(side=tk.BOTTOM, pady=(5, 10))
server = None
HOST_ADDR = "0.0.0.0"
HOST_PORT = 8080
client_name = " "
clients = []
clients_names = []
player_data = []
# Start server function
def start_server():
global server, HOST_ADDR, HOST_PORT # code is fine without this
btnStart.config(state=tk.DISABLED)
btnStop.config(state=tk.NORMAL)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print socket.AF_INET
print socket.SOCK_STREAM
server.bind((HOST_ADDR, HOST_PORT))
server.listen(5) # server is listening for client connection
threading._start_new_thread(accept_clients, (server, " "))
lblHost["text"] = "Address: " + HOST_ADDR
lblPort["text"] = "Port: " + str(HOST_PORT)
# Stop server function
def stop_server():
global server
server.close()
btnStart.config(state=tk.NORMAL)
btnStop.config(state=tk.DISABLED)
def accept_clients(the_server, y):
while True:
if len(clients) < 2:
client, addr = the_server.accept()
clients.append(client)
# use a thread so as not to clog the gui thread
threading._start_new_thread(send_receive_client_message, (client, addr))
# Function to receive message from current client AND
# Send that message to other clients
def send_receive_client_message(client_connection, client_ip_addr):
global server, client_name, clients, player_data, player0, player1
client_msg = " "
# send welcome message to client
client_name = client_connection.recv(4096)
if len(clients) < 2:
client_connection.send("welcome1")
else:
client_connection.send("welcome2")
clients_names.append(client_name)
update_client_names_display(clients_names) # update client names display
if len(clients) > 1:
sleep(1)
# send opponent name
clients[0].send("opponent_name$" + clients_names[1])
clients[1].send("opponent_name$" + clients_names[0])
# go to sleep
while True:
data = client_connection.recv(4096)
if not data: break
# get the player choice from received data
player_choice = data[11:len(data)]
msg = {
"choice": player_choice,
"socket": client_connection
}
if len(player_data) < 2:
player_data.append(msg)
if len(player_data) == 2:
# send player 1 choice to player 2 and vice versa
player_data[0].get("socket").send("$opponent_choice" + player_data[1].get("choice"))
player_data[1].get("socket").send("$opponent_choice" + player_data[0].get("choice"))
player_data = []
# find the client index then remove from both lists(client name list and connection list)
idx = get_client_index(clients, client_connection)
del clients_names[idx]
del clients[idx]
client_connection.close()
update_client_names_display(clients_names) # update client names display
# Return the index of the current client in the list of clients
def get_client_index(client_list, curr_client):
idx = 0
for conn in client_list:
if conn == curr_client:
break
idx = idx + 1
return idx
# Update client name display when a new client connects OR
# When a connected client disconnects
def update_client_names_display(name_list):
tkDisplay.config(state=tk.NORMAL)
tkDisplay.delete('1.0', tk.END)
for c in name_list:
tkDisplay.insert(tk.END, c+"\n")
tkDisplay.config(state=tk.DISABLED)
window.mainloop()