Example #1
0
        /// <summary>
        /// Reads all messages from the other client and raises the appropriate event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void GameReader_DoWork(object sender, DoWorkEventArgs e)
        {
            var              reader = (StreamReader)e.Argument;
            string           currentString;
            Message <object> currentMessage;

            while (tcpClient.Connected)
            {
                try
                {
                    currentString = reader.ReadLine();
                }
                catch (Exception)
                {
                    currentString = null;
                }

                if (!(currentString == null || currentString == ""))
                {
                    currentMessage = JsonConvert.Deserialise <Message <object> >(currentString);
                    if (currentMessage != default(Message <object>))
                    {
                        if (currentMessage.Type == typeof(string).ToString())
                        {
                            MessageRecieved?.Invoke(null, new GameConnectionEventArgs <string>(JsonConvert.Deserialise <Message <string> >(currentString).Data, currentString));
                        }
                        else if (currentMessage.Type == typeof(Point).ToString())
                        {
                            LocationRecieved?.Invoke(null, new GameConnectionEventArgs <Point>(JsonConvert.Deserialise <Message <Point> >(currentString).Data, currentString));
                        }
                    }
                }
            }
            StopGame();
        }
Example #2
0
        /// <summary>
        /// Calculates a valid move and sends this location to the game engine.
        /// </summary>
        private void SendMessage()
        {
            Thread.Sleep(350);
            int x, y, count = 100;

            do
            {
                count--;
                x = random.Next((int)GridSize.Width);
                y = GetHeight(x);
            } while (y == -1 && count > 0);
            if (y != -1)
            {
                Point point = new Point(x, y);
                LocationRecieved?.Invoke(this, new GameConnectionEventArgs <Point>(point, ""));
            }
            else
            {
                Console.WriteLine("failed to find a spot to move!");
            }
        }