Sending “secret” data to individual players in Lance Game

二次信任 提交于 2019-12-10 15:45:47

问题


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.


回答1:


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}`);
        });
    });
}


来源:https://stackoverflow.com/questions/44352173/sending-secret-data-to-individual-players-in-lance-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!