コード例 #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));
        }
コード例 #4
0
 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;
        }