From 96a40c71728851e0a2debd3a9e1896d8be1f759c Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Sat, 24 Feb 2018 20:54:27 +0000 Subject: [PATCH] Add new history-from-given-player API --- HanabiWeb/hanabi.py | 15 +++++++++++++-- README | 5 +++++ client.py | 31 ++++++++++++++++++++----------- server.py | 3 ++- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/HanabiWeb/hanabi.py b/HanabiWeb/hanabi.py index 9d7c464..1cfcd60 100644 --- a/HanabiWeb/hanabi.py +++ b/HanabiWeb/hanabi.py @@ -300,11 +300,13 @@ class Game(Resource): for c in data[cache.hands_key][p]: log(' {}'.format(str(c)), new_id) + log('-----') + return {'id': new_id} class History(Resource): - def get(self, game_id): + def get(self, game_id, player=None): _validate_game_id(game_id) _validate_game_exists(game_id) @@ -316,4 +318,13 @@ class History(Resource): with open(path) as f: lines = f.readlines() - return [l.strip() for l in lines] + if player is None: + return [l.strip() for l in lines] + + # Filter by what that player can see: all entries in the log past the + # line of dashes are public. + dashes = lines.index('-----') + if dashes == len(lines): + return [] + else: + return lines[dashes+1:] diff --git a/README b/README index db2fc43..e80c977 100644 --- a/README +++ b/README @@ -65,6 +65,11 @@ Returns a list of the indices of the matching cards in that player's hand. Retrieve the complete history of the specified game, output as a list of string descriptions. +## `/history//` +### GET +Retrieve the history of the specified game from the point of view of the given +player, output as a list of string descriptions. + # Future work ideas * Make the data storage format version-aware. diff --git a/client.py b/client.py index 50021c8..63c0f8a 100755 --- a/client.py +++ b/client.py @@ -24,6 +24,7 @@ _DEFAULT_PLAYER_FILE = os.path.join(os.path.expanduser("~"), _DEFAULT_SERVER_FILE = os.path.join(os.path.expanduser("~"), ".hanabi", "server.txt") + def get_player(): with open(_DEFAULT_PLAYER_FILE) as f: default_player = f.readlines()[0].strip() @@ -44,10 +45,6 @@ def get_server(): return input_str -def get_board_state(game_id, player): - pass - - def print_welcome(): print("Hanabi client.") print("Command-line version. Press Ctrl+C to exit.") @@ -58,15 +55,18 @@ class Actions(enum.Enum): PLAY = 1 DISCARD = 2 HISTORY = 3 - INFORM = 4 + ALL_HISTORY = 4 + INFORM = 5 _recognised_actions = {'print': (Actions.PRINT_GAMESTATE, 0), 'play': (Actions.PLAY, 1), 'discard': (Actions.DISCARD, 1), 'history': (Actions.HISTORY, 0), + 'all_history': (Actions.ALL_HISTORY, 0), 'inform': (Actions.INFORM, 1)} + def get_action(): """ Generator yielding the actions the user takes. @@ -111,20 +111,26 @@ def get_top(cards, colour): return max(iter) -def request_history(server, player, game_id): +def request_history(server, game_id, player=None): """ Request the history from the server from the point of view of a player. + + TODO: this API currently doesn't filter based on who is requesting. The + server needs to be fixed to do that. """ - # print("Requesting history...") - # url = server + '/history/{}'.format(game_id) - print("History is not yet implemented.") + print("Requesting history...") + url = server + '/history/{}'.format(game_id) + if player is not None: + url += '/{}'.format(player) + r = requests.get('http://' + url) + return r.json() def print_history(history): """ Print the history object retrieved from the server. """ - pass + print('\n'.join(history)) def print_gamestate(state): @@ -271,7 +277,10 @@ if __name__ == '__main__': state = request_gamestate(server, player, game_id) print_gamestate(state) elif action == Actions.HISTORY: - history = request_history(server, player, game_id) + history = request_history(server, game_id, player=player) + print_history(history) + elif action == Actions.ALL_HISTORY: + history = request_history(server, game_id) print_history(history) # TODO: need to pip install requests[security] when installing this \ No newline at end of file diff --git a/server.py b/server.py index 155e58a..2d5bf28 100644 --- a/server.py +++ b/server.py @@ -34,7 +34,8 @@ api.add_resource(HanabiWeb.hanabi.Inform, HanabiWeb.hanabi.History.method_decorators.append(limiter.limit('5 per minute')) api.add_resource(HanabiWeb.hanabi.History, - '/history/') + '/history/', + '/history//') if __name__ == "__main__": app.run(debug=True)