-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThudServerDjango.py
110 lines (87 loc) · 3.71 KB
/
ThudServerDjango.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
__author__ = 'wbw'
from datetime import date
from django.http import HttpResponse
from Thud import Thud
from django.core import serializers
import json
game_manager = Thud.GameManager()
def get_version(request):
response = {'name': 'ThudServer',
'version': '1.0',
'last_build': date.today().isoformat()}
return HttpResponse(json.dumps(response))
def get_game_by_id_handler(request, game_id):
response = {"id": 1,
"Name": "Thud",
"release_date": date.today().isoformat()}
return HttpResponse(serializers.serialize('json', response))
def StartGameWithPlayers(request):
start_data = json.loads(request.body.decode('utf-8'))
response = game_manager.process_start(start_data)
return HttpResponse(json.dumps(response))
def ExecuteMove(request):
move_data = json.loads(request.body.decode('utf-8'))
response = game_manager.process_move(move_data)
print(response)
return HttpResponse(json.dumps(response))
def ValidateMove(request):
move_data = json.loads(request.body.decode('utf-8'))
response = game_manager.process_move(move_data, test=True)
return HttpResponse(json.dumps(response))
def GetBoardState(request):
game = json.loads(request.body.decode('utf-8'))
response = game_manager.report_game_state(game)
return HttpResponse(json.dumps(response))
def SaveGame(request):
game = json.loads(request.body.decode('utf-8'))
response = game_manager.process_save(game)
return HttpResponse(json.dumps(response))
def LoadGame(request):
game = json.loads(request.body.decode('utf-8'))
response = game_manager.process_load(game)
return HttpResponse(json.dumps(response))
def EndGame(request):
end_data = json.loads(request.body.decode('utf-8'))
response = '' # game_manager.process_end(end_data)
return HttpResponse(json.dumps(response))
# def PlayerConnection():
# #ToDo:
# # Receive player name
# # Add playername to list
# # Return playerlist (without added name)
# # Receive player name, opponent name
# # Check that both are still in list
# # Remove both from list
# # Start game, return to both
#
# def check_origin(self, origin):
# # ToDo: only allow trusted domains?
# return True
#
# def open(self, player_id):
# self.player_id = player_id
# logging.debug("{}: Websocket opened for player {}".format(datetime.now().strftime('%d/%m/%y %H:%M:%S'),
# player_id))
# player_list = game_manager.add_player_to_server(self.player_id, self)
# logging.debug("{}: Reported player list {}".format(datetime.now().strftime('%d/%m/%y %H:%M:%S'),
# player_list))
# if player_list != False:
# self.write_message(json.dumps(["list", player_list]))
# else:
# self.write_message(json.dumps("Duplicate player name."))
# self.close()
#
# def on_message(self, message):
# response = game_manager.process_socket_message(json.loads(message), self.player_id)
# logging.debug("{}: Message {} received, response {}.".format(
# datetime.now().strftime('%d/%m/%y %H:%M:%S'),
# message, response))
# self.write_message(json.dumps(response))
#
# def send_message(self, message):
# self.write_message(json.dumps(message))
#
# def on_close(self):
# game_manager.remove_player_from_server(self.player_id)
# logging.debug("{}: Websocket closed for player {}".format(datetime.now().strftime('%d/%m/%y %H:%M:%S'),
# self.player_id))