Exemple #1
0
        public static void ReceiveMessage(Socket s)
        {
            int k = s.Receive(b);
            Console.WriteLine("Received:");
            string receivedMessage = "";
            for (int i = 0; i < k; i++)
                receivedMessage += Convert.ToChar(b[i]);
            Console.WriteLine(receivedMessage);

            string[] tokens = receivedMessage.Split(' ');

            switch (tokens[0])
            {
                case "isgameover?":
                {
                    if (game.IsOver())
                    {
                        Stats.AddVictory();
                        s.Send(enc.GetBytes("gameisover " + Stats.Victories));
                    }
                    else
                        s.Send(enc.GetBytes("gamenotover"));
                    break;
                }
                case "start":
                {
                    try
                    {
                        game = new Game(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3]));
                        s.Send(enc.GetBytes("ok"));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    break;
                }

                case "leftclick":
                {

                    string response = game.LeftClick(
                        Convert.ToInt32(tokens[1]),
                        Convert.ToInt32(tokens[2]));
                    //Console.WriteLine(response);
                    s.Send(enc.GetBytes(response));
                    break;
                }

                case "elapsedtime":
                {
                    s.Send(enc.GetBytes("elapsedtime " + game.time.ToString()));
                    break;
                }
                case "rightclick":
                {

                    string response = game.RightClick(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]));
                    //Console.WriteLine(response);
                    s.Send(enc.GetBytes(response));
                    break;
                }

                case "minesleft":
                {
                    string response = "minesleft " + game.MinesLeft;
                    s.Send(enc.GetBytes(response));
                    break;
                }

            }

            //s.Send(enc.GetBytes(receivedMessage.Split(' ')[0]));
            //Console.WriteLine("Sent Response");
        }
Exemple #2
0
        public void ReceiveMessage(Socket s)
        {
            int k = s.Receive(buffer);
            StringBuilder receivedMessageBuilder = new StringBuilder();
            for (int i = 0; i < k; i++)
                receivedMessageBuilder.Append((char)buffer[i]);
            string receivedMessage = receivedMessageBuilder.ToString();
            if (receivedMessage != "elapsedtime" && receivedMessage != "isgameover?" && receivedMessage != "minesleft")
            {
                Console.Write("Received [" + s.RemoteEndPoint + "]: ");
                Console.WriteLine(receivedMessage);
            }

            string[] tokens = receivedMessage.Split(' ');

            switch (tokens[0])
            {
                case "isgameover?":
                    {
                        if (game.IsOver())
                        {
                            Stats.AddVictory();
                            s.Send(enc.GetBytes("gameisover " + Stats.Victories));
                        }
                        else
                            s.Send(enc.GetBytes("gamenotover"));
                        break;
                    }
                case "start":
                    {
                        try
                        {
                            game = new Game(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3]));
                            s.Send(enc.GetBytes("ok"));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        break;
                    }

                case "leftclick":
                    {

                        string response = game.LeftClick(
                            Convert.ToInt32(tokens[1]),
                            Convert.ToInt32(tokens[2]));
                        //Console.WriteLine(response);
                        s.Send(enc.GetBytes(response));
                        break;
                    }

                case "elapsedtime":
                    {
                        s.Send(enc.GetBytes("elapsedtime " + game.time.ToString()));
                        break;
                    }
                case "rightclick":
                    {

                        string response = game.RightClick(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]));
                        //Console.WriteLine(response);
                        s.Send(enc.GetBytes(response));
                        break;
                    }

                case "minesleft":
                    {
                        string response = "minesleft " + game.MinesLeft;
                        s.Send(enc.GetBytes(response));
                        break;
                    }
            }
        }