Exemplo n.º 1
0
        public static async Task <int> ExecuteNonQueryAsync(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int val = await cmd.ExecuteNonQueryAsync();

            cmd.Parameters.Clear();
            return(val);
        }
Exemplo n.º 2
0
        public static async Task <long> ExecuteInsertAsync(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                int val = await cmd.ExecuteNonQueryAsync();

                cmd.Parameters.Clear();
                return(cmd.LastInsertedId);
            }
        }
Exemplo n.º 3
0
        private async Task InsertRecords <T>(string SPInitials, PropertyInfo[] propertyInfos, T item) where T : class
        {
            foreach (var pro in propertyInfos)
            {
                var _itemValue = pro.GetValue(item);

                // Convert DateTime to MySql DateTime Format
                if (pro.PropertyType.Name == "DateTime")
                {
                    _itemValue = Convert.ToDateTime(_itemValue).ToString("yyyy-MM-dd");
                    _itemValue = Convert.ToDateTime(_itemValue);
                }
                else if (pro.PropertyType.Name == "Guid")
                {
                    _itemValue = _itemValue.ToString();
                }

                _MyCommand.Parameters.AddWithValue($"@{SPInitials}{pro.Name}", _itemValue);
            }

            await _MyCommand.ExecuteNonQueryAsync();
        }
Exemplo n.º 4
0
        protected override void Process(Entities.User u)
        {
            ushort actionType = GetUShort(0);

            if (actionType >= (ushort)Enums.ItemAction.BuyItem && actionType <= (ushort)Enums.ItemAction.UseItem)
            {
                if (actionType == (ushort)Enums.ItemAction.BuyItem)
                {
                    string itemCode = GetString(1).ToUpper();

                    if (itemCode.Length == 4)
                    {
                        if (Managers.ItemManager.Instance.Items.ContainsKey(itemCode))
                        {
                            ItemData item = Managers.ItemManager.Instance.Items[itemCode];
                            if (item != null)
                            {
                                uint dbId = GetuInt(2);
                                //if (item.dbId == dbId) {
                                byte length = GetByte(4);
                                if (length < 4)
                                {
                                    if (item.Shop.IsBuyable)
                                    {
                                        if (u.Inventory.Items.Count < Objects.Inventory.Inventory.MAX_ITEMS)
                                        {
                                            if (!item.Shop.RequiresPremium || (item.Shop.RequiresPremium && (byte)u.Premium > (byte)Enums.Premium.Free2Play))
                                            {
                                                if (item.Shop.RequiredLevel <= Core.LevelCalculator.GetLevelforExp(u.XP))
                                                {
                                                    int price = item.Shop.Cost[length];
                                                    if (price >= 0)
                                                    {
                                                        int moneyCalc = (int)u.Money - price;
                                                        if (moneyCalc >= 0)
                                                        {
                                                            var invItem = (Objects.Inventory.Item)null;
                                                            try {
                                                                invItem = u.Inventory.Items.Select(n => n.Value).Where(n => n.ItemCode == item.Code).First();
                                                            } catch { invItem = null; }

                                                            uint utcTime    = (uint)DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
                                                            uint itemLength = (uint)(86400 * days[length]);
                                                            u.Money = (uint)moneyCalc;

                                                            if (invItem != null)   // Has item in inventory.

                                                            //check for a possible retail
                                                            {
                                                                if (u.Inventory.Retails.Contains(item.Code.ToUpper()))
                                                                {
                                                                    u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.CannotBeBougth));
                                                                }
                                                                else
                                                                {
                                                                    // Extend & insert into db :)
                                                                    Databases.Game.AsyncQuery(string.Concat("INSERT INTO user_inventory (`id`, `owner`, `code`, `startdate`, `length`, `price`, `expired`, `deleted`) VALUES (NULL, '", u.ID, "', '", item.Code.ToUpper(), "', '", utcTime, "', '", itemLength, "', '", price, "', '0', '0'); UPDATE user_details SET money='", u.Money, "' WHERE id = ", u.ID, ";"));
                                                                    invItem.ExpireDate = invItem.ExpireDate.AddSeconds(itemLength);
                                                                    u.Inventory.Rebuild();
                                                                    u.Send(new Packets.Itemshop(u));
                                                                }
                                                            }
                                                            else     // No item in invetory
                                                            // Insert & fetch id
                                                            {
                                                                uint itemdbId = 0;
                                                                MySql.Data.MySqlClient.MySqlCommand cmd;

                                                                try {
                                                                    cmd = new MySql.Data.MySqlClient.MySqlCommand(string.Concat("INSERT INTO user_inventory (`id`, `owner`, `code`, `startdate`, `length`, `price`, `expired`, `deleted`) VALUES (NULL, '", u.ID, "', '", item.Code.ToUpper(), "', '", utcTime, "', '", itemLength, "', '", price, "', '0', '0');"), Databases.Game.connection);
                                                                    cmd.ExecuteNonQueryAsync();
                                                                    itemdbId = (uint)cmd.LastInsertedId;
                                                                } catch { itemdbId = 0; }

                                                                if (itemdbId > 0)
                                                                {
                                                                    Databases.Game.AsyncQuery(string.Concat("UPDATE user_details SET money='", u.Money, "' WHERE id = ", u.ID, ";"));
                                                                    u.Inventory.Add(new Objects.Inventory.Item(-1, itemdbId, item.Code, (utcTime + itemLength)));
                                                                    u.Inventory.Rebuild();
                                                                    u.Send(new Packets.Itemshop(u));
                                                                }
                                                                else
                                                                {
                                                                    u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.CannotBeBougth));
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.NotEnoughDinar));
                                                        }
                                                    }
                                                    else
                                                    {
                                                        u.Disconnect(); // Item can't be bought for this period. - Cheating?
                                                    }
                                                }
                                                else
                                                {
                                                    u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.LevelRequirement));
                                                }
                                            }
                                            else
                                            {
                                                u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.PremiumOnly));
                                            }
                                        }
                                        else
                                        {
                                            u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.InventoryFull));     // Inventory is full.
                                        }
                                    }
                                    else
                                    {
                                        u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.CannotBeBougth));     // Buying an item that isn't for sale? - Cheating?
                                    }
                                }
                                else
                                {
                                    u.Disconnect();     // Cheating?
                                }
                            }
                            else
                            {
                                u.Disconnect();     // Invalid id for the item - Cheating?
                            }
                        }
                        else
                        {
                            u.Disconnect();     // Server error.
                        }
                    }
                    else
                    {
                        u.Disconnect();     // Code doesn't exist - Cheating?
                    }
                }
                else
                {
                    u.Disconnect();     // Wrong Code - Cheating?
                }
            }
            else if (actionType == (ushort)Enums.ItemAction.UseItem)
            {
                u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.InvalidItem));
            }
            else
            {
                u.Send(new Packets.Itemshop(Packets.Itemshop.ErrorCodes.InvalidItem));
            }

            /*} else {
             *  u.Disconnect(); // Invalid Action type - Cheating?
             * }*/
        }
Exemplo n.º 5
0
        /// <summary>
        /// Updates the record asynchronous.
        ///   If there is a property marked as EntityKey the update will consider that field as Unique Key
        ///  If there is not any property marked as EntityKey the function will throw an exception
        /// </summary>
        /// <param name="record">The record.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">
        /// There is not any property marked as EntityKey
        /// or
        /// A Class marked with attribute NotInsert cannot perform write opperations
        /// </exception>
        public async Task <bool> UpdateRecordAsync(IDataEntity record)
        {
            OnBeforeInsert(null);
            //find if there is an EntityKey property. If there is not we abort the operation
            DataInterface.Attribbutes.EntityKey EntityKey = (DataInterface.Attribbutes.EntityKey)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault();
            if (EntityKey != null)
            {
                throw new Exception("There is not any property marked as EntityKey");
            }


            DataInterface.Attribbutes.NotInsert NotInsertAttribute = (DataInterface.Attribbutes.NotInsert)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.NotInsert) select attr).FirstOrDefault();

            if (NotInsertAttribute != null)
            {
                throw new Exception("A Class marked with attribute NotInsert cannot perform write opperations");
            }


            //first find the table attribute to obtain the maped db table
            DataInterface.Attribbutes.Table TableAttribute = (DataInterface.Attribbutes.Table)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Table) select attr).FirstOrDefault();

            using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(this.Source))
            {
                connection.Open();
                try
                {
                    StringBuilder Query = new StringBuilder();
                    if (TableAttribute != null)
                    {
                        Query.Append($"Update {TableAttribute.Name} set ");
                    }
                    else
                    {
                        Query.Append($"Update  {record.GetType().Name} set");
                    }

                    //get the properties of the entity
                    List <PropertyInfo>         properties  = record.GetType().GetProperties().ToList();
                    Dictionary <string, string> FieldsNames = new Dictionary <string, string>();
                    // create the insert query

                    CreateUpdateQuery(record, Query, properties, FieldsNames);

                    var com = new MySql.Data.MySqlClient.MySqlCommand(Query.ToString(), connection);
                    //Create the sql parameters
                    if (_Encryptor != null)
                    {
                        if (record is IEncryptableClass)
                        {
                            _Encryptor.EncryptProperties((IEncryptableClass)record);
                        }
                    }
                    SetInsetParameters(record, properties, FieldsNames, com);

                    foreach (PropertyInfo property in properties)
                    {
                        DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault();
                        if (KeyProperty != null)
                        {
                            DataInterface.Attribbutes.Field FieldProperty = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault();
                            if (FieldProperty != null)
                            {
                                com.Parameters.AddWithValue($"@{FieldProperty.Name}", property.GetValue(record));
                            }
                            else
                            {
                                com.Parameters.AddWithValue($"@{property.Name}", property.GetValue(record));
                            }
                        }
                    }

                    await com.ExecuteNonQueryAsync();

                    //find the Key property of the entity

                    if (_Encryptor != null)
                    {
                        if (record is IEncryptableClass)
                        {
                            _Encryptor.Decryptproperties((IEncryptableClass)record);
                        }
                    }
                    connection.Close();
                }
                catch (Exception ex)
                {
                    if (connection.State == System.Data.ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    throw;
                }
            }
            return(true);
        }