コード例 #1
0
        public Dictionary <ushort, ShopObject> GetAllItems(ItemType type)
        {
            MySqlCommand    command = null;
            MySqlDataReader reader  = null;
            Dictionary <ushort, ShopObject> itemList = new Dictionary <ushort, ShopObject>();
            ShopObject item = null;

            try
            {
                if (!CreateConnection(ref Connection))
                {
                    return(itemList);
                }
                command = Connection.CreateCommand();
                if (type == ItemType.Item)
                {
                    command.CommandText = new QueryBuilder(QueryBuilderType.SELECT).Column("ItemID").Column("BuyCost").Column("SellMultiplier").Column("MinBuyPrice").Column("ChangeRate").Column("MaxBuyPrice").Column("RestrictBuySell").Table(TableItems).Build();
                }
                else
                {
                    command.CommandText = new QueryBuilder(QueryBuilderType.SELECT).Column("ItemID").Column("BuyCost").Column("SellMultiplier").Column("RestrictBuySell").Table(TableVehicles).Build();
                }
                reader = command.ExecuteReader();
                if (!reader.HasRows)
                {
                    return(itemList);
                }
                while (reader.Read())
                {
                    if (type == ItemType.Item)
                    {
                        item = ShopObjectBuild(ItemType.Item, reader);
                        itemList.Add(item.ItemID, item);
                    }
                    else
                    {
                        item = ShopObjectBuild(ItemType.Vehicle, reader);
                        itemList.Add(item.ItemID, item);
                    }
                }
                reader.Dispose();
            }
            catch (MySqlException ex)
            {
                HandleException(ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                Connection.Close();
            }
            return(itemList);
        }
コード例 #2
0
 public bool AddItem(ItemType type, ShopObject shopObject)
 {
     if (type == ItemType.Item)
     {
         ShopItem item = shopObject as ShopItem;
         if (Items.ContainsKey(item.ItemID))
         {
             Items[item.ItemID] = item;
             return(true);
         }
         else
         {
             Items.Add(item.ItemID, item);
             return(true);
         }
     }
     else
     {
         ShopVehicle vehicle = shopObject as ShopVehicle;
         if (Vehicles.ContainsKey(vehicle.ItemID))
         {
             Vehicles[vehicle.ItemID] = vehicle;
             return(true);
         }
         else
         {
             Vehicles.Add(vehicle.ItemID, vehicle);
             return(true);
         }
     }
 }
コード例 #3
0
        public void Execute(IRocketPlayer caller, string[] command)
        {
            ItemType type = ItemType.Item;

            if (command.Length >= 1)
            {
                type = command[0].ToLower() == "v" ? ItemType.Vehicle : ItemType.Item;
            }

            if (command.Length == (type == ItemType.Item ? 0 : 1) || command.Length > (type == ItemType.Item ? 1 : 2))
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("cost_help3"));
                return;
            }

            if (!DShop.Instance.Database.IsLoaded)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("db_load_error"));
                return;
            }

            if (!DShop.GetItemID(caller, command, type, 0, out ushort itemID))
            {
                return;
            }
            if (itemID.AssetFromID(type) == null)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                return;
            }
            ShopObject shopObject = DShop.Instance.Database.GetItem(type, itemID);

            if (shopObject.ItemID != itemID)
            {
                Asset asset = itemID.AssetFromID(type);
                if (type == ItemType.Item)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((ItemAsset)asset).itemName != null) ? ((ItemAsset)asset).itemName : string.Empty, itemID));
                }
                else
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((VehicleAsset)asset).vehicleName != null) ? ((VehicleAsset)asset).vehicleName : string.Empty, itemID));
                }
                return;
            }

            UnturnedChat.Say(caller, DShop.Instance.Translate(type == ItemType.Item ? "costs_item2" : "costs_vehicle2", shopObject.ItemName, shopObject.ItemID, Math.Round(shopObject.BuyCost, 2), Uconomy.Instance.Configuration.Instance.MoneyName,
                                                              Math.Round(decimal.Multiply(shopObject.BuyCost, shopObject.SellMultiplier), 2), Uconomy.Instance.Configuration.Instance.MoneyName, Enum.GetName(typeof(RestrictBuySell), shopObject.RestrictBuySell)));
        }
コード例 #4
0
        public ShopObject GetItem(ItemType type, ushort itemID)
        {
            ShopObject      shopObject = new ShopObject();
            MySqlDataReader reader     = null;
            MySqlCommand    command    = null;

            try
            {
                if (!CreateConnection(ref Connection))
                {
                    return(shopObject);
                }
                command = Connection.CreateCommand();
                if (type == ItemType.Item)
                {
                    command.CommandText = new QueryBuilder(QueryBuilderType.SELECT).Column("ItemID").Column("BuyCost").Column("SellMultiplier").Column("MinBuyPrice").Column("ChangeRate").Column("MaxBuyPrice").Column("RestrictBuySell").Where("ItemID", itemID).Table(TableItems).Build();
                }
                else
                {
                    command.CommandText = new QueryBuilder(QueryBuilderType.SELECT).Column("itemID").Column("BuyCost").Column("SellMultiplier").Column("RestrictBuySell").Table(TableVehicles).Where("ItemID", itemID).Build();
                }
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    shopObject = ShopObjectBuild(type, reader);
                }
            }
            catch (MySqlException ex)
            {
                HandleException(ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (command != null)
                {
                    command.Dispose();
                }
                Connection.Close();
            }
            // Returns either ShopItem or ShopVehicle based type if id is found(with conversion), ShopObject, if it's not found.
            return(shopObject);
        }
コード例 #5
0
        public bool AddItem(ItemType type, ShopObject shopObject)
        {
            MySqlCommand command = null;
            bool         result  = false;

            try
            {
                if (!CreateConnection(ref Connection))
                {
                    return(result);
                }
                command = Connection.CreateCommand();
                command.Parameters.AddWithValue("@itemName", shopObject.ItemName);

                if (type == ItemType.Item)
                {
                    ShopItem item = shopObject as ShopItem;
                    command.CommandText = new QueryBuilder(QueryBuilderType.INSERT).Table(TableItems).Column("ItemID", item.ItemID).Column("BuyCost", item.BuyCost).Column("SellMultiplier", item.SellMultiplier).Column("MinBuyPrice", item.MinBuyPrice).
                                          Column("ChangeRate", item.Change).Column("MaxBuyPrice", item.MaxBuyPrice).Column("RestrictBuySell", (byte)item.RestrictBuySell).Column("ItemName", "@itemName").DuplicateInsertUpdate().Build();
                }
                else
                {
                    ShopVehicle vehicle = shopObject as ShopVehicle;
                    command.CommandText = new QueryBuilder(QueryBuilderType.INSERT).Table(TableVehicles).Column("ItemID", vehicle.ItemID).Column("BuyCost", vehicle.BuyCost).Column("SellMultiplier", vehicle.SellMultiplier).
                                          Column("RestrictBuySell", (byte)vehicle.RestrictBuySell).Column("ItemName", "@itemName").DuplicateInsertUpdate().Build();
                }
                command.ExecuteNonQuery();
                result = true;
            }
            catch (MySqlException ex)
            {
                HandleException(ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                Connection.Close();
            }
            return(result);
        }
コード例 #6
0
        public ShopObject GetItem(ItemType type, ushort itemID)
        {
            ShopObject shopObject = new ShopObject();

            if (type == ItemType.Item)
            {
                if (Items.ContainsKey(itemID))
                {
                    shopObject = Items[itemID];
                }
            }
            else
            {
                if (Vehicles.ContainsKey(itemID))
                {
                    shopObject = Vehicles[itemID];
                }
            }
            // Return ShopObject if an id isn't found, Returns the right type of class otherwise(after conversion).
            return(shopObject);
        }
コード例 #7
0
ファイル: ShopItem.cs プロジェクト: cartman-2000/DynShop
        private void ProccessAttatchment(ushort itemID, byte amount, byte health, ref Dictionary <ushort, ShopObject> attatchments, ref decimal totalAttatchmentCost, ref decimal totalCost, UnturnedPlayer player)
        {
            ShopObject sObject = null;
            Asset      iAsset  = Assets.find(EAssetType.ITEM, itemID);
            Item       item    = null;

            if (iAsset != null)
            {
                if (attatchments.ContainsKey(itemID))
                {
                    sObject = attatchments[itemID];
                }
                else
                {
                    sObject = DShop.Instance.Database.GetItem(ItemType.Item, itemID);
                    attatchments.Add(itemID, sObject);
                }
                if (sObject.ItemID == itemID)
                {
                    item = new Item(itemID, amount, health);
                    ShopItem tmp = sObject as ShopItem;
                    totalAttatchmentCost = decimal.Add(totalAttatchmentCost, tmp.CalcSellCost(iAsset, item));
                    totalCost            = decimal.Add(totalCost, tmp.CalcSellCost(iAsset, item));
                    if (decimal.Subtract(sObject.BuyCost, tmp.Change) > tmp.MinBuyPrice && !DShop.Instance.Configuration.Instance.RunInStaticPrices)
                    {
                        sObject.BuyCost = decimal.Subtract(sObject.BuyCost, tmp.Change);
                    }
                }
                else
                {
                    // give the attachment to the player as it's not in the shop db.
                    item        = new Item(itemID, EItemOrigin.CRAFT, health);
                    item.amount = amount;
                    player.Inventory.forceAddItem(item, true);
                }
            }
        }
コード例 #8
0
ファイル: CommandSell.cs プロジェクト: cartman-2000/DynShop
        public void Execute(IRocketPlayer caller, string[] command)
        {
            {
                ItemType            type        = ItemType.Item;
                RaycastInfo         raycastInfo = null;
                InteractableVehicle vehicle     = null;
                if (command.Length >= 1)
                {
                    type = command[0].ToLower() == "v" ? ItemType.Vehicle : ItemType.Item;
                }

                if (command.Length == 0 || command.Length > (type == ItemType.Item ? 2 : 1))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("sell_help4"));
                    return;
                }

                if (!DShop.Instance.Database.IsLoaded)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("db_load_error"));
                    return;
                }

                ushort itemID = 0;
                ushort count  = 1;

                if (command.Length == 2 && type == ItemType.Item)
                {
                    if (!ushort.TryParse(command[1], out count))
                    {
                        if (command[1].ToLower() == "all" || command[1].ToLower() == "a")
                        {
                            count = ushort.MaxValue;
                        }
                        else
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_amount"));
                            return;
                        }
                    }
                    if (count == 0)
                    {
                        count = 1;
                    }
                }


                UnturnedPlayer player = caller as UnturnedPlayer;
                if (!ushort.TryParse(command[0], out itemID))
                {
                    if (type == ItemType.Item && command[0].ToLower() == "h")
                    {
                        itemID = player.Player.equipment.itemID;
                        if (itemID == 0)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("no_item_held"));
                            return;
                        }
                    }
                    else if (type == ItemType.Item)
                    {
                        itemID = command[0].AssetIDFromName(type);
                    }
                    else if (type == ItemType.Vehicle)
                    {
                        // Try to grab the vehicle that the player is looking at.
                        raycastInfo = DShop.RaycastInfoVehicle(player, 10);
                        if (raycastInfo.vehicle == null)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_to_far2"));
                            return;
                        }
                        else
                        {
                            itemID  = raycastInfo.vehicle.id;
                            vehicle = raycastInfo.vehicle;
                            // Run checks before accepting this vehicle to run through ShopVehicle.sell.
                            if (vehicle.isDead)
                            {
                                // Don't allow a destroyed vehicle to be sold.
                                UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_dead"));
                                return;
                            }
                            if (!vehicle.isLocked)
                            {
                                // Vehicle isn't locked to any player.
                                UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_unlocked2"));
                                return;
                            }
                            if (vehicle.isLocked && vehicle.lockedOwner != player.CSteamID)
                            {
                                // This vehicle isn't locked to this player.
                                UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_locked_mismatch"));
                                return;
                            }
                            if (!vehicle.isEmpty)
                            {
                                // The vehicle still has players in it, don't sell.
                                UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_has_player2"));
                                return;
                            }
                        }
                    }
                }
                if (itemID.AssetFromID(type) == null)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                    return;
                }
                ShopObject sObject = DShop.Instance.Database.GetItem(type, itemID);
                if (sObject.ItemID != itemID)
                {
                    Asset asset = itemID.AssetFromID(type);
                    if (type == ItemType.Item)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((ItemAsset)asset).itemName != null) ? ((ItemAsset)asset).itemName : string.Empty, itemID));
                    }
                    else
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((VehicleAsset)asset).vehicleName != null) ? ((VehicleAsset)asset).vehicleName : string.Empty, itemID));
                    }
                    return;
                }

                decimal balance              = Uconomy.Instance.Database.GetBalance(caller.Id);
                decimal newCost              = sObject.BuyCost;
                decimal totalCost            = 0;
                short   actualCount          = 0;
                decimal totalAttatchmentCost = 0;
                string  moneyName            = Uconomy.Instance.Configuration.Instance.MoneyName;

                if (type == ItemType.Item)
                {
                    ShopItem sItem = sObject as ShopItem;
                    if (sItem.Sell(balance, player, count, out newCost, out totalCost, out actualCount, out totalAttatchmentCost))
                    {
                        if (totalAttatchmentCost > 0)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("sold_items_complete_w_attatchments", actualCount, sObject.ItemName, sObject.ItemID,
                                                                              Math.Round(totalCost, 2), moneyName, Math.Round(totalAttatchmentCost, 2), moneyName, Math.Round(balance + totalCost, 2), moneyName));
                        }
                        else
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("sold_items_complete", actualCount, sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 2), moneyName, Math.Round(balance + totalCost, 2), moneyName));
                        }
                    }
                    else
                    {
                        if (actualCount == 0)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("no_items_sell", sObject.ItemName, sObject.ItemID));
                            return;
                        }
                        if (actualCount == -1)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("item_buy_only", sObject.ItemName, sObject.ItemID));
                            return;
                        }
                        if (actualCount < count)
                        {
                            if (totalAttatchmentCost > 0)
                            {
                                UnturnedChat.Say(caller, DShop.Instance.Translate("sold_items_partial_w_attatchments", actualCount, count, sObject.ItemName, sObject.ItemID,
                                                                                  Math.Round(totalCost, 2), moneyName, Math.Round(totalAttatchmentCost, 2), moneyName, Math.Round(balance + totalCost, 2), moneyName));
                            }
                            else
                            {
                                UnturnedChat.Say(caller, DShop.Instance.Translate("sold_items_partial", actualCount, count, sObject.ItemName, sObject.ItemID,
                                                                                  Math.Round(totalCost, 2), moneyName, Math.Round(balance + totalCost, 2), moneyName));
                            }
                        }
                    }
                }
                else
                {
                    ShopVehicle sVehicle = sObject as ShopVehicle;
                    // placeholder code until the vehicle buy/sell tracking update.
                    if (!DShop.Instance.Configuration.Instance.CanSellVehicles)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_not_allowed"));
                        return;
                    }
                    if (sVehicle.Sell(balance, player, raycastInfo, out totalCost, out actualCount))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sold2", sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 2), moneyName, Math.Round(balance + totalCost, 2), moneyName));
                    }
                    else
                    {
                        if (actualCount == -1)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_buy_only2"));
                            return;
                        }
                        if (actualCount == -2)
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("vehicel_sell_no_own2"));
                            return;
                        }
                    }
                }
                if (totalCost > 0)
                {
                    Uconomy.Instance.Database.IncreaseBalance(caller.Id, (Math.Round(totalCost, 2)));
                }
            }
        }
コード例 #9
0
ファイル: DShop.cs プロジェクト: cartman-2000/DynShop
 // logging for buys.
 private void Instance_OnShopBuy(decimal curBallance, UnturnedPlayer player, ushort numItems, ShopObject sObject, ItemType type, decimal newCost, decimal totalCost, short totalItems)
 {
     Logger.Log(string.Format("Player {0} [{1}] ({2}) at location: {3}, has bought {4} items, with type: {5}, with id: {6}({7}), for {8} credits. Players balance is now {9} credits.",
                              player.CharacterName, player.SteamName, player.CSteamID, player.IsInVehicle ? player.CurrentVehicle.transform.position.ToString() : player.Position.ToString(), totalItems, type.ToString(), sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 4), Math.Round(curBallance, 2)));
 }
コード例 #10
0
ファイル: DShop.cs プロジェクト: cartman-2000/DynShop
 internal void _OnShopSell(decimal curBallance, UnturnedPlayer player, ushort numItems, ShopObject sObject, ItemType type, decimal newCost, decimal totalCost, short totalItems, decimal totalAttatchmentCost)
 {
     OnShopSell?.Invoke(curBallance, player, numItems, sObject, type, newCost, totalCost, totalItems, totalAttatchmentCost);
 }
コード例 #11
0
ファイル: DShop.cs プロジェクト: cartman-2000/DynShop
 internal void _OnShopBuy(decimal curBallance, UnturnedPlayer player, ushort numItems, ShopObject sObject, ItemType type, decimal newCost, decimal totalCost, short totalItems)
 {
     OnShopBuy?.Invoke(curBallance, player, numItems, sObject, type, newCost, totalCost, totalItems);
 }
コード例 #12
0
ファイル: CommandBuy.cs プロジェクト: cartman-2000/DynShop
        public void Execute(IRocketPlayer caller, string[] command)
        {
            ItemType type = ItemType.Item;

            if (command.Length >= 1)
            {
                type = command[0].ToLower() == "v" ? ItemType.Vehicle : ItemType.Item;
            }

            if (command.Length == (type == ItemType.Item ? 0 : 1) || command.Length > 2)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("buy_help3"));
                return;
            }

            if (!DShop.Instance.Database.IsLoaded)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("db_load_error"));
                return;
            }

            ushort count = 1;

            if (command.Length == 2 && type == ItemType.Item)
            {
                if (!ushort.TryParse(command[1], out count))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_amount"));
                    return;
                }
                if (count > DShop.Instance.Configuration.Instance.MaxBuyCount)
                {
                    count = DShop.Instance.Configuration.Instance.MaxBuyCount;
                }
                if (count == 0)
                {
                    count = 1;
                }
            }

            UnturnedPlayer player = caller as UnturnedPlayer;

            if (!DShop.GetItemID(caller, command, type, 0, out ushort itemID))
            {
                return;
            }
            ShopObject sObject = DShop.Instance.Database.GetItem(type, itemID);

            if (sObject.ItemID != itemID)
            {
                Asset asset = itemID.AssetFromID(type);
                if (type == ItemType.Item)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((ItemAsset)asset).itemName != null) ? ((ItemAsset)asset).itemName : string.Empty, itemID));
                }
                else
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_db", (asset != null && ((VehicleAsset)asset).vehicleName != null) ? ((VehicleAsset)asset).vehicleName : string.Empty, itemID));
                }
                return;
            }

            decimal balance = Uconomy.Instance.Database.GetBalance(caller.Id);

            decimal newCost     = sObject.BuyCost;
            decimal totalCost   = 0;
            short   actualCount = 0;
            string  moneyName   = Uconomy.Instance.Configuration.Instance.MoneyName;

            if (type == ItemType.Item)
            {
                ShopItem sItem = sObject as ShopItem;
                if (sItem.Buy(balance, player, count, out newCost, out totalCost, out actualCount))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("bought_item_complete", actualCount, sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 2), moneyName, Math.Round(balance - totalCost, 2), moneyName));
                }
                else
                {
                    if (actualCount == 0)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("not_enough_to_buy", moneyName, sObject.ItemName, sObject.ItemID));
                        return;
                    }
                    if (actualCount == -2)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bought_item_error", sObject.ItemName, sObject.ItemID));
                        return;
                    }
                    if (actualCount == -3)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("item_sell_only", sObject.ItemName, sObject.ItemID));
                        return;
                    }
                    if (actualCount < count)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bought_item_partial", actualCount, count, sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 2), moneyName, Math.Round(balance - totalCost, 2), moneyName));
                    }
                }
            }
            else
            {
                ShopVehicle sVehicle = sObject as ShopVehicle;
                if (sVehicle.Buy(balance, player, out totalCost, out actualCount))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("bought_vehicle", sObject.ItemName, sObject.ItemID, Math.Round(totalCost, 2), moneyName, Math.Round(balance - totalCost, 2), moneyName));
                }
                else
                {
                    if (actualCount == 0)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("not_enough_to_buy", moneyName, sObject.ItemName, sObject.ItemID));
                        return;
                    }
                    if (actualCount == -2)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bought_vehicle_error", sObject.ItemName, sObject.ItemID));
                        return;
                    }
                    if (actualCount == -3)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("vehicle_sell_only", sObject.ItemName, sObject.ItemID));
                        return;
                    }
                }
            }
            if (totalCost > 0)
            {
                Uconomy.Instance.Database.IncreaseBalance(caller.Id, -(Math.Round(totalCost, 2)));
            }
        }
コード例 #13
0
ファイル: CommandDShop.cs プロジェクト: ConorJM51/DynShop
        public void Execute(IRocketPlayer caller, string[] command)
        {
            if (command.Length == 0)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("shop_help"));
                return;
            }

            if (!DShop.Instance.Database.IsLoaded)
            {
                UnturnedChat.Say(caller, DShop.Instance.Translate("db_load_error"));
                return;
            }

            ItemType type = ItemType.Item;

            if (command.Length >= 2 && command[1].ToLower() == "v")
            {
                type = ItemType.Vehicle;
            }
            ShopObject shopObject = null;

            switch (command[0].ToLower())
            {
            case "convert":
            {
                if (command.Length != 2)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("convert_help"));
                    return;
                }
                BackendType backend;
                try
                {
                    backend = (BackendType)Enum.Parse(typeof(BackendType), command[1], true);
                    UnturnedChat.Say(caller, DShop.Instance.Translate("converting", backend.ToString()));
                    if (DShop.Instance.Database.ConvertDB(backend))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("conversion_success"));
                    }
                    else
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("conversion_fail"));
                    }
                }
                catch (Exception ex)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_db_type", ex.Message));
                }
                break;
            }

            case "add":
            {
                if (command.Length < (type == ItemType.Item ? 2 : 3) || command.Length > (type == ItemType.Item ? 8 : 6))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("add_help5"));
                    return;
                }
                if (!DShop.GetItemID(caller, command, type, 1, out ushort itemID))
                {
                    return;
                }
                if (itemID.AssetFromID(type) == null)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                    return;
                }
                shopObject = DShop.Instance.Database.GetItem(type, itemID);
                if (shopObject.ItemID == itemID)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("duplicate"));
                }

                // Parse the vars used in command, if short of variables, or can't parse, defaults would be used for those vars.
                decimal buyCost = DShop.Instance.Configuration.Instance.DefaultBuyCost;
                if (command.Length >= (type == ItemType.Item ? 3 : 4) && !decimal.TryParse(type == ItemType.Item ? command[2] : command[3], out buyCost))
                {
                    buyCost = DShop.Instance.Configuration.Instance.DefaultBuyCost;
                    UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_cost"));
                }

                decimal sellMultiplier = DShop.Instance.Configuration.Instance.DefaultSellMultiplier;
                if (command.Length >= (type == ItemType.Item ? 4 : 5) && !decimal.TryParse(type == ItemType.Item ? command[3] : command[4], out sellMultiplier))
                {
                    decimal fraction = 0;
                    if ((type == ItemType.Item && command[3].IsFraction(out fraction)) || (type == ItemType.Vehicle && command[4].IsFraction(out fraction)))
                    {
                        sellMultiplier = fraction;
                    }
                    else
                    {
                        sellMultiplier = DShop.Instance.Configuration.Instance.DefaultSellMultiplier;
                        UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_mult"));
                    }
                }
                decimal minBuyPrice = DShop.Instance.Configuration.Instance.MinDefaultBuyCost;
                if (type == ItemType.Item && command.Length >= 5 && !decimal.TryParse(command[4], out minBuyPrice))
                {
                    minBuyPrice = DShop.Instance.Configuration.Instance.MinDefaultBuyCost;
                    UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_minprice"));
                }

                decimal changeRate = DShop.Instance.Configuration.Instance.DefaultIncrement;
                if (type == ItemType.Item && command.Length >= 6 && !decimal.TryParse(command[5], out changeRate))
                {
                    changeRate = DShop.Instance.Configuration.Instance.DefaultIncrement;
                    UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_changerate"));
                }

                decimal maxBuyPrice = 0;
                if (type == ItemType.Item && command.Length >= 7 && !decimal.TryParse(command[6], out maxBuyPrice))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_maxprice"));
                }

                RestrictBuySell restrictBuySell = RestrictBuySell.None;
                if (command.Length >= (type == ItemType.Item ? 8 : 6))
                {
                    try
                    {
                        restrictBuySell = (RestrictBuySell)Enum.Parse(typeof(RestrictBuySell), type == ItemType.Item ? command[7] : command[5], true);
                    }
                    catch
                    {
                        restrictBuySell = RestrictBuySell.None;
                        UnturnedChat.Say(caller, DShop.Instance.Translate("parse_fail_shoprestrict"));
                    }
                }

                // Construct new item to add to the database.
                shopObject = (type == ItemType.Item ? (ShopObject) new ShopItem(itemID, buyCost, sellMultiplier, minBuyPrice, changeRate, maxBuyPrice, restrictBuySell) : new ShopVehicle(itemID, buyCost, sellMultiplier, restrictBuySell));

                if (DShop.Instance.Database.AddItem(type, shopObject))
                {
                    UnturnedChat.Say(caller, FormatItemInfo("format_item_info_p1_addv2", shopObject, type));
                }
                else
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_add_fail"));
                }
                break;
            }

            case "rem":
            case "remove":
            {
                if (command.Length != (type == ItemType.Item ? 2 : 3))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("remove_help3"));
                    return;
                }
                if (!DShop.GetItemID(caller, command, type, 1, out ushort itemID))
                {
                    return;
                }
                if (itemID.AssetFromID(type) == null)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                    return;
                }

                shopObject = DShop.Instance.Database.GetItem(type, itemID);

                if (DShop.Instance.Database.DeleteItem(type, itemID))
                {
                    UnturnedChat.Say(caller, FormatItemInfo("format_item_info_p1_deletev2", shopObject, type));
                }
                else
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_shop_db"));
                }
                break;
            }

            case "get":
            {
                if (command.Length != (type == ItemType.Item ? 2 : 3))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("get_help3"));
                    return;
                }
                if (!DShop.GetItemID(caller, command, type, 1, out ushort itemID))
                {
                    return;
                }
                if (itemID.AssetFromID(type) == null)
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                    return;
                }
                shopObject = DShop.Instance.Database.GetItem(type, itemID);
                if (shopObject.ItemID == itemID)
                {
                    UnturnedChat.Say(caller, FormatItemInfo("format_item_info_p1_getv2", shopObject, type));
                }
                else
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_shop_db"));
                }
                break;
            }

            case "upd":
            case "update":
            {
                if (command.Length > (type == ItemType.Item ? 4 : 5) || command.Length < (type == ItemType.Item ? 2 : 4))
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("update_help5"));
                    return;
                }
                type = ItemType.Item;
                if (command.Length >= 3 && command[2].ToLower() == "v")
                {
                    type = ItemType.Vehicle;
                }
                if (command.Length == (type == ItemType.Item ? 4 : 5))
                {
                    if (!DShop.GetItemID(caller, command, type, 2, out ushort itemID))
                    {
                        return;
                    }
                    if (itemID.AssetFromID(type) == null)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("invalid_id"));
                        return;
                    }
                    shopObject = DShop.Instance.Database.GetItem(type, itemID);
                    if (shopObject.ItemID != itemID)
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("item_not_in_shop_db"));
                        return;
                    }
                }
                switch (command[1].ToLower())
                {
                case "cost":
                {
                    if (command.Length != (type == ItemType.Item ? 4 : 5))
                    {
                        goto default;
                    }

                    decimal buyCost = DShop.Instance.Configuration.Instance.DefaultBuyCost;
                    if (!decimal.TryParse(type == ItemType.Item ? command[3] : command[4], out buyCost))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bad_cost"));
                        return;
                    }
                    shopObject.BuyCost = buyCost;
                    goto set;
                }

                case "mult":
                {
                    if (command.Length != (type == ItemType.Item ? 4 : 5))
                    {
                        goto default;
                    }

                    decimal sellMult = DShop.Instance.Configuration.Instance.DefaultSellMultiplier;
                    if (!decimal.TryParse(type == ItemType.Item ? command[3] : command[4], out sellMult))
                    {
                        decimal fraction = 0;
                        if ((type == ItemType.Item && command[3].IsFraction(out fraction)) || (type == ItemType.Vehicle && command[4].IsFraction(out fraction)))
                        {
                            sellMult = fraction;
                        }
                        else
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("bad_mult"));
                            return;
                        }
                    }
                    shopObject.SellMultiplier = sellMult;
                    goto set;
                }

                case "min":
                {
                    if (command.Length != 4)
                    {
                        goto default;
                    }

                    decimal minCost = DShop.Instance.Configuration.Instance.MinDefaultBuyCost;
                    if (!decimal.TryParse(command[3], out minCost))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bad_minprice"));
                        return;
                    }
                    ((ShopItem)shopObject).MinBuyPrice = minCost;
                    goto set;
                }

                case "rate":
                {
                    if (command.Length != 4)
                    {
                        goto default;
                    }

                    decimal rate = DShop.Instance.Configuration.Instance.DefaultIncrement;
                    if (!decimal.TryParse(command[3], out rate))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bad_chagerate"));
                        return;
                    }
                    ((ShopItem)shopObject).Change = rate;
                    goto set;
                }

                case "max":
                {
                    if (command.Length != 4)
                    {
                        goto default;
                    }

                    decimal maxCost = 0;
                    if (!decimal.TryParse(command[3], out maxCost))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bad_maxprice"));
                        return;
                    }
                    ((ShopItem)shopObject).MaxBuyPrice = maxCost;
                    goto set;
                }

                case "sr":
                {
                    if (command.Length != (type == ItemType.Item ? 4 : 5))
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("update_shoprestrict_help"));
                        return;
                    }

                    RestrictBuySell restrictBuySell = RestrictBuySell.None;
                    try
                    {
                        restrictBuySell = (RestrictBuySell)Enum.Parse(typeof(RestrictBuySell), type == ItemType.Item ? command[3] : command[4], true);
                    }
                    catch
                    {
                        UnturnedChat.Say(caller, DShop.Instance.Translate("bad_shoprestrict"));
                        return;
                    }
                    shopObject.RestrictBuySell = restrictBuySell;
                    goto set;
                }

                default:
                {
                    UnturnedChat.Say(caller, DShop.Instance.Translate("update_help4"));
                    return;
                }
set:
                    {
                        if (DShop.Instance.Database.AddItem(type, shopObject))
                        {
                            UnturnedChat.Say(caller, FormatItemInfo("format_item_info_p1_updatev2", shopObject, type));
                        }
                        else
                        {
                            UnturnedChat.Say(caller, DShop.Instance.Translate("update_fail"));
                        }
                        break;
                    }
                }

                break;
            }

            default:
                UnturnedChat.Say(caller, Syntax);
                return;
            }
        }
コード例 #14
0
ファイル: CommandDShop.cs プロジェクト: ConorJM51/DynShop
 public string FormatItemInfo(string primaryLiteral, ShopObject shopObject, ItemType type)
 {
     return(DShop.Instance.Translate(primaryLiteral, shopObject.ItemName, shopObject.ItemID, type.ToString(), shopObject.BuyCost, shopObject.SellMultiplier, Enum.GetName(typeof(RestrictBuySell), shopObject.RestrictBuySell), ((byte)shopObject.RestrictBuySell).ToString(),
                                     type == ItemType.Item ? DShop.Instance.Translate("format_item_info_p2v2", ((ShopItem)shopObject).MinBuyPrice, ((ShopItem)shopObject).Change, ((ShopItem)shopObject).MaxBuyPrice) : string.Empty));
 }