Skip to content

Commit cea9144

Browse files
committed
Deathmatch Games: share statistics when leaving
1 parent 6beac35 commit cea9144

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

data/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@
178178
"GAME_REGISTRATION_UNAVAILABLE": "@error Sorry, the %s is currently unavailable.",
179179
"GAME_RESULT_FINISHED": "{CCD782}You have finished the {838F31}%s{CCD782} in {838F31}%d%s position{CCD782}%s.",
180180
"GAME_RESULT_FINISHED_AWARD": "{CCD782}You've been awarded {838F31}%${CCD782}!",
181+
"GAME_STATS_ACCURACY": "{D7B882}Your accuracy was at {F5EEE0}%d%%{D7B882}, with {F5EEE0}%d hit%s{D7B882} and {F5EEE0}%d miss%s{D7B882}.",
182+
"GAME_STATS_DAMAGE": "{D7B882}Finally, you inflicted {F5EEE0}%.2f damage{D7B882} and took {F5EEE0}%.2f damage{D7B882} yourself.",
183+
"GAME_STATS_KD_RATIO": "{D7B882}You killed {F5EEE0}%d time%s{D7B882} and died {F5EEE0}%d time%s{D7B882} yourself, giving a ratio of {F5EEE0}%.2f{D7B882}.",
181184

182185
"GANG_ANNOUNCE_CREATED": "%s has just founded the %s gang!",
183186
"GANG_ANNOUNCE_INTERNAL": "{29B6F6}*** %s",

javascript/features/games_deathmatch/deathmatch_game.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ export class DeathmatchGame extends GameBase {
246246
if (!this.#lagCompensation_)
247247
player.syncedData.lagCompensationMode = Player.kDefaultLagCompensationMode;
248248

249+
// Share the statistics with the player when they've been fighting. We tell them three lines
250+
// at most: kills/death (+ratio), shots hit/missed (+accuracy), and damage statistics.
251+
const statistics = player.stats.diff(state.statistics);
252+
if (statistics)
253+
this.shareStatisticsWithPlayer(player, statistics);
254+
249255
// Clear the player-specific state we had stored in this game.
250256
this.#state_.delete(player);
251257

@@ -266,6 +272,31 @@ export class DeathmatchGame extends GameBase {
266272
}
267273
}
268274

275+
// Shares the |statistics| with the |player|, based on how they've performed this game. A few
276+
// lines will be highlighted. The |statistics| is a PlayerStatsView instance.
277+
shareStatisticsWithPlayer(player, statistics) {
278+
if (statistics.deathCount > 0 || statistics.killCount > 0) {
279+
player.sendMessage(
280+
Message.GAME_STATS_KD_RATIO,
281+
statistics.killCount, statistics.killCount !== 1 ? 's' : '',
282+
statistics.deathCount, statistics.deathCount !== 1 ? 's' : '',
283+
statistics.ratio);
284+
}
285+
286+
if (statistics.shotsHit > 0 || statistics.shotsMissed > 0) {
287+
player.sendMessage(
288+
Message.GAME_STATS_ACCURACY,
289+
statistics.accuracy * 100,
290+
statistics.shotsHit, statistics.shotsHit !== 1 ? 's' : '',
291+
statistics.shotsMissed, statistics.shotsMissed !== 1 ? 'es' : '');
292+
}
293+
294+
if (statistics.damageGiven > 0 || statistics.damageTaken > 0) {
295+
player.sendMessage(
296+
Message.GAME_STATS_DAMAGE, statistics.damageGiven, statistics.damageTaken);
297+
}
298+
}
299+
269300
// Called periodically to update the game's state. For games on a time limit, this is where the
270301
// countdown will decrement, and we'll eventually finish the game.
271302
async onTick() {

javascript/features/games_deathmatch/games_deathmatch.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ describe('GamesDeathmatch', (it, beforeEach) => {
421421
assert.equal(stats.shotsMissed, 10);
422422
}
423423

424+
const guntherStats = game.getStatisticsForPlayer(gunther);
425+
const russellStats = game.getStatisticsForPlayer(russell);
426+
424427
assert.isTrue(await gunther.issueCommand('/leave'));
425428
assert.isTrue(await russell.issueCommand('/leave'));
426429

@@ -431,11 +434,15 @@ describe('GamesDeathmatch', (it, beforeEach) => {
431434

432435
assert.throws(() => game.getStatisticsForPlayer(gunther));
433436

434-
console.log(gunther.messages);
435-
assert.equal(gunther.messages.length, 3);
437+
assert.equal(gunther.messages.length, 6);
438+
assert.includes(gunther.messages[3], 'giving a ratio of');
439+
assert.includes(gunther.messages[4], guntherStats.shotsMissed + ' miss');
440+
assert.includes(gunther.messages[5], guntherStats.damageTaken + ' damage');
436441

437-
console.log(russell.messages);
438-
assert.equal(russell.messages.length, 3);
442+
assert.equal(russell.messages.length, 6);
443+
assert.includes(russell.messages[3], russellStats.killCount + ' time');
444+
assert.includes(russell.messages[4], Math.round(russellStats.accuracy * 100) + '%');
445+
assert.includes(russell.messages[5], russellStats.damageGiven + ' damage');
439446
});
440447

441448
it('should enable players to customise the settings', async (assert) => {

0 commit comments

Comments
 (0)