-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
98 lines (80 loc) · 3.28 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
from lib.Mastermind import *
from time import localtime, strftime
import traceback
import settings
import gameMap
class GameServer(MastermindServerTCP):
def __init__(self):
MastermindServerTCP.__init__(self,
settings.time_server_refresh,
settings.time_connection_refresh,
settings.time_connection_timeout)
self.driver_connected = False
self.navigator_connected = False
self.car_metrics = (settings.car_x, settings.car_y, 0)
def log_message(self, msg):
timestamp = strftime("%H:%M:%S", localtime())
print()
print(timestamp + " | " + msg)
def callback_connect(self):
self.log_message("Server connected")
return super(GameServer, self).callback_connect()
def callback_disconnect(self):
self.log_message("Server disconnected")
return super(GameServer, self).callback_disconnect()
def callback_connect_client(self, client):
self.log_message(str(client.address) + " has connected")
return super(GameServer, self).callback_connect_client(client)
def callback_disconnect_client(self, client):
self.log_message(str(client.address) + " has disconnected")
return super(GameServer, self).callback_disconnect_client(client)
def callback_client_handle(self, client, data):
if data == "which_map":
self.log_message(str(client.address) + " asked which map")
self.callback_client_send(client, settings.game_map)
elif data == "send_map":
self.log_message(str(client.address) + " asked for the map")
map_data = open(settings.game_map, "rb").read()
self.callback_client_send(client, map_data)
elif data == "get_metrics":
self.log_message(str(client.address) + " asked for car metrics, sending " + str(self.car_metrics))
self.callback_client_send(client, self.car_metrics)
elif type(data) == tuple:
self.log_message(str(client.address) + " sent car metrics " + str(data))
self.car_metrics = data
return super(GameServer, self).callback_client_handle(client, data)
def callback_client_send(self, client, data):
self.log_message("Sending data to " + str(client.address))
return super(GameServer, self).callback_client_send(client, data)
try:
get_input = raw_input
except:
get_input = input
def main():
print("=================================================")
print("= Don't Get Lost v1.0 =")
print("=================================================")
print()
print("Starting server . . .")
print("Type 'exit' to stop the server")
server = GameServer()
try:
server.connect(settings.ip, settings.port)
except:
server.log_message("Server failed to connect")
server.accepting_allow()
running = True
while running:
command = get_input(">>> ")
if command == "exit":
running = False
if server != None:
server.accepting_disallow()
server.disconnect_clients()
server.disconnect()
if __name__ == "__main__":
try:
main()
except:
traceback.print_exc()
get_input()