Esempio n. 1
0
        //Put a card on the marketplace ------------------------------------------------
        public void PutOutTradingOffer(List <string> login)
        {
            bool isOnline = false;

            for (int i = 0; i < login.Count; i++)
            {
                if (login[i] == authorization)
                {
                    isOnline = true;
                }
            }
            if (!isOnline)
            {
                string data   = "\nuser is not logged in \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

            dynamic jasondata     = JObject.Parse(body);
            string  cardId        = jasondata.CardToTrade;
            string  tradeType     = jasondata.Type;
            double  minimumDamage = jasondata.MinimumDamage;
            string  tradeId       = jasondata.Id;


            int    lenght   = authorization.IndexOf("-mtcgToken");
            string username = authorization.Substring(0, lenght);

            //ceck if card belongs to the player
            int cardBelonsToUser = Database.selectCardBelongsToPlayer(username, cardId);

            if (cardBelonsToUser == -1)
            {
                string data   = "\nDatabase Error \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (cardBelonsToUser == 0)
            {
                string data   = "\nYou dont own this card\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }



            //check if card is in the deck
            int inDeck = Database.selectCardInDeck(cardId);

            if (inDeck == -1)
            {
                string data   = "\nDatabase Error \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (inDeck == 1)
            {
                string data   = "\nCard can not be put on the marketplace as long as it is in your deck \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            //check if tradedeal is already on market
            int onMarket = Database.selectTradeBelongsToPlayer(username, tradeId);

            if (onMarket == -1 || onMarket > 1)
            {
                string data   = "\nDatabase Error while trying to delete trade deal\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (onMarket == 1)
            {
                string data   = "\nThis trade offer already exists \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

            //put card on the database table trading
            bool done = Database.insertTrading(username, tradeId, cardId, minimumDamage, tradeType);

            if (!done)
            {
                string data   = "\nDatabase Error \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            else
            {
                string data   = "\nCard was put on the Marketplace \n";
                string status = "200 Success";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
        }