Skip to content

Commit

Permalink
server: TrainVehicleList more strict checks for add/remove
Browse files Browse the repository at this point in the history
- Do not add vehicle to an active train if it is already
  in an other active train

- When removing vehicle from active train reset
  activeTrain property

- When adding vehicle to active train, set it's activeTrain
  property
  • Loading branch information
gfgit committed Aug 18, 2024
1 parent 1582497 commit 40d20b9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions server/src/train/trainvehiclelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void TrainVehicleList::rowsChanged(uint32_t first, uint32_t last)

void TrainVehicleList::addObject(std::shared_ptr<RailVehicle> vehicle)
{
if(!vehicle)
return; //Cannot add null vehicles
if(!vehicle || (vehicle->activeTrain.value() && train().active))
return; //Cannot add null vehicles or vehicles in other active trains to an active train

std::shared_ptr<TrainVehicleListItem> object = getItemFromVehicle(vehicle);
if(object)
Expand All @@ -181,6 +181,9 @@ void TrainVehicleList::addObject(std::shared_ptr<RailVehicle> vehicle)
object->vehicle.setValueInternal(vehicle);
object->vehicle->trains.appendInternal(parent().shared_ptr<Train>());

if(train().active)
object->vehicle->activeTrain.setValueInternal(parent().shared_ptr<Train>());

items.appendInternal(object);
m_propertyChanged.emplace(object.get(), object->propertyChanged.connect(std::bind(&TrainVehicleList::propertyChanged, this, std::placeholders::_1)));

Expand All @@ -197,6 +200,9 @@ void TrainVehicleList::removeObject(const std::shared_ptr<TrainVehicleListItem>
m_propertyChanged[item.get()].disconnect();
m_propertyChanged.erase(item.get());

if(item->vehicle->activeTrain.value() == parent().shared_ptr<Train>())
item->vehicle->activeTrain.setValueInternal(nullptr);

item->vehicle->trains.removeInternal(parent().shared_ptr<Train>());
item->destroy();
items.removeInternal(item);
Expand Down

0 comments on commit 40d20b9

Please sign in to comment.