コード例 #1
0
 static public void CreateTradingDeal(User user, TradeItem item)
 {
     using var conn = new NpgsqlConnection(connString);  //connect to db
     conn.Open();
     try
     {
         using (var cmd = new NpgsqlCommand("INSERT INTO tradings VALUES (@id, @ctt, @type, @md, @uid)", conn)) //inserting into db
         {
             cmd.Parameters.AddWithValue("@id", item.id);                                                       //adding parameters
             cmd.Parameters.AddWithValue("@ctt", item.cardToTrade);
             cmd.Parameters.AddWithValue("@type", item.type);
             cmd.Parameters.AddWithValue("@md", item.minimumDamage);
             cmd.Parameters.AddWithValue("@uid", user.uid);
             cmd.Prepare();
             cmd.ExecuteNonQuery();
         }
     }
     catch (Exception e)
     {
         conn.Close();
         throw new Exception(Output.TradeCreationAlreadyExists);
     }
     conn.Close();
     CardsUsersDatabaseHandler.updateShopStatusTrue(item.cardToTrade);
 }
コード例 #2
0
        static public void Trade(RequestContext request)
        {
            int userstate = UserHandler.AuthUser(request);

            if (userstate == 1 || userstate == 2)
            {
                string tradeId = ExtractIdFromRessource(request.Ressource);
                User   user    = UserHandler.GetUserDataByToken(request);
                if (!TradingDatabaseHandler.CheckDealToUser(tradeId, user))
                {
                    string    offeredCardId = request.Message.Trim('"');
                    DummyCard dummyCard     = CardsUsersDatabaseHandler.GetDummyCard(user, offeredCardId);
                    if (dummyCard != null)
                    {
                        ICard card = DummyCardConverter.Convert(dummyCard);
                        if (TradingDatabaseHandler.Trade(tradeId, user, offeredCardId, card.type, card.damage))
                        {
                            TradingDatabaseHandler.DeleteTradingDeal(tradeId);
                            Output.WriteConsole(Output.TradeSuccess);
                            return;
                        }
                        Output.WriteConsole(Output.TradeConditionsNotMet);
                        return;
                    }
                    Output.WriteConsole(Output.TradeInvalidCard);
                    return;
                }
                Output.WriteConsole(Output.TradeSelfTrade);
                return;
            }
            Output.WriteConsole(Output.AuthError);
        }
コード例 #3
0
        static public string ShowStack(RequestContext request)
        {
            int userstate = UserHandler.AuthUser(request);

            if (userstate == 1 || userstate == 2)
            {
                User user = UserHandler.GetUserDataByToken(request);

                return(CardsUsersDatabaseHandler.GetStackByUser(user));
            }
            return(Output.AuthError);
        }
コード例 #4
0
        public static BattleDeck GenerateBattleDeck(User user)
        {
            DummyCard[] dummyDeck = CardsUsersDatabaseHandler.GetDummyDeck(user);
            BattleDeck  deck      = new BattleDeck();

            foreach (DummyCard card in dummyDeck)
            {
                deck.AddCard(DummyCardConverter.Convert(card));
            }

            return(deck);
        }
コード例 #5
0
        static public void ConfigureDeck(RequestContext request)
        {
            int userstate = UserHandler.AuthUser(request);

            if (userstate == 1 || userstate == 2)
            {
                string[] strings = PrepareStrings(request.Message);
                User     user    = UserHandler.GetUserDataByToken(request);

                CardsUsersDatabaseHandler.UpdateDeck(user, strings);
                return;
            }
            Output.WriteConsole(Output.AuthError);
        }
コード例 #6
0
        public static void AcquirePackage(User user)
        {
            DummyCard[] cards = new DummyCard[5];
            for (int i = 0; i < 5; i++)
            {
                cards[i] = new DummyCard();
            }
            int counter = 0;
            int minId   = 0;

            using var conn = new NpgsqlConnection(connString);
            conn.Open();
            try
            {
                using (var cmd = new NpgsqlCommand("Select min(packid) FROM cards_packs WHERE bought = false", conn))
                    using (var reader = cmd.ExecuteReader())
                        while (reader.Read())
                        {
                            minId = (int)reader[0];
                        }
            }
            catch (Exception e)
            {
                conn.Close();
                throw new Exception(Output.PackageTransactionError);
            }

            string selectString = String.Format("SELECT id, cardname, damage FROM cards_packs WHERE packid = {0}", minId);

            using (var cmd = new NpgsqlCommand(selectString, conn))
                using (var reader = cmd.ExecuteReader())
                    while (reader.Read())
                    {
                        cards[counter].id     = reader[0].ToString();
                        cards[counter].name   = reader[1].ToString();
                        cards[counter].damage = (float)((double)reader[2]);
                        counter++;
                    }

            using (var cmd = new NpgsqlCommand("UPDATE cards_packs SET bought = true WHERE packid = @id", conn))
            {      //adding parameters
                cmd.Parameters.AddWithValue("@id", minId);
                cmd.Prepare();
                cmd.ExecuteNonQuery();
            }
            conn.Close();

            CardsUsersDatabaseHandler.InsertPackage(cards, user);
        }
コード例 #7
0
        static public void DeleteTradingDeal(string tradeId)
        {
            CardsUsersDatabaseHandler.updateShopStatusFalse(tradeId);
            using var conn = new NpgsqlConnection(connString);
            conn.Open();

            string deleteString = String.Format("DELETE FROM tradings WHERE id = '{0}'", tradeId);

            using (var cmd = new NpgsqlCommand(deleteString, conn))
            {
                cmd.Prepare();
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
コード例 #8
0
        static public bool Trade(string tradeId, User user, string offeredCardId, string cType, float cDamage)
        {
            string cttId  = "";
            string type   = "";
            float  minDam = 0;
            int    uId    = 0;

            using var conn = new NpgsqlConnection(connString);
            conn.Open();

            string selectString = String.Format("SELECT * FROM tradings WHERE id = '{0}'", tradeId);

            using (var cmd = new NpgsqlCommand(selectString, conn))
                using (var reader = cmd.ExecuteReader())
                    if (reader.Read())
                    {
                        cttId  = reader[1].ToString();
                        type   = reader[2].ToString();
                        minDam = (float)((double)reader[3]);
                        uId    = (int)reader[4];
                    }
                    else
                    {
                        conn.Close();
                        return(false);
                    }

            if (!cType.Equals(type, StringComparison.CurrentCultureIgnoreCase) || minDam > cDamage)
            {
                conn.Close();
                return(false);
            }
            conn.Close();

            CardsUsersDatabaseHandler.swapCards(user.uid, offeredCardId, uId, cttId);
            return(true);
        }
コード例 #9
0
 static public void SellCard(string cardId, User user)
 {
     CardsUsersDatabaseHandler.SellCard(cardId, user);
 }
コード例 #10
0
 static public bool CheckValidCardToUser(string cardId, User user)
 {
     return(CardsUsersDatabaseHandler.CheckValidCard(cardId, user));
 }