-
Notifications
You must be signed in to change notification settings - Fork 16
/
MainButtonController.cs
172 lines (142 loc) · 6.25 KB
/
MainButtonController.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
using AutomaticTypeMapper;
using EndlessClient.Dialogs.Actions;
using EndlessClient.GameExecution;
using EndlessClient.Rendering;
using EOLib.Domain;
using EOLib.Domain.Protocol;
using EOLib.Net.Communication;
using EOLib.Net.Connection;
using EOLib.Net.PacketProcessing;
using System.Threading;
using System.Threading.Tasks;
namespace EndlessClient.Controllers
{
[MappedType(BaseType = typeof(IMainButtonController))]
public class MainButtonController : IMainButtonController
{
private readonly INetworkConnectionActions _networkConnectionActions;
private readonly IErrorDialogDisplayAction _errorDialogDisplayAction;
private readonly IPacketProcessActions _packetProcessActions;
private readonly IBackgroundReceiveActions _backgroundReceiveActions;
private readonly IGameStateActions _gameStateActions;
private readonly IAccountDialogDisplayActions _accountDialogDisplayActions;
private readonly IResetStateAction _resetStateAction;
private readonly ISafeNetworkOperationFactory _networkOperationFactory;
private int _numberOfConnectionRequests;
public MainButtonController(INetworkConnectionActions networkConnectionActions,
IErrorDialogDisplayAction errorDialogDisplayAction,
IPacketProcessActions packetProcessActions,
IBackgroundReceiveActions backgroundReceiveActions,
IGameStateActions gameStateActions,
IAccountDialogDisplayActions accountDialogDisplayActions,
IResetStateAction resetStateAction,
ISafeNetworkOperationFactory networkOperationFactory)
{
_networkConnectionActions = networkConnectionActions;
_errorDialogDisplayAction = errorDialogDisplayAction;
_packetProcessActions = packetProcessActions;
_backgroundReceiveActions = backgroundReceiveActions;
_gameStateActions = gameStateActions;
_accountDialogDisplayActions = accountDialogDisplayActions;
_resetStateAction = resetStateAction;
_networkOperationFactory = networkOperationFactory;
}
public void GoToInitialState()
{
_gameStateActions.ChangeToState(GameStates.Initial);
}
public void GoToInitialStateAndDisconnect(bool showLostConnection = false)
{
GoToInitialState();
StopReceivingAndDisconnect();
_resetStateAction.ResetState();
if (showLostConnection)
_errorDialogDisplayAction.ShowConnectionLost(false);
}
public async Task ClickCreateAccount()
{
var result = await StartNetworkConnection().ConfigureAwait(false);
if (result)
{
await DispatcherGameComponent.Invoke(() =>
{
_gameStateActions.ChangeToState(GameStates.CreateAccount);
_accountDialogDisplayActions.ShowInitialCreateWarningDialog();
});
}
}
public async Task ClickLogin()
{
var result = await StartNetworkConnection().ConfigureAwait(false);
if (result)
{
await DispatcherGameComponent.Invoke(() => _gameStateActions.ChangeToState(GameStates.Login));
}
}
public void ClickViewCredits()
{
_gameStateActions.ChangeToState(GameStates.ViewCredits);
}
public void ClickExit()
{
StopReceivingAndDisconnect();
_gameStateActions.ExitGame();
}
private async Task<bool> StartNetworkConnection()
{
if (Interlocked.Increment(ref _numberOfConnectionRequests) != 1)
return false;
try
{
var connectResult = await _networkConnectionActions.ConnectToServer().ConfigureAwait(false);
if (connectResult == ConnectResult.AlreadyConnected)
return true;
if (connectResult != ConnectResult.Success)
{
_errorDialogDisplayAction.ShowError(connectResult);
return false;
}
_backgroundReceiveActions.RunBackgroundReceiveLoop();
var beginHandshakeOperation = _networkOperationFactory.CreateSafeBlockingOperation(
_networkConnectionActions.BeginHandshake,
ex => _errorDialogDisplayAction.ShowException(ex),
ex => _errorDialogDisplayAction.ShowException(ex));
if (!await beginHandshakeOperation.Invoke().ConfigureAwait(false))
{
StopReceivingAndDisconnect();
return false;
}
var initData = beginHandshakeOperation.Result;
if (initData.Response != InitReply.Success)
{
_errorDialogDisplayAction.ShowError(initData);
StopReceivingAndDisconnect();
return false;
}
_packetProcessActions.SetInitialSequenceNumber(initData[InitializationDataKey.SequenceByte1],
initData[InitializationDataKey.SequenceByte2]);
_packetProcessActions.SetEncodeMultiples(initData[InitializationDataKey.ReceiveMultiple], initData[InitializationDataKey.SendMultiple]);
_networkConnectionActions.CompleteHandshake(initData);
return true;
}
finally
{
Interlocked.Exchange(ref _numberOfConnectionRequests, 0);
}
}
private void StopReceivingAndDisconnect()
{
_backgroundReceiveActions.CancelBackgroundReceiveLoop();
_networkConnectionActions.DisconnectFromServer();
}
}
public interface IMainButtonController
{
void GoToInitialState();
void GoToInitialStateAndDisconnect(bool showLostConnection = false);
Task ClickCreateAccount();
Task ClickLogin();
void ClickViewCredits();
void ClickExit();
}
}