forked from JakeStanger/csm-exportelectricity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExportableManager.cs
226 lines (192 loc) · 6.76 KB
/
ExportableManager.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
using ColossalFramework.UI;
using ICities;
using System;
using System.Collections.Generic;
using System.IO;
namespace Exportable
{
public class ExportableManager
{
private SortedDictionary<string, Exportable> exportables = new SortedDictionary<string, Exportable>();
public const String CONF = "ExportElectricityModConfig.txt";
private float multiplier;
private int realtimeinterval = 2;
private const double interval = 1.0;
private double waited = 0.0;
private UISlider textbox;
public ExportableManager()
{
multiplier = 1.0f;
//new ExportableCremation (this);
new ExportableElementary(this);
new ExportableHighSchool(this);
new ExportableUniversity(this);
new ExportableElectricity(this);
//new ExportableIncineration (this);
new ExportableHealth(this);
new ExportableHeat(this);
new ExportableJail(this);
new ExportableSewage(this);
new ExportableWater(this);
LoadSettings();
}
public void Log(string msg)
{
ExportElectricityMod.Debugger.Write(msg);
}
public void AddExportable(Exportable exp)
{
if (!exportables.ContainsKey(exp.Id))
exportables.Add(exp.Id, exp);
}
public SortedDictionary<string, Exportable> GetExportables()
{
return exportables;
}
public void LoadSettings()
{
Log("Load Settings");
try
{
using (var file = new StreamReader(CONF, true))
{
var s = file.ReadLine();
var sections = s.Split(new char[1] { '|' });
var ids = sections[0].Split(new char[1] { ',' });
if (sections.Length == 3)
{
multiplier = float.Parse(sections[1]);
realtimeinterval = int.Parse(sections[2]);
}
else if (sections.Length == 2)
{
multiplier = float.Parse(sections[1]);
realtimeinterval = 2;
}
else
{
multiplier = 1.0f;
realtimeinterval = 2;
}
foreach (var id in ids)
{
if (exportables.ContainsKey(id))
{
exportables[id].SetEnabled(true, false);
}
}
}
}
catch (Exception e)
{
// no file? use defaults
Log("Using Defaults: " + e.ToString());
exportables[Ids.ELECTRICITY].SetEnabled(true);
}
}
public void ClearExportables()
{
foreach (var exportable in exportables)
{
var exp = exportable.Value;
exp.LastWeeklyEarning = 0;
if (exportables.ContainsKey(exportable.Key))
exportables[exportable.Key] = exp;
}
}
public void StoreSettings()
{
Log("Store Settings");
try
{
using (var file = new FileStream(CONF, FileMode.Create))
{
var enabled_ids = new List<string>();
using (var sw = new StreamWriter(file))
{
foreach (var pair in exportables)
{
if (pair.Value.GetEnabled())
{
enabled_ids.Add(pair.Key);
}
}
var cs = string.Join(",", enabled_ids.ToArray()) + "|" + multiplier.ToString() + "|" + realtimeinterval.ToString();
Log("Storing settings - enabled: " + cs);
sw.WriteLine(cs);
sw.Flush();
}
}
}
catch (Exception e)
{
Log("Error storing settings: " + e.ToString());
}
}
public double CalculateIncome(District d, string id, double weekPortion)
{
double income = 0.0;
if (exportables.ContainsKey(id))
{
Exportable exp = exportables[id];
if (exp.GetEnabled())
{
Log("Calculating Income for " + id);
income = exp.CalculateIncome(d, weekPortion);
}
}
return income;
}
public double CalculateIncome(District d, double weekPortion)
{
double total = 0.0;
waited += weekPortion;
if (waited < interval)
{
return 0;
}
Log("Calculating Income");
foreach (var id in exportables.Keys)
{
total += CalculateIncome(d, id, waited);
}
waited = 0.0;
return total * multiplier;
}
public void AddOptions(UIHelperBase group)
{
LoadSettings();
foreach (var id in exportables.Keys)
{
Exportable exp = exportables[id];
group.AddCheckbox(exp.Description, exp.GetEnabled(), exp.SetEnabled);
}
textbox = group.AddSlider($"Multiplier", 0.0f, 2.0f, 0.05f, multiplier, MultiplierSliderChanged) as UISlider;
var dropdown = group.AddDropdown("Real Time Mod Interval", new string[] { "3 Hours", "6 Hours", "12 Hours", "1 Day", "1 Week" }, realtimeinterval, RealTimeModInterval) as UIDropDown;
dropdown.tooltip = @"When using Real Time Mod, the payout cycle will be affected by the interval selected here. Default: 6 hours.";
group.AddCheckbox("Debug Mode", ExportElectricityMod.Debugger.enabled, SetDebug);
}
private void MultiplierSliderChanged(float val)
{
if (textbox != null)
{
textbox.tooltip = $"{multiplier}";
}
multiplier = val;
StoreSettings();
}
private void RealTimeModInterval(int index)
{
realtimeinterval = index;
StoreSettings();
}
public void SetDebug(bool b)
{
ExportElectricityMod.Debugger.enabled = b;
}
public int GetRealTimeInterval()
{
return realtimeinterval;
}
}
}