Ejemplo n.º 1
0
        public IResponse CreateTrade()
        {
            string token;

            if (!(Context.Information.TryGetValue("Authorization", out token)))
            {
                return(new TextResponse(StatusCodesEnum.Unauthorized, "Unauthorized command!"));
            }

            string username = Db.GetUsernameFromToken(token);

            if (username == null)
            {
                return(new TextResponse(StatusCodesEnum.Unauthorized, "Unauthorized command!"));
            }

            TradeCards trade = JsonConvert.DeserializeObject <TradeCards>(Context.Payload);

            string erg = Db.CreateTrade(username, trade);

            if (erg == null)
            {
                return(new TextResponse(StatusCodesEnum.InternalServerError, "Internal database error!"));
            }
            if (erg.Equals("Card was not found in the users stack!"))
            {
                return(new TextResponse(StatusCodesEnum.NotFound, erg));
            }
            if (erg.Equals("Card is currently in a deck!"))
            {
                return(new TextResponse(StatusCodesEnum.Forbidden, erg));
            }
            return(new TextResponse(StatusCodesEnum.Created, erg));
        }
Ejemplo n.º 2
0
        public List <TradeCards> ShowAvailableTrades()
        {
            using var conn = new NpgsqlConnection(ConnectionString);
            conn.Open();

            var cmd = new NpgsqlCommand("Select id, username, card_id, typ, element, damage from trade", conn);

            cmd.Prepare();


            try
            {
                // ReSharper disable once PossibleNullReferenceException
                var reader = cmd.ExecuteReader();

                List <TradeCards> trades = new List <TradeCards>();

                while (reader.Read())
                {
                    var trade = new TradeCards();
                    trade.Id                 = (string)reader[0];
                    trade.Username           = (string)reader[1];
                    trade.CardId             = (string)reader[2];
                    trade.RequirementType    = (string)reader[3];
                    trade.RequirementElement = (string)reader[4];
                    trade.RequirementDamage  = (double)((decimal)reader[5]);

                    trades.Add(trade);
                }

                return(trades);
            }
            catch (PostgresException)
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public int ExecuteTrade(string username, string tradeId, string cardId)
        {
            using var conn = new NpgsqlConnection(ConnectionString);
            conn.Open();

            var tradeCardJson = new JsonCard();
            var tradeRequest  = new TradeCards();

            //Check if trade exists
            var cmd = new NpgsqlCommand("Select * from trade where id = @id", conn);

            cmd.Parameters.AddWithValue("id", tradeId);
            cmd.Prepare();

            //Check if user owns trade
            var cmd2 = new NpgsqlCommand("Select * from trade where username = @username and id = @id", conn);

            cmd2.Parameters.AddWithValue("username", username);
            cmd2.Parameters.AddWithValue("id", tradeId);
            cmd2.Prepare();

            //Check if user owns card and get card data
            var cmd3 = new NpgsqlCommand("Select id, name, damage from player_cards inner join cards on player_cards.card_id = cards.id where username = @username and card_id = @card_id", conn);

            cmd3.Parameters.AddWithValue("username", username);
            cmd3.Parameters.AddWithValue("card_id", cardId);
            cmd3.Prepare();

            //Get trade data
            var cmd4 = new NpgsqlCommand("Select id, username, card_id, typ, element, damage from trade where id = @id", conn);

            cmd4.Parameters.AddWithValue("id", tradeId);
            cmd4.Prepare();


            //Check if card to be traded meets requirements

            //Delete trade request
            var cmd5 = new NpgsqlCommand("DELETE FROM trade where card_id = @card_id", conn);


            //Update player_cards information (needs card_id from tradeRequest)
            var cmd6 = new NpgsqlCommand("UPDATE player_cards SET username = @username where card_id = @card_id", conn);

            cmd6.Parameters.AddWithValue("username", username);


            //Update second player_cards information (needs username from tradeRequest)
            var cmd7 = new NpgsqlCommand("UPDATE player_cards SET username = @username where card_id = @card_id", conn);

            cmd7.Parameters.AddWithValue("card_id", cardId);

            try
            {
                //check if Trade request exists
                var reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return(-2);
                }

                reader.Close();

                //Check if user owns trade
                var reader2 = cmd2.ExecuteReader();
                if (reader2.HasRows)
                {
                    return(-3);
                }

                reader2.Close();

                //Check if user owns card and get card data
                var reader3 = cmd3.ExecuteReader();
                if (!reader3.HasRows)
                {
                    return(-4);
                }
                while (reader3.Read())
                {
                    tradeCardJson.Id     = (string)reader3[0];
                    tradeCardJson.Name   = (string)reader3[1];
                    tradeCardJson.Damage = (double)((decimal)reader3[2]);
                }

                var tradeCard = tradeCardJson.ConvertToCard();

                reader3.Close();

                var reader4 = cmd4.ExecuteReader();
                while (reader4.Read())
                {
                    //id, username, card_id, typ, element, damage
                    tradeRequest.Id                 = (string)reader4[0];
                    tradeRequest.Username           = (string)reader4[1];
                    tradeRequest.CardId             = (string)reader4[2];
                    tradeRequest.RequirementType    = (string)reader4[3];
                    tradeRequest.RequirementElement = (string)reader4[4];
                    tradeRequest.RequirementDamage  = (double)((decimal)reader4[5]);
                }

                reader4.Close();

                //Check if card to be traded meets requirements

                if (tradeCard.Damage < tradeRequest.RequirementDamage)
                {
                    return(-10);
                }

                if (!tradeRequest.RequirementType.Equals(""))
                {
                    if (tradeRequest.RequirementType.ToLower().Equals("monster") && tradeCard is SpellCard)
                    {
                        return(-11);
                    }
                    if (tradeRequest.RequirementType.ToLower().Equals("spell") && tradeCard.GetType().IsSubclassOf(typeof(MonsterCard)))
                    {
                        return(-11);
                    }
                }
                if (!tradeRequest.RequirementElement.Equals(""))
                {
                    if (tradeRequest.RequirementElement.ToLower().Equals("fire") && !(tradeCard.Element is Fire))
                    {
                        return(-12);
                    }
                    if (tradeRequest.RequirementElement.ToLower().Equals("water") && !(tradeCard.Element is Water))
                    {
                        return(-12);
                    }
                    if ((tradeRequest.RequirementElement.ToLower().Equals("normal") || tradeRequest.RequirementElement.ToLower().Equals("regular")) && !(tradeCard.Element is Normal))
                    {
                        return(-12);
                    }
                }


                cmd5.Parameters.AddWithValue("card_id", tradeRequest.CardId);
                cmd5.Prepare();

                if (cmd5.ExecuteNonQuery() == -1)
                {
                    return(-13);
                }

                cmd6.Parameters.AddWithValue("card_id", tradeRequest.CardId);
                cmd6.Prepare();

                if (cmd6.ExecuteNonQuery() == -1)
                {
                    return(-14);
                }

                cmd7.Parameters.AddWithValue("username", tradeRequest.Username);
                cmd7.Prepare();

                if (cmd7.ExecuteNonQuery() == -1)
                {
                    return(-14);
                }

                return(1);
            }
            catch (PostgresException)
            {
                return(-1);
            }
        }
Ejemplo n.º 4
0
        public string CreateTrade(string username, TradeCards trade)
        {
            using var conn = new NpgsqlConnection(ConnectionString);
            conn.Open();

            //Check if card is owned
            var cmd = new NpgsqlCommand("Select * from player_cards where username = @username and card_id = @card_id", conn);

            cmd.Parameters.AddWithValue("username", username);
            cmd.Parameters.AddWithValue("card_id", trade.CardId);
            cmd.Prepare();

            //Check if card is in deck
            var cmd2 = new NpgsqlCommand("Select * from player_deck where username = @username and card_id = @card_id", conn);

            cmd2.Parameters.AddWithValue("username", username);
            cmd2.Parameters.AddWithValue("card_id", trade.CardId);
            cmd2.Prepare();

            //Create Trade
            var cmd3 = new NpgsqlCommand("INSERT INTO trade (id, username, card_id, typ, element, damage) VALUES (@id, @username, @card_id, @typ, @element, @damage)", conn);

            cmd3.Parameters.AddWithValue("id", trade.Id);
            cmd3.Parameters.AddWithValue("username", username);
            cmd3.Parameters.AddWithValue("card_id", trade.CardId);
            if (trade.RequirementType != null)
            {
                cmd3.Parameters.AddWithValue("typ", trade.RequirementType);
            }
            else
            {
                cmd3.Parameters.AddWithValue("typ", "");
            }
            if (trade.RequirementElement != null)
            {
                cmd3.Parameters.AddWithValue("element", trade.RequirementElement);
            }
            else
            {
                cmd3.Parameters.AddWithValue("element", "");
            }
            cmd3.Parameters.AddWithValue("damage", trade.RequirementDamage);
            cmd3.Prepare();

            try
            {
                var reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return("Card was not found in the users stack!");
                }

                reader.Close();

                var reader2 = cmd2.ExecuteReader();
                if (reader2.HasRows)
                {
                    return("Card is currently in a deck!");
                }

                reader2.Close();

                cmd3.ExecuteNonQuery();
                return("Successfully created trade request!");
            }
            catch (PostgresException)
            {
                return("Internal database error!");
            }
        }