forked from msarilar/EDEngineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.cs
144 lines (117 loc) · 5.16 KB
/
State.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using EDEngineer.Models.Utils;
using EDEngineer.Models.Utils.Collections;
namespace EDEngineer.Models
{
using Comparer = Func<KeyValuePair<string, Entry>, KeyValuePair<string, Entry>, int>;
public class State : INotifyPropertyChanged
{
public const string NAME_COMPARER = "Name";
public const string COUNT_COMPARER = "Count";
public const string THRESHOLDS_COMPARER = "Thresholds";
public LinkedList<JournalEntry> Operations { get; } = new LinkedList<JournalEntry>();
private readonly List<EntryData> entryDatas;
private readonly object stateLock = new object();
public List<Blueprint> Blueprints { get; set; }
private readonly IReadOnlyDictionary<string, Comparer> comparers;
public State(List<EntryData> entryDatas, ILanguage languages, string comparer)
{
comparers = new Dictionary<string, Comparer>()
{
[NAME_COMPARER] = (a, b) => string.Compare(languages.Translate(a.Key), languages.Translate(b.Key), StringComparison.InvariantCultureIgnoreCase),
[COUNT_COMPARER] = (a, b) => b.Value.Count.CompareTo(a.Value.Count),
[THRESHOLDS_COMPARER] = (a, b) =>
{
if (a.Value.Threshold.HasValue && !b.Value.Threshold.HasValue)
{
return -1;
}
if (!a.Value.Threshold.HasValue && b.Value.Threshold.HasValue)
{
return 1;
}
if (a.Value.Threshold.HasValue && b.Value.Threshold.HasValue)
{
return
(b.Value.Count - b.Value.Threshold.Value).CompareTo(a.Value.Count -
a.Value
.Threshold
.Value);
}
return comparers[COUNT_COMPARER](a, b);
}
};
Cargo = new SortedObservableCounter(comparers[comparer]);
languages.PropertyChanged += (o, e) => Cargo.RefreshSort();
this.entryDatas = entryDatas;
LoadBaseData();
}
public void ChangeComparer(string newComparer)
{
Cargo.RefreshSort(comparers[newComparer]);
}
public SortedObservableCounter Cargo { get; }
public int MaterialsCount => EntryCount(Kind.Material);
public int DataCount => EntryCount(Kind.Data);
public int MaxMaterials { get; } = 1000;
public int MaxData { get; } = 500;
private int EntryCount(Kind kind)
{
return Cargo
.Where(i => i.Value.Data.Kind == kind)
.Where(i => i.Value.Count > 0)
.Select(i => i.Value.Count)
.Sum();
}
public void LoadBaseData()
{
lock (stateLock)
{
var toAdd = entryDatas.Where(e => !Cargo.ContainsKey(e.Name));
foreach (var item in toAdd)
{
Cargo.Add(new KeyValuePair<string, Entry>(item.Name, new Entry(item)));
}
}
}
public void InitLoad()
{
loading = true;
}
public void CompleteLoad()
{
loading = false;
}
private bool loading;
public void IncrementCargo(string name, int change)
{
lock (stateLock)
{
Cargo.Increment(name, change);
}
switch (Cargo[name].Data.Kind)
{
case Kind.Data:
OnPropertyChanged(nameof(DataCount));
break;
case Kind.Material:
OnPropertyChanged(nameof(MaterialsCount));
break;
}
if (!loading)
{
Cargo.SortInPlace();
}
OnPropertyChanged(name);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}