Kenneth van Ewijk пре 10 година
родитељ
комит
51dc7adc0d
3 измењених фајлова са 176 додато и 3 уклоњено
  1. 1 0
      YJMPD-UWP/Helpers/Settings.cs
  2. 70 2
      YJMPD-UWP/Model/GameHandler.cs
  3. 105 1
      YJMPD-UWP/Model/NetworkHandler.cs

+ 1 - 0
YJMPD-UWP/Helpers/Settings.cs

@@ -10,6 +10,7 @@ namespace YJMPD_UWP.Helpers
         static Settings()
         {
             //Define default settings here
+            Values["hostname"] = "imegumii.space";
         }
     }
 }

+ 70 - 2
YJMPD-UWP/Model/GameHandler.cs

@@ -1,4 +1,5 @@
-using YJMPD_UWP.Helpers.EventArgs;
+using System.Threading.Tasks;
+using YJMPD_UWP.Helpers.EventArgs;
 
 namespace YJMPD_UWP.Model
 {
@@ -7,13 +8,80 @@ namespace YJMPD_UWP.Model
         public delegate void OnStatusUpdateHandler(object sender, GameStatusUpdatedEventArgs e);
         public event OnStatusUpdateHandler OnStatusUpdate;
 
-        public enum GameStatus { STOPPED, SEARCHING, WAITING, ENDED, STARTING, STARTED }
+        public enum GameStatus { STOPPING, STOPPED, SEARCHING, WAITING, ENDED, STARTING, STARTED }
+        public GameStatus Status { get; private set; }
 
         private void UpdateGameStatus(GameStatus status)
         {
+            Status = status;
+
             if (OnStatusUpdate == null) return;
 
             OnStatusUpdate(this, new GameStatusUpdatedEventArgs(status));
         }
+
+        public GameHandler()
+        {
+            Status = GameStatus.STOPPED;
+        }
+
+
+
+
+        //Searching
+
+        public async Task<bool> Search()
+        {
+            return await SearchGame();
+        }
+
+        private async Task<bool> SearchGame()
+        {
+            UpdateGameStatus(GameStatus.SEARCHING);
+
+            bool foundgame;
+
+            foundgame = await App.Network.SearchGame();
+
+            //Do stuff
+
+            if (foundgame)
+                await Start();
+            else
+                await Stop();
+            return true;
+        }
+
+        //Starting and Stopping
+
+        public async Task<bool> Start()
+        {
+            return await StartGame();
+        }
+
+        public async Task<bool> Stop()
+        {
+            return await StopGame();
+        }
+
+        private async Task<bool> StartGame()
+        {
+            UpdateGameStatus(GameStatus.STARTING);
+
+            //Do stuff
+
+            UpdateGameStatus(GameStatus.STARTED);
+            return true;
+        }
+
+        private async Task<bool> StopGame()
+        {
+            UpdateGameStatus(GameStatus.STOPPING);
+
+            //Do stuff
+
+            UpdateGameStatus(GameStatus.STOPPED);
+            return true;
+        }
     }
 }

+ 105 - 1
YJMPD-UWP/Model/NetworkHandler.cs

@@ -1,4 +1,10 @@
-using YJMPD_UWP.Helpers.EventArgs;
+using System;
+using System.Threading.Tasks;
+using Windows.Networking;
+using Windows.Networking.Sockets;
+using Windows.Storage.Streams;
+using YJMPD_UWP.Helpers;
+using YJMPD_UWP.Helpers.EventArgs;
 
 namespace YJMPD_UWP.Model
 {
@@ -8,9 +14,16 @@ namespace YJMPD_UWP.Model
         public OnStatusUpdatedHandler OnStatusUpdated;
 
         public enum NetworkStatus { DISCONNECTED, CONNECTING, CONNECTED }
+        public NetworkStatus Status { get; private set; }
+
+        private StreamSocket client;
+        DataWriter dout;
+        DataReader din;
 
         private void UpdateNetworkStatus(NetworkStatus status)
         {
+            Status = status;
+
             if (OnStatusUpdated == null) return;
 
             OnStatusUpdated(this, new NetworkStatusUpdatedEventArgs(status));
@@ -18,7 +31,98 @@ namespace YJMPD_UWP.Model
 
         public NetworkHandler()
         {
+            Status = NetworkStatus.DISCONNECTED;
+            OpenConnection();
+        }
+
+
+        //API stuff
+        public async Task<bool> SearchGame()
+        {
+            Random r = new Random();
+            int l = r.Next(0, 2);
+            return l < 1;
+        }
+
+
+        //Writing and Reading
+        public async Task<string> Read()
+        {
+            return await ReadData();
+        }
+
+        public async Task<bool> Write(string data)
+        {
+            return await WriteData(data);
+        }
+
+        private async Task<string> ReadData()
+        {
+            uint length = din.ReadUInt32();
+            string data = din.ReadString(length);
+
+            return data;
+        }
+
+        private async Task<bool> WriteData(string data)
+        {
+            dout.WriteUInt32((uint)data.Length);
+            dout.WriteString(data);
+
+            await dout.FlushAsync();
+            
+            return true;
+        }
+
+        //Connecting and Disconnecting
+
+        public async Task<bool> Connect()
+        {
+            return await OpenConnection();
+        }
+
+        public async Task<bool> Disconnect()
+        {
+            return await CloseConnection();
+        }
+
+        private async Task<bool> OpenConnection()
+        {
+            UpdateNetworkStatus(NetworkStatus.CONNECTING);
+
+            client = new StreamSocket();
+
+            StreamSocketControl controller = client.Control;
+            controller.KeepAlive = true;
+
+            await client.ConnectAsync(new HostName(Settings.Values["hostname"] as string), "YJMPD-UWP-Server");
+
+
+            din = new DataReader(client.InputStream);
+            dout = new DataWriter(client.OutputStream);
+
+            din.UnicodeEncoding = UnicodeEncoding.Utf8;
+            din.ByteOrder = ByteOrder.LittleEndian;
+
+            dout.UnicodeEncoding = UnicodeEncoding.Utf8;
+            dout.ByteOrder = ByteOrder.LittleEndian;
+
+
+            UpdateNetworkStatus(NetworkStatus.CONNECTED);
+
+            return true;
+        }
+
+        private async Task<bool> CloseConnection()
+        {
+            UpdateNetworkStatus(NetworkStatus.DISCONNECTED);
+
+            din.Dispose();
+            dout.Dispose();
+
+            client.Dispose();
 
+            return true;
         }
     }
 }