boardgameio / boardgame.io

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

Possible feature request: INVALID_TURN constant? #344

Closed blunket closed 4 years ago

blunket commented 5 years ago

Currently my game client can call endTurn. However, I need to validate the turn one last time on the server-side in such a way that cannot easily be done on the client side while the turn is being made. I'd like to validate turns on both the client and server anyhow.

So after reading this: https://github.com/nicolodavis/boardgame.io/issues/91 ... It looks like the best solution (as of now) is to disable client-side endTurn calls, add an actual "endTurn" move, validate the turn there, and then call endTurn from the actual Game instance.

I really like that I can return INVALID_MOVE from a move function. Is there some possibility to have like an INVALID_TURN constant that i can return from the onTurnEnd hook?

(For a clearer picture: in my game, capturing an enemy piece on a turn is mandatory if at all possible, similar to checkers; I need to check and make sure, at the end of the turn, that the player has made the mandatory captures)

blunket commented 5 years ago

This is probably not high priority. Just may be convenient. The behavior I'd like to implement is that if the turn is considered invalid, all moves in the turn will be undone so the player can retry. (probably not all games will want to undo everything though.)

blunket commented 5 years ago

Quick update on this:

Actually as it turns out, I get the following error whenever my game move functions return ANYTHING (in this case, i have them returning INVALID_MOVE)

image

nicolodavis commented 5 years ago

The error is because you're modifying G in the same function where you return INVALID_MOVE, which is not supported by Immer. You should either:

  1. Modify G and not return anything, OR
  2. Return the new value of G (or INVALID_MOVE) without mutating G. but you can't do both.

I can see how this might be inconvenient. We should find a workaround.

blunket commented 5 years ago

Oh! Thank you for the tip!

The more I use this library the more I love it. And I think I'll be able to start helping you out sooner or later!! Just still learning but I'll do my best

nicolodavis commented 5 years ago

Great. Your feature requests and bug requests are quite helpful too, so keep them coming in as you notice issues!

nicolodavis commented 4 years ago

@blunket I think your idea is a good one (having a condition be satisfied before the turn is ended). However, I'd prefer to not add too many conditionals and validators to the API at the moment. I'll close this given that you can implement this idea using an endTurn move.

blunket commented 4 years ago

Hey @nicolodavis. Totally understandable for now, though I wonder how this will work with AI implementation given that it's not intended to be a valid move.

nicolodavis commented 4 years ago

Can you elaborate? A bot would never make invalid moves in the first place (i.e you would not have it end the turn before the condition has been fulfilled), so I'm not sure if we need this for AI purposes.

blunket commented 4 years ago

Oh, okay, I'm sorry! Thanks for explaining.