boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
10.01k stars 706 forks source link

Python bots cannot provide credentials #468

Closed tonyhschu closed 5 years ago

tonyhschu commented 5 years ago

The python bot base class in python/boardgameio.py does not have a way of providing credentials to the server. Thus bots created with it cannot join games created via the server REST API. i.e. the following wouldn't work:

# Returns gameID, which is the ID of the newly created game instance.
requestToCreateGame = requests.post(f"http://localhost:8000/games/tic-tac-toe/create")
createGameResponse = requestToCreateGame.json()
gameID = createGameResponse['gameID']

payload = {'playerID': playerID, 'playerName': playerName}

# Returns playerCredentials which is the token this player will require to authenticate their actions in the future.
requestToJoin = requests.post(f"http://localhost:8000/games/tictactoe/{gameID}/join", data=payload)
joinGameResponse = requestToJoin.json()

# This is the credentials
credentials = joinGameResponse['playerCredentials']

Bot(options={
  'playerID': playerID,
  'gameName': 'tic-tac-toe',
  'gameID': gameID
})

Seems like it would be straight forward to add it to the base class via options  – indeed, this is what I did to work around it for myself: https://github.com/tonyhschu/boardgame.io/tree/add-credentialing-to-python-bots

Essentially it's just adding credentials to the _create_action method

def _create_action(self, action, typ, args=None):
  if not args:
    args = []
  return {
    'type': action,
    'payload': {
      'type': typ,
      'args': args,
      'playerID': self.player_id,
      'credentials': self.credentials
    }
  }

Does that seem right?

nicolodavis commented 5 years ago

Looks good to me! @francoijs Do you have any thoughts?

Also, do you guys think it makes more sense to keep the Python stuff in a separate repo? It's a bit confusing to have both Python and JS code in one repo.

@francoijs Is this something that you would be interested in maintaining? If not, I can just keep it on my account in a new repo.

francoijs commented 5 years ago

@tonyhschu That would be a nice improvement! I can create a PR later for this, but feel free to submit one.

@nicolodavis Thanks for the proposal but would you mind if the new repo stays in the BGIO galaxy on your account? Of course I will actively contribute to maintenance and evolutions.

nicolodavis commented 5 years ago

Sure, no problem!

tonyhschu commented 5 years ago

Great! I'll prepare a PR tomorrow. Is there any kind of test around this that I should add/fix? (Other than the obvious one in test_boardgameio.py?

nicolodavis commented 5 years ago

The only tests for the Python code are in test_boardgameio.py.

nicolodavis commented 5 years ago

Thanks for the fix @tonyhschu!