201 lines
10 KiB
Diff
201 lines
10 KiB
Diff
|
|
Subject: [PATCH] clean up code
|
||
|
|
---
|
||
|
|
Index: Manager/WebApiManager.cs
|
||
|
|
IDEA additional info:
|
||
|
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||
|
|
<+>UTF-8
|
||
|
|
===================================================================
|
||
|
|
diff --git a/Manager/WebApiManager.cs b/Manager/WebApiManager.cs
|
||
|
|
--- a/Manager/WebApiManager.cs (revision 5129f68dadfebc3477fa29452cc914fcbcbb0106)
|
||
|
|
+++ b/Manager/WebApiManager.cs (revision cc94e7fcc8edfcb500c176ec8e111d0f8ca98866)
|
||
|
|
@@ -4,23 +4,23 @@
|
||
|
|
using System.Text;
|
||
|
|
namespace Zerolauncher.Manager
|
||
|
|
{
|
||
|
|
- internal class WebApiManager
|
||
|
|
+ internal static class WebApiManager
|
||
|
|
{
|
||
|
|
- static HttpListener listener = new HttpListener();
|
||
|
|
- static CancellationTokenSource cts = new CancellationTokenSource();
|
||
|
|
+ static readonly HttpListener Listener = new HttpListener();
|
||
|
|
+ static readonly CancellationTokenSource Cts = new CancellationTokenSource();
|
||
|
|
public static async Task StartListener()
|
||
|
|
{
|
||
|
|
- listener.Prefixes.Add("http://127.0.0.1:7233/");
|
||
|
|
- listener.Start();
|
||
|
|
+ Listener.Prefixes.Add("http://127.0.0.1:7233/");
|
||
|
|
+ Listener.Start();
|
||
|
|
Trace.WriteLine("Listening...");
|
||
|
|
|
||
|
|
- while (!cts.Token.IsCancellationRequested)
|
||
|
|
+ while (!Cts.Token.IsCancellationRequested)
|
||
|
|
{
|
||
|
|
- HttpListenerContext context = await listener.GetContextAsync();
|
||
|
|
- HttpListenerRequest request = context.Request;
|
||
|
|
- HttpListenerResponse response = context.Response;
|
||
|
|
+ var context = await Listener.GetContextAsync();
|
||
|
|
+ var request = context.Request;
|
||
|
|
+ var response = context.Response;
|
||
|
|
var raw = request.RawUrl;
|
||
|
|
- ResponesBody? rb = null;
|
||
|
|
+ ResponseBody? rb = null;
|
||
|
|
if (raw != null && raw.Contains("?"))
|
||
|
|
{
|
||
|
|
var lines = raw.Split("?");
|
||
|
|
@@ -34,121 +34,79 @@
|
||
|
|
switch (lines[0])
|
||
|
|
{
|
||
|
|
case "/CloseGame":
|
||
|
|
- {
|
||
|
|
- if (EngineManager.ExitGame(acc))
|
||
|
|
- {
|
||
|
|
- rb = new ResponesBody(1, 0, $"Account [{nick}] successful to close.");
|
||
|
|
- }
|
||
|
|
- else
|
||
|
|
- {
|
||
|
|
- rb = new ResponesBody($"Account [{nick}] is unluacher.");
|
||
|
|
- break;
|
||
|
|
- }
|
||
|
|
- }
|
||
|
|
+ {
|
||
|
|
+ rb = EngineManager.ExitGame(acc) ? new ResponseBody(1, 0, $"Account [{nick}] successful to close.") : new ResponseBody($"Account [{nick}] is unluacher.");
|
||
|
|
+ }
|
||
|
|
break;
|
||
|
|
case "/ToNormalSize":
|
||
|
|
- {
|
||
|
|
- switch (EngineManager.TurnGameSizeNormal(acc))
|
||
|
|
- {
|
||
|
|
- case 2:
|
||
|
|
- rb = new ResponesBody(1, 0, $"Account [{nick}] now is normal size.");
|
||
|
|
- break;
|
||
|
|
- case 1:
|
||
|
|
- rb = new ResponesBody(0, 0, $"Account [{nick}] is loading, please wait.");
|
||
|
|
- break;
|
||
|
|
- case 0:
|
||
|
|
- rb = new ResponesBody($"Account [{nick}] is unluacher.");
|
||
|
|
- break;
|
||
|
|
- }
|
||
|
|
- }
|
||
|
|
+ {
|
||
|
|
+ rb = EngineManager.TurnGameSizeNormal(acc) switch
|
||
|
|
+ {
|
||
|
|
+ 2 => new ResponseBody(1, 0, $"Account [{nick}] now is normal size."),
|
||
|
|
+ 1 => new ResponseBody(0, 0, $"Account [{nick}] is loading, please wait."),
|
||
|
|
+ 0 => new ResponseBody($"Account [{nick}] is unluacher."),
|
||
|
|
+ _ => rb
|
||
|
|
+ };
|
||
|
|
+ }
|
||
|
|
break;
|
||
|
|
case "/ToMiniSize":
|
||
|
|
- {
|
||
|
|
- switch (EngineManager.TurnGameSizeMini(acc))
|
||
|
|
- {
|
||
|
|
- case 2:
|
||
|
|
- rb = new ResponesBody(1, 0, $"Account [{nick}] now is mini size.");
|
||
|
|
- break;
|
||
|
|
- case 1:
|
||
|
|
- rb = new ResponesBody(0, 0, $"Account [{nick}] is loading, please wait.");
|
||
|
|
- break;
|
||
|
|
- case 0:
|
||
|
|
- rb = new ResponesBody($"Account [{nick}] is unluacher.");
|
||
|
|
- break;
|
||
|
|
- }
|
||
|
|
- }
|
||
|
|
+ {
|
||
|
|
+ rb = EngineManager.TurnGameSizeMini(acc) switch
|
||
|
|
+ {
|
||
|
|
+ 2 => new ResponseBody(1, 0, $"Account [{nick}] now is mini size."),
|
||
|
|
+ 1 => new ResponseBody(0, 0, $"Account [{nick}] is loading, please wait."),
|
||
|
|
+ 0 => new ResponseBody($"Account [{nick}] is unluacher."),
|
||
|
|
+ _ => rb
|
||
|
|
+ };
|
||
|
|
+ }
|
||
|
|
break;
|
||
|
|
case "/LoginGame":
|
||
|
|
{
|
||
|
|
if (EngineManager.CreateGame(acc))
|
||
|
|
{
|
||
|
|
- rb = new ResponesBody(0, 0, $"Account [{nick}] successful to start.");
|
||
|
|
+ rb = new ResponseBody(0, 0, $"Account [{nick}] successful to start.");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
- int hwnd = EngineManager.CheckGameState(acc);
|
||
|
|
- if (hwnd <= 0)
|
||
|
|
- {
|
||
|
|
- rb = new ResponesBody(0, 0, $"Account [{nick}] is loading, please wait.");
|
||
|
|
- }
|
||
|
|
- else
|
||
|
|
- {
|
||
|
|
- rb = new ResponesBody(1, hwnd, $"Account [{nick}] now is running.");
|
||
|
|
- }
|
||
|
|
+ var hwnd = EngineManager.CheckGameState(acc);
|
||
|
|
+ rb = hwnd <= 0 ? new ResponseBody(0, 0, $"Account [{nick}] is loading, please wait.") : new ResponseBody(1, hwnd, $"Account [{nick}] now is running.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
- rb = new ResponesBody($"commad [{lines[0]}] undefind.");
|
||
|
|
+ rb = new ResponseBody($"commad [{lines[0]}] undefind.");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
- rb = new ResponesBody($"Account [{nick}] undefind.");
|
||
|
|
+ rb = new ResponseBody($"Account [{nick}] undefind.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
- rb = new ResponesBody("args is missing.");
|
||
|
|
+ rb = new ResponseBody("args is missing.");
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
- if (rb == null)
|
||
|
|
- {
|
||
|
|
- rb = new ResponesBody();
|
||
|
|
- }
|
||
|
|
- byte[] buffer = Encoding.UTF8.GetBytes(rb.Encode());
|
||
|
|
+ rb ??= new ResponseBody();
|
||
|
|
+ var buffer = Encoding.UTF8.GetBytes(rb.Encode());
|
||
|
|
|
||
|
|
response.ContentLength64 = buffer.Length;
|
||
|
|
- Stream output = response.OutputStream;
|
||
|
|
+ var output = response.OutputStream;
|
||
|
|
await output.WriteAsync(buffer, 0, buffer.Length);
|
||
|
|
output.Close();
|
||
|
|
}
|
||
|
|
- listener.Stop();
|
||
|
|
+ Listener.Stop();
|
||
|
|
}
|
||
|
|
|
||
|
|
- public static void StopListener() { cts.Cancel(); }
|
||
|
|
+ public static void StopListener() { Cts.Cancel(); }
|
||
|
|
}
|
||
|
|
|
||
|
|
- class ResponesBody
|
||
|
|
+ class ResponseBody(int status = -1, int hwnd = 0, string message = "Hello, World!")
|
||
|
|
{
|
||
|
|
- public int status;
|
||
|
|
- public int hwnd;
|
||
|
|
- public string message;
|
||
|
|
-
|
||
|
|
- public ResponesBody(string message)
|
||
|
|
+ public ResponseBody(string message) : this(-1, 0, message)
|
||
|
|
{
|
||
|
|
- this.status = -1;
|
||
|
|
- this.hwnd = 0;
|
||
|
|
- this.message = message;
|
||
|
|
- }
|
||
|
|
-
|
||
|
|
- public ResponesBody(int status=-1, int hwnd=0, string message= "Hello, World!")
|
||
|
|
- {
|
||
|
|
- this.status = status;
|
||
|
|
- this.hwnd = hwnd;
|
||
|
|
- this.message = message;
|
||
|
|
}
|
||
|
|
|
||
|
|
public string Encode()
|