Skip to content

Commit

Permalink
Resolve #2645 - Cargo Needs are not always correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkael committed Nov 4, 2024
1 parent e3d6dba commit a74c30d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 38 deletions.
55 changes: 29 additions & 26 deletions CargoMonitor/CargoMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand Down Expand Up @@ -68,12 +68,16 @@ public CargoMonitor(CargoMonitorConfiguration configuration = null)
{
BindingOperations.CollectionRegistering += Inventory_CollectionRegistering;
readInventory( configuration );
Task.Run( async () =>
ConfigService.Instance.PropertyChanged += ConfigChanged;
Logging.Info( $"Initialized {MonitorName()}" );
}

private void ConfigChanged ( object sender, PropertyChangedEventArgs e )
{
if ( e.PropertyName.Equals( nameof(ConfigService.Instance.missionMonitorConfiguration) ) )
{
await Task.Delay( TimeSpan.FromMilliseconds( 500 ) );
CalculateCargoNeeds();
} ).ConfigureAwait( false );
Logging.Info( $"Initialized {MonitorName()}" );
}
}

private void Inventory_CollectionRegistering(object sender, CollectionRegisteringEventArgs e)
Expand Down Expand Up @@ -178,13 +182,17 @@ public void PreHandle(Event @event)
public void PostHandle ( Event @event )
{
// Calculate cargo needs using the post handler (so that mission configuration information is already updated)
if ( @event.type.Contains("Depot") || @event.type.Contains("Mission") )
if ( @event.type.Contains( "Cargo" ) ||
@event.type.Contains( "Contribution" ) ||
@event.type.Contains( "Market" ) ||
@event.type.Contains( "Mining" ) ||
@event.type.Contains( "Mission" ) )
{
Task.Run( async () =>
if ( @event.timestamp >= updateDat )
{
await Task.Delay( TimeSpan.FromMilliseconds( 250 ) );
updateDat = @event.timestamp;
CalculateCargoNeeds();
} ).ConfigureAwait( false );
}
}
}

Expand Down Expand Up @@ -666,9 +674,10 @@ private void CalculateCargoNeeds ()
var missionsConfig = ConfigService.Instance.missionMonitorConfiguration.missions.ToList();
var missions = missionsConfig
.Where( m =>
m.statusDef == MissionStatus.Active &&
m.CommodityDefinition != null &&
m.amount != null )
m.amount != null &&
m.delivered < m.amount &&
!m.communal )
.ToList();

List<Cargo> currentCargo;
Expand All @@ -677,31 +686,25 @@ private void CalculateCargoNeeds ()
currentCargo = inventory.ToList();
}

// Add any mission commodities we need and do not currently possess
foreach ( var mission in missions )
// Add any mission cargo types we need and which are not already present in our inventory
foreach ( var mission in missions.Where( m => !currentCargo.Any( c =>
c.edname.Equals( m.CommodityDefinition.edname,
StringComparison.InvariantCultureIgnoreCase ) ) ) )
{
if ( currentCargo.SelectMany( c => c.missionCargo ).All( kv => kv.Key != mission.missionid ) )
{
var cargo = new Cargo( mission.CommodityDefinition.edname );
cargo.need += mission.amount ?? 0;
AddOrUpdateCargo( cargo );
}
var cargo = new Cargo( mission.CommodityDefinition.edname );
AddOrUpdateCargo( cargo );
}

// Update need for mission commodities we do possess
// Update need for each cargo type
foreach ( var cargo in currentCargo )
{
var missionsData = missions
.Where( m => m.CommodityDefinition.edname == cargo.commodityDef.edname )
.ToList();
var missionNeeds = missionsData.Sum( m => m.amount - m.delivered ) ?? 0;
var shipCargo = cargo.missionCargo
.Where( kv => missionsData.Select( m => m.missionid ).Contains( kv.Key ) )
.Sum( kv => kv.Value );
var wingCargo = missionsData
.Sum( m => m.wingCollected );
var wingCargo = missionsData.Sum( m => m.wingCollected );

cargo.need = missionNeeds - shipCargo - wingCargo;
cargo.need = missionNeeds - cargo.haulage - wingCargo;
TryRemoveCargo( cargo );
}

Expand Down
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Full details of the variables available for each noted event, and VoiceAttack in
* Added `Coriolis (Beta)` export target to Ship Monitor and `coriolisbeta` plugin command.
* Added new ship type `Mandalay` and modules.
* Added and updated powerplay object definitions.
* Fixed cargo need calculations. (#2645)
* Fixed child scripts not always using the latest state variables in their contexts.
* Fixed missing `systemname` property in the `Star scanned` event.
* Fixed a null reference exception which could occur when deleting scripts.
Expand Down
47 changes: 36 additions & 11 deletions DataDefinitions/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public string edname
}

// The number of stolen items
[PublicAPI]
[JsonProperty, PublicAPI]
public int stolen
{
get => _stolen;
Expand All @@ -44,14 +44,26 @@ public int stolen
}
}
}
[JsonProperty(nameof(stolen))]
private int _stolen;

// The number of items related to a mission currently on-board
public int haulage => missionCargo.Values.Sum();
[JsonIgnore]
public int haulage
{
get => _haulage;
set
{
if (_haulage != value )
{
_haulage = value;
NotifyPropertyChanged ( nameof(haulage ) );
}
}
}
private int _haulage;

// The number of collected/purchased items
[PublicAPI]
[JsonProperty, PublicAPI]
public int owned
{
get => _owned;
Expand All @@ -64,14 +76,13 @@ public int owned
}
}
}
[JsonProperty(nameof(owned))]
private int _owned;

[Obsolete( "please use owned instead" )]
public int other => owned;

// Mission items on board (with MissionID and count)
[PublicAPI("A dictionary where the key is a mission ID and the value is the amount of cargo associated with that mission ID")]
[JsonProperty, PublicAPI( "A dictionary where the key is a mission ID and the value is the amount of cargo associated with that mission ID")]
public Dictionary<long, int> missionCargo
{
get => _missionCargo;
Expand All @@ -80,14 +91,27 @@ public Dictionary<long, int> missionCargo
if ( _missionCargo != value )
{
_missionCargo = value;
NotifyPropertyChanged( nameof( missionCargo ) );
haulage = value.Values.Sum();
}
NotifyPropertyChanged( nameof( haulage ) );
}
}
[ JsonProperty(nameof(missionCargo)) ]
private Dictionary<long, int> _missionCargo = new Dictionary<long, int>();

[ PublicAPI ] public int need { get; set; }
[JsonProperty, PublicAPI]
public int need
{
get => _need;
set
{
if (_need != value )
{
_need = value;
NotifyPropertyChanged ( nameof(need ) );
}
}
}
private int _need;

// Total amount of the commodity

Expand Down Expand Up @@ -206,8 +230,7 @@ public void AddDetailedQty ( long missionID, int acquistionAmount )
{
missionCargo.Add( missionID, acquistionAmount );
}

NotifyPropertyChanged( nameof(missionCargo) );
haulage = missionCargo.Values.Sum();
}

/// <summary> Remove non-mission cargo </summary>
Expand Down Expand Up @@ -244,6 +267,8 @@ public void RemoveDetailedQty ( long missionID, int removedAmount )
{
missionCargo.Remove( missionID );
}

haulage = missionCargo.Values.Sum();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Tests/JournalMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,7 +2628,6 @@ public void TestStoredModulesEvent ()
Assert.AreEqual( "Jameson Memorial", storedModule.station );
}


[TestMethod]
public void TestShipTransferEvent ()
{
Expand Down

0 comments on commit a74c30d

Please sign in to comment.