-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathCargo.cs
More file actions
276 lines (240 loc) · 8.64 KB
/
Cargo.cs
File metadata and controls
276 lines (240 loc) · 8.64 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using Utilities;
namespace EddiDataDefinitions
{
/// <summary>
/// Cargo defines a number of commodities carried along with some additional data
/// </summary>
[JsonObject( MemberSerialization.OptIn )]
public class Cargo : INotifyPropertyChanged
{
// The commodity name
public string invariantName => commodityDef?.invariantName ?? "";
public string localizedName => commodityDef?.localizedName ?? "";
[PublicAPI, Obsolete( "Please use localizedName or invariantName" )]
public string name => localizedName;
[JsonProperty( nameof( edname ) )]
public string edname
{
get => commodityDef.edname;
set => commodityDef = CommodityDefinition.FromEDName( value );
}
// The number of stolen items
[JsonProperty, PublicAPI]
public int stolen
{
get => _stolen;
set
{
if ( _stolen != value )
{
_stolen = value;
NotifyPropertyChanged( nameof( stolen ) );
}
}
}
private int _stolen;
// The number of items related to a mission currently on-board
[JsonIgnore]
public int haulage
{
get => _haulage;
set
{
if (_haulage != value )
{
_haulage = value;
NotifyPropertyChanged ( nameof(haulage ) );
}
}
}
private int _haulage;
// The number of collected/purchased items
[JsonProperty, PublicAPI]
public int owned
{
get => _owned;
set
{
if ( _owned != value )
{
_owned = value;
NotifyPropertyChanged( nameof( owned ) );
}
}
}
private int _owned;
[Obsolete( "please use owned instead" )]
public int other => owned;
// Mission items on board (with MissionID and count)
[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<ulong, int> missionCargo
{
get => _missionCargo;
set
{
if ( _missionCargo != value )
{
_missionCargo = value;
haulage = value.Values.Sum();
}
NotifyPropertyChanged( nameof( haulage ) );
}
}
private Dictionary<ulong, int> _missionCargo = [ ];
[JsonProperty, PublicAPI]
public int need
{
get => _need;
set
{
if (_need != value )
{
_need = value;
NotifyPropertyChanged ( nameof(need ) );
}
}
}
private int _need;
// Total amount of the commodity
[PublicAPI]
public int total => haulage + stolen + owned;
// How much we actually paid for it (per unit)
[PublicAPI]
public int price => decimal.ToInt32( weightedAvgPrice );
[JsonProperty(nameof(price))]
private decimal weightedAvgPrice;
// The commodity category, localized
public string localizedCategory => commodityDef?.Category?.localizedName;
// deprecated commodity category (exposed to Cottle and VA)
[PublicAPI, Obsolete( "Please use localizedCategory instead" )]
public string category => localizedCategory;
private CommodityDefinition _commodityDef;
public CommodityDefinition commodityDef
{
get => _commodityDef;
set
{
_commodityDef = value;
NotifyPropertyChanged( nameof(invariantName) );
NotifyPropertyChanged( nameof(localizedName) );
NotifyPropertyChanged( nameof(localizedCategory) );
}
}
[PublicAPI, Obsolete("Please use commodityDef instead")]
public CommodityDefinition commodity => commodityDef;
[JsonExtensionData]
private IDictionary<string, JToken> _additionalJsonData;
[OnDeserialized]
private void OnDeserialized ( StreamingContext context )
{
if ( commodityDef == null )
{
// legacy JSON with no edname in the top level
edname = (string)_additionalJsonData[ "commodity" ][ "edname" ];
owned = (int)_additionalJsonData[ "other" ];
}
_additionalJsonData = null;
}
// Default Constructor
public Cargo () { }
[JsonConstructor]
public Cargo ( string edname )
{
commodityDef = CommodityDefinition.FromEDName( edname );
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged ( string propName )
{
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propName ) );
}
public void UpdateWeightedPrice ( decimal newPrice, int newAmount )
{
if ( newAmount == 0 ) { return; }
var weightedValueSum = (weightedAvgPrice * owned) + (newPrice * newAmount);
var weightedQtySum = owned + newAmount;
if ( weightedQtySum > 0 )
{
weightedAvgPrice = weightedValueSum / weightedQtySum;
NotifyPropertyChanged( "price" );
}
}
/// <summary> Add non-mission cargo </summary>
/// <param name="cargoType">The type of cargo to add (e.g. legal or stolen)</param>
/// <param name="acquistionAmount">The amount of cargo to add</param>
/// <param name="acquistionPrice">The acquisition price per unit (if not zero)</param>
public void AddDetailedQty ( CargoType cargoType, int acquistionAmount, decimal? acquistionPrice = 0)
{
UpdateWeightedPrice( acquistionPrice ?? 0, acquistionAmount );
switch ( cargoType )
{
case CargoType.stolen:
{
stolen += acquistionAmount;
break;
}
default:
{
owned += acquistionAmount;
break;
}
}
}
/// <summary> Add mission cargo </summary>
/// <param name="missionID">Add mission cargo by mission ID</param>
/// <param name="acquistionAmount">The amount of cargo to add</param>
public void AddDetailedQty ( ulong missionID, int acquistionAmount )
{
if ( !missionCargo.TryAdd( missionID, acquistionAmount ) )
{
missionCargo[ missionID ] += acquistionAmount;
}
haulage = missionCargo.Values.Sum();
}
/// <summary> Remove non-mission cargo </summary>
/// <param name="cargoType">The type of cargo to remove (e.g. legal or stolen)</param>
/// <param name="removedAmount">The amount of cargo to remove</param>
public void RemoveDetailedQty ( CargoType cargoType, int removedAmount )
{
switch ( cargoType )
{
case CargoType.stolen:
{
stolen -= removedAmount;
break;
}
default:
{
owned -= removedAmount;
break;
}
}
}
/// <summary> Remove mission cargo </summary>
/// <param name="missionID">Remove mission cargo by mission ID</param>
/// <param name="removedAmount">The amount of cargo to remove</param>
public void RemoveDetailedQty ( ulong missionID, int removedAmount )
{
if ( missionCargo.TryGetValue( missionID, out var cargoAmount ) )
{
missionCargo[ missionID ] -= Math.Min( removedAmount, cargoAmount );
if ( missionCargo[ missionID ] == 0 )
{
missionCargo.Remove( missionID );
}
haulage = missionCargo.Values.Sum();
}
}
}
public enum CargoType
{
legal,
stolen
}
}