-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVehicleScheduleDataManager.cs
249 lines (226 loc) · 10.2 KB
/
VehicleScheduleDataManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using HarmonyLib;
using System;
using System.Collections.Generic;
using UnityEngine;
using VoxelTycoon;
using VoxelTycoon.Serialization;
using VoxelTycoon.Tracks;
using VoxelTycoon.Tracks.Tasks;
using static ScheduleStopwatch.VehicleScheduleCapacity;
using TaskTransfers = ScheduleStopwatch.VehicleScheduleCapacity.TaskTransfers;
namespace ScheduleStopwatch
{
[SchemaVersion(3)]
class VehicleScheduleDataManager : Manager<VehicleScheduleDataManager>
{
private const int DURATION_PER_STATION_RENEW_TIME_DAYS = 2;
private Dictionary<Vehicle, VehicleScheduleData> _vehiclesData = new Dictionary<Vehicle, VehicleScheduleData>();
private DurationsPerStationsContainer _durationPerStation;
private DateTime? _actualTimeOfDurationPerSt;
public VehicleScheduleData this[Vehicle vehicle] {
get
{
return _vehiclesData.TryGetValue(vehicle, out VehicleScheduleData vehicleScheduleData) ? vehicleScheduleData : null;
}
}
public void SubscribeDataChanged(Vehicle vehicle, Action<VehicleScheduleData, RootTask> handler, bool priority = false)
{
VehicleScheduleData data = GetOrCreateVehicleScheduleData(vehicle);
data.SubscribeDataChanged(handler, priority);
}
public void UnsubscribeDataChanged(Vehicle vehicle, Action<VehicleScheduleData, RootTask> handler)
{
VehicleScheduleData scheduleData = this[vehicle];
if (scheduleData != null)
{
scheduleData.UnsubscribeDataChanged(handler);
}
}
/** start listen events for measuring data */
public void StartMeasuring()
{
VehicleScheduleHelper.Current.DestinationReached -= OnDestinationReached;
VehicleScheduleHelper.Current.DestinationReached += OnDestinationReached;
VehicleScheduleHelper.Current.StationLeaved -= OnStationLeaved;
VehicleScheduleHelper.Current.StationLeaved += OnStationLeaved;
VehicleScheduleHelper.Current.MeasurementInvalidated -= OnMeasurementInvalidated;
VehicleScheduleHelper.Current.MeasurementInvalidated += OnMeasurementInvalidated;
VehicleScheduleHelper.Current.ScheduleChanged -= OnScheduleChanged;
VehicleScheduleHelper.Current.ScheduleChanged += OnScheduleChanged;
VehicleScheduleHelper.Current.VehicleRouteChanged -= OnVehicleRouteChanged;
VehicleScheduleHelper.Current.VehicleRouteChanged += OnVehicleRouteChanged;
}
protected override void OnInitialize()
{
LazyManager<VehicleManager>.Current.VehicleRemoved += OnRemoveVehicle;
LazyManager<VehicleManager>.Current.VehicleEdited += OnVehicleEdited;
}
public VehicleScheduleData GetOrCreateVehicleScheduleData(Vehicle vehicle)
{
if (!_vehiclesData.ContainsKey(vehicle) || _vehiclesData[vehicle] == null)
{
_vehiclesData[vehicle] = new VehicleScheduleData(vehicle);
}
return _vehiclesData[vehicle];
}
/** copies average values of all vehicles in the route and add it as one-time data (will be overwriten when own data are available) */
public VehicleScheduleData ReplaceVehicleScheduleDataFromRouteCopy(Vehicle vehicle)
{
if (vehicle.Route == null || vehicle.Route.Vehicles.Count <= 1)
{
throw new InvalidOperationException("Vehicle route is null or have only one vehicle");
}
VehicleScheduleData result = GetOrCreateVehicleScheduleData(vehicle);
result.NotificationsTurnedOff = true;
try
{
result.ClearAllData();
result.ChangeDataBufferSize(vehicle.Route.Vehicles.Count);
foreach (Vehicle currVehicle in vehicle.Route.Vehicles.ToList())
{
if (currVehicle == vehicle)
continue;
if (!_vehiclesData.TryGetValue(currVehicle, out VehicleScheduleData currData))
{
continue;
}
currData.AddAverageValuesToVehicleData(result);
}
result.AdjustDataAfterCopy();
}
finally
{
result.NotificationsTurnedOff = false;
}
return result;
}
public IReadOnlyDictionary<Item, TransferData> GetStationTaskTransfersSum(ImmutableList<Vehicle> vehicles, VehicleStationLocation station, out bool isIncomplete, out bool isEstimated)
{
TaskTransfers transfersSum = new TaskTransfers();
GetStationTaskTransfersSum(vehicles, station, out isIncomplete, out isEstimated, transfersSum);
return transfersSum.Transfers;
}
public void GetStationTaskTransfersSum(ImmutableList<Vehicle> vehicles, VehicleStationLocation station, out bool isIncomplete, out bool isEstimated, TaskTransfers transfersSum)
{
int count = vehicles.Count;
isIncomplete = false;
isEstimated = false;
for (int i = 0; i < vehicles.Count; i++)
{
if (!vehicles[i].IsEnabled)
{
continue;
}
VehicleScheduleData scheduleData = this[vehicles[i]];
IReadOnlyDictionary<Item, TransferData> transfers = scheduleData?.Capacity.GetTransfersPerStation()?[station];
float? mult;
if (transfers != null && (mult = scheduleData.ScheduleMonthlyMultiplier) != null)
{
isEstimated |= scheduleData.ScheduleAvereageDuration.Estimated;
transfersSum.Add(transfers, mult, scheduleData.ScheduleAvereageDuration.Estimated);
} else
{
isIncomplete = true;
}
}
}
public TimeSpan? GetGlobalTravelDuration(VehicleStationLocation startStation, VehicleStationLocation endStation, List<VehicleStationLocation> nonstopStations = null)
{
InvalidateDurationPerStation();
TimeSpan? result = _durationPerStation.GetTravelTime(startStation, endStation, nonstopStations);
if (result == null)
{
InvalidateDurationPerStation(true);
result = _durationPerStation.GetTravelTime(startStation, endStation, nonstopStations);
}
return result;
}
public TimeSpan? GetGlobalStationDuration(VehicleStationLocation station)
{
InvalidateDurationPerStation();
TimeSpan? result = _durationPerStation.GetStationTime(station);
if (result == null)
{
InvalidateDurationPerStation(true);
result = _durationPerStation.GetStationTime(station);
}
return result;
}
internal void InvalidateDurationPerStation(bool forceReload = false)
{
DateTime actTime = TimeManager.Current.DateTime;
if (_durationPerStation == null || _actualTimeOfDurationPerSt == null || (actTime - _actualTimeOfDurationPerSt.Value).TotalDays > DURATION_PER_STATION_RENEW_TIME_DAYS
|| (forceReload && actTime != _actualTimeOfDurationPerSt))
{
_actualTimeOfDurationPerSt = actTime;
if (_durationPerStation == null)
{
_durationPerStation = new DurationsPerStationsContainer();
}
_durationPerStation.MarkForOverwrite();
foreach (VehicleScheduleData scheduleData in _vehiclesData.Values)
{
_durationPerStation.AddTimes(scheduleData);
}
}
}
private void OnDestinationReached(Vehicle vehicle, VehicleStationLocation station, RootTask task)
{
GetOrCreateVehicleScheduleData(vehicle).OnDestinationReached(station, task);
}
private void OnStationLeaved(Vehicle vehicle, VehicleStationLocation station, RootTask task)
{
GetOrCreateVehicleScheduleData(vehicle).OnStationLeaved(station, task);
}
private void OnScheduleChanged(Vehicle vehicle, RootTask task, bool minorChange)
{
GetOrCreateVehicleScheduleData(vehicle).OnScheduleChanged(task, minorChange);
}
private void OnMeasurementInvalidated(Vehicle vehicle)
{
GetOrCreateVehicleScheduleData(vehicle).OnMeasurementInvalidated();
}
private void OnVehicleRouteChanged(Vehicle vehicle, VehicleRoute oldRoute, VehicleRoute newRoute)
{
if (newRoute != null && oldRoute != newRoute && newRoute.Vehicles.Count > 1)
{
VehicleScheduleData vehicleData = ReplaceVehicleScheduleDataFromRouteCopy(vehicle);
vehicleData.CallDataChangedEventsForRoute(null);
}
}
internal void OnRemoveVehicle(Vehicle vehicle)
{
if (_vehiclesData.TryGetValue(vehicle, out VehicleScheduleData data)) {
data.OnVehicleRemoved();
}
_vehiclesData.Remove(vehicle);
}
internal void OnVehicleEdited(Vehicle vehicle)
{
if (_vehiclesData.TryGetValue(vehicle, out VehicleScheduleData data))
{
data.OnVehicleEdited();
}
}
internal void Write(StateBinaryWriter writer)
{
writer.WriteInt(_vehiclesData.Count);
foreach (KeyValuePair<Vehicle, VehicleScheduleData> pair in _vehiclesData)
{
writer.WriteInt(pair.Key.Id);
pair.Value.Write(writer);
}
}
internal void Read(StateBinaryReader reader)
{
_vehiclesData.Clear();
int count = reader.ReadInt();
for (int i = 0; i < count; i++)
{
int vehicleId = reader.ReadInt();
Vehicle vehicle = LazyManager<VehicleManager>.Current.FindById(vehicleId);
_vehiclesData.Add(vehicle, VehicleScheduleData.Read(reader, vehicle));
}
}
}
}