This repository has been archived by the owner on Jan 10, 2021. It is now read-only.
/
apps.py
212 lines (190 loc) · 7.67 KB
/
apps.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from collections import defaultdict
import traceback
from util import command, is_command
import re
def takes(**types):
def inner(func):
def func_wrapper(self, **kwargs):
errors = []
for i, j in kwargs.items():
if i not in types:
errors.append("%s is an illegal parameter" % i)
continue
if not isinstance(j, types[i]):
errors.append("%s should be a %s, (received %s)" %
(i, types[i], j.__class__.__name__))
continue
for i in types:
if i not in kwargs:
errors.append("%s expected but not received" % i)
if errors:
return {'type': 'bad arguments',
'args': {
'message': '\n'.join(errors)
}
}
return func(self, **kwargs)
return func_wrapper
return inner
class App(object):
def send_json(self, message):
self.connection.send_json(message)
def send_error(self, command, message, status='error'):
error = {'args': {}}
error['type'] = status
error['args']['command'] = command
error['args']['message'] = message
self.connection.send_json(error)
def __init__(self, connection):
self.connection = connection
def disconnect(self, reason):
pass
def get_command(self, command_name):
return getattr(self, command_name, None)
def run(self, command):
try:
command_name = command['type']
args = command['args']
except KeyError:
self.send_error(command, 'was missing values')
return
function = self.get_command(command_name)
if not function or not is_command(function):
self.send_error(command,
"%s is not a valid command" % command_name,
'command not found')
return
try:
result = function(**args)
except:
traceback.print_exc()
self.send_error(command, traceback.format_exc())
return
if result != None:
self.connection.send_json(result)
class LoginApp(App):
"""
This is the starting app for connections.
This facilitates any authentication we want to do
and let's them specify what kind of connection they want.
This should detect all the game plugins and expose them.
Also, the arena app, and any others we think of later.
"""
@command
@takes(connection_type=basestring, username=basestring)
def login(self, connection_type=str, username = ''):
# TODO Actual login stuff
self.connection.username = username
try:
self.connection.app = GameApp(self.connection, connection_type)
except ImportError:
return {'type': 'failure', 'args':
{'message': '%s is not a game type' % connection_type}}
return {'type': 'success'}
class GameApp(App):
games = defaultdict(dict)
def __init__(self, connection, game_type):
if '.' in game_type:
raise ValueError("Game name cannot contain '.'")
self.game_module = __import__('plugins.%s.game' % game_type,
fromlist=['*'])
App.__init__(self, connection)
self.game_type = game_type
self.game = None
self.game_name = None
@command
def join_game(self, **game_details):
# Check if game you want to join exists, otherwise create it.
# If game exists, make certain you can join it
# Transition to the GameApp state
if 'game_name' in game_details:
game_name = game_details['game_name']
else:
# If you don't specify a game number, find the lowest unused
game_number = 1
game_name = str(game_number)
while game_name in self.games[self.game_type]:
game_number += 1
game_name = str(game_number)
game_details['game_name'] = game_name
if len(game_name) > 40:
return {'type': 'failure', 'args': {'message': 'game name too long'}}
if re.search('[/\\:*?"<>|@.\00]', game_name):
return {'type': 'failure', 'args': {'message': 'game name contains illegal characters'}}
if game_name in self.games[self.game_type]:
# Join existing game
self.game = self.games[self.game_type][game_name]
else:
# Create new game
self.game = self.game_module.Game(game_details)
self.games[self.game_type][game_name] = self.game
# Attempt to connect to the game
if self.game.state != 'new':
self.game = None
return {'type': 'failure', 'args': {'message': 'game has already started'}}
else:
self.game_name = game_name
self.send_json({'type': 'success', 'args': {'name': game_name}})
self.game.add_connection(self, game_details)
return None
@command
def end_turn(self, **args):
if not self.game or self.game.state != 'running':
return {'type': 'failure',
'args': {'message': 'the game has not begun'}}
if self.game.current_player._connection != self:
return {'type': 'failure',
'args': {'message': 'not your turn'}}
self.send_json({'type': 'success', 'args': {}})
self.game.end_turn()
return None
@command
def get_log(self, **args):
if not self.game or self.game.state != 'over':
return {'type': 'failure',
'args': {'message': 'the game has not begun'}}
log = open(self.game.logger.path).read()
return {'type': 'success', 'args':{
'log': log}}
def get_command(self, command_name):
command = App.get_command(self, command_name)
if command:
return command
if not self.game or self.game.state != 'running':
return None
def command(**args):
if self.game.current_player._connection != self:
return {'type': 'failure',
'args': {'message': 'not your turn'}}
if 'actor' not in args:
command = getattr(self.game, command_name, None)
if not command or not is_command(command):
return {'type': 'error',
'args': {'message': 'global command %s does not exist' %
command_name
}}
else:
actor = self.game.objects.get(args['actor'], None)
del args['actor']
if not actor:
return {'type': 'bad arguments',
'args': {'message': 'actor does not exist'}}
command = getattr(actor, command_name, None)
if not command or not is_command(command):
return {'type': 'error',
'args': {'message': '%s does not have command %s' %
(actor.__class__.__name__, command_name)}}
value = command(**args)
self.game.flush()
if not value:
return {'type': 'success', 'args': {}}
else:
return value
command.is_command = True
return command
def disconnect(self, reason):
if self.game:
self.game.remove_connection(self)
if not self.game.connections:
del self.games[self.game_type][self.game_name]
self.game = None