forked from Gyoo/LiveSplit.SplitsBet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
377 lines (343 loc) · 18.1 KB
/
Settings.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveSplit.Model;
using System.Xml;
using LiveSplit.TimeFormatters;
using LiveSplit.Model.Comparisons;
using System.Xml.XPath;
using System.Xml.Serialization;
namespace LiveSplit.SplitsBet
{
public partial class Settings : UserControl
{
public String Path { get; set; }
public LiveSplitState LivesplitState { get; set; }
public bool CanUnBet { get; set; }
public int UnBetPenalty { get; set; }
public TimeSpan MinimumTime { get; set; }
public int NbScores { get; set; }
public TimingMethod? OverridenTimingMethod { get { return ParseTimingMethod(Method); } }
public String Method { get; set; }
public bool UseGlobalTime { get; set; }
public bool AllowMods { get; set; }
public bool SingleLineScores { get; set; }
public string TimeToShow { get; set; }
public int Delay { get; set; }
public bool ParentSubSplits { get; set; }
public string msgEnable { get; set; }
public string msgDisable { get; set; }
public string msgReset { get; set; }
public string msgTimerNotRunning { get; set; }
public string msgTimerPaused { get; set; }
public string msgTimerEnded { get; set; }
public string msgNoScore { get; set; }
public string msgNoHighscore { get; set; }
public string msgNoUnbet { get; set; }
public string msgUnbetTimerEnd { get; set; }
public string msgCheckTimerEnd { get; set; }
public string msgTooLateToBet { get; set; }
public List<string> SelectedSegments { get; set; }
private ITimeFormatter Formatter { get; set; }
public Settings()
{
//Init setting to default values
InitializeComponent();
CanUnBet = true;
UnBetPenalty = 50;
MinimumTime = new TimeSpan(0, 0, 1);
NbScores = 5;
UseGlobalTime = false;
Method = "Current Timing Method";
AllowMods = false;
SingleLineScores = false;
TimeToShow = "Best Segments";
Delay = 0;
ParentSubSplits = false;
SelectedSegments = new List<string>();
//Init messages to default values
msgEnable = "SplitsBet enabled!";
msgDisable = "SplitsBet disabled!";
msgReset = "Run is kill. RIP :(";
msgTimerNotRunning = "Timer is not running; bets are closed;";
msgTimerPaused = "Timer is paused; bets are paused too.";
msgTimerEnded = "Run is over; there is nothing to bet on!";
msgNoScore = "Timer is not running; no score available.";
msgNoHighscore = "No highscore yet!";
msgNoUnbet = "You can't unbet :(";
msgUnbetTimerEnd = "The run has ended, you can't unbet!";
msgCheckTimerEnd = "The run has ended; nothing to check!";
msgTooLateToBet = "Too late to bet for this split; wait for the next one!";
Formatter = new RegularTimeFormatter();
//DataBindings for Settings
chkCancelBets.DataBindings.Add("Checked", this, "CanUnBet", false, DataSourceUpdateMode.OnPropertyChanged);
txtCancelingPenalty.DataBindings.Add("Text", this, "UnBetPenalty", false, DataSourceUpdateMode.OnPropertyChanged);
numScores.DataBindings.Add("Value", this, "NbScores", false, DataSourceUpdateMode.OnPropertyChanged);
chkGlobalTime.DataBindings.Add("Checked", this, "UseGlobalTime", false, DataSourceUpdateMode.OnPropertyChanged);
cmbTimingMethod.DataBindings.Add("SelectedItem", this, "OverridenTimingMethod", false, DataSourceUpdateMode.OnPropertyChanged);
chkAllowMods.DataBindings.Add("Checked", this, "AllowMods", false, DataSourceUpdateMode.OnPropertyChanged);
chkSingleLineScores.DataBindings.Add("Checked", this, "SingleLineScores", false, DataSourceUpdateMode.OnPropertyChanged);
cmbTimeToShow.DataBindings.Add("SelectedItem", this, "TimeToShow", false, DataSourceUpdateMode.OnPropertyChanged);
txtDelay.DataBindings.Add("Text", this, "Delay", false, DataSourceUpdateMode.OnPropertyChanged);
chkSubsplits.DataBindings.Add("Checked", this, "ParentSubSplits", false, DataSourceUpdateMode.OnPropertyChanged);
//DataBindings for Bot Messages
txtMsgEnable.DataBindings.Add("Text", this, "msgEnable", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgDisable.DataBindings.Add("Text", this, "msgDisable", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgReset.DataBindings.Add("Text", this, "msgReset", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgTimerNotRunning.DataBindings.Add("Text", this, "msgTimerNotRunning", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgTimerPaused.DataBindings.Add("Text", this, "msgTimerPaused", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgTimerEnded.DataBindings.Add("Text", this, "msgTimerEnded", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgNoScore.DataBindings.Add("Text", this, "msgNoScore", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgNoHighscore.DataBindings.Add("Text", this, "msgNoHighscore", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgNoUnbet.DataBindings.Add("Text", this, "msgNoUnbet", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgUnbetTimerEnd.DataBindings.Add("Text", this, "msgUnbetTimerEnd", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgCheckTimerEnd.DataBindings.Add("Text", this, "msgCheckTimerEnd", false, DataSourceUpdateMode.OnPropertyChanged);
txtMsgTooLateToBet.DataBindings.Add("Text", this, "msgTooLateToBet", false, DataSourceUpdateMode.OnPropertyChanged);
this.Load += Settings_Load;
}
void Settings_Load(object sender, EventArgs e)
{
//Settings
txtMinBetTime.Text = Formatter.Format(MinimumTime);
cmbTimeToShow.Items.Clear();
cmbTimeToShow.Items.AddRange(LivesplitState.Run.Comparisons.Where(x => x != BestSplitTimesComparisonGenerator.ComparisonName).ToArray());
if (!cmbTimeToShow.Items.Contains(TimeToShow))
cmbTimeToShow.Items.Add(TimeToShow);
if (LivesplitState.Layout.Components.Any(x => x.ComponentName == "Subsplits")){
chkSubsplits.Enabled = true;
}
else
{
chkSubsplits.Enabled = false;
ParentSubSplits = false;
}
List<string> segments = new List<string>();
foreach(Segment s in LivesplitState.Run){
if (chkSubsplits.Checked && s.Name.StartsWith("-")) continue;
else segments.Add(s.Name);
}
listSegments.DataSource = segments;
SelectItems();
// Bot Messages
txtMsgEnable.Text = msgEnable;
txtMsgDisable.Text = msgDisable;
txtMsgReset.Text = msgReset;
txtMsgTimerEnded.Text = msgTimerEnded;
txtMsgTimerNotRunning.Text = msgTimerNotRunning;
txtMsgTimerPaused.Text = msgTimerPaused;
txtMsgNoScore.Text = msgNoScore;
txtMsgNoHighscore.Text = msgNoHighscore;
txtMsgNoUnbet.Text = msgNoUnbet;
txtMsgUnbetTimerEnd.Text = msgUnbetTimerEnd;
txtMsgCheckTimerEnd.Text = msgCheckTimerEnd;
txtMsgTooLateToBet.Text = msgTooLateToBet;
}
private void SelectItems()
{
if (SelectedSegments.Count > 0)
{
foreach (string s in SelectedSegments)
{
if (listSegments.Items.Contains(s))
{
listSegments.SetSelected(listSegments.Items.IndexOf(s), true);
}
}
}
else
{
for (int i = 0; i < listSegments.Items.Count; i++)
{
listSegments.SetSelected(i, true);
}
}
}
public System.Xml.XmlNode GetSettings(System.Xml.XmlDocument document)
{
var settingsNode = document.CreateElement("Settings");
settingsNode.AppendChild(ToElement(document, "Version", SplitsBetFactory.VersionString));
//Settings
settingsNode.AppendChild(ToElement(document, "UnBet", CanUnBet));
settingsNode.AppendChild(ToElement(document, "UnBetPenalty", UnBetPenalty));
settingsNode.AppendChild(ToElement(document, "MinimumTime", MinimumTime));
settingsNode.AppendChild(ToElement(document, "NbScores", NbScores));
settingsNode.AppendChild(ToElement(document, "UseGlobalTime", UseGlobalTime));
settingsNode.AppendChild(ToElement(document, "TimingMethod", Method));
settingsNode.AppendChild(ToElement(document, "AllowMods", AllowMods));
settingsNode.AppendChild(ToElement(document, "SingleLineScores", SingleLineScores));
settingsNode.AppendChild(ToElement(document, "TimeToShow", TimeToShow));
settingsNode.AppendChild(ToElement(document, "Delay", Delay));
settingsNode.AppendChild(ToElement(document, "ParentSubSplits", ParentSubSplits));
settingsNode.AppendChild(ToElement(document, "SelectedSegments", SelectedSegments));
//Bot Messages
settingsNode.AppendChild(ToElement(document, "msgEnable", msgEnable));
settingsNode.AppendChild(ToElement(document, "msgDisable", msgDisable));
settingsNode.AppendChild(ToElement(document, "msgReset", msgReset));
settingsNode.AppendChild(ToElement(document, "msgTimerEnded", msgTimerEnded));
settingsNode.AppendChild(ToElement(document, "msgTimerNotRunning", msgTimerNotRunning));
settingsNode.AppendChild(ToElement(document, "msgTimerPaused", msgTimerPaused));
settingsNode.AppendChild(ToElement(document, "msgNoScore", msgNoScore));
settingsNode.AppendChild(ToElement(document, "msgNoHighscore", msgNoHighscore));
settingsNode.AppendChild(ToElement(document, "msgNoUnbet", msgNoUnbet));
settingsNode.AppendChild(ToElement(document, "msgUnbetTimerEnd", msgUnbetTimerEnd));
settingsNode.AppendChild(ToElement(document, "msgCheckTimerEnd", msgCheckTimerEnd));
settingsNode.AppendChild(ToElement(document, "msgTooLateToBet", msgTooLateToBet));
serializeAppend(settingsNode, SelectedSegments);
return settingsNode;
}
void serializeAppend(XmlNode parentNode, object obj)
{
XPathNavigator nav = parentNode.CreateNavigator();
using (var writer = nav.AppendChild())
{
var serializer = new XmlSerializer(obj.GetType());
writer.WriteWhitespace("");
serializer.Serialize(writer, obj);
writer.Close();
}
}
public void SetSettings(System.Xml.XmlNode settings)
{
//Settings
if(settings["UnBet"] != null) CanUnBet = bool.Parse(settings["UnBet"].InnerText);
else CanUnBet = true;
if(settings["UnBetPenalty"] != null) UnBetPenalty = int.Parse(settings["UnBetPenalty"].InnerText);
else UnBetPenalty = 50;
if(settings["MinimumTime"] != null) MinimumTime = TimeSpanParser.Parse(settings["MinimumTime"].InnerText);
else MinimumTime = new TimeSpan(0, 0, 1);
if(settings["NbScores"] != null) NbScores = int.Parse(settings["NbScores"].InnerText);
else NbScores = 5;
if(settings["UseGlobalTime"] != null) UseGlobalTime = bool.Parse(settings["UseGlobalTime"].InnerText);
else UseGlobalTime = false;
if(settings["TimingMethod"] != null) Method = settings["TimingMethod"].InnerText;
else Method = "Current Timing Method";
if(settings["AllowMods"] != null) AllowMods = bool.Parse(settings["AllowMods"].InnerText);
else AllowMods = false;
if(settings["SingleLineScores"] != null) SingleLineScores = bool.Parse(settings["SingleLineScores"].InnerText);
else SingleLineScores = false;
if(settings["TimeToShow"] != null) TimeToShow = settings["TimeToShow"].InnerText;
else TimeToShow = "Best Segments";
if (settings["Delay"] != null) Delay = int.Parse(settings["Delay"].InnerText);
else Delay = 0;
if (settings["ParentSubSplits"] != null) ParentSubSplits = bool.Parse(settings["ParentSubSplits"].InnerText);
else ParentSubSplits = false;
if (settings["ArrayOfString"] != null) SelectedSegments = deserialize<List<string>>(settings["ArrayOfString"]);
else SelectedSegments = new List<string>();
//Bot messages
if (settings["msgEnable"] != null) msgEnable = settings["msgEnable"].InnerText;
else msgEnable = "SplitsBet enabled!";
if (settings["msgDisable"] != null) msgDisable = settings["msgDisable"].InnerText;
else msgDisable = "SplitsBet disabled!";
if (settings["msgReset"] != null) msgReset = settings["msgReset"].InnerText;
else msgReset = "Run is kill. RIP :(";
if (settings["msgTimerEnded"] != null) msgTimerEnded = settings["msgTimerEnded"].InnerText;
else msgTimerEnded = "Run is over; there is nothing to bet on!";
if (settings["msgTimerNotRunning"] != null) msgTimerNotRunning = settings["msgTimerNotRunning"].InnerText;
else msgTimerNotRunning = "Timer is not running; bets are closed;";
if (settings["msgTimerPaused"] != null) msgTimerPaused = settings["msgTimerPaused"].InnerText;
else msgTimerPaused = "Timer is paused; bets are paused too.";
if (settings["msgNoScore"] != null) msgNoScore = settings["msgNoScore"].InnerText;
else msgNoScore = "Timer is not running; no score available.";
if (settings["msgNoHighscore"] != null) msgNoHighscore = settings["msgNoHighscore"].InnerText;
else msgNoHighscore = "No highscore yet!";
if (settings["msgNoUnbet"] != null) msgNoUnbet = settings["msgNoUnbet"].InnerText;
else msgNoUnbet = "You can't unbet :(";
if (settings["msgUnbetTimerEnd"] != null) msgUnbetTimerEnd = settings["msgUnbetTimerEnd"].InnerText;
else msgUnbetTimerEnd = "The run has ended, you can't unbet!";
if (settings["msgCheckTimerEnd"] != null) msgCheckTimerEnd = settings["msgCheckTimerEnd"].InnerText;
else msgCheckTimerEnd = "The run has ended; nothing to check!";
if (settings["msgTooLateToBet"] != null) msgTooLateToBet = settings["msgTooLateToBet"].InnerText;
else msgTooLateToBet = "Too late to bet for this split; wait for the next one!";
}
T deserialize<T>(XmlNode node)
{
XPathNavigator nav = node.CreateNavigator();
using (var reader = nav.ReadSubtree())
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
}
private TimingMethod? ParseTimingMethod(String method)
{
TimingMethod? timingMethod = null;
if (method == "Real Time")
timingMethod = TimingMethod.RealTime;
else if (method == "Game Time")
timingMethod = TimingMethod.GameTime;
return timingMethod;
}
private XmlElement ToElement<T>(XmlDocument document, String name, T value)
{
var element = document.CreateElement(name);
element.InnerText = value.ToString();
return element;
}
private void txtCancelingPenalty_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
e.Handled = true;
}
private void txtMinBetTime_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && !e.KeyChar.Equals(':'))
e.Handled = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
txtCancelingPenalty.Enabled = chkCancelBets.Checked;
}
private void txtMinBetTime_Leave(object sender, EventArgs e)
{
try
{
MinimumTime = TimeSpanParser.Parse(txtMinBetTime.Text);
}
finally
{
txtMinBetTime.Text = Formatter.Format(MinimumTime);
}
}
private void txtDelay_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
e.Handled = true;
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void chkSubsplits_CheckedChanged(object sender, EventArgs e)
{
List<string> segments = new List<string>();
foreach (Segment s in LivesplitState.Run)
{
if (chkSubsplits.Checked && s.Name.StartsWith("-")) continue;
else segments.Add(s.Name);
}
listSegments.DataSource = segments;
SelectItems();
}
private void listSegments_Click(object sender, EventArgs e)
{
SelectedSegments = new List<string>();
for (int i = 0; i < listSegments.Items.Count; i++)
{
if (listSegments.GetSelected(i))
{
SelectedSegments.Add(listSegments.GetItemText(listSegments.Items[i]));
}
}
}
}
}