-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
426 lines (367 loc) · 15.5 KB
/
Program.cs
File metadata and controls
426 lines (367 loc) · 15.5 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.WebSockets;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using EchoButtons;
namespace Klaxio
{
// l11n
static class L
{
static ResourceManager _rm = new ResourceManager(
"Klaxio.Strings", Assembly.GetExecutingAssembly());
public static CultureInfo Culture { get; private set; } = CultureInfo.InvariantCulture; // = English
public static void SetLanguage(string lang)
{
Culture = lang == "de"
? new CultureInfo("de")
: CultureInfo.InvariantCulture;
}
public static string Get(string key) =>
_rm.GetString(key, Culture) ?? $"[{key}]";
public static string Get(string key, params object[] args) =>
string.Format(Get(key), args);
// All UI strings as a dictionary for the browser
public static Dictionary<string, string> UiStrings() => new Dictionary<string, string>
{
["player1"] = Get("Player1"),
["player2"] = Get("Player2"),
["phaseReady"] = Get("PhaseReady"),
["phaseArmed"] = Get("PhaseArmed"),
["phaseBuzzed"] = Get("PhaseBuzzed"),
["phaseCorrect"] = Get("PhaseCorrect"),
["phaseWrong"] = Get("PhaseWrong"),
["btnArm"] = Get("BtnArm"),
["btnCorrect"] = Get("BtnCorrect"),
["btnWrong"] = Get("BtnWrong"),
["btnReset"] = Get("BtnReset"),
["btnResetScores"] = Get("BtnResetScores"),
["toastBuzzed"] = Get("ToastBuzzed"),
["confirmReset"] = Get("ConfirmReset"),
["connConnecting"] = Get("ConnConnecting"),
["connConnected"] = Get("ConnConnected"),
["connReconnecting"]= Get("ConnReconnecting"),
["toastConnected"] = Get("ToastConnected"),
["langToggle"] = Get("LangToggle"),
};
}
// Quiz logic
enum Phase { Waiting, Armed, Buzzed, Correct, Wrong }
class QuizGame
{
public Phase Phase { get; private set; } = Phase.Waiting;
public int[] Scores { get; } = { 0, 0 };
public string[] Names { get; } = { L.Get("Player1"), L.Get("Player2") };
public int Winner { get; private set; } = -1;
public WebSocketServer WebSocketServer { get; set; }
EchoButton _echoButton;
List<string> _buttonOrder = new List<string>();
object _lock = new object();
public void InitButtons()
{
_echoButton = new EchoButton();
_echoButton.FoundPairedDevice += (s, e) =>
{
lock (_lock)
{
int idx = _buttonOrder.Count;
_buttonOrder.Add(e.ButtonName);
if (idx < 2) Names[idx] = $"{L.Get(idx == 0 ? "Player1" : "Player2")} [{e.ButtonName}]";
}
Console.WriteLine(L.Get("BtFound", e.ButtonName));
};
_echoButton.NoPairedDevices += (s, e) =>
Console.WriteLine(L.Get("BtNone"));
_echoButton.Connected += (s, e) =>
Console.WriteLine(L.Get("BtConnected", e.ButtonName));
_echoButton.Disconnected += (s, e) =>
Console.WriteLine(L.Get("BtDisconnected", e.ButtonName));
_echoButton.Pressed += OnButtonPressed;
_echoButton.Released += (s, e) => { };
_echoButton.StartListening();
Console.WriteLine(L.Get("BtSearching"));
}
void OnButtonPressed(object sender, ButtonEventArgs e)
{
int playerIdx = GetPlayerIndex(e.ButtonName);
Console.WriteLine(L.Get("BtnPressed", e.ButtonName, playerIdx + 1));
lock (_lock)
{
if (Phase != Phase.Armed) return;
Winner = playerIdx;
Phase = Phase.Buzzed;
}
WebSocketServer?.BroadcastAsync(new
{
@event = "buzzed",
winner = playerIdx,
name = Names[playerIdx]
}).GetAwaiter().GetResult();
}
int GetPlayerIndex(string buttonName)
{
lock (_lock)
{
int idx = _buttonOrder.IndexOf(buttonName);
return idx >= 0 ? idx : 0;
}
}
public object ProcessCommand(string cmd)
{
lock (_lock)
{
switch (cmd)
{
case "arm":
if (Phase == Phase.Waiting || Phase == Phase.Correct || Phase == Phase.Wrong)
{
Phase = Phase.Armed;
Winner = -1;
Console.WriteLine(L.Get("QuizArmed"));
return new { @event = "armed" };
}
break;
case "correct":
if (Phase == Phase.Buzzed && Winner >= 0)
{
Scores[Winner]++;
Phase = Phase.Correct;
Console.WriteLine(L.Get("QuizCorrect", Winner + 1, Scores[Winner]));
return new { @event = "correct", winner = Winner, scores = Scores };
}
break;
case "wrong":
if (Phase == Phase.Buzzed && Winner >= 0)
{
Phase = Phase.Wrong;
Console.WriteLine(L.Get("QuizWrong", Winner + 1));
return new { @event = "wrong", winner = Winner, scores = Scores };
}
break;
case "reset":
Phase = Phase.Waiting;
Winner = -1;
Console.WriteLine(L.Get("QuizReset"));
return new { @event = "reset" };
case "reset_scores":
Scores[0] = 0;
Scores[1] = 0;
Phase = Phase.Waiting;
Winner = -1;
Console.WriteLine(L.Get("QuizResetScores"));
return new { @event = "reset_scores", scores = Scores };
case "lang_de":
L.SetLanguage("de");
for (int i = 0; i < Math.Min(_buttonOrder.Count, 2); i++)
Names[i] = $"{L.Get(i == 0 ? "Player1" : "Player2")} [{_buttonOrder[i]}]";
if (_buttonOrder.Count == 0) { Names[0] = L.Get("Player1"); Names[1] = L.Get("Player2"); }
return new { @event = "strings", strings = L.UiStrings(), names = Names };
case "lang_en":
L.SetLanguage("en");
for (int i = 0; i < Math.Min(_buttonOrder.Count, 2); i++)
Names[i] = $"{L.Get(i == 0 ? "Player1" : "Player2")} [{_buttonOrder[i]}]";
if (_buttonOrder.Count == 0) { Names[0] = L.Get("Player1"); Names[1] = L.Get("Player2"); }
return new { @event = "strings", strings = L.UiStrings(), names = Names };
}
}
return null;
}
public object GetInitState() => new
{
@event = "init",
names = Names,
scores = Scores,
phase = Phase.ToString().ToLower(),
strings = L.UiStrings()
};
}
// WebSocket and HTTP server
class WebSocketServer
{
readonly QuizGame _game;
readonly List<WebSocket> _clients = new List<WebSocket>();
readonly object _clientLock = new object();
// The HTML is embedded as a resource; we read it once and patch in the WS URL.
static readonly string _html = LoadHtml();
public WebSocketServer(QuizGame game) => _game = game;
static string LoadHtml()
{
var asm = Assembly.GetExecutingAssembly();
// Resource name: <AssemblyName>.<filename>
using var stream = asm.GetManifestResourceStream("Klaxio.frontend.html");
if (stream == null)
throw new Exception("Embedded resource 'Klaxio.frontend.html' not found. " +
"Make sure frontend.html is in the project with Build Action = EmbeddedResource.");
using var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
public async Task StartAsync(string url)
{
var listener = new HttpListener();
string httpUrl = url.Replace("ws://", "http://");
if (!httpUrl.EndsWith("/")) httpUrl += "/";
listener.Prefixes.Add(httpUrl);
listener.Start();
Console.WriteLine(L.Get("WsListening", url));
Console.WriteLine(L.Get("Hotkeys"));
Console.WriteLine();
// Open browser automatically
Console.WriteLine(L.Get("BrowserOpening"));
try { Process.Start(new ProcessStartInfo(httpUrl) { UseShellExecute = true }); }
catch { /* ignore if no default browser */ }
_ = Task.Run(ConsoleInputLoop);
while (true)
{
var ctx = await listener.GetContextAsync();
if (ctx.Request.IsWebSocketRequest)
_ = Task.Run(() => HandleClientAsync(ctx));
else
_ = Task.Run(() => ServeHttp(ctx));
}
}
// Serve the embedded HTML for any non-WS GET request
void ServeHttp(HttpListenerContext ctx)
{
try
{
var path = ctx.Request.Url.AbsolutePath.TrimStart('/');
// Statische Assets aus wwwroot
if (path.StartsWith("wwwroot/") || path.StartsWith("css/") || path.StartsWith("fonts/") || path.StartsWith("img/") || path.StartsWith("js/"))
{
var resourceName = $"Klaxio.wwwroot.{path.Replace('/', '.')}";
var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName);
if (stream != null)
{
ctx.Response.ContentType = GetMimeType(path);
ctx.Response.ContentLength64 = stream.Length;
stream.CopyTo(ctx.Response.OutputStream);
ctx.Response.Close();
return;
}
}
// Fallback: HTML ausliefern
var bytes = Encoding.UTF8.GetBytes(_html);
ctx.Response.ContentType = "text/html; charset=utf-8";
ctx.Response.ContentLength64 = bytes.Length;
ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
ctx.Response.Close();
}
catch { }
}
static string GetMimeType(string path) => Path.GetExtension(path) switch
{
".css" => "text/css",
".js" => "application/javascript",
".woff2" => "font/woff2",
".woff" => "font/woff",
".ttf" => "font/ttf",
".svg" => "image/svg+xml",
".ico" => "image/x-icon",
_ => "application/octet-stream"
};
async Task HandleClientAsync(HttpListenerContext ctx)
{
var wsCtx = await ctx.AcceptWebSocketAsync(null);
var ws = wsCtx.WebSocket;
Console.WriteLine(L.Get("WsClientConn", ctx.Request.RemoteEndPoint));
lock (_clientLock) _clients.Add(ws);
await SendAsync(ws, _game.GetInitState());
var buffer = new byte[4096];
try
{
while (ws.State == WebSocketState.Open)
{
var result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close) break;
var text = Encoding.UTF8.GetString(buffer, 0, result.Count);
try
{
var doc = JsonDocument.Parse(text);
if (doc.RootElement.TryGetProperty("cmd", out var cmdEl))
{
var response = _game.ProcessCommand(cmdEl.GetString());
if (response != null) await BroadcastAsync(response);
}
}
catch { }
}
}
catch { }
finally
{
lock (_clientLock) _clients.Remove(ws);
Console.WriteLine(L.Get("WsClientDisconn", ctx.Request.RemoteEndPoint));
try { await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } catch { }
}
}
public async Task BroadcastAsync(object message)
{
var json = JsonSerializer.Serialize(message);
var bytes = Encoding.UTF8.GetBytes(json);
var segment = new ArraySegment<byte>(bytes);
List<WebSocket> snapshot;
lock (_clientLock) snapshot = new List<WebSocket>(_clients);
foreach (var ws in snapshot)
{
try { if (ws.State == WebSocketState.Open) await ws.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None); }
catch { }
}
}
async Task SendAsync(WebSocket ws, object message)
{
var json = JsonSerializer.Serialize(message);
var bytes = Encoding.UTF8.GetBytes(json);
await ws.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Text, true, CancellationToken.None);
}
void ConsoleInputLoop()
{
while (true)
{
var key = Console.ReadKey(intercept: true).Key;
string cmd = key switch
{
ConsoleKey.A => "arm",
ConsoleKey.C => "correct",
ConsoleKey.W => "wrong",
ConsoleKey.R => "reset",
_ => null
};
if (key == ConsoleKey.Q)
{
Console.WriteLine(L.Get("Quitting"));
Environment.Exit(0);
}
if (cmd != null)
{
var response = _game.ProcessCommand(cmd);
if (response != null) BroadcastAsync(response).GetAwaiter().GetResult();
}
}
}
}
// Entry point
class Program
{
static async Task Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(L.Get("AppTitle"));
Console.WriteLine();
var game = new QuizGame();
var server = new WebSocketServer(game);
game.WebSocketServer = server;
game.InitButtons();
await server.StartAsync("http://localhost:8765/");
}
}
}