-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
256 lines (213 loc) · 9.07 KB
/
Game.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
using Jeffpardy.Hubs;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
namespace Jeffpardy
{
/// <summary>
/// Represents a game in progress.
/// Operations on this class may cause side-effects such as outgoing SignalR calls to other clients.
/// </summary>
class Game
{
/// <summary>
/// Unique code for the game
/// </summary>
public string GameCode { get; private set; }
/// <summary>
/// Code to allow the host to join with a secondary client
/// </summary>
public string HostCode { get; private set; }
/// <summary>
/// Name of the group of clients connected as the host.
/// </summary>
private readonly string hostGroupName;
/// <summary>
/// Singleton gameHubContext to send requests to
/// </summary>
private readonly IHubContext<GameHub> gameHubContext;
/// <summary>
/// Players in the game; by connection Id
/// </summary>
readonly Dictionary<string, Player> players = new Dictionary<string, Player>();
/// <summary>
/// Dictionary of SignalR connections. Needed to track when we can remove this game from memory.
/// </summary>
readonly Dictionary<string, bool> connections = new Dictionary<string, bool>();
private Dictionary<string, Team> TeamDictionary
{
get
{
var buzzerTeams = this.players.Values
.GroupBy(x => x.Team)
.OrderBy(p => p.Key.ToString())
.ToDictionary(x => x.Key,
x => new Team()
{
Name = x.Key,
Players = x.OrderBy(o => o.Name).ToList()
});
return buzzerTeams;
}
}
int winningBuzzerTimeInMilliseconds = int.MaxValue;
Player winningBuzzerUser;
/// <summary>
/// List of all winners for this session. A team can win only once per session.
/// </summary>
readonly List<string> buzzerWinnerTeams = new List<string>();
readonly Timer buzzerWindowTimer;
public Game(IHubContext<GameHub> buzzerHubContext, string gameCode, string hostCode)
{
this.GameCode = gameCode;
this.HostCode = hostCode;
this.hostGroupName = gameCode + "-HOST";
this.gameHubContext = buzzerHubContext;
this.buzzerWindowTimer = new Timer(500);
this.buzzerWindowTimer.Elapsed += async (sender, args) =>
{
await this.AssignWinnerAsync();
};
}
public bool IsEmptyGame => this.connections.Count == 0;
public async Task ConnectHostAsync(string connectionId)
{
await this.gameHubContext.Groups.AddToGroupAsync(connectionId, this.hostGroupName);
await this.AddConnectionToGame(connectionId);
await this.SendUserListAsync(connectionId);
}
public async Task ConnectPlayerLobbyAsync(string connectionId)
{
await this.AddConnectionToGame(connectionId);
await this.SendUserListAsync(connectionId);
}
public async Task ConnectPlayerAsync(string connectionId, string team, string name)
{
await this.AddConnectionToGame(connectionId);
lock (this)
{
this.players.Add(connectionId, new Player()
{
ConnectionId = connectionId,
Team = team,
Name = name
});
}
await this.SendUserListToAllClientsAsync();
}
public async Task RemoveUserAsync(string connectionId)
{
if (this.players.ContainsKey(connectionId))
{
var item = this.players[connectionId];
this.players.Remove(item.ConnectionId);
await SendUserListToAllClientsAsync();
}
}
public async Task ResetBuzzerAsync()
{
lock (this)
{
this.buzzerWinnerTeams.Clear();
this.winningBuzzerUser = null;
this.winningBuzzerTimeInMilliseconds = int.MaxValue;
this.buzzerWindowTimer.Stop();
}
await gameHubContext.Clients.Group(this.GameCode).SendAsync("resetBuzzer");
}
public async Task ActivateBuzzerAsync()
{
lock (this)
{
this.winningBuzzerUser = null;
this.winningBuzzerTimeInMilliseconds = int.MaxValue;
this.buzzerWindowTimer.Stop();
}
await gameHubContext.Clients.Group(this.GameCode).SendAsync("activateBuzzer");
}
public async Task AssignWinnerAsync()
{
lock (this)
{
this.buzzerWindowTimer.Stop();
this.buzzerWinnerTeams.Add(this.winningBuzzerUser.Team);
}
await gameHubContext.Clients.Group(this.GameCode).SendAsync("assignWinner", this.winningBuzzerUser, this.winningBuzzerTimeInMilliseconds);
}
public void BuzzIn(string connectionId, int timeInMilliseconds, int handicapInMilliseconds)
{
lock (this)
{
Player buzzerUser = players[connectionId];
if (this.buzzerWinnerTeams.Contains(buzzerUser.Team))
{
// This team already won this session and isn't eligible; ignore it.
return;
}
if (!this.buzzerWindowTimer.Enabled)
{
buzzerWindowTimer.Start();
}
// Adjust the time by the handcicap - note the handicap must be positive.
// This prevents people like Nick from changing the handicap to a negative number to get sub-0 times.
if (handicapInMilliseconds > 0)
{
timeInMilliseconds += handicapInMilliseconds;
}
if (timeInMilliseconds < this.winningBuzzerTimeInMilliseconds)
{
this.winningBuzzerTimeInMilliseconds = timeInMilliseconds;
this.winningBuzzerUser = buzzerUser;
}
}
}
public async Task StartRoundAsync(GameRound round)
{
await gameHubContext.Clients.Groups(this.hostGroupName).SendAsync("startRound", round);
}
public async Task ShowClueAsync(CategoryClue clue)
{
await gameHubContext.Clients.Groups(this.hostGroupName).SendAsync("showClue", clue);
}
public async Task StartFinalJeffpardyAsync(Dictionary<string, int> scores)
{
await gameHubContext.Clients.Group(this.GameCode).SendAsync("startFinalJeffpardy", scores);
}
public async Task SubmitWagerAsync(string connectionId, int wager)
{
await gameHubContext.Clients.Group(this.hostGroupName).SendAsync("submitWager",
players[connectionId],
wager);
}
public async Task SubmitAnswerAsync(string connectionId, string answer, int timeInMilliseconds)
{
await gameHubContext.Clients.Group(this.hostGroupName).SendAsync("submitAnswer",
players[connectionId],
answer,
timeInMilliseconds);
}
private async Task SendUserListAsync(string connectionId)
{
await gameHubContext.Clients.Client(connectionId).SendAsync("updateUsers", this.TeamDictionary);
}
private async Task SendUserListToAllClientsAsync()
{
await gameHubContext.Clients.Groups(this.GameCode).SendAsync("updateUsers", this.TeamDictionary);
}
private async Task AddConnectionToGame(string connectionId)
{
this.connections[connectionId] = true;
await this.gameHubContext.Groups.AddToGroupAsync(connectionId, this.GameCode);
}
public async Task ShowFinalJeffpardyClueAsync()
{
await gameHubContext.Clients.Group(this.GameCode).SendAsync("showFinalJeffpardyClue");
}
public async Task EndFinalJeffpardyAsync()
{
await gameHubContext.Clients.Group(this.GameCode).SendAsync("endFinalJeffpardy");
}
}
}