diff --git a/tools/api-client/python/lib/bmapi.py b/tools/api-client/python/lib/bmapi.py index f9b57f985..17c671f1d 100644 --- a/tools/api-client/python/lib/bmapi.py +++ b/tools/api-client/python/lib/bmapi.py @@ -163,6 +163,28 @@ def load_game_data(self, gameId, logEntryLimit=10): } 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 @@ -216,6 +238,14 @@ def submit_die_values(self, gameId, swingArray, optionArray, roundNumber, timest 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', @@ -267,3 +297,39 @@ def choose_auxiliary_dice(self, gameId, action, dieIdx=None): 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) diff --git a/tools/api-client/python/lib/bmutils.py b/tools/api-client/python/lib/bmutils.py index 43cc3b4ab..47ffd6175 100644 --- a/tools/api-client/python/lib/bmutils.py +++ b/tools/api-client/python/lib/bmutils.py @@ -120,12 +120,46 @@ def wrap_react_to_new_game(self, game, accept): raise ValueError("Failed to call reactToNewGame, got: " + retval.message) return retval.data + def wrap_load_forum_thread(self, thread): + retval = self.client.load_forum_thread(thread) + if not retval.status == 'ok': + raise ValueError("Failed to call loadForumThread, got: " + retval.message) + return retval.data + + def wrap_edit_forum_post(self, postId, body): + retval = self.client.edit_forum_post(postId, body) + if not retval.status == 'ok': + raise ValueError("Failed to call editForumThread, got: " + retval.message) + return retval.data + + def wrap_submit_chat(self, game, chat): + retval = self.client.submit_chat(game, chat) + if not retval.status == 'ok': + raise ValueError("Failed to call submitChat, got: " + retval.message) + return retval.data + def wrap_load_completed_games(self): retval = self.client.load_completed_games() if not retval.status == 'ok': raise ValueError("Failed to call loadCompletedGames, got: " + retval.message) return self._wrap_game_list_data(retval.data) + def wrap_search_game_history(self, sortColumn, searchDirection="DESC", + numberOfResults=20, page=1, status=None, playerNameA=None, playerNameB=None, + buttonNameA=None, buttonNameB=None, gameStartMin=None, gameStartMax=None, + lastMoveMin=None, lastMoveMax=None): + retval = self.client.search_game_history( + sortColumn=sortColumn, sortDirection=searchDirection, + numberOfResults=numberOfResults, page=page, status=status, + playerNameA=playerNameA, playerNameB=playerNameB, + buttonNameA=buttonNameA, buttonNameB=buttonNameB, + gameStartMin=gameStartMin, gameStartMax=gameStartMax, + lastMoveMin=lastMoveMin, lastMoveMax=lastMoveMax + ) + if not retval.status == 'ok': + raise ValueError("Failed to call searchGameHistory, got: " + retval.message) + return retval.data + def wrap_create_game(self, pbutton, obutton='', player='', opponent='', description=''): retval = self.client.create_game(pbutton, obutton, player, opponent, description) if not retval.status == 'ok': @@ -172,3 +206,38 @@ def wrap_load_game_data(self, game): data['player'] = data['playerDataArray'][playerIdx] data['opponent'] = data['playerDataArray'][opponentIdx] return data + + def wrap_load_button_data(self, button): + # if we're using a cache directory + if self.cachedir: + # if the cache directory doesn't exist, create it + if not os.path.isdir(self.cachedir): + os.makedirs(self.cachedir) + # set the path to the cache file for this game + cachefile = self.cachedir + "/{}.json".format(urlsafe_b64encode(button.encode('utf-8')).decode('utf-8')) + # if the cache already has a file for this game + if os.path.isfile(cachefile): + # get the game data from the cache + with open(cachefile) as cache_fh: + data = json.load(cache_fh) + # otherwise (the cache didn't already have a file for this game) + else: + # load the game + retval = self.client.load_button_data(button) + # if that didn't work, raise an exception + if not retval.status == 'ok': + raise ValueError("Failed to call loadButtonData, got: " + retval.message) + # if we're still here, we have the game data + data = retval.data[0] + # put the data for this game into the cache + with open(cachefile, 'w') as cache_fh: + json.dump(data, cache_fh, indent=1, sort_keys=True) + # otherwise (we aren't using a cache directory), load the game + else: + retval = self.client.load_button_data(button) + if not retval.status == 'ok': + raise ValueError("Failed to call loadButtonData, got: " + retval.message) + data = retval.data[0] + # either way, at this point we have the button data in 'data', + # and we're done doing anything cache-related + return data