From f69e5da31f9a2ee877d8cd6f53437ab6e1c2ef7f Mon Sep 17 00:00:00 2001
From: sili98 <sili98@sch.bme.hu>
Date: Fri, 12 Mar 2021 12:42:55 +0100
Subject: [PATCH] Room related issues rectified.

---
 zsirozas/Form1.cs           | 47 +++++++++++++++++++++++++++----------
 zsirozas/GameClient.cs      | 10 ++++++++
 zsirozas/GameServer.cs      |  6 +++++
 zsirozas/InfoDialog.cs      |  2 ++
 zsirozas/RawTcpTransport.cs |  2 +-
 5 files changed, 53 insertions(+), 14 deletions(-)

diff --git a/zsirozas/Form1.cs b/zsirozas/Form1.cs
index 0f9e320..d1d913d 100644
--- a/zsirozas/Form1.cs
+++ b/zsirozas/Form1.cs
@@ -333,7 +333,8 @@ namespace zsirozas
             {
                 try
                 {
-                    //client.remoteServer.RemoveUser(client.userID); //todo visszarakni amint nem fedi el a többi hibát
+                    client.LeaveCurrentRoom();
+                    //TODO: logout?
                 }
                 catch (Exception)
                 {
@@ -375,7 +376,10 @@ namespace zsirozas
                 {
                     var info = infos[(sender as ListBox).Items[idx].ToString()];
                     bool joined = InfoDialog.ShowRoomInfo(info, client.roomID);
-                    //TODO: be is lépni
+                    if (joined)
+                    {
+                        client.JoinRoom(info.roomID);
+                    }
                 }
         }
         private void listBoxUsers_DrawItem(object sender, DrawItemEventArgs e)
@@ -467,14 +471,20 @@ namespace zsirozas
 
         private void buttonCreateRoom_Click(object sender, EventArgs e)
         {
-            string roomID = (sender as GameClient).roomID;
-            if (roomID == null)
+            try
             {
-                //TODO: belépni a szobába
+                if (client.roomID == null)
+                {
+                    client.CreateRoom();
+                }
+                else
+                {
+                    client.LeaveCurrentRoom();
+                }
             }
-            else
+            catch (ServerErrorException ex)
             {
-                (sender as GameClient).remoteServer.LeaveRoom(roomID);
+                HandleServerError(this, ex.Message);
             }
         }
 
@@ -482,12 +492,23 @@ namespace zsirozas
         {
             int idx = (sender as ListBox).IndexFromPoint(e.Location);
             //todo akarom-e a klienst lock-olni?
-            lock (client) if (idx != ListBox.NoMatches)
-                {
-                    var info = client.remoteServer.UserInfo((sender as ListBox).Items[idx].ToString());
-                    bool kickedOut = InfoDialog.ShowUserInfo(info, !info.occupied && info.roomID == null && info.userID != client.userID && client.roomID != null, "KickOut");
-                    //TODO: ki is rúgni
-                }
+            try
+            {
+                lock (client) if (idx != ListBox.NoMatches)
+                    {
+                        var info = client.remoteServer.UserInfo((sender as ListBox).Items[idx].ToString());
+                        bool kickedOut = InfoDialog.ShowUserInfo(info, !info.occupied && info.roomID == null && info.userID != client.userID && client.roomID != null, "KickOut");
+                        //TODO: ki is rúgni
+                        if (kickedOut)
+                        {
+                            client.remoteServer.LeaveRoom(info.userID);
+                        }
+                    }
+            }
+            catch (ServerErrorException ex)
+            {
+                HandleServerError(this, ex.Message);
+            }
         }
 
         private void buttonUseSignalR_Click(object sender, EventArgs e)
diff --git a/zsirozas/GameClient.cs b/zsirozas/GameClient.cs
index 0c192bf..7132cd7 100644
--- a/zsirozas/GameClient.cs
+++ b/zsirozas/GameClient.cs
@@ -118,6 +118,16 @@ namespace zsirozas
             remoteServer.JoinRoom(userID, roomID);
         }
 
+        public void LeaveCurrentRoom()
+        {
+            remoteServer.LeaveRoom(userID);
+            roomID = null;
+        }
+
+        public void CreateRoom()
+        {
+            roomID = remoteServer.CreateRoom(userID);
+        }
 
         public void SimpleNotify(ServerEvent @event, string param0)
         {
diff --git a/zsirozas/GameServer.cs b/zsirozas/GameServer.cs
index b3a6d38..0d00711 100644
--- a/zsirozas/GameServer.cs
+++ b/zsirozas/GameServer.cs
@@ -330,6 +330,10 @@ namespace zsirozas
                     catch (GameOverException ex)
                     {
                         Console.WriteLine("[LOG] Game " + ex.Message + "ended.");
+
+                        //TODO: occupioed állítás?
+                        RoomsByID[UsersByID[GamesByID[ex.Message].playersByOrder[0]].roomID].gameID = null;
+
                         GamesByID.Remove(ex.Message);
                         Games.Remove(ex.Message);
                         if (this.oneShot)
@@ -366,6 +370,7 @@ namespace zsirozas
                         Rooms.Add(r.roomID);
                         UsersByID[userID].roomID = r.roomID;
                         Console.WriteLine("[LOG] Room " + r.roomID + " has been created.");
+                        UsersByID[userID].client.SimpleNotify(ServerEvent.RoomChanged, r.roomID);
                         return r.roomID;
                     }
                     else throw new InvalidActionException("This user is already in a room. Leve the room to create a new one.");
@@ -440,6 +445,7 @@ namespace zsirozas
                     if (Rooms.Contains(UsersByID[userID].roomID))
                     {
                         RoomsByID[UsersByID[userID].roomID].userList.Remove(userID);
+                        UsersByID[userID].client.SimpleNotify(ServerEvent.RoomChanged, null);
                         Console.WriteLine("[LOG] " + userID + " left room " + UsersByID[userID].roomID);
                         if (!bigRoom && RoomsByID[UsersByID[userID].roomID].userList.Count == 0)
                         {
diff --git a/zsirozas/InfoDialog.cs b/zsirozas/InfoDialog.cs
index 4cbca57..d2752c7 100644
--- a/zsirozas/InfoDialog.cs
+++ b/zsirozas/InfoDialog.cs
@@ -28,6 +28,7 @@ namespace zsirozas
                 dialog.Height -= dialog.listBox1.Height;
                 dialog.buttonOK.Text = redefineOKButtonText;
                 dialog.buttonOK.Enabled = invitable;
+                dialog.buttonOK.Click += (sender, e) => { dialog.DialogResult = DialogResult.OK; };
                 return dialog.ShowDialog() == DialogResult.OK;
             }
         }
@@ -42,6 +43,7 @@ namespace zsirozas
                 dialog.listBox1.Items.AddRange(room.players);
                 dialog.buttonOK.Text = "Join";
                 dialog.buttonOK.Enabled = (room.gameID == null) && ownRoom == null;
+                dialog.buttonOK.Click += (sender, e) => { dialog.DialogResult = DialogResult.OK; };
                 return dialog.ShowDialog() == DialogResult.OK;
             }
         }
diff --git a/zsirozas/RawTcpTransport.cs b/zsirozas/RawTcpTransport.cs
index af84a8a..2685e0e 100644
--- a/zsirozas/RawTcpTransport.cs
+++ b/zsirozas/RawTcpTransport.cs
@@ -59,7 +59,7 @@ namespace zsirozas
 
         public bool ValidateConnection(object connInfo)
         {
-            return (msgConn.Client.RemoteEndPoint != null) && ((connInfo is IPAddress) && (IPAddress)connInfo == ((IPEndPoint)msgConn.Client.RemoteEndPoint).Address);
+            return (msgConn.Client.RemoteEndPoint != null) && ((connInfo is IPAddress) && ((IPAddress)connInfo).Equals(((IPEndPoint)msgConn.Client.RemoteEndPoint).Address));
         }
 
         public void ParseNextMessage(StreamReader sr)
-- 
GitLab