-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game Server.py
114 lines (109 loc) · 2.98 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
import socket
import fileinput
import sys
import threading
import random
from datetime import datetime
class ServerThread(threading.Thread):
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
def run(self):
def game(room,player):
rmsg = connectionSocket.recv(1024).decode()
recv=rmsg.split()
if recv[0]=="/guess":
if recv[2]=="true":
games[room][player]==2
if recv[2]=="false":
games[room][player]==1
if games[room][player]==games[room][player-1]:
msg="3023 The result is a tie"
elif games[room][player]==answer:
msg="3021 You are the winner"
elif games[room][player]!=answer:
msg="3022 You lost the game"
connectionSocket.send(msg.encode('ascii'))
connectionSocket, addr = self.client
while True:
rmsg = connectionSocket.recv(1024).decode()
recv=rmsg.split()
if recv[0]=="/login":
username=recv[1]
password=recv[2]
userpass= username+":"+password
f=open("UserInfo.txt", "r")
userinfo=f.read()
lines=userinfo.splitlines()
i=0
for line in lines:
if line==userpass:
i=1
if i==1:
msg="1001 Authentication successful"
else:
msg="1002 Authentication failed"
connectionSocket.send(msg.encode('ascii'))
f.close()
if i == 1:
if recv[0]=="/list":
msg="Room\tPlayers\n"
i=0
for room in rooms:
i+=1
msg+=str(i)+"\t"+str(room)
connectionSocket.send(msg.encode('ascii'))
if recv[0]=="/enter":
if len(rooms)>=int(recv[1])-1:
if rooms[int(recv[1])-1]==0:
msg="3011 Wait"
rooms[int(recv[1])-1]+=1
connectionSocket.send(msg.encode('ascii'))
roomwait(int(recv[1])-1)
elif rooms[int(recv[1])-1]==1:
msg="3012 Game started. Please guess true or false"
rooms[int(recv[1])-1]+=1
random.seed(datetime.now().timestamp())
global answer
answer=random.randint(1,2)
connectionSocket.send(msg.encode('ascii'))
game(int(recv[1])-1,1)
else:
msg="3013 The room is full"
connectionSocket.send(msg.encode('ascii'))
else:
msg="3014 Invalid room"
def roomwait(room):
while rooms[room]!=2:
continue
msg="3012 Game started. Please guess true or false"
game(room,0)
connectionSocket.send(msg.encode('ascii'))
class ServerMain:
def server_run(self):
serverPort = 12000
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind( ("", serverPort) )
serverSocket.listen(5)
print("The server is ready to receive")
global rooms
global games
rooms=[0]
games=[[]]
while True:
client = serverSocket.accept()
t = ServerThread(client)
t.start()
j=0
for room in rooms:
if room == 2:
i=1
else:
i=0
j+=i
if j==len(rooms):
rooms.append(0)
games.append([])
if __name__ == '__main__':
server = ServerMain()
server.server_run()