boardgameio / boardgame.io

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

Multiplayer ctx.Numplayers stay at 2 ReactNative + Server #702

Open cynnfx opened 4 years ago

cynnfx commented 4 years ago

Hi first thank's for this awesome work!

I'm currently work on a little game with a react-native client. I've made a couple request to the lobby first to know the gameID, playerID and numPlayers. Then I call the client this way :

const App = Client({ game: game, numPlayers: numPlayer, board: board, multiplayer: SocketIO({ ... }), })

<App gameID={gameID} playerID={playerID} playerName={playerName} />

But if i look in ctx.numPlayers it always 2.

I am missing something?

delucis commented 4 years ago

Hey @cynnfx! How are you creating the game?

If you’re using the Lobby REST API, you need to set numPlayers in the body of your create request. Docs: https://boardgame.io/documentation/#/api/Lobby?id=creating-a-room

For example, using the Fetch API from a browser:

fetch('http://localhost:8000/games/game-name/create', {
  method: 'POST',
  body: JSON.stringify({ numPlayers: 4 }),
  headers: { 'Content-Type': 'application/json' },
});
cynnfx commented 4 years ago

Hi @delucis, yes i do a post request to : /games/game/create with body

{ "numPlayers": x }

and the lobby seems to handle it proprely because i can list rooms and the number of player slot is good

delucis commented 4 years ago

Hmm, interesting! Do you also see the incorrect numPlayers in the game state initialised by the server?

For example, if you add something like this as your game setup function:

const game = {
  name: 'game',
  setup: ctx => {
    console.log(ctx.numPlayers);
  },
};

Do you see the correct value in your server logs?

popey456963 commented 4 years ago

Have also found this whilst using the default lobby setup. Can request a one player game but the ctx has two players in it.

When logging the ctx variable on the server I see the same incorrect value (where there are two players).

mankuthimma commented 2 years ago

Not sure if this is a work around, but setting numPlayers: 1 in the Client initialisation in addition to the Lobby creation, works. Can anybody clarify if this is by design? (Seeing this behaviour in 0.49.10; the Client initialisation wasn't needed in 0.39.10). const GameClient = Client({ game: StarWarsGame, board: StarWarsBoard, numPlayers: 4, multiplayer: SocketIO({ server: server, }), });