Beispiel #1
0
        //make trade deal---------------------------------------------
        public void MakeTradeDeal(List <string> login, string tradeId)
        {
            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;
            }

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

            dynamic jasondata = JProperty.Parse(body);
            string  cardId    = jasondata;

            //check if trade deal exists
            int tradeExists = Database.selectTradeExists(tradeId);

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

            //check if trade deal belongs to the user
            int tradeDealbelongsToPlayer = Database.selectTradeBelongsToPlayer(username, tradeId);

            if (tradeDealbelongsToPlayer == -1 || tradeDealbelongsToPlayer > 1)
            {
                string data   = "\nDatabase Error while trying to trade\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (tradeDealbelongsToPlayer == 1)
            {
                string data   = "\n You can not trade with yourselfe \n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

            //check if the trade card belongs to user
            int cardBelongsToPlayer = Database.selectCardBelongsToPlayer(username, cardId);

            if (cardBelongsToPlayer == -1 || cardBelongsToPlayer > 1)
            {
                string data   = "\nDatabase Error while trying to trade\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (cardBelongsToPlayer == 0)
            {
                string data   = "\nYou can not trade a card that does not belong to you\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

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

            if (cardInDeck == -1 || cardInDeck > 1)
            {
                string data   = "\nDatabase Error while trying to trade\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (cardInDeck == 1)
            {
                string data   = "\nYou can not trade a card that is in your deck\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

            //check if the trade card meets the conditions
            double carddamage = Database.selectCardDamage(cardId);
            string cardtype   = Database.selectCardType(cardId);

            int meetsCondition = Database.selectMeetsTradeCondition(tradeId, carddamage, cardtype);

            if (meetsCondition == -1 || meetsCondition > 1)
            {
                string data   = "\nDatabase Error while trying to trade\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            if (meetsCondition == 0)
            {
                string data   = "\nYour offer does not meet the trading conditions\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }

            //trade
            string OwnerOfTrade = Database.selectUserInTrade(tradeId);
            string TradeCard    = Database.selectCardInTrade(tradeId);

            bool firstrade   = Database.updateCardBelongsToPlayer(OwnerOfTrade, cardId);
            bool secondtrade = Database.updateCardBelongsToPlayer(username, TradeCard);
            bool tradeDelete = Database.deleteTrade(tradeId);

            if (!firstrade || !secondtrade)
            {
                string data   = "\nDatabase Error while trying to trade\n";
                string status = "404 Not found";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
            else
            {
                string data   = "\nTrade went through\n";
                string status = "200 Success";
                string mime   = "text/plain";
                ServerResponse(status, mime, data);
                return;
            }
        }