Permalink
Browse files

Apply vehicle locks on stream in

Fixes #390
  • Loading branch information...
RussellLVP committed Oct 20, 2016
1 parent fef0ff5 commit d5ace6663222685e699244c9fbbe578a99a019a3
Showing with 46 additions and 0 deletions.
  1. +15 −0 javascript/entities/vehicle_manager.js
  2. +31 −0 javascript/entities/vehicle_manager.test.js
@@ -27,6 +27,8 @@ class VehicleManager {
'vehiclespawn', VehicleManager.prototype.onVehicleSpawn.bind(this));
this.callbacks_.addEventListener(
'vehicledeath', VehicleManager.prototype.onVehicleDeath.bind(this));
this.callbacks_.addEventListener(
'vehiclestreamin', VehicleManager.prototype.onVehicleStreamIn.bind(this));
// TODO(Russell): Handle OnVehicleDamangeStatusUpdate
// TODO(Russell): Handle OnVehicleMod
@@ -116,6 +118,19 @@ class VehicleManager {
this.notifyObservers('onVehicleDeath', vehicle);
}
// Called when a vehicle has streamed in for a particular player. The vehicle needs to be re-
// locked if a lock was in place for the particular player.
onVehicleStreamIn(event) {
const player = server.playerManager.getById(event.forplayerid);
const vehicle = this.vehicles_.get(event.vehicleid);
if (!player || !vehicle)
return; // either the player or the vehicle are not recognized
if (vehicle.isLockedForPlayer(player))
vehicle.lockForPlayer(player);
}
// ---------------------------------------------------------------------------------------------
// Called by the Player Manager when the player potentially requests to enter or exit their
@@ -369,4 +369,35 @@ describe('VehicleManager', (it, beforeEach) => {
assert.isNull(russell.vehicle);
}
});
it('should reapply locks to vehicles when they stream in for a player', assert => {
const vehicle = manager.createVehicle({ modelId: 441, position: new Vector(200, 300, 50) });
const gunther = server.playerManager.getById(0 /* Gunther */);
let counter = 0;
// Override the |lockForPlayer| method for the |vehicle| so that we can instrument it. The
// parent method will still be called, so that normal functionality continues to work.
vehicle.lockForPlayer = (function(player) {
this.__proto__.lockForPlayer.call(this, player);
counter++;
}).bind(vehicle);
vehicle.lockForPlayer(gunther);
assert.isTrue(vehicle.isLockedForPlayer(gunther));
assert.equal(counter, 1);
vehicle.streamInForPlayer(gunther);
assert.isTrue(vehicle.isLockedForPlayer(gunther));
assert.equal(counter, 2);
vehicle.unlockForPlayer(gunther);
assert.isFalse(vehicle.isLockedForPlayer(gunther));
assert.equal(counter, 2);
vehicle.streamInForPlayer(gunther);
assert.isFalse(vehicle.isLockedForPlayer(gunther));
assert.equal(counter, 2);
});
});

0 comments on commit d5ace66

Please sign in to comment.