RanvierMUD / core

Core engine code for Ranvier
https://ranviermud.com
MIT License
45 stars 41 forks source link

Feature Suggestion: Stream-specific broadcasting #108

Open Sakeran opened 4 years ago

Sakeran commented 4 years ago

One issue developers might run into while developing for multiple TransportStream types is the need to send different types of information to different streams. For example, a websockets/GMCP-enabled stream might render a sidebar for character status (done with a single JSON payload), while telnet-only streams require printed text (ten calls to Broadcast#say). Notably, we don't want the calls to say to be sent to non-telnet streams.

The TransportStream#command method solves this to some extent, but requires the developer to bypass Broadcast and interface directly with the player's stream. Additionally, while command prevents streams from trying to do something they can't (sending JSON), it doesn't prevent them from doing something they can do but shouldn't (ten calls to Broadcast#say).

With the ability for TransportStreams to identify themselves via an identifier property (as specified in the stream decorator issue: https://github.com/RanvierMUD/core/issues/59), it is possible for Broadcast to pick and choose what socket types it should send to. For the sake of explicitness, I'd propose this callback-based approach, Broadcast#for, which is easiest to show by example:

// Show the character's status.

Broadcast.for("telnet", b => {
  // 'b' is a decorated version of Broadcast that will only
  // send to telnet-type streams.

  b.sayAt(player, '------');
  b.sayAt(player, "HP: 100");
  b.sayAt(player, "Strength: 12");
  b.sayAt(player, "Intellect: 6");
  b.sayAt(player, '------');
});

Broadcast.for("ws", b => {
  // 'b' is a decorated version of Broadcast that will only
  // send to websocket-type streams.

  // (This 'send' API is currently made up, but Broadcast could probably use
  // some function like this to generalize the "send arbitrary data" action)
  b.send(player, {
    type: 'character_status',
    hp: 100,
    strength: 12,
    intellect: 6
  });
});

This method would allow developers a clean way to separate the presentation of data based on the interface being used by the player.