Skip to content

Commit 356eebd

Browse files
committed
Games: announce joining/leaving of players in continuous games
1 parent 18b7033 commit 356eebd

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

data/messages.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@
160160
"FRIENDS_REMOVED": "@success %s has been removed from your friends list.",
161161
"FRIENDS_USAGE": "{FFFFFF}Use '/friends add' to add a friend or '/friends remove' to remove one.",
162162

163+
"GAME_CONTINUOUS_JOINED": "{CCD782}* %s has joined the %s.",
164+
"GAME_CONTINUOUS_LEFT": "{CCD782}* %s has left the %s.",
163165
"GAME_REGISTRATION_ANNOUNCEMENT": "{CCD782}Sign up for the {838F31}%s{CCD782}! Type {838F31}/%s{CCD782} to join for %$.",
164166
"GAME_REGISTRATION_ANNOUNCEMENT_FREE": "{CCD782}Sign up for the {838F31}%s{CCD782}! Type {838F31}/%s{CCD782} to join.",
165167
"GAME_REGISTRATION_CREATED": "{CCD782}Please allow for up to {838F31}%d seconds{CCD782} for the game to start.",

javascript/features/games/game_runtime.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ export class GameRuntime extends GameActivity {
160160

161161
// Formally introduce the |player| to the game.
162162
await this.game_.onPlayerAdded(player);
163+
164+
// If the game had already started, we need to immediately spawn the |player| as well and
165+
// tell the other players that they have joined the game.
166+
if (this.state_ === GameRuntime.kStateRunning) {
167+
const gameName = this.description_.nameFn(this.settings_);
168+
169+
for (const target of this.players_) {
170+
if (target === player)
171+
continue; // no need to inform the |player| about their participation
172+
173+
target.sendMessage(Message.GAME_CONTINUOUS_JOINED, player.name, gameName);
174+
}
175+
176+
await this.onPlayerSpawn(player);
177+
}
163178
}
164179

165180
// Called when the |player| has to be removed from the game, either because they disconnected,
@@ -180,6 +195,15 @@ export class GameRuntime extends GameActivity {
180195
// Restore the |player|'s state -- back as if nothing ever happened.
181196
if (!disconnecting)
182197
player.restoreState();
198+
199+
// If the game is continuous, inform the remaining participants (if any) about the |player|
200+
// having left. This might influence their decision to stick around.
201+
if (this.description_.continuous) {
202+
const gameName = this.description_.nameFn(this.settings_);
203+
204+
for (const target of this.players_)
205+
target.sendMessage(Message.GAME_CONTINUOUS_LEFT, player.name, gameName);
206+
}
183207
}
184208

185209
// ---------------------------------------------------------------------------------------------

javascript/features/games/game_runtime.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ describe('GameRuntime', (it, beforeEach) => {
467467
// (1) The game should start immediately for |gunther|, even though other players are
468468
// available. This is because it's been marked as a continuous game.
469469
assert.isTrue(await gunther.issueCommand('/bubblegame'));
470+
assert.equal(gunther.messages.length, 1);
470471

471472
assert.isNotNull(manager.getPlayerActivity(gunther));
472473
assert.equal(
@@ -477,6 +478,12 @@ describe('GameRuntime', (it, beforeEach) => {
477478
// (2) Have Russell join the game as well. They should be added to the active game, rather
478479
// than be routed through registration and in a new game instead.
479480
assert.isTrue(await russell.issueCommand('/bubblegame'));
481+
assert.equal(russell.messages.length, 1);
482+
483+
assert.equal(gunther.messages.length, 2);
484+
assert.equal(
485+
gunther.messages[1],
486+
Message.format(Message.GAME_CONTINUOUS_JOINED, russell.name, 'Bubble'));
480487

481488
assert.isNotNull(manager.getPlayerActivity(russell));
482489
assert.equal(
@@ -488,6 +495,11 @@ describe('GameRuntime', (it, beforeEach) => {
488495
// (3) Have Gunther leave the game. Russell should still be playing.
489496
assert.isTrue(await gunther.issueCommand('/leave'));
490497

498+
assert.equal(russell.messages.length, 2);
499+
assert.equal(
500+
russell.messages[1],
501+
Message.format(Message.GAME_CONTINUOUS_LEFT, gunther.name, 'Bubble'));
502+
491503
assert.isNull(manager.getPlayerActivity(gunther));
492504
assert.equal(activePlayers, 1);
493505

0 commit comments

Comments
 (0)