Skip to content

Commit

Permalink
Deathmatch Games: share statistics when leaving
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLVP committed Aug 2, 2020
1 parent 6beac35 commit cea9144
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions data/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
"GAME_REGISTRATION_UNAVAILABLE": "@error Sorry, the %s is currently unavailable.",
"GAME_RESULT_FINISHED": "{CCD782}You have finished the {838F31}%s{CCD782} in {838F31}%d%s position{CCD782}%s.",
"GAME_RESULT_FINISHED_AWARD": "{CCD782}You've been awarded {838F31}%${CCD782}!",
"GAME_STATS_ACCURACY": "{D7B882}Your accuracy was at {F5EEE0}%d%%{D7B882}, with {F5EEE0}%d hit%s{D7B882} and {F5EEE0}%d miss%s{D7B882}.",
"GAME_STATS_DAMAGE": "{D7B882}Finally, you inflicted {F5EEE0}%.2f damage{D7B882} and took {F5EEE0}%.2f damage{D7B882} yourself.",
"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}.",

"GANG_ANNOUNCE_CREATED": "%s has just founded the %s gang!",
"GANG_ANNOUNCE_INTERNAL": "{29B6F6}*** %s",
Expand Down
31 changes: 31 additions & 0 deletions javascript/features/games_deathmatch/deathmatch_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ export class DeathmatchGame extends GameBase {
if (!this.#lagCompensation_)
player.syncedData.lagCompensationMode = Player.kDefaultLagCompensationMode;

// Share the statistics with the player when they've been fighting. We tell them three lines
// at most: kills/death (+ratio), shots hit/missed (+accuracy), and damage statistics.
const statistics = player.stats.diff(state.statistics);
if (statistics)
this.shareStatisticsWithPlayer(player, statistics);

// Clear the player-specific state we had stored in this game.
this.#state_.delete(player);

Expand All @@ -266,6 +272,31 @@ export class DeathmatchGame extends GameBase {
}
}

// Shares the |statistics| with the |player|, based on how they've performed this game. A few
// lines will be highlighted. The |statistics| is a PlayerStatsView instance.
shareStatisticsWithPlayer(player, statistics) {
if (statistics.deathCount > 0 || statistics.killCount > 0) {
player.sendMessage(
Message.GAME_STATS_KD_RATIO,
statistics.killCount, statistics.killCount !== 1 ? 's' : '',
statistics.deathCount, statistics.deathCount !== 1 ? 's' : '',
statistics.ratio);
}

if (statistics.shotsHit > 0 || statistics.shotsMissed > 0) {
player.sendMessage(
Message.GAME_STATS_ACCURACY,
statistics.accuracy * 100,
statistics.shotsHit, statistics.shotsHit !== 1 ? 's' : '',
statistics.shotsMissed, statistics.shotsMissed !== 1 ? 'es' : '');
}

if (statistics.damageGiven > 0 || statistics.damageTaken > 0) {
player.sendMessage(
Message.GAME_STATS_DAMAGE, statistics.damageGiven, statistics.damageTaken);
}
}

// Called periodically to update the game's state. For games on a time limit, this is where the
// countdown will decrement, and we'll eventually finish the game.
async onTick() {
Expand Down
15 changes: 11 additions & 4 deletions javascript/features/games_deathmatch/games_deathmatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ describe('GamesDeathmatch', (it, beforeEach) => {
assert.equal(stats.shotsMissed, 10);
}

const guntherStats = game.getStatisticsForPlayer(gunther);
const russellStats = game.getStatisticsForPlayer(russell);

assert.isTrue(await gunther.issueCommand('/leave'));
assert.isTrue(await russell.issueCommand('/leave'));

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

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

console.log(gunther.messages);
assert.equal(gunther.messages.length, 3);
assert.equal(gunther.messages.length, 6);
assert.includes(gunther.messages[3], 'giving a ratio of');
assert.includes(gunther.messages[4], guntherStats.shotsMissed + ' miss');
assert.includes(gunther.messages[5], guntherStats.damageTaken + ' damage');

console.log(russell.messages);
assert.equal(russell.messages.length, 3);
assert.equal(russell.messages.length, 6);
assert.includes(russell.messages[3], russellStats.killCount + ' time');
assert.includes(russell.messages[4], Math.round(russellStats.accuracy * 100) + '%');
assert.includes(russell.messages[5], russellStats.damageGiven + ' damage');
});

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

0 comments on commit cea9144

Please sign in to comment.