-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbmapi.py
More file actions
335 lines (298 loc) · 9.75 KB
/
bmapi.py
File metadata and controls
335 lines (298 loc) · 9.75 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
##### bmapi.py
# This library strictly implements the Button Men API in python.
# The BMClient class provides one function for each API method,
# which is called with the arguments to be passed to that method,
# and returns the JSON response to the method invocation.
### Imports
# Import stuff from the future.
from __future__ import absolute_import, division, print_function, unicode_literals
from future import standard_library
standard_library.install_aliases()
# Import regular stuff.
import configparser
import json
import os
from http.cookiejar import CookieJar, LWPCookieJar
import requests
### Classes
class BMAPIResponse():
def __init__(self, response_dict):
for mandatory_arg in ['data', 'message', 'status']:
if not mandatory_arg in response_dict:
raise ValueError("Malformed API response is missing key '%s': %s" % (
mandatory_arg, response_dict))
self.data = response_dict['data']
self.message = response_dict['message']
self.status = response_dict['status']
self.rand_vals = None
self.skill_rand_vals = None
# Only replay testing uses these return values.
# A production site should never return them.
if 'BM_RAND_VALS_ROLLED' in response_dict:
self.rand_vals = response_dict['BM_RAND_VALS_ROLLED']
if 'BM_SKILL_RAND_VALS_ROLLED' in response_dict:
self.skill_rand_vals = response_dict['BM_SKILL_RAND_VALS_ROLLED']
class BMClient():
def _read_rcfile(self, rcfile, site):
config = configparser.ConfigParser()
config.read(rcfile)
self.url = config.get(site, "url")
self.username = config.get(site, "username")
self.password = config.get(site, "password")
try:
self.cookiefile = os.path.expanduser(config.get(site, "cookiefile"))
except configparser.NoOptionError:
self.cookiefile = None
try:
self.cachedir = os.path.expanduser(config.get(site, "cachedir"))
except configparser.NoOptionError:
pass
def _setup_cookies(self):
# create a session so all requests can use a shared cookie jar
self.session = requests.session()
if self.cookiefile is not None:
self.cookiejar = LWPCookieJar(self.cookiefile)
if os.path.isfile(self.cookiefile):
# load existing cookies from file
self.cookiejar.load(ignore_discard=True)
else:
# use in-memory cookie jar
self.cookiejar = CookieJar()
self.session.cookies = self.cookiejar
def __init__(self, rcfile, site):
self.username = None
self.password = None
self.cookiefile = None
self.cachedir = None
self._read_rcfile(rcfile, site)
self._setup_cookies()
def _make_request(self, args):
data = json.dumps(args)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
response = self.session.post(url=self.url, data=data, headers=headers)
try:
retval = response.json()
return BMAPIResponse(retval)
except ValueError as e:
print("could not parse return: " + response.text)
return False
def login(self):
args = {
'type': 'login',
'username': self.username,
'password': self.password,
}
retval = self._make_request(args)
if retval.status == 'ok':
if isinstance(self.cookiejar, LWPCookieJar):
self.cookiejar.save(ignore_discard=True)
return True
return False
def load_player_name(self):
args = {
'type': 'loadPlayerName',
}
return self._make_request(args)
def verify_login(self):
retval = self.load_player_name()
if retval.status == 'ok':
return True
return self.login()
def load_button_names(self):
args = {
'type': 'loadButtonData',
}
return self._make_request(args)
def load_player_names(self):
args = {
'type': 'loadPlayerNames',
}
return self._make_request(args)
def load_active_games(self):
args = {
'type': 'loadActiveGames',
}
return self._make_request(args)
def load_new_games(self):
args = {
'type': 'loadNewGames',
}
return self._make_request(args)
def react_to_new_game(self, gameId, action):
args = {
'type': 'reactToNewGame',
'gameId': gameId,
'action': action
}
return self._make_request(args)
def load_completed_games(self):
args = {
'type': 'loadCompletedGames',
}
return self._make_request(args)
def load_game_data(self, gameId, logEntryLimit=10):
args = {
'type': 'loadGameData',
'game': gameId,
'logEntryLimit': logEntryLimit,
}
return self._make_request(args)
def load_button_data(self, button):
args = {
'type': 'loadButtonData',
'buttonName': button,
}
return self._make_request(args)
def load_forum_thread(self, thread):
args = {
'type': 'loadForumThread',
'threadId': thread,
}
return self._make_request(args)
def edit_forum_post(self, postId, body):
args = {
'type': 'editForumPost',
'postId': postId,
'body': body,
}
return self._make_request(args)
def create_game(self, pbutton, obutton='', player='', opponent='', description='', max_wins=3, use_prev_game=False, custom_recipe_array=None):
if player is None or player == '':
player = self.username
if not obutton:
obutton = ''
player_info_array = [
[ player, pbutton, ],
[ opponent, obutton, ],
]
args = {
'type': 'createGame',
'playerInfoArray': player_info_array,
'maxWins': max_wins,
}
if use_prev_game:
args['previousGameId'] = use_prev_game
if description:
args['description'] = description
if custom_recipe_array:
args['customRecipeArray'] = custom_recipe_array
return self._make_request(args)
def submit_turn(self, gameId, attackerIdx, defenderIdx, dieSelectStatus, attackType, roundNumber, timestamp, turboVals, chat=''):
args = {
'type': 'submitTurn',
'game': gameId,
'attackerIdx': attackerIdx,
'defenderIdx': defenderIdx,
'attackType': attackType,
'roundNumber': roundNumber,
'timestamp': timestamp,
'chat': chat,
'dieSelectStatus': dieSelectStatus,
}
if turboVals:
args['turboVals'] = turboVals
return self._make_request(args)
def submit_die_values(self, gameId, swingArray, optionArray, roundNumber, timestamp):
args = {
'type': 'submitDieValues',
'game': gameId,
'roundNumber': roundNumber,
'timestamp': timestamp,
}
if swingArray:
for [key, value] in sorted(swingArray.items()):
args['swingValueArray'] = swingArray
if optionArray:
for [key, value] in sorted(optionArray.items()):
args['optionValueArray'] = optionArray
return self._make_request(args)
def submit_chat(self, game, chat):
args = {
'type': 'submitChat',
'game': game,
'chat': chat,
}
return self._make_request(args)
def react_to_new_game(self, gameId, action):
args = {
'type': 'reactToNewGame',
'gameId': gameId,
'action': action,
}
return self._make_request(args)
def react_to_initiative(self, gameId, action, idxArray, valueArray, roundNumber, timestamp):
args = {
'type': 'reactToInitiative',
'game': gameId,
'roundNumber': roundNumber,
'timestamp': timestamp,
'action': action,
'dieIdxArray': idxArray,
'dieValueArray': valueArray,
}
return self._make_request(args)
def adjust_fire_dice(self, gameId, action, idxArray, valueArray, roundNumber, timestamp):
args = {
'type': 'adjustFire',
'game': gameId,
'roundNumber': roundNumber,
'timestamp': timestamp,
'action': action,
'dieIdxArray': idxArray,
'dieValueArray': valueArray,
}
return self._make_request(args)
def choose_reserve_dice(self, gameId, action, dieIdx=None):
args = {
'type': 'reactToReserve',
'game': gameId,
'action': action,
}
if dieIdx is not None:
args['dieIdx'] = dieIdx
return self._make_request(args)
def choose_auxiliary_dice(self, gameId, action, dieIdx=None):
args = {
'type': 'reactToAuxiliary',
'game': gameId,
'action': action,
}
if dieIdx is not None:
args['dieIdx'] = dieIdx
return self._make_request(args)
def search_game_history(self, sortColumn, sortDirection="DESC",
numberOfResults=20, page=1, status=None, playerNameA=None, playerNameB=None,
buttonNameA=None, buttonNameB=None, gameStartMin=None, gameStartMax=None,
lastMoveMin=None, lastMoveMax=None):
"""
{"status":"COMPLETE","sortColumn":"lastMove","sortDirection":"ASC","numberOfResults":"100","page":"1","type":"searchGameHistory","automatedApiCall":false}:
{"playerNameA":"glassonion","buttonNameA":"Bunnies","buttonNameB":"McGinty","playerNameB":"Jota","gameStartMin":1546326000,"gameStartMax":1577775600,"status":"COMPLETE","sortColumn":"lastMove","sortDirection":"ASC","numberOfResults":"100","page":"1","type":"searchGameHistory","automatedApiCall":false}:
"""
args = {
'type': 'searchGameHistory',
'sortColumn': sortColumn,
'sortDirection': sortDirection,
'numberOfResults': numberOfResults,
'page': page
}
if status is not None:
args['status'] = status
if playerNameA is not None:
args['playerNameA'] = playerNameA
if playerNameB is not None:
args['playerNameB'] = playerNameB
if buttonNameA is not None:
args['buttonNameA'] = buttonNameA
if buttonNameB is not None:
args['buttonNameB'] = buttonNameB
if gameStartMin is not None:
args['gameStartMin'] = gameStartMin
if gameStartMax is not None:
args['gameStartMax'] = gameStartMax
if lastMoveMin is not None:
args['lastMoveMin'] = lastMoveMin
if lastMoveMax is not None:
args['lastMoveMax'] = lastMoveMax
return self._make_request(args)