Beispiel #1
0
        /*
         * Executes the HTTP request
         *
         * @params:
         *      - client: Socket to which the User is connected
         *      - usernameToAuth: Username of the connected User
         */
        public void DoRequest(ref Socket client, string usernameToAuth)
        {
            string resMassage;
            string username;

            string resource = RC.Resource;
            string content  = RC.Body;

            if (resource.Equals("/pick"))
            {
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    if (username.Equals(usernameToAuth))
                    {
                        User user;
                        BC.ma.Users.TryGetValue(username, out user);

                        if (user.name.Equals(user1.name))
                        {
                            user1Pick = Int32.Parse(content);
                            Console.WriteLine("User 1 picks: " + user1Pick);
                        }
                        else if (user.name.Equals(user2.name))
                        {
                            user2Pick = Int32.Parse(content);
                            Console.WriteLine("User 2 picks: " + user2Pick);
                        }

                        BC.SendBody("Picks done!\n", ref client);

                        if (user1Pick != -1 && user2Pick != -1)
                        {
                            Console.WriteLine("Cards Chosen!");
                            B.ChooseCard(user1Pick, user2Pick);
                            BC.SendBody(B.roundEndText + "\n", ref client1);
                            BC.SendBody(B.roundEndText + "\n", ref client2);
                        }
                    }
                    else
                    {
                        BC.SendResponse(401, "text/plain", "Unauthorized!", ref client);
                    }
                }
                else
                {
                    BC.SendResponse(401, "text/plain", resMassage, ref client);
                }
            }
        }
Beispiel #2
0
        /*
         * Handler for GET
         *
         * @params:
         *      - resource: Requested resource
         */
        private void Get(string resource)
        {
            string resMassage = "";
            string username;

            if (resource.Equals("/users"))
            {
                List <string> keyList = new List <string>(TcpL.ma.Users.Keys);
                foreach (var l in keyList)
                {
                    resMassage += "\nKey: " + l;
                }
                TcpL.SendResponse(200, "text/plain", "\n" + resMassage);
            }
            else if (resource.Contains("/users/"))
            {
                string[] splited = resource.Split('/');
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    User user;
                    TcpL.ma.Users.TryGetValue(splited[2], out user);
                    TcpL.SendResponse(200, "text/plain", "\n" + user.ShowStats());
                }
                else
                {
                    TcpL.SendResponse(401, "text/plain", resMassage);
                }
            }
            else if (resource.Equals("/cards"))
            {
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    User user;
                    TcpL.ma.Users.TryGetValue(username, out user);
                    TcpL.SendResponse(200, "text/plain", "\n" + user.StackToString());
                }
                else
                {
                    TcpL.SendResponse(401, "text/plain", resMassage);
                }
            }
            else if (resource.Equals("/deck"))
            {
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    User user;
                    TcpL.ma.Users.TryGetValue(username, out user);
                    TcpL.SendResponse(200, "text/plain", "\n" + user.DeckToString());
                }
                else
                {
                    TcpL.SendResponse(401, "text/plain", resMassage);
                }
            }
            else if (resource.Equals("/score"))
            {
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    TcpL.SendResponse(200, "text/plain", "\n" + buildScoreboardString());
                }
                else
                {
                    TcpL.SendResponse(401, "text/plain", resMassage);
                }
            }
            else if (resource.Equals("/stats"))
            {
                if (UH.AuthorizeUser(this, out resMassage, out username) == 0)
                {
                    User user;
                    TcpL.ma.Users.TryGetValue(username, out user);
                    TcpL.SendResponse(200, "text/plain", "\n" + user.ShowStats());
                }
                else
                {
                    TcpL.SendResponse(401, "text/plain", resMassage);
                }
            }
            else
            {
                TcpL.SendResponse(400, "text/plain", "Bad Request!");
            };
        }