-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
246 lines (206 loc) · 9.1 KB
/
Copy pathProgram.cs
File metadata and controls
246 lines (206 loc) · 9.1 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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization;
using System.Threading;
using ConsoleApplication1.Game;
using FM.WebSync.Core;
using System.Runtime.Serialization.Json;
namespace ConsoleApplication1
{
class Program
{
private static Dictionary<string, string> playersInGame = new Dictionary<string, string>();
private static string stackTrace;
private static void parseParse(string input) {
new HeartParser(input).Run();
}
static void Main(string[] args) {
parseParse(File.ReadAllText(@"C:\shuffly\Shuffly-Brain\papa.txt"));
string channel = "/consolepublisher";
Console.WriteLine("Initializing...");
Client client = new Client(new ClientArgs
{
// replace with your domain key
DomainKey = "035d9877-9eaf-45ef-936f-80087fb3a221",
// replace with your domain name
DomainName = "localhost",
Synchronous = true
});
Publisher publisher = new Publisher(new PublisherArgs
{
// replace with your domain key
DomainKey = "035d9877-9eaf-45ef-936f-80087fb3a221",
// replace with your domain name
DomainName = "localhost",
Synchronous = true
});
client.Connect(new ConnectArgs
{
OnSuccess = (successArgs) =>
{
Console.WriteLine("The client connected and has ID " + successArgs.ClientId + ".");
},
OnFailure = (failureArgs) =>
{
Console.WriteLine("The client could not connect... " + failureArgs.Exception.Message);
},
OnStreamFailure = (streamFailureArgs) =>
{
Console.WriteLine("The client could not stream... " + streamFailureArgs.Exception.Message);
Console.WriteLine("The client " + (streamFailureArgs.WillReconnect ? "will" : "will not") + " automatically reconnect." + Environment.NewLine);
}
});
// client is configured as "synchronous", so this
// won't execute until the connect method is complete
if (client.State != ClientState.Connected)
{
Console.WriteLine();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return;
}
Console.WriteLine("Connected");
client.Subscribe(new SubscribeArgs
{
Channel = channel,
OnSuccess = (successArgs) =>
{
Console.WriteLine("The client " + (successArgs.IsResubscribe ? "re-" : "") + "subscribed to " + channel + ".");
},
OnFailure = (failureArgs) =>
{
Console.WriteLine("The client could not subscribe to " + channel + "... " + failureArgs.Exception.Message);
},
OnReceive = (receiveArgs) =>
{
Payload payload = JSON.Deserialize<Payload>(receiveArgs.DataJson);
Tuple<SpokeQuestion, string, GameBoard> vf = null;
switch (payload.Type)
{
case SpokeMessageType.AskQuestion:
return;
case SpokeMessageType.JoinGame:
playersInGame.Add(receiveArgs.PublishingClient.Id, payload.PlayerName);
if (playersInGame.Count == 2)
{
vf = RunGame.StartGame("sevens", playersInGame);
stackTrace = vf.Item2;
}
else return;
break;
case SpokeMessageType.AnswerQuestion:
vf = RunGame.ResumeGame("sevens", stackTrace, payload.AnswerIndex, playersInGame);
stackTrace = vf.Item2;
Console.WriteLine(vf.Item1);
break;
default:
throw new ArgumentOutOfRangeException();
}
PublicationArgs fc;
foreach (KeyValuePair<string, string> user in playersInGame)
{
fc = new PublicationArgs
{
Publication = new Publication
{
Channel = channel + "/gamedata/" + user.Value,
DataJson = JSON.Serialize(vf.Item3)
},
OnComplete = (completeArgs) =>
{
if (completeArgs.Publication.Successful == true)
{
Console.WriteLine("The publisher published to " + channel +
".");
}
else
{
Console.WriteLine("The publisher could not publish to " +
channel + "... " +
completeArgs.Publication.Error);
}
},
OnException = (exceptionArgs) =>
{
Console.WriteLine("The publisher threw an exception... " +
exceptionArgs.Exception.Message);
}
};
publisher.Publish(fc);
}
SpokeQuestion q = vf.Item1;
fc = new PublicationArgs
{
Publication = new Publication
{
Channel = channel + "/" + q.User,
DataJson = JSON.Serialize(new Payload(q.Question, q.Answers))
},
OnComplete = (completeArgs) =>
{
if (completeArgs.Publication.Successful == true)
{
Console.WriteLine("The publisher published to " + channel + ".");
}
else
{
Console.WriteLine("The publisher could not publish to " + channel + "... " + completeArgs.Publication.Error);
}
},
OnException = (exceptionArgs) =>
{
Console.WriteLine("The publisher threw an exception... " + exceptionArgs.Exception.Message);
}
};
publisher.Publish(fc);
// Console.WriteLine("The client received data... (text: " + payload.Text + ", time: " + payload.Time + ")");
}
});
Console.WriteLine();
Console.WriteLine("Press Enter to publish text from the publisher. Press Escape to quit.");
while (true)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
return;
}
if (consoleKeyInfo.Key == ConsoleKey.Enter)
{
}
}
}
}
[DataContract]
public class Payload
{
[DataMember(Name = "PlayerName")]
public string PlayerName;
[DataMember(Name = "Type")]
public SpokeMessageType Type { get; set; }
[DataMember(Name = "AnswerIndex")]
public int AnswerIndex { get; set; }
[DataMember(Name = "Question")]
public string Question { get; set; }
[DataMember(Name = "Answers")]
public string[] Answers { get; set; }
public Payload()
{
}
public Payload(string question, string[] answers)
{
Type = SpokeMessageType.AskQuestion;
Question = question;
Answers = answers;
}
}
public enum SpokeMessageType : int
{
JoinGame = 0,
AnswerQuestion = 1,
AskQuestion = 2
}
}