forked from lempamo/ChaosIV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PollProxy.cs
165 lines (135 loc) · 3.8 KB
/
PollProxy.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
using System;
using System.Diagnostics;
using ChaosIV.WS.Messages;
using GTA;
using Newtonsoft.Json;
namespace ChaosIV
{
class PollProxy {
private PipeServer _pipeServer;
private Process _proxyProcess;
private readonly string _ffzPassphrase;
private bool _isAuthenticated = false;
private bool _waitPollResult = false;
public Action OnConnect;
public Action OnDisconnect;
public Action<Error> OnError;
public Action OnCreate;
public Action<PollResult> OnEnd;
public Action OnAuth;
public PollProxy(int port, string ffzPassphrase, string scriptPath) {
_ffzPassphrase = ffzPassphrase;
OnDisconnect += OnClientDisconnect;
_pipeServer = new PipeServer();
_pipeServer.OnMessage += OnMessage;
_proxyProcess = new Process() {
EnableRaisingEvents = false,
StartInfo = {
FileName = scriptPath + @"\for Developers\ChaosIVTwitchPollProxy\bin\Release\net45\ChaosIVTwitchPollProxy.exe",
Arguments = $"{port}",
RedirectStandardOutput = false,
RedirectStandardInput = false,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
},
};
_proxyProcess.Exited += new EventHandler((object s, EventArgs e) => {
Game.Console.Print("ChaosIVTwitchPollProxy.exe closed.");
//Stop();
});
var started = _proxyProcess.Start();
}
private void OnMessage(string message) {
//Game.Console.Print($"Received msg: {message}");
if (message == "connected") {
Game.Console.Print("FFZ connected");
OnConnect?.Invoke();
return;
}
else if (message == "disconnected") {
Game.Console.Print("FFZ disconnected");
OnDisconnect?.Invoke();
return;
}
try {
var pollResult = JsonConvert.DeserializeObject<PollResult>(message);
switch (pollResult.type) {
// Client Passphrase
case "auth":
// Check passphrase
// if (poll.data === '123test') ...
SendAuth();
break;
case "auth_ok":
_isAuthenticated = true;
OnAuth?.Invoke();
break;
case "created":
_waitPollResult = true;
OnCreate?.Invoke();
break;
case "update":
if (pollResult.poll.ended) {
_waitPollResult = false;
OnEnd?.Invoke(pollResult);
}
break;
case "error":
Game.Console.Print($"FFZ error: {message}");
var error = JsonConvert.DeserializeObject<Error>(message);
OnError?.Invoke(error);
break;
}
}
catch (JsonException ex) {
ChaosMain.ReportException(ex);
}
}
public void Send(string message) {
_pipeServer.Send(message);
}
public void SendAuth() {
if (_isAuthenticated) {
return;
}
Game.Console.Print("Send auth request to FFZ.");
var authMessage = new Auth {
data = _ffzPassphrase
};
Send(JsonConvert.SerializeObject(authMessage));
}
public void CreatePoll(in string[] choices, in int durationSec) {
if (_waitPollResult) {
Game.Console.Print("Skipped CreatePoll: Still waiting for poll results...");
return;
}
if (Game.Paused) {
//Game.Console.Print("Skipped CreatePoll: The game is paused");
System.Threading.Thread.Sleep(5000);
CreatePoll(in choices, in durationSec);
return;
}
if (!_isAuthenticated) {
//SendAuth();
return;
}
// Game.Console.Print("Trying to create a poll.");
var poll = new Poll() {
choices = choices,
duration = durationSec,
};
Send(JsonConvert.SerializeObject(poll));
}
private void OnClientDisconnect() {
_isAuthenticated = false;
_waitPollResult = false;
}
public void Stop() {
_pipeServer.Close();
if (!_proxyProcess.HasExited) {
_proxyProcess.Kill();
}
}
}
}