Skip to main content

Possibilities of Channels

Overview

Based on channels, you can implement various options for communication and interaction between players: chats and guilds, lists of friends and activity feeds, groups, lobbies and rooms. Channels can be configured quite flexibly, with their help it is possible to create turn-based battles and even organize transfer of gifts to players.

tip

The possibilities of using channels are huge and depend only on your ideas and imagination

To start working with channels, go to control panel, select the added game. From the menu on the left, select Channels - Add Channel, for details, see Manage Channels.

Players can create channels themselves. To do this, you need to create a channel template in the control panel: Channels - Add Template, see the Templates section for details:

Consider the concept of using channels using the example of:

  • 🌳 creating a location chat
  • 📢 channel change implementation
  • ⚔️ creating turn-based battles

Chat of Location

For example, a developer has created a chat called Dragon Peak with chat ID ID: 10. When entering the game, each player connects to the server and waits for notifications from other players or the administrator.

tip

The SDK does this automatically

If a player wants to go to the Dragon Peak location, then this can be done by entering the channel:

gp.channels.join({ channelId: 10 });

Once a player has joined a channel, they can receive notifications in that channel. Presence in the channel can be used as a guarantee of the presence of the player at the location in the game. The player can greet other chat participants by sending a message:

gp.channels.sendMessage({
channelId: 10,
text: 'Guys, hello everyone! Is it very difficult here?',
});

In order for other players to receive the message, you need to subscribe to it. When a message arrives, it can be rendered:

// message subscription
gp.channels.on('event:message', (message) => {
// here you render the chat message, example
mygame.displayMessage(message.player.name, message.text);
});

Next, consider changing the channel using the example of the same location.

Channel Change

Having increased the level, the player is ready to go to the next location House of Kings with identifier ID: 11. To do this, you need to exit the previous channel Dragon Peak and enter the channel House of Kings:

// exit the Dragon Peak Canal
gp.channels.leave({ channelId: 10 });
// join the House of Kings channel
gp.channels.join({ channelId: 11 });

Turn-based Battles

A full-fledged multiplayer through channels will not work, but you can implement a step-by-step mode. The idea is quite simple and is to send messages in the form of a battle log:

> "Player 1234" casts "Righteous Light" on "Zombie 2"
> "Zombie 2" takes 132 damage
> "Zombie 2" attacks "Player 1234"
> "Player 1234" takes 11 damage

Combat log entries in this format are visually understandable, but they can be difficult to work with in game. Let's represent messages as JSON objects:

gp.channels.sendMessage({
channelId: 123,
text: JSON.stringify({
type: 'ATTACK',
spell: 'RIGHTEOUS_LIGHT',
target: 'ZOMBIE_2',
}),
});

Result:

> {"type":"ATTACK","spell":"RIGHTEOUS_LIGHT","target":"ZOMBIE_2"}

In order to react to taking damage, one of the players must assume the role of a host. Usually this role is performed by the player - owner of the channel, it is he who will play the damage and reaction of the monsters.

To do this, subscribe to messages and, if the player is the owner of the channel, then play the battle mechanics:

// message subscription
gp.channels.on('event:message', (message) => {
// battle channel
if (message.channelId === battleChannel.id) {
// Parsing JSON messages
const result = JSON.parse(message.text);

// message type - attack and channel owner our player
if (result.type === 'ATTACK' && channel.ownerId === gp.player.id) {
handleAttackMessage(result);
}

// message type - the result of the attack, notification for everyone, including the owner of the channel
if (result.type === 'ATTACK_RESULT') {
handleAttackResultMessage(result);
}
}
});

// catch attack message
function handleAttackMessage(result) {
// get spell type
const spell = getSpell(result.spell);
// get the target of the spell
const target = getTarget(result.target);
// calculate damage
const damage = spell.damage - target.armor;
// notify everyone that the target has been damaged
gp.channels.sendMessage({
channelId: battleChannel.id,
text: JSON.stringify({
type: 'ATTACK_RESULT',
target: 'ZOMBIE_2',
damage: damage,
}),
});
}

// catch the attack result message
function handleAttackResultMessage(result) {
// get the target of the spell
const target = getTarget(result.target);
// change target's HP value to damage
target.hp -= result.damage;
}

Stay in Touch

Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.

GamePush Community Telegram: @gs_community.

For your suggestions e-mail: [email protected]

We Wish you Success!