-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.py
142 lines (115 loc) · 3.53 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
from socket import *
import time
import _thread
import select
teamsdict={}
#litsen for TSP massage
def listenT(s):
global teamsdict
startTime=time.time()
while True:
readable, writeable, erro = select.select([s],[],[],startTime+10-time.time())
if len(readable)<=0:
break
conn, addr = s.accept()
data = conn.recv(1024)
teamsdict[addr]=[conn,data.decode("utf-8")]
#send broadcast offer
def broadCast():
s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
portBC=13117
#The broadcast message
msg = b'\xfe\xed\xbe\xef\x02\x25\x13'
for i in range(10):
s.sendto(msg, ('<broadcast>', portBC))
time.sleep(1)
#game start to listen to clint key smashing
def gameStart(s,inx,a,flag):
while True:
if(flag[0]):
break
data=s.recv(1024)
a[inx]+=1
#split to group send the massage and start thread for each client
def KSBR():
global teamsdict
flagG=0
group1=[]
group2=[]
#divide the clients to 2 groups, odd group and even group
for key in teamsdict.keys():
if(flagG==0):
group1.append([key,teamsdict[key][1]])
flagG=1
else:
group2.append([key,teamsdict[key][1]])
flagG=0
##building the message - what we will print
mssage="Welcome to Keyboard Spamming Battle Royale.\n"
mssage+="Group 1:\n==\n"
for team in group1:
mssage+=team[1]+"\n"
mssage+="Group 2:\n==\n"
for team in group2:
mssage+=team[1]+"\n"
mssage+="Start pressing keys on your keyboard as fast as you can!!"
#sending the message to everyone
for key in teamsdict.keys():
s=teamsdict[key][0]
s.sendall(bytes(mssage,"utf-8"))
#counter of the number of pressing
a=[0]*len(teamsdict)
inx=0
flag=[False]
for key in teamsdict.keys():
_thread.start_new_thread(gameStart,(teamsdict[key][0],inx,a,flag))
inx+=1
time.sleep(10)
flag[0]=True
time.sleep(0.2)
group1point=0
group2point=0
flagG=0
for i in range(len(a)):
if(flagG==0):
group1point+=a[i]
flagG=1
else:
group2point+=a[i]
flagG=0
fmssage="Group 1 typed in "+ str(group1point)+" characters. Group 2 typed in " + str(group2point) +" characters.\n"
if group1point>=group2point:
fmssage+="Group 1 wins!\n\n"
fmssage+="Congratulations to the winners:\n==\n"
for team in group1:
fmssage+=team[1]+"\n"
else:
fmssage+="Group 2 wins!\n\n"
fmssage+="Congratulations to the winners:\n==\n"
for team in group2:
fmssage+=team[1]+"\n"
#send the message to everyone(clients)
for key in teamsdict.keys():
s=teamsdict[key][0]
s.sendall(bytes(fmssage,"utf-8"))
#Cant closing the socket until revicing from client
for key in teamsdict.keys():
s=teamsdict[key][0]
s.recv(1024)
s.close()
s = socket(AF_INET, SOCK_STREAM)
s.bind(("172.1.0."+gethostbyname(gethostname()).split(".")[3], 9491))
s.listen()
print("Server started, listening on IP address ", "172.1.0."+gethostbyname(gethostname()).split(".")[3])
while True:
try:
_thread.start_new_thread(broadCast,())
_thread.start_new_thread(listenT,(s,))
time.sleep(11)
KSBR()
print("Server disconnected, listening for offer requests...")
time.sleep(1)
teamsdict={}
except:
print("code fall")