Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
179 additions
and
13 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
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
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
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,50 @@ | ||
// 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 { Color } from 'base/color.js'; | ||
import { CommandBuilder } from 'components/command_manager/command_builder.js'; | ||
import { PlayerCommand } from 'features/player_commands/player_command.js'; | ||
|
||
// Implements the "/my color" command, which is available to VIPs who wish to change their color. | ||
// Administrators further have the ability to execute this command on other players. | ||
export class ColorCommand extends PlayerCommand { | ||
get name() { return 'color'; } | ||
get parameters() { | ||
return [ | ||
{ name: 'color', type: CommandBuilder.WORD_PARAMETER, optional: true } | ||
]; | ||
} | ||
|
||
// This command is only available to VIPs for now. | ||
get requireVip() { return true; } | ||
|
||
// Called when a player executes the "/my color" or "/p [player] color" command. Optionally a | ||
// RGB color can be given in |color|, which is only available for administrators. | ||
async execute(player, target, color) { | ||
if (color && !color.match(/^#[0-9a-f]{6}$/i)) { | ||
player.sendMessage(Message.PLAYER_COMMANDS_COLOR_INVALID_FORMAT); | ||
return; | ||
} | ||
|
||
// Sanitizes the color, either from HEX or by showing a color picker. | ||
color = color ? Color.fromHex(color.substring(1)) | ||
: await this.playerColors().displayColorPickerForPlayer(player); | ||
|
||
if (!color) | ||
return; // the |player| has aborted out of the colour selection flow | ||
|
||
target.colors.customColor = color; | ||
|
||
if (player === target) { | ||
player.sendMessage(Message.PLAYER_COMMANDS_COLOR_UPDATED_SELF); | ||
} else { | ||
this.announce().announceToAdministrators( | ||
Message.PLAYER_COMMANDS_COLOR_UPDATED_FYI_ADMIN, player.name, player.id, | ||
target.name, target.id, color.toHexRGB()); | ||
|
||
player.sendMessage(Message.PLAYER_COMMANDS_COLOR_UPDATED_OTHER, target.name, target.id); | ||
target.sendMessage(Message.PLAYER_COMMANDS_COLOR_UPDATED_FYI, player.name, player.id); | ||
} | ||
} | ||
} |
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,85 @@ | ||
// 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('ColorCommand', (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 players to change their colors', async (assert) => { | ||
// (1) Non-VIP players get an error message when changing their colour. | ||
assert.isTrue(await gunther.issueCommand('/my color')); | ||
|
||
assert.equal(gunther.messages.length, 1); | ||
assert.equal(gunther.messages[0], Message.PLAYER_COMMANDS_REQUIRES_VIP); | ||
|
||
assert.isTrue(await russell.issueCommand('/p Russell color')); | ||
|
||
assert.equal(russell.messages.length, 1); | ||
assert.equal(russell.messages[0], Message.PLAYER_COMMANDS_REQUIRES_VIP); | ||
|
||
gunther.setVip(true); | ||
russell.setVip(true); | ||
|
||
// (2) It's possible for players to change their own colour. | ||
{ | ||
const currentColor = russell.color; | ||
|
||
assert.isTrue(await russell.issueCommand('/my color')); | ||
assert.notDeepEqual(russell.color, currentColor); | ||
|
||
assert.equal(russell.messages.length, 2); | ||
assert.equal(russell.messages[1], Message.PLAYER_COMMANDS_COLOR_UPDATED_SELF); | ||
} | ||
|
||
// (3) It's possible for administrators to change other player's colours. | ||
{ | ||
const currentColor = gunther.color; | ||
|
||
assert.isTrue(await russell.issueCommand('/p gunth color')); | ||
assert.notDeepEqual(gunther.color, currentColor); | ||
|
||
assert.equal(russell.messages.length, 4); | ||
assert.includes( | ||
russell.messages[2], | ||
Message.format(Message.PLAYER_COMMANDS_COLOR_UPDATED_FYI_ADMIN, russell.name, | ||
russell.id, gunther.name, gunther.id, 'FFFF00')); | ||
|
||
assert.equal( | ||
russell.messages[3], | ||
Message.format(Message.PLAYER_COMMANDS_COLOR_UPDATED_OTHER, gunther.name, | ||
gunther.id)); | ||
|
||
assert.equal(gunther.messages.length, 2); | ||
assert.equal( | ||
gunther.messages[1], | ||
Message.format(Message.PLAYER_COMMANDS_COLOR_UPDATED_FYI, russell.name, | ||
russell.id)); | ||
} | ||
|
||
// (4) A warning message is shown when trying to update to an invalid color. | ||
assert.isTrue(await russell.issueCommand('/my color bananarama')); | ||
|
||
assert.equal(russell.messages.length, 5); | ||
assert.equal(russell.messages[4], Message.PLAYER_COMMANDS_COLOR_INVALID_FORMAT); | ||
|
||
// (5) Administrators can update colours to specific RGB values. | ||
{ | ||
const color = 'FFFF00'; | ||
|
||
assert.isTrue(await russell.issueCommand('/my color #' + color)); | ||
assert.equal(russell.color.toHexRGB(), color); | ||
} | ||
}); | ||
}); |
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
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
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
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,22 @@ | ||
// 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('PlayerCommandRegistry', (it, beforeEach) => { | ||
let registry = null; | ||
|
||
beforeEach(async () => { | ||
const feature = server.featureManager.loadFeature('player_commands'); | ||
|
||
registry = feature.registry_; | ||
|
||
// If this line throws an exception, then there's an import error with one of the commands | ||
// that's being dynamically imported. Read the exception message, and off you go. | ||
await registry.initialize(); | ||
}); | ||
|
||
it('should expose the /my and /p commands to the server', assert => { | ||
assert.isTrue(server.commandManager.hasCommand('my')); | ||
assert.isTrue(server.commandManager.hasCommand('p')); | ||
}); | ||
}); |
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