boardgame.io icon indicating copy to clipboard operation
boardgame.io copied to clipboard

Choice of processing game events on client

Open joepinion opened this issue 2 years ago • 4 comments

From reducer.ts https://github.com/boardgameio/boardgame.io/blob/main/src/core/reducer.ts

` case Actions.GAME_EVENT: { state = { ...state, deltalog: [] };

    // Process game events only on the server.
    // These events like `endTurn` typically
    // contain code that may rely on secret state
    // and cannot be computed on the client.
    if (isClient) {
      return state;
    }`

This seems like too big of an assumption. Just now realized why some of my stuff wouldn't process on client - moves that change game phases automatically make a move unresolvable on the client.

Ideally, move.client could resolve to a function, much like undoable does, and then the above code only triggers if move.client isn't explicitly set.

Honestly, this seems like an easy task and one I'd love to take a stab at making my first contribution with... What I'm missing is basic instructions on how to set up a dev environment for boardgame.io and test my code live. I understand how to fork the repo and make a pull request, I just don't know what the best way to get a dev environment up and running on the code. Maybe it's too basic of a question but it's not covered at https://github.com/boardgameio/boardgame.io/blob/main/CONTRIBUTING.md

joepinion avatar Aug 29 '21 19:08 joepinion

Related: Moves that do manage to process on client are missing some stuff - ctx doesn't update, and the log doesn't update - only G and client-allowed plugins update. Once again, something I'd like to work on, if I can be pointed to dev environment setup help.

joepinion avatar Aug 29 '21 20:08 joepinion

@joepinion That code snippet actually isn’t run for moves at all, but for events that are called directly, like client.events.endTurn() as opposed to a client.moves.someMove() that calls ctx.events.endTurn() internally. I’m guessing that’s partly why the assumption is made there: there’s no way to disable onEnd and other hooks from running once we process an event, so removing that check would mean any game that uses secret state from a hook would crash on the client. I guess to be fully flexible, each hook would have to be able to declare client: false like moves do and if any hook triggered by events had that flag, then we would bail out of processing and wait for the server.

If you are looking for events triggered by moves, that same behaviour is implemented by the plugin API here:

https://github.com/boardgameio/boardgame.io/blob/1368d55e181cc628e9e92d9abcfee2f0660382d2/src/plugins/plugin-events.ts#L18

We still run into the same issue though: a move itself might be safe to run, but a hook isn’t, so we need a way for the hook to tell us that.


Regarding getting a dev environment set up: we recently had a PR adding “dev container” support for VS Code that allows you to clone the repository in a preconfigured Docker container. Instructions are here: https://github.com/boardgameio/boardgame.io/blob/main/CONTRIBUTING.md#vs-code-remote-dev-container-support Would that help at all? It would be a good opportunity for us to road test how usable it is and if the instructions are clear enough. If that doesn’t work for whatever reason, let me know, and I’ll be happy to offer some alternative options.

delucis avatar Aug 30 '21 09:08 delucis

I will definitely try to use the VS instructions, thank you. Is there an issue related to that to comment on?

I do the hooks should all allow this, yes. Will do the VS instructions first, and see if I can dig in and see if that seems viable.

joepinion avatar Sep 02 '21 21:09 joepinion

There’s no open issue regarding the VS Code set-up at the moment. If you have any questions, you can just post them here and then if you want to put together an issue describing any problems you run into — or a PR helping clarify them — please do.

delucis avatar Sep 02 '21 21:09 delucis