예제 #1
0
        private bool CheckForPrevClient(TcpClient prevClient)
        {
            TcpHandler.WriteMessage(prevClient, TcpProtocol.CheckForDisconnectSend());

            int  waitTimeInMillis     = 500;
            bool noResponsePrevClient = false;

            long startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

            while (prevClient.Available <= 0)
            {
                long currentTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                if (currentTime - startTime >= waitTimeInMillis)
                {
                    noResponsePrevClient = true;
                    break;
                }
            }
            if (noResponsePrevClient)
            {
                return(false);
            }

            TcpHandler.ReadMessage(prevClient);
            return(true);
        }
예제 #2
0
        private Highscore ReadHighscore()
        {
            string    received  = TcpHandler.ReadMessage(client);
            string    data      = JObject.Parse(received)["data"].ToString();
            Highscore highscore = JsonConvert.DeserializeObject <Highscore>(data);

            return(highscore);
        }
예제 #3
0
        private Tuple <int, GameData> ReadId()
        {
            string   received = TcpHandler.ReadMessage(client);
            JObject  jObject  = JObject.Parse(received);
            string   data     = (string)jObject["data"];
            GameData gameData = JsonConvert.DeserializeObject <GameData>(data);

            return(Tuple.Create((int)jObject["id"], gameData));
        }
 public JObject Read()
 {
     try
     {
         return(JsonConvert.DeserializeObject <dynamic>(TcpHandler.ReadMessage(client)));
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception");
         Disconnected = true;
         return(null);
     }
 }
예제 #5
0
        private GameData ReadData()
        {
            string   received = TcpHandler.ReadMessage(client);
            JObject  jObject  = JObject.Parse(received);
            string   data     = (string)jObject["data"];
            GameData gameData = JsonConvert.DeserializeObject <GameData>(data);

            // Fix bodies because for some reason it parses with an extra 0,0 coord in the beginning
            foreach (Snake snake in gameData.Snakes)
            {
                snake.Body.RemoveFirst();
            }
            return(gameData);
        }
예제 #6
0
        private bool ReadEndGame()
        {
            string  message = TcpHandler.ReadMessage(client);
            JObject jObject = JObject.Parse(message);
            string  command = (string)jObject["command"];

            switch (command)
            {
            case "tick/send":
                return(false);

            case "end/send":
                return(true);
            }
            return(false);
        }
예제 #7
0
        public void StartGame()
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");

//            IPAddress ip = IPAddress.Parse("145.49.59.202");
            client = new TcpClient(ip.ToString(), 6963);

            while (client.Available <= 0)
            {
                if (Disconnected)
                {
                    Console.WriteLine("");
                    return;
                }
            }

            string  received = TcpHandler.ReadMessage(client);
            JObject jObject  = JObject.Parse(received);
            string  command  = (string)jObject["command"];
            Tuple <int, GameData> tuple;

            if (command != "disconnect/send")
            {
                tuple = Tuple.Create((int)jObject["id"], JsonConvert.DeserializeObject <GameData>((string)jObject["data"]));
            }
            else
            {
                TcpHandler.WriteMessage(client, new JObject());
                tuple = ReadId();
            }
            SendName();

            id            = tuple.Item1;
            Disconnected  = false;
            gameData      = tuple.Item2;
            draw.GameData = gameData;
        }