mirror of
https://github.com/Smaug123/hanabi-server
synced 2025-10-05 16:48:40 +00:00
Add new history-from-given-player API
This commit is contained in:
@@ -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:]
|
||||
|
5
README
5
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/<game>/<player>`
|
||||
### 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.
|
||||
|
31
client.py
31
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
|
@@ -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/<int:game_id>')
|
||||
'/history/<int:game_id>',
|
||||
'/history/<int:game_id>/<string:player>')
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
Reference in New Issue
Block a user