-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitchPredictionsComponent.cs
More file actions
105 lines (88 loc) · 3.23 KB
/
TwitchPredictionsComponent.cs
File metadata and controls
105 lines (88 loc) · 3.23 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
using LiveSplit.Model;
using LiveSplit.UI;
using LiveSplit.UI.Components;
using System;
using System.Windows.Forms;
using System.Xml;
namespace LiveSplit.TwitchPredictions
{
public class TwitchPredictionsComponent : LogicComponent
{
public override string ComponentName { get { return "Twitch Predictions"; } }
public TwitchPredictionsSettings Settings { get; set; }
public bool Disposed { get; private set; }
public bool IsLayoutComponent { get; private set; }
private TimerModel _timer;
private TwitchConnection _twitchConnection;
private LiveSplitState _state;
public TwitchPredictionsComponent(LiveSplitState state, bool isLayoutComponent)
{
_state = state;
this.IsLayoutComponent = isLayoutComponent;
this.Settings = new TwitchPredictionsSettings(state);
_state.RunManuallyModified += Settings._state_RunManuallyModified;
_timer = new TimerModel { CurrentState = state };
_twitchConnection = TwitchConnection.GetInstance();
if (_twitchConnection._connectionData.ConnectOnLaunch)
_twitchConnection.Connect();
//Set required timer events
_timer.CurrentState.OnStart += CurrentState_OnStart;
_timer.CurrentState.OnSkipSplit -= CurrentState_OnSplit; //essentially identical on split
_timer.CurrentState.OnSplit += CurrentState_OnSplit;
_timer.CurrentState.OnReset += CurrentState_OnReset;
}
private void CurrentState_OnStart(object sender, EventArgs e)
{
Settings.SplitsToEventsInstance.ClearWasUsedFlags();
Settings.SplitsToEventsInstance.DoSplitEvent(0);
}
private void CurrentState_OnSplit(object sender, EventArgs e)
{
var cast = (LiveSplitState)sender;
var runEnded = cast.CurrentSplit == null;
if (!runEnded)
Settings.SplitsToEventsInstance.DoSplitEvent(cast.CurrentSplitIndex);
else
{
var currentTime = cast.CurrentTime;
var pbTime = cast.Run[cast.CurrentSplitIndex - 1].PersonalBestSplitTime;
bool isPB;
if (cast.CurrentTimingMethod == TimingMethod.RealTime)
isPB = cast.CurrentTime.RealTime < pbTime.RealTime;
else
isPB = currentTime.GameTime != null && pbTime.GameTime != null ? currentTime.GameTime < pbTime.GameTime : cast.CurrentTime.RealTime < pbTime.RealTime;
Settings.SplitsToEventsInstance.DoCompleteRunEvent(isPB);
}
}
private void CurrentState_OnReset(object sender, TimerPhase value)
{
Settings.SplitsToEventsInstance.DoResetEvent();
}
public override void Dispose()
{
this.Disposed = true;
_state.RunManuallyModified -= Settings._state_RunManuallyModified;
_timer.CurrentState.OnStart -= CurrentState_OnStart;
_timer.CurrentState.OnSkipSplit -= CurrentState_OnStart;
_timer.CurrentState.OnSplit -= CurrentState_OnSplit;
_timer.CurrentState.OnReset -= CurrentState_OnReset;
if (_twitchConnection != null)
{
_twitchConnection.Disconnect();
}
}
public override XmlNode GetSettings(XmlDocument document)
{
return this.Settings.GetSettings(document);
}
public override Control GetSettingsControl(LayoutMode mode)
{
return this.Settings;
}
public override void SetSettings(XmlNode settings)
{
this.Settings.SetSettings(settings);
}
public override void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode) { }
}
}