boardgameio / boardgame.io

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

QoL improvements for the context #317

Closed RGBKnights closed 5 years ago

RGBKnights commented 5 years ago

I often found myself writing the same code to get the my opponent or a list of player. I did this so often I ended up writing my own helpers to do just these functions. I could it be helpful for others too and it should be cached in the context as to not compute it all the time...

I think you should add the following to the context: Players which returns an array of all the players enumerated from '0' to ctx.numPlayers Opponents which returns a array of players without the current player

I thought an Opponent field maybe equally as useful but might be hard to generalize for all games.

In theory: For 1 player games it would return undefined/null, for 2 player it would be the opposite of the current player, for 3 or more it would come down to the game and that is where a method in flow could be used to determine the opponent.

It is useful in basically every two player game and a number of card games where there are more then one player but your "Opponent" is directly left/right which could be looked up from the play order.

nicolodavis commented 5 years ago

I've been thinking about a general Player API myself. One that actually manages player state.

For example, it would be great if you can just do:

G.currentPlayer.field = value

and that updates data for the appropriate player each turn. The underlying data can be stored in an array, one element per player.

I think this should be an enhancement over G rather than ctx, which I'd like to keep read only as much as possible.

nicolodavis commented 5 years ago

Added this in https://github.com/nicolodavis/boardgame.io/commit/d34c213053e2c24a5d36cdff14ea1201307ccd2d which will be in the next release.

RGBKnights commented 5 years ago

I like what i see! I will test it out..

nicolodavis commented 5 years ago

Released in 0.29.0.

PluginPlayer adds fields player and opponent. Feel free to send me a PR to add more stuff that you see fit (and we can discuss on the PR).