From 76a6cd4ddbb20ae071217c0cbd6a861050d1ce8d Mon Sep 17 00:00:00 2001
From: sili98 <sili98@sch.bme.hu>
Date: Thu, 7 May 2020 19:13:30 +0200
Subject: [PATCH] Included SignalRTrasport.cs.

---
 zsirozas/Common.cs            |   2 +-
 zsirozas/SignalR-Transport.cs | 130 ++++++++++++++++++++++++++++++++++
 zsirozas/zsirozas.csproj      |   1 +
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 zsirozas/SignalR-Transport.cs

diff --git a/zsirozas/Common.cs b/zsirozas/Common.cs
index 0b8abb9..d3fa310 100644
--- a/zsirozas/Common.cs
+++ b/zsirozas/Common.cs
@@ -17,7 +17,7 @@ namespace zsirozas
 
         string CreateUser(string nickname, ConnectionType type, object backTalk);
         void RemoveUser(string userID);
-        string NewGame(string roomID);
+        string NewGame(string roomID);//TODO: hozzáadni hogy (string userID,...)
         string[] ListRooms();
         string[] ListUsers();
         ServerInfo ServerInfo();
diff --git a/zsirozas/SignalR-Transport.cs b/zsirozas/SignalR-Transport.cs
new file mode 100644
index 0000000..a97584d
--- /dev/null
+++ b/zsirozas/SignalR-Transport.cs
@@ -0,0 +1,130 @@
+using System;
+using System.Threading.Tasks;
+using Microsoft.AspNet.SignalR;
+//és a klienshez
+using Microsoft.AspNet.SignalR.Client;
+using Microsoft.AspNet.SignalR.Client.Transports;
+
+namespace zsirozas
+{
+    //............................................................
+    class SignalRRemoteServer : IServerApiProvider
+    {
+        IHubProxy hub;
+
+        public string CreateUser(string name, ConnectionType type, object backTalk)
+        {
+            return hub.Invoke<string>(nameof(CreateUser), name, type, backTalk).Result;
+        }
+
+        public string CreateRoom(string userID)
+        {
+            return hub.Invoke<string>(nameof(CreateRoom), userID).Result;
+        }
+
+        public void LeaveRoom(string userID)
+        {
+            hub.Invoke(nameof(LeaveRoom), userID).Wait();
+        }
+
+        public void JoinRoom(string userID, string roomID)
+        {
+            hub.Invoke(nameof(JoinRoom), roomID).Wait();
+        }
+
+        public void RemoveUser(string userID)
+        {
+            hub.Invoke(nameof(RemoveUser), userID).Wait();
+        }
+
+        public string NewGame(string roomID)
+        {
+            return hub.Invoke<string>(nameof(NewGame), roomID).Result;
+        }
+
+        IAppClient client;
+        public SignalRRemoteServer(string server_url)
+        {
+            hub = new HubConnection(server_url).CreateHubProxy(nameof(SignalRServerHub));
+
+            hub.On<ServerEvent, object>(nameof(client.SimpleNotify), client.SimpleNotify);
+            hub.On<GameEvent, object>(nameof(client.InGameNotify), client.InGameNotify);
+        }
+    }
+
+    //................................................................................
+
+    class SignalRServerHub : Hub<IAppClient>, IServerApiProvider
+    {
+        //a hubok tranziesnek, úgyhogy ezt csak így tudom értelmesen
+        //TODO: van-e ennek "szebb" módja?
+        public static IServerApiProvider appServer = new ActualServer();
+        public SignalRServerHub(IServerApiProvider serverInstance)
+        {
+            appServer = serverInstance;
+        }
+        public string CreateUser(string name, ConnectionType type, object backTalk)
+        {
+            switch (type)
+            {
+                case ConnectionType.SignalR:
+                    if (backTalk != null) goto default;
+
+                    backTalk = Context.ConnectionId;
+                    break;
+                case ConnectionType.RawTCP:
+                    throw new Exception("What? Why?");
+                default:
+                    throw new NotSupportedException("You can't do that");
+            }
+            return appServer.CreateUser(name, type, backTalk);
+        }
+
+        public string CreateRoom(string userID)
+        {
+            return appServer.CreateRoom(userID);
+        }
+
+        public void LeaveRoom(string userID)
+        {
+            appServer.LeaveRoom(userID);
+        }
+
+        public void JoinRoom(string userID, string roomID)
+        {
+            appServer.JoinRoom(userID, roomID);
+        }
+
+        public void RemoveUser(string userID)
+        {
+            appServer.RemoveUser(userID);
+        }
+
+        public string NewGame(string roomID)
+        {
+            return appServer.NewGame(roomID);
+        }
+    }
+    class AppClientWithSignalR : IAppClient
+    {
+        public string signalRConnID;
+
+        public AppClientWithSignalR(string backTalk)
+        {
+            this.signalRConnID = backTalk;
+        }
+
+        public void InGameNotify(GameEvent @event, object param0)
+        {
+            var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>();
+            (context as IAppClient).InGameNotify(@event, param0);
+        }
+
+        public void SimpleNotify(ServerEvent @event, object param0)
+        {
+            var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>();
+            (context as IAppClient).SimpleNotify(@event, param0);
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/zsirozas/zsirozas.csproj b/zsirozas/zsirozas.csproj
index 57cd48c..763a1be 100644
--- a/zsirozas/zsirozas.csproj
+++ b/zsirozas/zsirozas.csproj
@@ -61,6 +61,7 @@
     <Compile Include="Common.cs" />
     <Compile Include="RawTcpTransport.cs" />
     <Compile Include="Rooms.cs" />
+    <Compile Include="SignalR-Transport.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />
-- 
GitLab