Skip to content

Commit

Permalink
Move "/p freeze" and "/p unfreeze" to JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLVP committed Jul 22, 2020
1 parent 5084b19 commit 4e677b9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 25 deletions.
10 changes: 10 additions & 0 deletions data/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@
"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_FREEZE_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has frozen you.",
"PLAYER_COMMANDS_FREEZE_OTHER_ADMIN": "%s (Id:%d) has frozen %s (Id:%d).",
"PLAYER_COMMANDS_FREEZE_OTHER": "@success %s (Id:%d) has been frozen.",
"PLAYER_COMMANDS_FREEZE_SELF_ADMIN": "%s (Id:%d) has frozen themselves.",
"PLAYER_COMMANDS_FREEZE_SELF": "@success You have been frozen.",
"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.",
Expand All @@ -513,6 +518,11 @@
"PLAYER_COMMANDS_SPAWN_WEAPONS_TELEPORT_TARGET" : "@error Sorry, the player can't get weapons now because %s %s.",
"PLAYER_COMMANDS_SPAWN_WEAPONS_WEAPON": "@success %s with ammo multiplier '%d' has been bought.",
"PLAYER_COMMANDS_REQUIRES_VIP": "@error Sorry, this command is only available for VIPs. Check out /vip!",
"PLAYER_COMMANDS_UNFREEZE_FYI": "{33AA33}FYI{FFFFFF}: %s (Id:%d) has unfrozen you.",
"PLAYER_COMMANDS_UNFREEZE_OTHER_ADMIN": "%s (Id:%d) has unfrozen %s (Id:%d).",
"PLAYER_COMMANDS_UNFREEZE_OTHER": "@success %s (Id:%d) has been unfrozen.",
"PLAYER_COMMANDS_UNFREEZE_SELF_ADMIN": "%s (Id:%d) has unfrozen themselves.",
"PLAYER_COMMANDS_UNFREEZE_SELF": "@success You have been frozen.",

"POSITIONING_CURRENT_POSITION": "Your current position is X: %d, Y: %d Z: %d, Rotation: %d",
"POSITIONING_OTHER_USAGE_POS": "{FF9900}Other usage{FFFFFF}: /pos [x] [y] [z]",
Expand Down
5 changes: 4 additions & 1 deletion javascript/entities/test/mock_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class MockPlayer extends Player {

#color_ = Color.WHITE;
#colorOverrides_ = new Map();
#controllable_ = true;
#health_ = 100.0;
#armour_ = 0.0;
#skin_ = 308; // San Fierro Paramedic (EMT)
Expand Down Expand Up @@ -213,7 +214,9 @@ export class MockPlayer extends Player {
set rawColor(value) { this.#color_ = value; this.#colorOverrides_.clear(); }

get controllable() { throw new Error('Unable to get whether the player is controllable.'); }
set controllable(value) { /* no need to mock write-only values */ }
set controllable(value) { this.#controllable_ = value; }

get controllableForTesting() { return this.#controllable_; }

get health() { return this.#health_; }
set health(value) { this.#health_ = value; }
Expand Down
64 changes: 64 additions & 0 deletions javascript/features/player_commands/commands/freeze_command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 { PlayerCommand } from 'features/player_commands/player_command.js';

// Implements the "/my freeze" and "/p [player] freeze" commands, which makes it possible to freeze
// a particular player. This action will be relayed to the player directly.
export class FreezeCommand extends PlayerCommand {
get name() { return 'freeze'; }

// This command is not available to all players, only to administrators.
get playerLevel() { return Player.LEVEL_ADMINISTRATOR; }

// Called when a player executes the command. Will immediately freeze the |target|, and let them
// know about the adminstrative action being taken on them.
async execute(player, target) {
target.controllable = false;

if (player === target) {
this.announce().announceToAdministrators(
Message.PLAYER_COMMANDS_FREEZE_SELF_ADMIN, player.name, player.id);

player.sendMessage(Message.PLAYER_COMMANDS_FREEZE_SELF);

} else {
this.announce().announceToAdministrators(
Message.PLAYER_COMMANDS_FREEZE_OTHER_ADMIN, player.name, player.id, target.name,
target.id);

player.sendMessage(Message.PLAYER_COMMANDS_FREEZE_OTHER, target.name, target.id);
target.sendMessage(Message.PLAYER_COMMANDS_FREEZE_FYI, player.name, player.id);
}
}
}

// Implements the "/my unfreeze" and "/p [player] unfreeze" commands, which do the reverse of the
// freeze command: it makes it possible for the player to move around again.
export class UnfreezeCommand extends PlayerCommand {
get name() { return 'unfreeze'; }

// This command is not available to all players, only to administrators.
get playerLevel() { return Player.LEVEL_ADMINISTRATOR; }

// Called when a player executes the command. Will immediately release the given |target|.
async execute(player, target) {
target.controllable = true;

if (player === target) {
this.announce().announceToAdministrators(
Message.PLAYER_COMMANDS_UNFREEZE_SELF_ADMIN, player.name, player.id);

player.sendMessage(Message.PLAYER_COMMANDS_UNFREEZE_SELF);

} else {
this.announce().announceToAdministrators(
Message.PLAYER_COMMANDS_UNFREEZE_OTHER_ADMIN, player.name, player.id, target.name,
target.id);

player.sendMessage(Message.PLAYER_COMMANDS_UNFREEZE_OTHER, target.name, target.id);
target.sendMessage(Message.PLAYER_COMMANDS_UNFREEZE_FYI, player.name, player.id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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('FreezeCommand', (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 freeze and unfreeze players', async (assert) => {
assert.isTrue(gunther.controllableForTesting);
assert.isTrue(russell.controllableForTesting);

// (1) Russell has the ability to freeze Gunther.
assert.isTrue(await russell.issueCommand('/p gunt freeze'));
assert.isFalse(gunther.controllableForTesting);

assert.equal(russell.messages.length, 2);
assert.includes(
russell.messages[0],
Message.format(Message.PLAYER_COMMANDS_FREEZE_OTHER_ADMIN, russell.name, russell.id,
gunther.name, gunther.id));

assert.equal(
russell.messages[1],
Message.format(Message.PLAYER_COMMANDS_FREEZE_OTHER, gunther.name, gunther.id));

assert.equal(gunther.messages.length, 1);
assert.equal(
gunther.messages[0],
Message.format(Message.PLAYER_COMMANDS_FREEZE_FYI, russell.name, russell.id));

// (2) Russell has the ability to unfreeze Gunther.
assert.isTrue(await russell.issueCommand('/p gunt unfreeze'));
assert.isTrue(gunther.controllableForTesting);

assert.equal(russell.messages.length, 4);
assert.includes(
russell.messages[2],
Message.format(Message.PLAYER_COMMANDS_UNFREEZE_OTHER_ADMIN, russell.name, russell.id,
gunther.name, gunther.id));

assert.equal(
russell.messages[3],
Message.format(Message.PLAYER_COMMANDS_UNFREEZE_OTHER, gunther.name, gunther.id));

assert.equal(gunther.messages.length, 2);
assert.equal(
gunther.messages[1],
Message.format(Message.PLAYER_COMMANDS_UNFREEZE_FYI, russell.name, russell.id));

// (3) Russell has the ability to freeze and unfreeze themselves.
assert.isTrue(await russell.issueCommand('/my freeze'));
assert.isFalse(russell.controllableForTesting);

assert.equal(russell.messages.length, 6);
assert.includes(
russell.messages[4],
Message.format(Message.PLAYER_COMMANDS_FREEZE_SELF_ADMIN, russell.name, russell.id));

assert.equal(russell.messages[5], Message.PLAYER_COMMANDS_FREEZE_SELF);

assert.isTrue(await russell.issueCommand('/my unfreeze'));
assert.isTrue(russell.controllableForTesting);

assert.equal(russell.messages.length, 8);
assert.includes(
russell.messages[6],
Message.format(Message.PLAYER_COMMANDS_UNFREEZE_SELF_ADMIN, russell.name, russell.id));

assert.equal(russell.messages[7], Message.PLAYER_COMMANDS_UNFREEZE_SELF);
});
});
24 changes: 0 additions & 24 deletions pawn/Elements/AdministratorCommands.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -499,30 +499,6 @@ lvp_p(playerId, params[]) {
return 1;
}

if (!strcmp(playerParameter, "freeze", true, 6)) {
TogglePlayerControllable(subjectId, 0);

SendClientMessage(playerId, Color::Success, "Player frozen.");

format(g_message, sizeof(g_message), "%s (Id:%d) has frozen %s (Id:%d).",
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId);
Admin(playerId, g_message);

return 1;
}

if (!strcmp(playerParameter, "unfreeze", true, 8)) {
TogglePlayerControllable(subjectId, 1);

SendClientMessage(playerId, Color::Success, "Player unfrozen.");

format(g_message, sizeof(g_message), "%s (Id:%d) has unfrozen %s (Id:%d).",
Player(playerId)->nicknameString(), playerId, Player(subjectId)->nicknameString(), subjectId);
Admin(playerId, g_message);

return 1;
}

if (!strcmp(playerParameter, "weapon", true, 6) && Player(playerId)->isAdministrator() == true) {
new weaponId = Command->integerParameter(params, 2);
if (WeaponUtilities->isWeaponValid(weaponId) == false) {
Expand Down

0 comments on commit 4e677b9

Please sign in to comment.