Skip to content
Permalink
Browse files
Migrate "/p armor" and "/p health" to JavaScript
  • Loading branch information
RussellLVP committed Jul 22, 2020
1 parent 2d2318b commit 74a34415f19dedf6f9cba2efb0c1961d1a78f184
Showing 4 changed files with 129 additions and 62 deletions.
@@ -484,15 +484,27 @@
"PLAYER_COMMANDS_COLOR_UPDATED_OTHER": "@success The color of %s (Id:%d) has been updated!",
"PLAYER_COMMANDS_COLOR_UPDATED_SELF": "@success Your color has been updated!",

"PLAYER_COMMANDS_ARMOR_STATUS_OTHER": "@usage /p [player] armor [0-100]. %s (Id:%d)'s armor currently is %.0f.",
"PLAYER_COMMANDS_ARMOR_STATUS_SELF": "@usage /my armor [0-100]. Your armor currently is %.0f.",
"PLAYER_COMMANDS_ARMOR_UPDATED_OTHER_ADMIN": "%s (Id:%d) has updated %s (Id:%d)'s armor to %d.",
"PLAYER_COMMANDS_ARMOR_UPDATED_OTHER": "@success %s (Id:%d)'s armor has been updated to %d.",
"PLAYER_COMMANDS_ARMOR_UPDATED_SELF_ADMIN": "%s (Id:%d) has updated their armor to %d.",
"PLAYER_COMMANDS_ARMOR_UPDATED_SELF": "@success Your armor has been updated to %d.",
"PLAYER_COMMANDS_HEALTH_STATUS_OTHER": "@usage /p [player] health [0-100]. %s (Id:%d)'s health currently is %.0f.",
"PLAYER_COMMANDS_HEALTH_STATUS_SELF": "@usage /my health [0-100]. Your health currently is %.0f.",
"PLAYER_COMMANDS_HEALTH_UPDATED_OTHER_ADMIN": "%s (Id:%d) has updated %s (Id:%d)'s health to %d.",
"PLAYER_COMMANDS_HEALTH_UPDATED_OTHER": "@success %s (Id:%d)'s health has been updated to %d.",
"PLAYER_COMMANDS_HEALTH_UPDATED_SELF_ADMIN": "%s (Id:%d) has updated their health to %d.",
"PLAYER_COMMANDS_HEALTH_UPDATED_SELF": "@success Your health has been updated to %d.",
"PLAYER_COMMANDS_HIDE_STATUS_OTHER": "@usage /p [player] hide [on/off]. They are currently %s.",
"PLAYER_COMMANDS_HIDE_STATUS_SELF": "@usage /my hide [on/off]. You are currently %s.",
"PLAYER_COMMANDS_HIDE_NO_CHANGE_OTHER": "@error What are you trying to do? They already are %s!",
"PLAYER_COMMANDS_HIDE_NO_CHANGE_SELF": "@error What are you trying to do? You already are %s!",
"PLAYER_COMMANDS_UPDATED_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has changed you to be %s.",
"PLAYER_COMMANDS_UPDATED_OTHER_ADMIN": "%s (Id:%d) has made %s (Id:%d) %s.",
"PLAYER_COMMANDS_UPDATED_OTHER": "@success %s (Id:%d) now is %s.",
"PLAYER_COMMANDS_UPDATED_SELF_ADMIN": "%s (Id:%d) has made themselves %s.",
"PLAYER_COMMANDS_UPDATED_SELF": "@success You are now %s.",
"PLAYER_COMMANDS_HIDE_UPDATED_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has changed you to be %s.",
"PLAYER_COMMANDS_HIDE_UPDATED_OTHER_ADMIN": "%s (Id:%d) has made %s (Id:%d) %s.",
"PLAYER_COMMANDS_HIDE_UPDATED_OTHER": "@success %s (Id:%d) now is %s.",
"PLAYER_COMMANDS_HIDE_UPDATED_SELF_ADMIN": "%s (Id:%d) has made themselves %s.",
"PLAYER_COMMANDS_HIDE_UPDATED_SELF": "@success You are now %s.",
"PLAYER_COMMANDS_SPAWN_WEAPONS_ARMOUR": "@success Spawn armour has been bought.",
"PLAYER_COMMANDS_SPAWN_WEAPONS_INVALID_AMOUNT" : "@error Sorry, you can only have a multiplier of 1-100.",
"PLAYER_COMMANDS_SPAWN_WEAPONS_INVALID_WEAPON" : "@error Sorry, id %d is not a valid spawn weapon.",
@@ -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);
}
}
}
@@ -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));
});
});
@@ -610,63 +610,6 @@ lvp_p(playerId, params[]) {
return 1;
}

if (!strcmp(playerParameter, "health", true, 6) && Player(playerId)->isAdministrator() == true) {
new healthAmount = Command->integerParameter(params, 2), Float: health;
GetPlayerHealth(subjectId, health);

if (healthAmount < 0 || healthAmount > 100) {
format(g_message, sizeof(g_message), "The health of %s (Id:%d) is %.1f",
Player(subjectId)->nicknameString(), subjectId, health);
SendClientMessage(playerId, Color::Success, g_message);

SendClientMessage(playerId, Color::Information, "Usage: /p [player] health [amount] to set health.");

return 1;
}

SetPlayerHealth(subjectId, healthAmount);

format(g_message, sizeof(g_message), "The health of %s (Id:%d) is now %d.",
Player(subjectId)->nicknameString(), subjectId, healthAmount);
SendClientMessage(playerId, Color::Success, g_message);

format(g_message, sizeof(g_message), "%s (Id:%d) has set the health for %s (Id:%d) to %d.",
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId,
healthAmount);
Admin(playerId, g_message);

return 1;
}

if ((!strcmp(playerParameter, "armor", true, 6) || !strcmp(playerParameter, "armour", true, 7))
&& Player(playerId)->isAdministrator() == true) {
new armourAmount = Command->integerParameter(params, 2), Float: armour;
GetPlayerArmour(subjectId, armour);

if (armourAmount < 0 || armourAmount > 100) {
format(g_message, sizeof(g_message), "The armour of %s (Id:%d) is %.1f",
Player(subjectId)->nicknameString(), subjectId, armour);
SendClientMessage(playerId, Color::Success, g_message);

SendClientMessage(playerId, Color::Information, "Usage: /p [player] armour [amount] to set armour.");

return 1;
}

SetPlayerArmour(subjectId, armourAmount);

format(g_message, sizeof(g_message), "The armour of %s (Id:%d) is now %d.",
Player(subjectId)->nicknameString(), subjectId, armourAmount);
SendClientMessage(playerId, Color::Success, g_message);

format(g_message, sizeof(g_message), "%s (Id:%d) has set the armour for %s (Id:%d) to %d.",
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId,
armourAmount);
Admin(playerId, g_message);

return 1;
}

if (!strcmp(playerParameter, "god", true, 3) && Player(playerId)->isAdministrator() == true) {
if (Command->parameterCount(params) != 3)
goto GodHelp;

0 comments on commit 74a3441

Please sign in to comment.