Fix: Vehicle list windows did not update when this year's profit changed

Remove caching from vehicle group object. and recalculate it whenever
required instead.
This commit is contained in:
Charles Pigott
2021-02-21 15:35:15 +00:00
parent d79398a1d5
commit 6b8f9b50b9
2 changed files with 39 additions and 27 deletions

View File

@@ -14,32 +14,54 @@
#include "date_type.h"
#include "economy_type.h"
#include "sortlist_type.h"
#include "vehicle_base.h"
#include "vehiclelist.h"
#include "window_gui.h"
#include "widgets/dropdown_type.h"
#include <iterator>
#include <numeric>
typedef GUIList<const Vehicle*> GUIVehicleList;
struct GUIVehicleGroup {
VehicleList::const_iterator vehicles_begin; ///< Pointer to beginning element of this vehicle group.
VehicleList::const_iterator vehicles_end; ///< Pointer to past-the-end element of this vehicle group.
Money display_profit_this_year; ///< Total profit for the vehicle group this year.
Money display_profit_last_year; ///< Total profit for the vehicle group laste year.
Date age; ///< Age in days of oldest vehicle in the group.
GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end, Money display_profit_this_year, Money display_profit_last_year, Date age)
: vehicles_begin(vehicles_begin), vehicles_end(vehicles_end), display_profit_this_year(display_profit_this_year), display_profit_last_year(display_profit_last_year), age(age) {}
GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
: vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
std::ptrdiff_t NumVehicles() const
{
return std::distance(vehicles_begin, vehicles_end);
return std::distance(this->vehicles_begin, this->vehicles_end);
}
const Vehicle *GetSingleVehicle() const
{
assert(NumVehicles() == 1);
return vehicles_begin[0];
assert(this->NumVehicles() == 1);
return this->vehicles_begin[0];
}
Money GetDisplayProfitThisYear() const
{
return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
return acc + v->GetDisplayProfitThisYear();
});
}
Money GetDisplayProfitLastYear() const
{
return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
return acc + v->GetDisplayProfitLastYear();
});
}
Date GetOldestVehicleAge() const
{
const Vehicle *oldest = *std::max_element(this->vehicles_begin, this->vehicles_end, [](const Vehicle *v_a, const Vehicle *v_b) {
return v_a->age < v_b->age;
});
return oldest->age;
}
};