Exemplo n.º 1
0
        public string MakeRequest(RequestContextClient request, string temp_msg)
        {
            string message = advice + path + http_version;

            message += "Content-Type: " + content_type;
            message += "Content-Lenght: " + (temp_msg.Length).ToString() + "\n";
            message += "Host: " + request.ip + ":" + request.port.ToString();
            message += "\nUserName: "******"_" + token;
            message += "\nPassword: "******"\n\n" + temp_msg + "\n";

            return(message);
        }
Exemplo n.º 2
0
        public static void EditYourDeckFunc(string response, RequestContextClient request, TcpClient client, NetworkStream stream, string incomeChoice)
        {
            Console.WriteLine(response);
            Console.WriteLine("Enter Cards for Deck (exact 4)");
            string number, message;
            var    msg     = new Message();
            int    counter = 1;

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(": ");
                if (incomeChoice == "n")
                {
                    number = Console.ReadLine();
                }
                else
                {
                    number = counter.ToString();
                    Console.WriteLine("Choice was {0}", counter);
                }

                message = msg.MakeRequest(request, number);
                sendData(stream, message);

                Console.WriteLine("Your current Deck");
                response = receiveData(client, stream);
                Console.WriteLine(response);

                if (response == "cardAlreadyUsed")
                {
                    Console.WriteLine("Card is already in the Deck");
                    i--;
                }
                if (response == "NumberToHigh")
                {
                    Console.WriteLine("Card does not exist");
                    i--;
                }

                counter++;
            }
        }
Exemplo n.º 3
0
        public static bool NotLoggedIn(bool loggedIn, RequestContextClient request, TcpClient client, NetworkStream stream, string incomeChoice)
        {
            while (loggedIn == false)
            {
                var    msg = new Message();
                string response, message;
                message = msg.CreateMessageForSend(request, incomeChoice); //da wird die message zum anmelden erstellt
                //Console.WriteLine();

                sendData(stream, message);
                //Console.Write("Sent:\n{0}", message);

                response = receiveData(client, stream);


                //Console.Write("Received:\n{0}", response);
                //Console.WriteLine("\ndu bist bis hier gekommen!");
                if (response.Trim('\n') == "Succsessful") //you are logged in
                {
                    return(true);
                }
                else if (response.Trim('\n') == "AccessDenied")
                {
                    Console.WriteLine("no more Attempts left!");
                    return(false);
                }
                else if (response.Trim('\n') == "TryAgain")
                {
                    continue;
                }
                if (response.Trim('\n') == "YouAreRegistred")
                {
                    response = "Succsessful";
                    receiveData(client, stream);
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        public string CreateMessageForSend(RequestContextClient request, string incomeChoice)
        {
            string temp_msg = null;
            string message  = "";


            if (request.message_number == "1")
            {
                if (incomeChoice == "n")
                {
                    Console.WriteLine("Username: "******"Password: "******"Login";
                    message     = MakeRequest(request, temp_msg);
                }
                else
                {
                    Console.WriteLine("Username: "******"Robert";
                    Console.WriteLine("User: {0}", request.username);
                    Console.WriteLine("Password: "******"admin";
                    Console.WriteLine("Pw: {0}", request.pwd);


                    message = MakeRequest(request, "Login");
                }
            }
            else if (request.message_number == "2")
            {
                //prepare for register
                temp_msg = "Register";
                Console.WriteLine("Username: "******"Password: "******"email: ");
                request.email = Console.ReadLine();
                message       = MakeRegisterRequest(request, temp_msg);
            }
            else if (request.message_number == "3")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "StartTheBattle";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "4")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "OptainNewCards";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "5")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "ShowDeck";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "6")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "ShowCardCollection";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "7")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "Trade4Coins";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "8")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "TradeWithPlayer";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "9")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "ChangeTheDeck";
                message  = MakeRequest(request, temp_msg);
            }
            else if (request.message_number == "10")
            {
                //kommt erst, wenn eingeloggt
                temp_msg = "ShowScoreboard";
                message  = MakeRequest(request, temp_msg);
            }
            else
            {
                temp_msg = "Error";
                message  = MakeRequest(request, temp_msg);
            }

            return(message);
        }
Exemplo n.º 5
0
        public void SocketConnection()
        {
            Program.PrtinMenueZero();
            string incomeChoice = "";
            int    counter4Demo = 3;



            while (incomeChoice != "y" && incomeChoice != "n")
            {
                incomeChoice = Console.ReadLine().Trim('\n');
                if (incomeChoice == "0")
                {
                    return;
                }
            }

            string choiceWhenLoggedOut;

            Program.PrintMenueOne();

            //überall sachen adden für demo mode
            if (incomeChoice == "n")
            {
                choiceWhenLoggedOut = MyChoice1(); //inut error handling
            }
            else
            {
                choiceWhenLoggedOut = "1";
            }

            //0 beendet das Programm
            if (choiceWhenLoggedOut == "0")
            {
                return;
            }

            //für die Verbindung von Server und Client
            var request = new RequestContextClient();

            request.port           = 6543;
            request.ip             = "127.0.0.1";
            request.message_number = choiceWhenLoggedOut;

            var client = new TcpClient(request.ip, request.port);
            var stream = client.GetStream();

            bool loggedIn = false;

            try
            {
                loggedIn = NotLoggedIn(loggedIn, request, client, stream, incomeChoice);
                if (loggedIn == false)
                {
                    return;
                }

                LoggedInFunc(request, client, stream, incomeChoice, counter4Demo);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }
            finally
            {
                stream.Close();
                client.Close();
            }

            Console.Read();
        }
Exemplo n.º 6
0
        public static void Trade4CoinsFunc(string response, RequestContextClient request, TcpClient client, NetworkStream stream, string incomeChoice)
        {
            var    msg = new Message();
            string message;
            int    counterForDemo = 0;

            Console.WriteLine(response);

            while (true)
            {
                string cardToTrade;

                //überall sachen adden für demo mode
                if (incomeChoice == "n")
                {
                    cardToTrade = MyChoice3();
                }
                else
                {
                    if (counterForDemo == 0)
                    {
                        cardToTrade = "7";
                        Console.WriteLine("Choosen card = 7");
                    }
                    else
                    {
                        cardToTrade = "0";
                        Console.WriteLine("entered 0 to leave");
                    }
                }

                if (cardToTrade == "0")
                {
                    message = msg.MakeRequest(request, "END");
                    sendData(stream, message);
                    break;
                }

                message = msg.MakeRequest(request, cardToTrade);
                sendData(stream, message);

                response = receiveData(client, stream);
                Console.WriteLine(response);
                Console.WriteLine("y/n ?");
                Console.Write(": ");
                string choice;

                if (incomeChoice == "n")
                {
                    choice = Console.ReadLine();
                }
                else
                {
                    choice = "y";
                    Console.WriteLine("Choice is y");
                }
                //wenn man die karte tauschen will
                if (choice.Trim('\n') == "y")
                {
                    message = msg.MakeRequest(request, "YES");
                    sendData(stream, message);
                    //neue liste empfangen
                    response = receiveData(client, stream);
                }
                else
                {
                    message = msg.MakeRequest(request, "NO");
                    sendData(stream, message);
                    break;
                }

                counterForDemo++;
            }
        }
Exemplo n.º 7
0
        public static void TradeWithPeopleFunc(string response, RequestContextClient request, TcpClient client, NetworkStream stream, string incomeChoice)

        {
            var    msg = new Message();
            string message;
            string answerMessage, cardToTrade;


            Console.WriteLine("0 .. to exit");
            Console.WriteLine("1 .. to add a card");
            Console.WriteLine("2 .. to see other cards");
            Console.WriteLine(": ");

            if (incomeChoice == "n")
            {
                cardToTrade = Console.ReadLine(); //eingabe prüfen
            }
            else
            {
                cardToTrade = "2";
                Console.WriteLine("Choice was 2");
            }

            Console.WriteLine(response);
            message = msg.MakeRequest(request, cardToTrade);
            sendData(stream, message);

            if (cardToTrade == "1")
            {
                do
                {
                    Console.Write("choose a card to trade: ");
                    cardToTrade = MyChoice3();
                    message     = msg.MakeRequest(request, cardToTrade);
                    sendData(stream, message);
                    answerMessage = receiveData(client, stream);
                }while (answerMessage != "OK");

                answerMessage = SpellOrMonsterFunc(answerMessage);
                message       = msg.MakeRequest(request, answerMessage);
                sendData(stream, message);

                answerMessage = DamageValue();

                message = msg.MakeRequest(request, answerMessage);
                sendData(stream, message);
            }
            else if (cardToTrade == "2")
            {
                answerMessage = receiveData(client, stream);
                Console.WriteLine(answerMessage);

                Console.WriteLine("Choose a number from a traiding offer:");
                if (incomeChoice == "n")
                {
                    cardToTrade = Console.ReadLine();
                }
                else
                {
                    cardToTrade = "2";
                    Console.WriteLine("Choice was 2");
                }

                message = msg.MakeRequest(request, cardToTrade);
                sendData(stream, message);
                answerMessage = receiveData(client, stream);
                //Console.WriteLine(answerMessage);

                Console.Write("choose one of your cards to trade: ");
                if (incomeChoice == "n")
                {
                    cardToTrade = Console.ReadLine();
                }
                else
                {
                    cardToTrade = "10";
                    Console.WriteLine("Choice was 10");
                }
                message = msg.MakeRequest(request, cardToTrade);
                sendData(stream, message);

                answerMessage = receiveData(client, stream);
                Console.WriteLine(answerMessage);
            }
        }
Exemplo n.º 8
0
        public static void LoggedInFunc(RequestContextClient request, TcpClient client, NetworkStream stream, string incomeChoice, int counter4Demo)
        {
            while (true) //wenn eingeloggt
            {
                Program.PrintMenueTwo();
                var    msg = new Message();
                string response, message, choiceWhenLoggedIn;

                //nur richtige eingabe zulassen
                if (incomeChoice == "n")
                {
                    choiceWhenLoggedIn = MyChoiceBigMenue();
                }
                else
                {
                    choiceWhenLoggedIn = counter4Demo.ToString(); //wieso so kompliziert hochzählen haha
                    Console.WriteLine("The coice was {0}", choiceWhenLoggedIn);
                }
                //0 beendet das Programm
                if (choiceWhenLoggedIn == "0")
                {
                    return;
                }

                //handelt eig alles, wo man keine zusätzliche user eingabe machen muss
                request.message_number = choiceWhenLoggedIn;               //die auswahl in die mesasge speichern
                message = msg.CreateMessageForSend(request, incomeChoice); //verwaltet wieder die nachricht
                sendData(stream, message);
                response = receiveData(client, stream);

                //alles was zusätzliche user eingabe benötigt
                if (choiceWhenLoggedIn == "7")
                {
                    Trade4CoinsFunc(response, request, client, stream, incomeChoice);
                }

                if (choiceWhenLoggedIn == "8")
                {
                    TradeWithPeopleFunc(response, request, client, stream, incomeChoice);
                }

                if (choiceWhenLoggedIn == "9")
                {
                    EditYourDeckFunc(response, request, client, stream, incomeChoice);
                }

                //einige spezielle antworten des servers abfragen und auch ausgeben
                if (response == "TradeWithPlayer")
                {
                    Console.WriteLine("coming soon");
                }
                if (response == "ZuWenigeCoins")
                {
                    Console.WriteLine("Bro, kauf dir Münzen");
                }
                if (response == "NoCards")
                {
                    Console.WriteLine("Du hast keine Karten");
                }
                Console.WriteLine("");
                Console.Write("Received:\n{0}\n\n", response);

                counter4Demo++; //hochzählen nicht vergessen

                if (counter4Demo == 11)
                {
                    counter4Demo = 0;
                    Console.WriteLine("you have rached the end");
                }
            }
        }