Sending "secret" data to individual players in Lance Game
Asked Answered
A

1

5

I'm wondering if there is an easy way in lance-gg to send player specific data to each player only, rather than emitting all data to all players.

I wish to have create a poker game, and don't want the data around what each player is holding broadcast to all players and instead only have each player receive information regarding their own cards.

Is this easily achievable between the current GameEngine and ServerEngine?

Over the course of the game the following steps will need to occur:

  • "deal" cards to each player (emit cards to each player individually, and also broadcast an event to indicate the other clients should animate the player being dealt cards receiving them)
  • store and hold the dealt cards outside the other data to be updated
  • retrieve player cards should the player be disconnected and then reconnect during the hand
  • "reveal" cards (broadcast any revealed cards like the flop and the showdown cards to all players)

Player's cards also need to be stored on the server but not re-broadcast with each step.

Androsphinx answered 4/6, 2017 at 8:28 Comment(0)
A
8

There is a low-level networking layer which can be used for client-server communication in Lance.

For example, if the server wants to send the event shakeItUp with data shakeData = { ... } to all clients, the game's serverEngine would call:

this.io.sockets.emit('shakeItUp', shakeData);

To send events and data to specific players, the serverEngine class can do

for (let socketId of Object.keys(this.connectedPlayers)) {
    let player = this.connectedPlayers[socketId];
    let playerId = player.socket.playerId;

    let message = `hello player ${playerId}`;
    this.connectedPlayers[socketId].socket.emit('secret', message);
}

The client listens to messages from the ClientEngine sub-class, after the connection is established:

// extend ClientEngine connect to add own events
connect() {
    return super.connect().then(() => {
        this.socket.on('secret', (e) => {
            console.log(`my secret: ${e}`);
        });
    });
}
Affirmation answered 4/6, 2017 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.