-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoll.cs
330 lines (291 loc) · 9.81 KB
/
Poll.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using SignalR;
using System.Xml.Serialization;
using System.Text;
using System.IO;
namespace VoteR
{
/// <summary>
/// Represents a poll.
/// </summary>
public class Poll
{
private readonly static Lazy<Poll> _instance = new Lazy<Poll>(() => new Poll());
private readonly static object _pollStateLock = new object();
private readonly static ConcurrentDictionary<string, VotingOption> _votingOptions = new ConcurrentDictionary<string, VotingOption>();
private static string _title;
private PollState _pollState = PollState.Closed;
private Poll()
{
LoadDefaultValues();
}
public static Poll Instance
{
get
{
return _instance.Value;
}
}
/// <summary>
/// State of the poll
/// </summary>
public PollState PollState
{
get { return _pollState; }
private set { _pollState = value; }
}
/// <summary>
/// Gets the current voting options.
/// </summary>
/// <returns>Collection of voting options.</returns>
public IEnumerable<VotingOption> GetVotingOptions()
{
return _votingOptions.Values;
}
/// <summary>
/// Poll title.
/// </summary>
public string Title
{
get
{
return _title;
}
set
{
_title = value;
}
}
/// <summary>
/// Poll total votes.
/// </summary>
public int TotalVotes
{
get
{
return _votingOptions.Sum(v => v.Value.Votes);
}
}
/// <summary>
/// Broadcasts the polls state of all clients.
/// </summary>
/// <param name="pollState">Poll state to broadcast.</param>
private static void BroadcastPollStateChange(PollState pollState)
{
var clients = GetClients();
switch (pollState)
{
case PollState.Open:
clients.pollOpened();
break;
case PollState.Closed:
clients.pollClosed();
break;
case PollState.Reset:
clients.pollReset();
break;
default:
break;
}
}
/// <summary>
/// Opens the poll.
/// </summary>
public void Open()
{
if (PollState != VoteR.PollState.Open || PollState != VoteR.PollState.Opening)
{
lock (_pollStateLock)
{
if (PollState != VoteR.PollState.Open || PollState != VoteR.PollState.Opening)
{
PollState = VoteR.PollState.Open;
BroadcastPollStateChange(VoteR.PollState.Open);
}
}
}
}
/// <summary>
/// Closes the poll.
/// </summary>
public void Close()
{
if (PollState == VoteR.PollState.Open || PollState == VoteR.PollState.Opening)
{
lock (_pollStateLock)
{
if (PollState == VoteR.PollState.Open || PollState == VoteR.PollState.Opening)
{
PollState = VoteR.PollState.Closed;
BroadcastPollStateChange(VoteR.PollState.Closed);
}
}
}
}
/// <summary>
/// Resets the poll.
/// </summary>
public void Reset()
{
lock (_pollStateLock)
{
if (PollState != VoteR.PollState.Closed)
{
throw new InvalidOperationException("Poll must be closed before it can be reset.");
}
_votingOptions.Clear();
BroadcastPollStateChange(VoteR.PollState.Reset);
}
}
public void Export()
{
XmlSerializer serializer = new XmlSerializer(typeof(Poll));
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
serializer.Serialize(writer, this);
}
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(sb.ToString());
//// ParseErrors is an ArrayList containing any errors from the Load statement
//if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
//{
// // Handle any parse errors as required
//}
//else
//{
// if (htmlDoc.DocumentNode != null)
// {
// HtmlAgilityPack.HtmlNode containerNode = htmlDoc.DocumentNode.SelectSingleNode("//container");
// if (containerNode != null)
// {
// StringBuilder resultsHtml = new StringBuilder();
// List<VotingOption> votingOptions = this.GetVotingOptions().ToList();
// resultsHtml.AppendLine("<ul>");
// foreach (var item in votingOptions)
// {
// resultsHtml.AppendLine(string.Concat("<li>", item.Name, "</li>"));
// }
// foreach (var item in votingOptions)
// {
// resultsHtml.AppendLine(string.Concat("<li>", item.Name, "</li>"));
// }
// }
// }
//}
}
/// <summary>
/// Adds a voting option to the poll.
/// </summary>
/// <param name="name">Name of the voting option.</param>
/// <returns>True if the option has been added, otherwise false.</returns>
public bool AddVotingOption(string name)
{
return _votingOptions.TryAdd(name, new VotingOption(name));
}
/// <summary>
/// Makes a vote in the poll.
/// </summary>
/// <param name="name">Name of the voting option.</param>
/// <param name="cid">Connection id of the user.</param>
/// <returns>True if the vote has been added, otherwise false.</returns>
public bool PlaceVote(string name, string cid)
{
// only open polls can be voted on
if (PollState != VoteR.PollState.Open)
{
return false;
}
string votedOptionName;
if (UserHasVoted(cid, out votedOptionName))
{
// check if vote is for same option
if (votedOptionName == name)
{
return false;
}
RemoveUserVote(cid);
}
AddUserVote(name, cid);
return true;
}
/// <summary>
/// Loads default poll options.
/// </summary>
private void LoadDefaultValues()
{
new List<VotingOption>
{
new VotingOption("A"),
new VotingOption("B")
}.ForEach(votingOption => _votingOptions.TryAdd(votingOption.Name, votingOption));
_title = "Poll";
}
/// <summary>
/// Gets the current clients.
/// </summary>
/// <returns></returns>
private static dynamic GetClients()
{
return GlobalHost.ConnectionManager.GetHubContext<VotingHub>().Clients;
}
/// <summary>
/// Checks if a user has already voted.
/// </summary>
/// <param name="cid">Connection id of the user.</param>
/// <param name="name">Name of the voting option which has been voted for.</param>
/// <returns>True if user has already voted, otherwise false.</returns>
private bool UserHasVoted(string cid, out string name)
{
name = _votingOptions.SingleOrDefault(v => v.Value.UserHasVoted(cid)).Key;
return name != null;
}
/// <summary>
/// Removes a users vote.
/// </summary>
/// <param name="cid">Connection id of the user.</param>
private void RemoveUserVote(string cid)
{
foreach (var votingOption in _votingOptions.Where(v => v.Value.UserHasVoted(cid)))
{
votingOption.Value.RemoveVoter(cid);
}
}
/// <summary>
/// Adds a user's vote to a voting option.
/// </summary>
/// <param name="name">Name of voting option to add vote to.</param>
/// <param name="cid">Connection id of the user.</param>
private void AddUserVote(string name, string cid)
{
var votingOption = _votingOptions.SingleOrDefault(v => v.Value.Name == name);
if (votingOption.Value != null)
{
votingOption.Value.AddVoter(cid);
}
}
/// <summary>
/// Checks if a voting option exists.
/// </summary>
/// <param name="name">Name of the voting option to check for.</param>
/// <returns>True if the voting option exists, otherwise false.</returns>
private bool VotingOptionExists(string name)
{
return _votingOptions.Any(v => v.Value.Name == name);
}
}
/// <summary>
/// State of the poll.
/// </summary>
public enum PollState
{
Open,
Opening,
Closing,
Closed,
Reset
}
}