-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gameserver.py
91 lines (76 loc) · 3.52 KB
/
Gameserver.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
from Mastermind import *
server_ip = 'localhost'
port = 42042
connected_clients = 0
class ServerGame(MastermindServerTCP):
def __init__(self):
MastermindServerTCP.__init__(self, 0.5,0.5,30.0) #Server refresh, Connection refresh, Connection timeout
#Grunddaten
self.data_Player1 = None # Data ist jetzt ein 3er Tupel mit Links/Rechts pos, Koords und erschaffener Kugel
self.data_Player2 = None
self.data_to_send = [None,None] # Liste der Tupel
self.indicator = ["",0]
#Für die Objekte
self.stuff_data_Player1 = None
self.stuff_data_Player2 = None
self.stuff_data_to_send = [None,None]
def callback_client_handle(self, connection_object,data):
if data == "gg" or self.indicator == "gg":
self.indicator = "gg"
data = "gg"
self.callback_client_send(connection_object, data)
elif data[0] == "level finished" or self.indicator[0] == "level finished":
if data[0] == "level finished": self.indicator = data
else:
data = self.indicator
self.indicator = ["",0]
self.callback_client_send(connection_object, data)
elif data[0] == 0: # Verbindungsaufbau, Spielerzuweisung
if self.indicator == ["duell",0] and data[1] == True:
self.indicator = ["",0]
self.callback_client_send(connection_object, "duell")
if data[1] == True: # Ghostmode
self.callback_client_send(connection_object, self.stuff_data_to_send)
self.stuff_data_to_send = "cookie"
else:
self.indicator = ["duell",0]
self.callback_client_send(connection_object, self.data_to_send)
elif data[0] == -42: # Objektabgleich im Spiel
if data[1] == 1:
self.stuff_data_Player1 = None
self.stuff_data_to_send[0] = self.stuff_data_Player1
else:
self.stuff_data_Player2 = None
self.stuff_data_to_send[1] = self.stuff_data_Player2
self.callback_client_send(connection_object, self.stuff_data_to_send)
else: # Datenaustausch im Spiel
if data[0] == 1:
self.data_Player1 = (data[1],data[2],data[3],data[4])
self.data_to_send[0] = self.data_Player1
else:
self.data_Player2 = (data[1],data[2],data[3],data[4])
self.data_to_send[1] = self.data_Player2
self.callback_client_send(connection_object, self.data_to_send)
def callback_connect_client(self,connection_object):
global connected_clients
#print ("A new client connected!")
connected_clients += 1
return super(MastermindServerTCP,self).callback_connect_client(connection_object)
def callback_disconnect_client(self,connection_object):
global connected_clients
#print("the client disconnected!")
connected_clients -= 1
if connected_clients == 0:
self.data_Player1 = None
self.data_Player2 = None
self.data_to_send = [None,None]
return super(MastermindServerTCP,self).callback_disconnect_client(connection_object)
if __name__ == "__main__":
server = ServerGame()
server.connect(server_ip,port)
try:
server.accepting_allow_wait_forever()
except:
pass
server.disconnect_clients()
server.disconnect()