Permalink
Browse files

Display a disambiguation dialog for /v when necessary

Fixes #388
  • Loading branch information...
RussellLVP committed Oct 20, 2016
1 parent 6d0de34 commit 0c8a426985dddcd0d46a5ea7c4e230fd699323a4
@@ -27,8 +27,9 @@ class VehicleModel {
}
// Returns a VehicleModel instance by its |modelName|. Returns NULL when given an invalid name.
// Optionally a |fuzzy| search can be done too.
static getByName(modelName, fuzzy = false) {
// Optionally a |fuzzy| search can be done too. An array with all matches will be returned when
// the |all| property has been set.
static getByName(modelName, fuzzy = false, all = false) {
if (!fuzzy)
return modelsByName.get(modelName) || null;
@@ -40,6 +41,9 @@ class VehicleModel {
matches.push(model);
}
if (all)
return matches;
if (matches.length == 1)
return matches[0];
@@ -43,6 +43,14 @@ describe('VehicleModel', it => {
assert.equal(VehicleModel.getByName('RAM', true /* fuzzy */).id, 449);
});
it('should be able to find all vehicles with a given name', assert => {
const vehicles = VehicleModel.getByName('Firetruck', true /* fuzzy */, true /* all */);
assert.equal(vehicles.length, 2);
assert.equal(vehicles[0].id, 407);
assert.equal(vehicles[1].id, 544);
});
it('should be able to tell whether a vehicle is a trailer', assert => {
assert.isFalse(VehicleModel.getById(489 /* Rancher */).isTrailer());
assert.isFalse(VehicleModel.getById(411 /* Infernus */).isTrailer());
@@ -4,6 +4,7 @@
const CommandBuilder = require('components/command_manager/command_builder.js');
const DatabaseVehicle = require('features/vehicles/database_vehicle.js');
const Menu = require('components/menu/menu.js');
const VehicleAccessManager = require('features/vehicles/vehicle_access_manager.js');
// The maximum distance from the player to the vehicle closest to them, in units.
@@ -241,10 +242,31 @@ class VehicleCommands {
let vehicleModel = null;
const potentialModelId = modelIdentifier.toSafeInteger();
if (potentialModelId && potentialModelId >= 400 && potentialModelId <= 611)
if (potentialModelId && potentialModelId >= 400 && potentialModelId <= 611) {
vehicleModel = VehicleModel.getById(potentialModelId);
else
vehicleModel = VehicleModel.getByName(modelIdentifier, true /* fuzzy */);
} else {
const vehicleModels = VehicleModel.getByName(
modelIdentifier, true /* fuzzy */, true /* all */);
if (vehicleModels.length == 1) {
vehicleModel = vehicleModels[0];
} else if (vehicleModels.length > 1) {
const disambiguationDialog =
new Menu('Please disambiguate the vehicle!', ['Name', 'Model ID']);
// Sort the vehicles it found by name, alphabetically.
vehicleModels.sort((lhs, rhs) => lhs.name.localeCompare(rhs));
for (const model of vehicleModels)
disambiguationDialog.addItem(model.name, model.id);
const decision = await disambiguationDialog.displayForPlayer(player);
if (!decision || !decision.item[1])
return; // the player closed or mocked with the disambiguation dialog
vehicleModel = VehicleModel.getById(decision.item[1]);
}
}
if (!vehicleModel) {
player.sendMessage(Message.VEHICLE_SPAWN_NOT_FOUND, modelIdentifier);
@@ -394,6 +394,26 @@ describe('VehicleCommands', (it, beforeEach) => {
assert.equal(gunther.vehicle.modelId, 520 /* Hydra */);
});
it('should display a disambiguation dialog when the model is ambiguous', async(assert) => {
assert.equal(
VehicleModel.getByName('Firetruck', true /* fuzzy */, true /* all */).length, 2);
gunther.respondToDialog({ listitem: 1 /* Firetruck 2 */ });
const commandPromise = gunther.issueCommand('/v Firetruck');
await Promise.resolve(); // to trigger the command
await server.clock.advance(350); // to enter the vehicle
await commandPromise;
assert.equal(gunther.messages.length, 1);
assert.equal(
gunther.messages[0], Message.format(Message.VEHICLE_SPAWN_CREATED, 'Firetruck 2'));
assert.isNotNull(gunther.vehicle);
assert.equal(gunther.vehicle.modelId, 544 /* Firetruck 2 */);
});
it('should be able to display the current access restrictions of a vehicle', async(assert) => {
// Only administrators can display the restrictions of a vehicle.
gunther.level = Player.LEVEL_ADMINISTRATOR;

0 comments on commit 0c8a426

Please sign in to comment.