Skip to content

Commit 74a3441

Browse files
committed
Migrate "/p armor" and "/p health" to JavaScript
1 parent 2d2318b commit 74a3441

File tree

4 files changed

+129
-62
lines changed

4 files changed

+129
-62
lines changed

data/messages.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,27 @@
484484
"PLAYER_COMMANDS_COLOR_UPDATED_OTHER": "@success The color of %s (Id:%d) has been updated!",
485485
"PLAYER_COMMANDS_COLOR_UPDATED_SELF": "@success Your color has been updated!",
486486

487+
"PLAYER_COMMANDS_ARMOR_STATUS_OTHER": "@usage /p [player] armor [0-100]. %s (Id:%d)'s armor currently is %.0f.",
488+
"PLAYER_COMMANDS_ARMOR_STATUS_SELF": "@usage /my armor [0-100]. Your armor currently is %.0f.",
489+
"PLAYER_COMMANDS_ARMOR_UPDATED_OTHER_ADMIN": "%s (Id:%d) has updated %s (Id:%d)'s armor to %d.",
490+
"PLAYER_COMMANDS_ARMOR_UPDATED_OTHER": "@success %s (Id:%d)'s armor has been updated to %d.",
491+
"PLAYER_COMMANDS_ARMOR_UPDATED_SELF_ADMIN": "%s (Id:%d) has updated their armor to %d.",
492+
"PLAYER_COMMANDS_ARMOR_UPDATED_SELF": "@success Your armor has been updated to %d.",
493+
"PLAYER_COMMANDS_HEALTH_STATUS_OTHER": "@usage /p [player] health [0-100]. %s (Id:%d)'s health currently is %.0f.",
494+
"PLAYER_COMMANDS_HEALTH_STATUS_SELF": "@usage /my health [0-100]. Your health currently is %.0f.",
495+
"PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN": "%s (Id:%d) has updated %s (Id:%d)'s health to %d.",
496+
"PLAYER_COMMANDS_HEALTH_UPDATED_OTHER": "@success %s (Id:%d)'s health has been updated to %d.",
497+
"PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN": "%s (Id:%d) has updated their health to %d.",
498+
"PLAYER_COMMANDS_HEALTH_UPDATED_SELF": "@success Your health has been updated to %d.",
487499
"PLAYER_COMMANDS_HIDE_STATUS_OTHER": "@usage /p [player] hide [on/off]. They are currently %s.",
488500
"PLAYER_COMMANDS_HIDE_STATUS_SELF": "@usage /my hide [on/off]. You are currently %s.",
489501
"PLAYER_COMMANDS_HIDE_NO_CHANGE_OTHER": "@error What are you trying to do? They already are %s!",
490502
"PLAYER_COMMANDS_HIDE_NO_CHANGE_SELF": "@error What are you trying to do? You already are %s!",
491-
"PLAYER_COMMANDS_UPDATED_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has changed you to be %s.",
492-
"PLAYER_COMMANDS_UPDATED_OTHER_ADMIN": "%s (Id:%d) has made %s (Id:%d) %s.",
493-
"PLAYER_COMMANDS_UPDATED_OTHER": "@success %s (Id:%d) now is %s.",
494-
"PLAYER_COMMANDS_UPDATED_SELF_ADMIN": "%s (Id:%d) has made themselves %s.",
495-
"PLAYER_COMMANDS_UPDATED_SELF": "@success You are now %s.",
503+
"PLAYER_COMMANDS_HIDE_UPDATED_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has changed you to be %s.",
504+
"PLAYER_COMMANDS_HIDE_UPDATED_OTHER_ADMIN": "%s (Id:%d) has made %s (Id:%d) %s.",
505+
"PLAYER_COMMANDS_HIDE_UPDATED_OTHER": "@success %s (Id:%d) now is %s.",
506+
"PLAYER_COMMANDS_HIDE_UPDATED_SELF_ADMIN": "%s (Id:%d) has made themselves %s.",
507+
"PLAYER_COMMANDS_HIDE_UPDATED_SELF": "@success You are now %s.",
496508
"PLAYER_COMMANDS_SPAWN_WEAPONS_ARMOUR": "@success Spawn armour has been bought.",
497509
"PLAYER_COMMANDS_SPAWN_WEAPONS_INVALID_AMOUNT" : "@error Sorry, you can only have a multiplier of 1-100.",
498510
"PLAYER_COMMANDS_SPAWN_WEAPONS_INVALID_WEAPON" : "@error Sorry, id %d is not a valid spawn weapon.",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2020 Las Venturas Playground. All rights reserved.
2+
// Use of this source code is governed by the MIT license, a copy of which can
3+
// be found in the LICENSE file.
4+
5+
import { CommandBuilder } from 'components/command_manager/command_builder.js';
6+
import { PlayerCommand } from 'features/player_commands/player_command.js';
7+
8+
// Implements the "/my health" and "/p [player] health" commands, which makes it possible for admins
9+
// to change the amount of health a particular player has.
10+
export class HealthCommand extends PlayerCommand {
11+
get name() { return 'health'; }
12+
get parameters() {
13+
return [
14+
{ name: 'amount', type: CommandBuilder.NUMBER_PARAMETER, optional: true },
15+
];
16+
}
17+
18+
// This command is not available to all players, only to administrators.
19+
get playerLevel() { return Player.LEVEL_ADMINISTRATOR; }
20+
21+
// Called when a player executes the command. When |amount| is not given, their current health
22+
// level will be displayed to the administrator instead.
23+
async execute(player, target, amount) {
24+
if (typeof amount !== 'number' || amount < 0 || amount > 100) {
25+
if (player === target) {
26+
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_STATUS_SELF, player.health);
27+
} else {
28+
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_STATUS_OTHER, target.name,
29+
target.id, target.health);
30+
}
31+
32+
return;
33+
}
34+
35+
target.health = amount;
36+
37+
if (player === target) {
38+
this.announce().announceToAdministrators(
39+
Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN, player.name, player.id, amount);
40+
41+
player.sendMessage(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF, amount);
42+
43+
} else {
44+
this.announce().announceToAdministrators(
45+
Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN, player.name, player.id,
46+
target.name, target.id, amount);
47+
48+
player.sendMessage(
49+
Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER, target.name, target.id, amount);
50+
}
51+
}
52+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 Las Venturas Playground. All rights reserved.
2+
// Use of this source code is governed by the MIT license, a copy of which can
3+
// be found in the LICENSE file.
4+
5+
describe('HealthCommand', (it, beforeEach) => {
6+
let gunther = null;
7+
let russell = null;
8+
9+
beforeEach(async () => {
10+
const feature = server.featureManager.loadFeature('player_commands');
11+
12+
gunther = server.playerManager.getById(/* Gunther= */ 0);
13+
russell = server.playerManager.getById(/* Russell= */ 1);
14+
russell.level = Player.LEVEL_ADMINISTRATOR;
15+
16+
await feature.registry_.initialize();
17+
await russell.identify();
18+
});
19+
20+
it('should enable administrators to change player health values', async (assert) => {
21+
// (1) Russell, as an admin, is able to change their own health.
22+
assert.isTrue(await russell.issueCommand('/my health 75'));
23+
24+
assert.equal(russell.messages.length, 2);
25+
assert.includes(
26+
russell.messages[0],
27+
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN, russell.name,
28+
russell.id, 75));
29+
30+
assert.equal(
31+
russell.messages[1], Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_SELF, 75));
32+
33+
assert.equal(russell.health, 75);
34+
35+
// (2) Russell, as an admin, is able to change other player's health values.
36+
assert.isTrue(await russell.issueCommand('/p gunt health 65'));
37+
38+
assert.equal(russell.messages.length, 4);
39+
assert.includes(
40+
russell.messages[2],
41+
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN, russell.name,
42+
russell.id, gunther.name, gunther.id, gunther.health));
43+
44+
assert.equal(
45+
russell.messages[3],
46+
Message.format(Message.PLAYER_COMMANDS_HEALTH_UPDATED_OTHER, gunther.name, gunther.id,
47+
gunther.health));
48+
49+
assert.equal(gunther.health, 65);
50+
51+
// (3) It should be possible for Russell to see a player's current health level.
52+
assert.isTrue(await russell.issueCommand('/p 0 health'));
53+
54+
assert.equal(russell.messages.length, 5);
55+
assert.equal(
56+
russell.messages[4],
57+
Message.format(Message.PLAYER_COMMANDS_HEALTH_STATUS_OTHER, gunther.name, gunther.id,
58+
gunther.health));
59+
});
60+
});

pawn/Elements/AdministratorCommands.pwn

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -610,63 +610,6 @@ lvp_p(playerId, params[]) {
610610
return 1;
611611
}
612612

613-
if (!strcmp(playerParameter, "health", true, 6) && Player(playerId)->isAdministrator() == true) {
614-
new healthAmount = Command->integerParameter(params, 2), Float: health;
615-
GetPlayerHealth(subjectId, health);
616-
617-
if (healthAmount < 0 || healthAmount > 100) {
618-
format(g_message, sizeof(g_message), "The health of %s (Id:%d) is %.1f",
619-
Player(subjectId)->nicknameString(), subjectId, health);
620-
SendClientMessage(playerId, Color::Success, g_message);
621-
622-
SendClientMessage(playerId, Color::Information, "Usage: /p [player] health [amount] to set health.");
623-
624-
return 1;
625-
}
626-
627-
SetPlayerHealth(subjectId, healthAmount);
628-
629-
format(g_message, sizeof(g_message), "The health of %s (Id:%d) is now %d.",
630-
Player(subjectId)->nicknameString(), subjectId, healthAmount);
631-
SendClientMessage(playerId, Color::Success, g_message);
632-
633-
format(g_message, sizeof(g_message), "%s (Id:%d) has set the health for %s (Id:%d) to %d.",
634-
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId,
635-
healthAmount);
636-
Admin(playerId, g_message);
637-
638-
return 1;
639-
}
640-
641-
if ((!strcmp(playerParameter, "armor", true, 6) || !strcmp(playerParameter, "armour", true, 7))
642-
&& Player(playerId)->isAdministrator() == true) {
643-
new armourAmount = Command->integerParameter(params, 2), Float: armour;
644-
GetPlayerArmour(subjectId, armour);
645-
646-
if (armourAmount < 0 || armourAmount > 100) {
647-
format(g_message, sizeof(g_message), "The armour of %s (Id:%d) is %.1f",
648-
Player(subjectId)->nicknameString(), subjectId, armour);
649-
SendClientMessage(playerId, Color::Success, g_message);
650-
651-
SendClientMessage(playerId, Color::Information, "Usage: /p [player] armour [amount] to set armour.");
652-
653-
return 1;
654-
}
655-
656-
SetPlayerArmour(subjectId, armourAmount);
657-
658-
format(g_message, sizeof(g_message), "The armour of %s (Id:%d) is now %d.",
659-
Player(subjectId)->nicknameString(), subjectId, armourAmount);
660-
SendClientMessage(playerId, Color::Success, g_message);
661-
662-
format(g_message, sizeof(g_message), "%s (Id:%d) has set the armour for %s (Id:%d) to %d.",
663-
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId,
664-
armourAmount);
665-
Admin(playerId, g_message);
666-
667-
return 1;
668-
}
669-
670613
if (!strcmp(playerParameter, "god", true, 3) && Player(playerId)->isAdministrator() == true) {
671614
if (Command->parameterCount(params) != 3)
672615
goto GodHelp;

0 commit comments

Comments
 (0)