Example #1
0
        public static ServerReply GET(RequestKontext req)
        {
            if (req.Options.Contains("/") == false)    //Get - localhost:123123/messages/1
            {
                return(new ServerReply(req.Protocol, "404 Not Found", "", "text"));
            }
            string[] frag = req.Options.Split('/'); //---/messages


            if (frag[1] == "cards" && frag.Length == 2)
            {
                string username = req.Authorization;
                string result   = DBManagmentShowCards.Show_acquired_cards(username);
                if (result == "Error: User is not logged in / Invalid Token!")
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", "Error: Not logged in or invalid token", "text"));
                }
                else if (result.Contains("Exception caught: "))
                {
                    return(BadRequest(req));
                }
                else
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
            }


            else if (frag[1] == "deck" && frag.Length == 2)
            {
                string result = DBManagmentDeck.Show_Deck(req.Authorization);
                if (result == "No deck found!")
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
                else if (result == "Error: User doesn't have a session / invalid Token!")
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", result, "text"));
                }
                else
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
            }


            else if (frag[1] == "users" && frag.Length == 3 && frag[2] != "")
            {
                if (frag[2] == req.Authorization)
                {
                    string result = DBManagmentUserData.Show_Players_Data(req.Authorization);
                    if (result == "Error: User doesn't have a session / invalid Token!")
                    {
                        return(new ServerReply(req.Protocol, "401 Unauthorized", result, "text"));
                    }
                    else
                    {
                        return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                    }
                }
                else
                {
                    return(BadRequest(req));
                }
            }

            else if (frag[1] == "stats" && frag.Length == 2)
            {
                string result = DBManagmentStats.Show_stats(req.Authorization);
                if (result == "Error: User is not logged in / Invalid Token!")
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", result, "text"));
                }
                else
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
            }

            else if (frag[1] == "score" && frag.Length == 2)
            {
                string result = DBManagment.Show_scoreboard(req.Authorization);
                if (result == "Error: User is not logged in / Invalid Token!")
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", result, "text"));
                }
                else
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
            }

            else if (frag[1] == "tradings" && frag.Length == 2)
            {
                string result = DBManagmentTrade.Show_tradings(req.Authorization);
                if (result == "User doesn't have a session / invalid Token!")
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", result, "text"));
                }
                else
                {
                    return(new ServerReply(req.Protocol, "200 OK", result, "text"));
                }
            }

            else
            {
                return(BadRequest(req));
            }
        }
Example #2
0
        public static ServerReply PUT(RequestKontext req)
        {
            if (req.Options.Contains("/") == false)
            {
                return(BadRequest(req));
            }
            string[] frag = req.Options.Split('/');

            if (frag[1] == "deck" && frag.Length == 2)
            {
                List <string> deck_cards = JsonConvert.DeserializeObject <List <string> >(req.Body);
                foreach (string s in deck_cards)
                {
                    Console.WriteLine(s);
                }
                int result = DBManagmentDeck.Configure_Deck(req.Authorization, deck_cards);
                if (result == 0)
                {
                    return(new ServerReply(req.Protocol, "200 OK", "Deck configured!", "text"));
                }
                else if (result == 2)
                {
                    return(new ServerReply(req.Protocol, "416 Range Not Satisfiable", "Error: Must be 4 Cards to create or edit deck!", "text"));
                }
                else if (result == 1)
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", "Error: Not logged in or invalid token", "text"));
                }
                else if (result == 3)
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", "Error: One or more Cards are not obtained by the User!", "text"));
                }
                else
                {
                    return(BadRequest(req));
                }
            }
            else if (frag[1] == "users" && frag.Length == 3 && frag[2] != "")
            {
                if (frag[2] == req.Authorization) //if username in token is same as in link
                {
                    User new_data = JsonConvert.DeserializeObject <User>(req.Body);
                    if (DBManagmentUserData.Edit_data(req.Authorization, new_data.Name, new_data.Bio, new_data.Email))
                    {
                        return(new ServerReply(req.Protocol, "200 OK", "Data configured!", "text"));
                    }
                    else
                    {
                        return(new ServerReply(req.Protocol, "401 Unauthorized", "Error: Not logged in or invalid token", "text"));
                    }
                }
                else
                {
                    return(new ServerReply(req.Protocol, "401 Unauthorized", "Error: You can't change the data of someone else!", "text"));
                }
            }
            else
            {
                return(BadRequest(req));
            }
        }