-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate "/p armor" and "/p health" to JavaScript
- Loading branch information
1 parent
2d2318b
commit 74a3441
Showing
4 changed files
with
129 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
javascript/features/player_commands/commands/health_command.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2020 Las Venturas Playground. All rights reserved. | ||
// Use of this source code is governed by the MIT license, a copy of which can | ||
// be found in the LICENSE file. | ||
|
||
import { CommandBuilder } from 'components/command_manager/command_builder.js'; | ||
import { PlayerCommand } from 'features/player_commands/player_command.js'; | ||
|
||
// Implements the "/my health" and "/p [player] health" commands, which makes it possible for admins | ||
// to change the amount of health a particular player has. | ||
export class HealthCommand extends PlayerCommand { | ||
get name() { return 'health'; } | ||
get parameters() { | ||
return [ | ||
{ name: 'amount', type: CommandBuilder.NUMBER_PARAMETER, optional: true }, | ||
]; | ||
} | ||
|
||
// This command is not available to all players, only to administrators. | ||
get playerLevel() { return Player.LEVEL_ADMINISTRATOR; } | ||
|
||
// Called when a player executes the command. When |amount| is not given, their current health | ||
// level will be displayed to the administrator instead. | ||
async execute(player, target, amount) { | ||
if (typeof amount !== 'number' || amount < 0 || amount > 100) { | ||
if (player === target) { | ||
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_STATUS_SELF, player.health); | ||
} else { | ||
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_STATUS_OTHER, target.name, | ||
target.id, target.health); | ||
} | ||
|
||
return; | ||
} | ||
|
||
target.health = amount; | ||
|
||
if (player === target) { | ||
this.announce().announceToAdministrators( | ||
Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN, player.name, player.id, amount); | ||
|
||
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF, amount); | ||
|
||
} else { | ||
this.announce().announceToAdministrators( | ||
Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN, player.name, player.id, | ||
target.name, target.id, amount); | ||
|
||
player.sendMessage( | ||
Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER, target.name, target.id, amount); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
javascript/features/player_commands/commands/health_command.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2020 Las Venturas Playground. All rights reserved. | ||
// Use of this source code is governed by the MIT license, a copy of which can | ||
// be found in the LICENSE file. | ||
|
||
describe('HealthCommand', (it, beforeEach) => { | ||
let gunther = null; | ||
let russell = null; | ||
|
||
beforeEach(async () => { | ||
const feature = server.featureManager.loadFeature('player_commands'); | ||
|
||
gunther = server.playerManager.getById(/* Gunther= */ 0); | ||
russell = server.playerManager.getById(/* Russell= */ 1); | ||
russell.level = Player.LEVEL_ADMINISTRATOR; | ||
|
||
await feature.registry_.initialize(); | ||
await russell.identify(); | ||
}); | ||
|
||
it('should enable administrators to change player health values', async (assert) => { | ||
// (1) Russell, as an admin, is able to change their own health. | ||
assert.isTrue(await russell.issueCommand('/my health 75')); | ||
|
||
assert.equal(russell.messages.length, 2); | ||
assert.includes( | ||
russell.messages[0], | ||
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN, russell.name, | ||
russell.id, 75)); | ||
|
||
assert.equal( | ||
russell.messages[1], Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF, 75)); | ||
|
||
assert.equal(russell.health, 75); | ||
|
||
// (2) Russell, as an admin, is able to change other player's health values. | ||
assert.isTrue(await russell.issueCommand('/p gunt health 65')); | ||
|
||
assert.equal(russell.messages.length, 4); | ||
assert.includes( | ||
russell.messages[2], | ||
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN, russell.name, | ||
russell.id, gunther.name, gunther.id, gunther.health)); | ||
|
||
assert.equal( | ||
russell.messages[3], | ||
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER, gunther.name, gunther.id, | ||
gunther.health)); | ||
|
||
assert.equal(gunther.health, 65); | ||
|
||
// (3) It should be possible for Russell to see a player's current health level. | ||
assert.isTrue(await russell.issueCommand('/p 0 health')); | ||
|
||
assert.equal(russell.messages.length, 5); | ||
assert.equal( | ||
russell.messages[4], | ||
Message.format(Message.PLAYER_COMMANDS_HEALTH_STATUS_OTHER, gunther.name, gunther.id, | ||
gunther.health)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters