Иконка ресурса

FREE Market : 2.0.1 2.0.1

Нет прав для скачивания
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins {
    [Info("Market", "TopPlugin.ru", "2.0.1")]
    internal class Market : RustPlugin {
#region Static
        private readonly Dictionary<ulong, Lot> playerLots = new Dictionary<ulong, Lot>();
        private MItem exampleItem;
        private const string perm = "market.moder";
        private static readonly string Layer = "MARKET_UI";
        private string borderColor = "0.19 0.18 0.16 1.00";
        private int ImageLibraryCheck;
        private Configuration _config;
        private Data data;
        [PluginReference]
        private Plugin ImageLibrary, Economics;
        private readonly List<string> types = new List<string> {
            "BUY", "SELL", "MY"
        }
        ;
        private readonly List<string> attachments = new List<string> {
            "weapon.mod.silencer", "weapon.mod.simplesight", "weapon.mod.8x.scope", "weapon.mod.small.scope", "weapon.mod.holosight", "weapon.mod.muzzleboost", "weapon.mod.muzzlebrake", "weapon.mod.flashlight", "weapon.mod.lasersight"
        }
        ;
        private readonly Dictionary<ItemCategory, string> itemTypes = new Dictionary<ItemCategory, string> {
            [ItemCategory.All]
            = "ALL ITEMS", [ItemCategory.Weapon] = "WEAPON", [ItemCategory.Construction] = "CONSTRUCTION", [ItemCategory.Items] = "ITEMS", [ItemCategory.Resources] = "RECOURCES", [ItemCategory.Attire] = "ATTIRE", [ItemCategory.Tool] = "TOOL", [ItemCategory.Medical] = "MEDICAL", [ItemCategory.Food] = "FOOD", [ItemCategory.Ammunition] = "AMMUNITION", [ItemCategory.Traps] = "TRAPS", [ItemCategory.Misc] = "MISC", [ItemCategory.Component] = "COMPONENT", [ItemCategory.Electrical] = "ELECTRICAL", [ItemCategory.Fun] = "FUN"
        }
        ;
#endregion
#region Config
        private class Configuration {
            [JsonProperty("Commission towards the server when using the economy(as a percentage)")]
            public float Comission = 10;
            [JsonProperty("Restriction on the creation of lots")]
            public int LotRestriction = 5;
            [JsonProperty(PropertyName = "Add all the items from the game to the price selection?")]
            public readonly bool addDefaultItems = true;
            [JsonProperty(PropertyName = "Color settings")]
            public readonly ColorSettings color = new ColorSettings();
            [JsonProperty(PropertyName = "Economics settings")]
            public readonly Economic economic = new Economic {
                useEconomic = true, name = "Coin", url = "https://i.imgur.com/wz9ofzX.png", economicsName = "Economics", depositHook = "Deposit", withdrawHook = "Withdraw", balanceHook = "Balance"
            }
            ;
            [JsonProperty(PropertyName = "Selling items settings(shornames)", ObjectCreationHandling = ObjectCreationHandling.Replace)]
            public readonly List<ItemInfo> items = new List<ItemInfo>();
        }
        private class ItemInfo {
            public string displayName;
            public string shortname;
        }
        private class Economic {
            [JsonProperty(PropertyName = "GetBalance hook")]
            public string balanceHook;
            [JsonProperty(PropertyName = "Deposit hook")]
            public string depositHook;
            [JsonProperty(PropertyName = "Full name of economics plugin")]
            public string economicsName;
            [JsonProperty(PropertyName = "Currency display name")]
            public string name;
            [JsonProperty(PropertyName = "Url for currency image")]
            public string url;
            [JsonProperty(PropertyName = "Use economic?")]
            public bool useEconomic;
            [JsonProperty(PropertyName = "Withdraw hook")]
            public string withdrawHook;
        }
        private class ColorSettings {
            [JsonProperty(PropertyName = "Background color")]
            public readonly string colorBg = "0.25 0.25 0.25 0.83";
            [JsonProperty(PropertyName = "Color of borders (lines)")]
            public readonly string colorBorder = "0.19 0.18 0.16 1.00";
            [JsonProperty(PropertyName = "Header panel color lots")]
            public readonly string colorHeaderPanelLots = "1 1 1 0.3";
            [JsonProperty(PropertyName = "Color of refresh button")]
            public readonly string colorRefreshButton = "1 1 1 0.4";
            [JsonProperty(PropertyName = "Color of top panel")]
            public readonly string colorTopPanel = "0.12 0.24 0.34 1.00";
        }
        protected override void LoadConfig() {
            base.LoadConfig();
            try {
                _config = Config.ReadObject<Configuration>();
                if (_config == null) throw new Exception();
                SaveConfig();
            }
            catch {
                PrintError("Your configuration file contains an error.Using default configuration values.");
                LoadDefaultConfig();
            }
        }
        protected override void SaveConfig() {
            Config.WriteObject(_config);
        }
        protected override void LoadDefaultConfig() {
            _config = new Configuration();
        }
#endregion
#region Data
        private class Data {
            public readonly List<Lot> lots = new List<Lot>();
            public readonly Dictionary<ulong, List<MItem>> playerInventory = new Dictionary<ulong, List<MItem>>();
        }
        private class Lot {
            public ulong id;
            public MItem itemPrice;
            public string name;
            public int price;
            public int sellingCurrency;
            public MItem sellingItem;
        }
        private class MItem {
            public int ammoAmount;
            public string ammoName;
            public int amount;
            public float condition;
            public string displayName;
            public float maxCondition;
            public List<MItem> mods;
            public string shortName;
            public ulong skinid;
            public ItemCategory type;
            public bool weapon;
        }
        private void LoadData() {
            if (Interface.Oxide.DataFileSystem.ExistsDatafile($"{Name}/data")) data = Interface.Oxide.DataFileSystem.ReadObject<Data>($"{Name}/data");
            else data = new Data();
            Interface.Oxide.DataFileSystem.WriteObject($"{Name}/data", data);
        }
        private void OnServerSave() {
            SaveData();
        }
        private void SaveData() {
            if (data != null) Interface.Oxide.DataFileSystem.WriteObject($"{Name}/data", data);
        }
#endregion
#region OxideHooks
        private void OnServerInitialized() {
            PrintError("|-----------------------------------|");
            PrintWarning($"|  Plugin {Title} v{Version} is loaded    |");
            PrintWarning("|          Author: TopPlugin.ru            |");
            PrintWarning("|          VK: vk.com/rustnastroika        |");
            PrintWarning("|          Discord: discord.com/invite/5DPTsRmd3G           |");
            PrintError("|-----------------------------------|");
            if (!ImageLibrary) {
                if (ImageLibraryCheck == 3) {
                    PrintError("ImageLibrary not found!Unloading");
                    Interface.Oxide.UnloadPlugin(Name);
                    return;
                }
                timer.In(1, () => {
                    ImageLibraryCheck++;
                    OnServerInitialized();
                }
                );
                return;
            }
            if (_config.economic.useEconomic) {
                var economic = _config.economic;
                Economics = plugins.Find(economic.economicsName);
                if (Economics == null) {
                    PrintError($"Plugin '{economic.economicsName} ' not found!");
                    Interface.Oxide.UnloadPlugin(Name);
                    return;
                }
                if (!ImageLibrary.Call<bool>("HasImage", economic.url)) ImageLibrary.Call("AddImage", economic.url, economic.url);
            }
            if (_config.addDefaultItems && _config.items.Count == 0) {
                foreach (var check in ItemManager.itemList.Skip(7)) if (itemTypes.ContainsKey(check.category)) _config.items.Add(new ItemInfo {
                    shortname = check.shortname, displayName = check.displayName.english
                }
                );
                SaveConfig();
            }
            permission.RegisterPermission(perm, this);
            borderColor = _config.color.colorBorder;
            LoadData();
            exampleItem = ConvertToMItem(ItemManager.CreateByName("rifle.ak"));
            foreach (var check in BasePlayer.activePlayerList) OnPlayerConnected(check);
        }
        private void Unload() {
            SaveData();
            foreach (var check in BasePlayer.activePlayerList) CuiHelper.DestroyUi(check, Layer + ".backend");
        }
        private void OnPlayerConnected(BasePlayer player) {
            // player.SendConsoleCommand("chat.say /market");
            if (player == null || data.playerInventory.ContainsKey(player.userID)) return;
            data.playerInventory.Add(player.userID, new List<MItem>());
        }
        private void OnPlayerDisconnected(BasePlayer player) {
            if (player == null || !playerLots.ContainsKey(player.userID)) return;
            playerLots.Remove(player.userID);
        }
        private void OnEntityDeath(BaseCombatEntity entity) {
            if (entity == null || entity.ToPlayer() == null) return;
            CuiHelper.DestroyUi(entity.ToPlayer(), Layer + ".backend");
        }
#endregion
#region Commands
        [ChatCommand("market")]
        private void cmdChatshop(BasePlayer player, string command, string[] args) {
            ShowUIMain(player);
        }
        [ConsoleCommand("UI_OPENSHOP")]
        private void cmdConsoleUI_OPENSHOP(ConsoleSystem.Arg arg) {
            if (arg == null) return;
            var player = arg.Player();
            if (arg.Args == null || arg.Args.Length == 0) {
                ShowUIBUY(player);
                return;
            }
            var length = arg.Args.Length;
            switch (arg.Args[0]) {
                case "BUY" : if (playerLots.ContainsKey(player.userID)) playerLots.Remove(player.userID);
                if (length == 1) {
                    ShowUIBUY(player);
                    return;
                }
                ShowUIBUY(player, int.Parse(arg.Args[1]), int.Parse(arg.Args[2]), string.Join(" ", arg.Args.Skip(3).ToArray()));
                return;
                case "SELL" : if (length == 1) {
                    ShowUISELL(player);
                    return;
                }
                ShowUISELL(player, int.Parse(arg.Args[1]), length == 3 ? int.Parse(arg.Args[2]) : -1);
                return;
                case "MY" : if (playerLots.ContainsKey(player.userID)) playerLots.Remove(player.userID);
                if (length == 1) {
                    ShowUIMYORDERS(player);
                    return;
                }
                // Debug.Log(arg.Args.Length);
                ShowUIMYORDERS(player, int.Parse(arg.Args[1]), int.Parse(arg.Args[2]));
                return;
            }
        }
        [ConsoleCommand("UI_CLOSESHOP")]
        private void cmdConsoleUI_CLOSESHOP(ConsoleSystem.Arg arg) {
            var player = arg.Player();
            if (playerLots.ContainsKey(player.userID)) playerLots.Remove(player.userID);
        }
        [ConsoleCommand("UI_FINDITEMSELL")]
        private void cmdConsoleUI_FINDITEMSELL(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length == 0) return;
            ShowUISELL(arg.Player(), int.Parse(arg.Args[1]), int.Parse(arg.Args[0]), string.Join(" ", arg.Args.Skip(2).ToArray()));
        }
        [ConsoleCommand("UI_CHOSELOTITEM")]
        private void cmdConsoleUI_CHOSELOTITEM(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length == 0) return;
            var player = arg.Player();
            var index = int.Parse(arg.Args[0]);
            var l = playerLots[player.userID];
            if (index == -1) {
                l.sellingCurrency = 1;
                l.sellingItem = null;
            }
            else {
                l.sellingItem = ConvertToMItem(player.inventory.AllItems()[index]);
                l.sellingCurrency = 0;
            }
            ShowUISELL(player, int.Parse(arg.Args[1]), index);
        }
        [ConsoleCommand("UI_CHANGEAMOUNT")]
        private void cmdConsoleUI_CHANGEAMOUNT(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length < 3) return;
            var player = arg.Player();
            var amount = 0;
            var suc = int.TryParse(arg.Args[2], out amount);
            if (!suc || amount < 1) return;
            var index = int.Parse(arg.Args[0]);
            var l = playerLots[player.userID];
            if (index == -1) {
                var balance = Economics.Call<double>(_config.economic.balanceHook, player.userID);
                if (balance < amount) amount = (int) balance;
                l.sellingCurrency = amount;
            }
            else {
                if (amount > player.inventory.AllItems()[index].amount) amount = player.inventory.AllItems()[index].amount;
                l.sellingItem.amount = amount;
            }
            ShowUISELL(player, int.Parse(arg.Args[1]), index);
        }
        [ConsoleCommand("UI_CHANGEPRICE")]
        private void cmdConsoleUI_CHANGEPRICE(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length == 0) return;
            var player = arg.Player();
            var index = int.Parse(arg.Args[0]);
            var l = playerLots[player.userID];
            if (l.price > 0) {
                l.price = 0;
                l.itemPrice = exampleItem;
            }
            else {
                l.price = 1;
                l.itemPrice = null;
            }
            ShowUISELL(player, int.Parse(arg.Args[1]), index);
        }
        [ConsoleCommand("UI_CHANGECONDITION")]
        private void cmdConsoleUI_CHANGECONDITION(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length < 3) return;
            int cond;
            var s = int.TryParse(arg.Args[2], out cond);
            if (!s) return;
            if (cond > 100) cond = 100;
            if (cond < 1) cond = 1;
            var player = arg.Player();
            var l = playerLots[player.userID];
            l.itemPrice.condition = (int)(l.itemPrice.maxCondition * cond / 100);
            ShowUISELL(player, int.Parse(arg.Args[1]), int.Parse(arg.Args[0]));
        }
        [ConsoleCommand("UI_CHANGEPRICEAMOUNT")]
        private void cmdConsoleUI_CHANGEPRICEAMOUNT(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length < 3) return;
            var player = arg.Player();
            int p;
            var suc = int.TryParse(arg.Args[2], out p);
            if (!suc || p < 1) return;
            var index = int.Parse(arg.Args[0]);
            var l = playerLots[player.userID];
            if (l.price > 0) l.price = p;
            else l.itemPrice.amount = p;
            ShowUISELL(player, int.Parse(arg.Args[1]), index);
        }
        [ConsoleCommand("UI_CHANGEPRICEITEM")]
        private void cmdConsoleUI_CHANGEPRICEITEM(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length == 0) return;
            var player = arg.Player();
            var index = int.Parse(arg.Args[0]);
            var l = playerLots[player.userID];
            l.itemPrice = ConvertToMItem(ItemManager.CreateByName(arg.Args[2]));
            ShowUISELL(player, int.Parse(arg.Args[1]), index);
        }
        [ConsoleCommand("UI_UPLOADLOT")]
        private void cmdConsoleUI_UPLOADLOT(ConsoleSystem.Arg arg) {
            if (arg == null) return;
            var player = arg.Player();
            if (GetLotsPlayer(player) >= _config.LotRestriction) {
                player.ChatMessage(GetMsg(player, "RESTRICTION", new[] {
                    ""
                }
                ));
                return;
            }
            var lot = playerLots[player.userID];
            if (lot.sellingCurrency > 0) RemoveMoney(player.userID, (double) lot.sellingCurrency);
            else Take(player.inventory.AllItems(), lot.sellingItem);
            data.lots.Add(playerLots[player.userID]);
            playerLots.Remove(player.userID);
            player.ChatMessage(GetMsg(player, "LOTUPLOADEDMESSAGE", new[] {
                ""
            }
            ));
            CuiHelper.DestroyUi(player, Layer + ".backend");
        }
        private int GetLotsPlayer(BasePlayer player) {
            int amount = 0;
            foreach (var check in data.lots) {
                if (check.id == player.userID) amount++;
            }
            return amount;
        }
        [ConsoleCommand("UI_CHANGEATTACHMENT")]
        private void cmdConsoleUI_CHANGEATTACHMENT(ConsoleSystem.Arg arg) {
            if (arg?.Args == null || arg.Args.Length < 3) return;
            var player = arg.Player();
            var mod = arg.Args[2];
            var lot = playerLots[player.userID];
            if (lot.itemPrice.mods.Count > 0) {
                if (mod == "weapon.mod.silencer" || mod == "weapon.mod.muzzleboost" || mod == "weapon.mod.muzzlebrake") {
                    var likeMod = lot.itemPrice.mods.Where(x => x.shortName == "weapon.mod.silencer" || x.shortName == "weapon.mod.muzzleboost" || x.shortName == "weapon.mod.muzzlebrake");
                    foreach (var check in likeMod.ToArray()) lot.itemPrice.mods.Remove(check);
                    lot.itemPrice.mods.Add(ConvertToMItem(ItemManager.CreateByName(mod)));
                }
                else if (mod == "weapon.mod.8x.scope" || mod == "weapon.mod.small.scope" || mod == "weapon.mod.holosight" || mod == "weapon.mod.simplesight") {
                    var likeMod = lot.itemPrice.mods.Where(x => x.shortName == "weapon.mod.8x.scope" || x.shortName == "weapon.mod.small.scope" || x.shortName == "weapon.mod.holosight" || x.shortName == "weapon.mod.simplesight");
                    foreach (var check in likeMod.ToArray()) lot.itemPrice.mods.Remove(check);
                    lot.itemPrice.mods.Add(ConvertToMItem(ItemManager.CreateByName(mod)));
                }
                else {
                    var likeMod = lot.itemPrice.mods.Where(x => x.shortName == "weapon.mod.flashlight" || x.shortName == "weapon.mod.lasersight");
                    foreach (var check in likeMod.ToArray()) lot.itemPrice.mods.Remove(check);
                    lot.itemPrice.mods.Add(ConvertToMItem(ItemManager.CreateByName(mod)));
                }
            }
            else lot.itemPrice.mods.Add(ConvertToMItem(ItemManager.CreateByName(mod)));
            ShowUISELL(player, int.Parse(arg.Args[1]), int.Parse(arg.Args[0]));
        }
        [ConsoleCommand("UI_CONFIRMBUY")]
        private void cmdConsoleUI_CONFIMBUY(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length < 1) return;
            var player = arg.Player();
            var lot = data.lots[int.Parse(arg.Args[0])];
            var can = CanBuyLot(lot, player);
            var container = new CuiElementContainer();
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0.5 1", AnchorMax = "0.5 1", OffsetMin = "-120 -47", OffsetMax = "120 -4"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".confirmbuy");
            if (can == "can") {
                container.Add(new CuiLabel {
                    RectTransform = {
                        AnchorMin = "0 0.5", AnchorMax = "1 1"
                    }
                    , Text = {
                        Text = GetMsg(player, "CONFIRMPURCHASE", new[] {
                            ""
                        }
                        ), Font = "robotocondensed-bold.ttf", FontSize = 17, Align = TextAnchor.MiddleCenter, Color = "0.85 0.85 0.85 1.00", FadeIn = 1f
                    }
                }
                , Layer + ".confirmbuy");
                container.Add(new CuiButton {
                    RectTransform = {
                        AnchorMin = "0.5 0", AnchorMax = "0.5 0", OffsetMin = "-40 0", OffsetMax = "-15 25"
                    }
                    , Button = {
                        Color = "0.55 0.78 0.24 1", Close = Layer + ".confirmbuy", Command = $"UI_BUYITEMEND {arg.Args[0]}", Sprite = "assets/icons/check.png", FadeIn = 1f
                    }
                    , Text = {
                        Text = ""
                    }
                }
                , Layer + ".confirmbuy");
                container.Add(new CuiButton {
                    RectTransform = {
                        AnchorMin = "0.5 0", AnchorMax = "0.5 0", OffsetMin = "16 1", OffsetMax = "39 24"
                    }
                    , Button = {
                        Color = "0.8 0.28 0.2 1", Close = Layer + ".confirmbuy", Sprite = "assets/icons/close.png", FadeIn = 1f
                    }
                    , Text = {
                        Text = ""
                    }
                }
                , Layer + ".confirmbuy");
            }
            else {
                container.Add(new CuiLabel {
                    RectTransform = {
                        AnchorMin = "0 0", AnchorMax = "1 1"
                    }
                    , Text = {
                        Text = can, Font = "robotocondensed-bold.ttf", FontSize = 20, Align = TextAnchor.MiddleCenter, Color = "0.85 0.85 0.85 1.00", FadeIn = 1f
                    }
                }
                , Layer + ".confirmbuy");
            }
            CuiHelper.DestroyUi(player, Layer + ".confirmbuy");
            CuiHelper.AddUi(player, container);
        }
        [ConsoleCommand("UI_CONFIRMLOTREMOVE")]
        private void cmdConsoleUI_CONFIRMLOTREMOVE(ConsoleSystem.Arg arg) {
            var player = arg?.Player();
            if (arg == null || arg.Args == null || arg.Args.Length != 1) return;
            var container = new CuiElementContainer();
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0.5 1", AnchorMax = "0.5 1", OffsetMin = "-120 -47", OffsetMax = "120 -4"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".confirmbuy");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0.5", AnchorMax = "1 1"
                }
                , Text = {
                    Text = GetMsg(player, "CONFIRMDELETION", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 17, Align = TextAnchor.MiddleCenter, Color = "0.85 0.85 0.85 1.00", FadeIn = 1f
                }
            }
            , Layer + ".confirmbuy");
            container.Add(new CuiButton {
                RectTransform = {
                    AnchorMin = "0.5 0", AnchorMax = "0.5 0", OffsetMin = "-40 0", OffsetMax = "-15 25"
                }
                , Button = {
                    Color = "0.55 0.78 0.24 1", Close = Layer + ".confirmbuy", Command = $"UI_REMOVELOTEND {arg.Args[0]}", Sprite = "assets/icons/check.png", FadeIn = 1f
                }
                , Text = {
                    Text = ""
                }
            }
            , Layer + ".confirmbuy");
            container.Add(new CuiButton {
                RectTransform = {
                    AnchorMin = "0.5 0", AnchorMax = "0.5 0", OffsetMin = "16 1", OffsetMax = "39 24"
                }
                , Button = {
                    Color = "0.8 0.28 0.2 1", Close = Layer + ".confirmbuy", Sprite = "assets/icons/close.png", FadeIn = 1f
                }
                , Text = {
                    Text = ""
                }
            }
            , Layer + ".confirmbuy");
            CuiHelper.DestroyUi(player, Layer + ".confirmbuy");
            CuiHelper.AddUi(player, container);
        }
        [ConsoleCommand("UI_REMOVELOTEND")]
        private void cmdConsoleUI_REMOVELOTEND(ConsoleSystem.Arg arg) {
            var player = arg?.Player();
            if (arg == null || arg.Args == null || arg.Args.Length < 1) return;
            var lot = data.lots[int.Parse(arg.Args[0])];
            if (lot.sellingCurrency > 0) GiveMoney(lot.id, (double) lot.sellingCurrency);
            else {
                CreateItem(player, lot.sellingItem);
                //   data.playerInventory[lot.id].Add(lot.sellingItem);
            }
            data.lots.Remove(lot);
            ShowUIMYORDERS(player);
            var container = new CuiElementContainer();
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0.5 1", AnchorMax = "0.5 1", OffsetMin = "-120 -47", OffsetMax = "120 -4"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".confirmbuy");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Text = {
                    Text = GetMsg(player, "HASBEENREMOVED", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 17, Align = TextAnchor.MiddleCenter, Color = "0.96 0.93 0.71 1.00", FadeIn = 1f
                }
            }
            , Layer + ".confirmbuy");
            CuiHelper.DestroyUi(player, Layer + ".confirmbuy");
            CuiHelper.AddUi(player, container);
        }
        private void GiveMoney(ulong userID, double amount) {
            Economics.Call(_config.economic.depositHook, userID, amount);
        }
        private void RemoveMoney(ulong userID, double amount) {
            Economics.Call(_config.economic.withdrawHook, userID, amount);
        }
        [ConsoleCommand("UI_BUYITEMEND")]
        private void cmdConsoleUI_BUYITEMEND(ConsoleSystem.Arg arg) {
            var player = arg?.Player();
            if (arg == null || arg.Args == null || arg.Args.Length < 1) return;
            var lot = data.lots[int.Parse(arg.Args[0])];
            if (lot.price > 0) {
                RemoveMoney(player.userID, (double) lot.price);
                GiveMoney(lot.id, (lot.price * (100 - _config.Comission) / 100));
                if (lot.sellingCurrency > 0) GiveMoney(player.userID, lot.sellingCurrency);
                else data.playerInventory[player.userID].Add(lot.sellingItem);
            }
            else {
                Take(player.inventory.AllItems(), lot.itemPrice);
                data.playerInventory[lot.id].Add(lot.itemPrice);
                if (lot.sellingCurrency > 0) GiveMoney(player.userID, lot.sellingCurrency);
                else data.playerInventory[player.userID].Add(lot.sellingItem);
            }
            data.lots.Remove(lot);
            ShowUIBUY(player);
            var container = new CuiElementContainer();
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0.5 1", AnchorMax = "0.5 1", OffsetMin = "-120 -47", OffsetMax = "120 -4"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".confirmbuy");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Text = {
                    Text = GetMsg(player, "Purchasecompleted", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 17, Align = TextAnchor.MiddleCenter, Color = "0.96 0.93 0.71 1.00", FadeIn = 1f
                }
            }
            , Layer + ".confirmbuy");
            CuiHelper.DestroyUi(player, Layer + ".confirmbuy");
            CuiHelper.AddUi(player, container);
        }
        private void CreateItem(BasePlayer player, MItem item) {
            var a = ItemManager.CreateByName(item.shortName, item.amount, item.skinid);
            a.condition = item.condition;
            if (item.weapon) {
                foreach (var check in item.mods) {
                    var b = ItemManager.CreateByName(check.shortName, check.amount, check.skinid);
                    b.condition = check.condition;
                    b.MoveToContainer(a.contents);
                }
                var proj = a.GetHeldEntity() as BaseProjectile;
                proj.primaryMagazine.contents = item.ammoAmount;
                proj.primaryMagazine.ammoType = ItemManager.FindItemDefinition(item.ammoName);
                proj.SendNetworkUpdateImmediate();
            }
            data.playerInventory[player.userID].Remove(item);
            player.GiveItem(a);
        }
        [ConsoleCommand("UI_TAKEITEMFROMINV")]
        private void cmdConsoleUI_TAKEITEMFROMINV(ConsoleSystem.Arg arg) {
            var player = arg?.Player();
            if (arg == null || arg.Args == null || arg.Args.Length < 1) return;
            var item = data.playerInventory[player.userID][int.Parse(arg.Args[0])];
            CreateItem(player, item);
            ShowUIMYORDERS(player);
            var container = new CuiElementContainer();
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0.5 1", AnchorMax = "0.5 1", OffsetMin = "-120 -47", OffsetMax = "120 -4"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".confirmbuy");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "1 1"
                }
                , Text = {
                    Text = GetMsg(player, "Itemsuccessfully", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 17, Align = TextAnchor.MiddleCenter, Color = "0.96 0.93 0.71 1.00", FadeIn = 1f
                }
            }
            , Layer + ".confirmbuy");
            CuiHelper.DestroyUi(player, Layer + ".confirmbuy");
            CuiHelper.AddUi(player, container);
        }
#endregion
#region Functions
        private string CanBuyLot(Lot lot, BasePlayer player) {
            if (lot.price > 0) {
                if (Economics.Call<double>(_config.economic.balanceHook, player.userID) < lot.price) return GetMsg(player, "BAKANCENO", new[] {
                    ""
                }
                );
            }
            else {
                if (ItemCount(player.inventory.AllItems(), lot.itemPrice) < lot.itemPrice.amount) return GetMsg(player, "Haventitems", new[] {
                    ""
                }
                );
            }
            return "can";
        }
        private void Take(IEnumerable<Item> itemList, MItem t) {
            var num1 = 0;
            if (t.amount == 0) return;
            var list = Pool.GetList<Item>();
            foreach (var obj in itemList) {
                if (obj.info.shortname != t.shortName || t.skinid != 0 && obj.skin != t.skinid || obj.condition != t.condition) continue;
                var num2 = t.amount - num1;
                if (num2 <= 0) continue;
                if (obj.amount > num2) {
                    obj.MarkDirty();
                    obj.amount -= num2;
                    num1 += num2;
                    break;
                }
                if (obj.amount <= num2) {
                    if (t.mods.Count > 0) {
                        if (CheckMods(obj, t)) {
                            num1 += obj.amount;
                            list.Add(obj);
                        }
                    }
                    else {
                        num1 += obj.amount;
                        list.Add(obj);
                    }
                }
                if (num1 == t.amount) break;
            }
            foreach (var obj in list) obj.RemoveFromContainer();
            Pool.FreeList(ref list);
        }
        private bool CheckMods(Item item, MItem titem) {
            var amount = 0;
            foreach (var check in item.contents.itemList) {
                for (int i = 0; i < titem.mods.Count; i++) {
                    var mods = titem.mods[i];
                    if (mods.shortName == check.info.shortname) amount++;
                }
            }
            return amount == titem.mods.Count;
        }
        private int ItemCount(IReadOnlyList<Item> items, MItem t) {
            var result = 0;
            for (var i = 0; i < items.Count; i++) {
                var item = items[i];
                if (item.info.shortname == t.shortName && (t.skinid == 0 || item.skin == t.skinid) && item.condition >= t.condition) {
                    if (t.mods.Count > 0) {
                        if (CheckMods(item, t)) result += item.amount;
                    }
                    else {
                        result += item.amount;
                    }
                }
            }
            return result;
        }
        private MItem ConvertToMItem(Item xItem) {
            var item = new MItem {
                skinid = xItem.skin, amount = xItem.amount, condition = xItem.condition, maxCondition = xItem.maxCondition, mods = new List<MItem>()
            }
            ;
            var info = xItem.info;
            item.displayName = info.displayName.english;
            item.shortName = info.shortname;
            item.type = info.category;
            var weapon = xItem.GetHeldEntity()?.GetComponent<BaseProjectile>();
            item.weapon = weapon != null;
            if (!item.weapon) return item;
            if (xItem.contents != null) foreach (var check in xItem.contents.itemList) item.mods.Add(ConvertToMItem(check));
            item.ammoName = weapon.primaryMagazine.ammoType.shortname;
            item.ammoAmount = weapon.primaryMagazine.contents;
            return item;
        }
        private string GetCondition(float current, float max) {
            if (current == max) return "100%";
            if (current == 0) return "BROKEN";
            var cond = current / max;
            return $"{cond * 100:00}%";
        }
        private int PageCount(int amount) {
            return amount == 0 ? 1 : (int) Math.Ceiling(amount / 12f);
        }
#endregion
#region UI
        private void ShowUIBUY(BasePlayer player, int page = 0, int type = (int) ItemCategory.All, string search = "") {
            var container = new CuiElementContainer();
            ShowHeaderUI(ref container, "BUY", player);
#region Vars
            var y = 0;
            var economic = _config.economic;
            var id = player.userID;
            var allCategoryes = type != (int) ItemCategory.All;
            var searchEmpty = !string.IsNullOrEmpty(search);
            var typesC = new Dictionary<ItemCategory, string>();
            var lots = new List<Lot>();
            var canLast = page > 0;
            for (var i = 0; i < data.lots.Count; i++) {
                var check = data.lots[i];
                if (check.id == id || allCategoryes && ((int) check.sellingItem.type != type || check.sellingCurrency > 0) || searchEmpty && (check.sellingCurrency > 0 && !economic.name.ToLower().Contains(search) || !check.sellingItem.displayName.ToLower().Contains(search))) continue;
                lots.Add(check);
            }
            foreach (var check in itemTypes) {
                if ((int) check.Key == type) continue;
                typesC.Add(check.Key, check.Value);
            }
            var canNext = lots.Count - 12 * (page + 1) > 0;
#endregion
#region Static
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "58 -168", OffsetMax = "391 -134"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".search");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "4 2", OffsetMax = "36 32"
                }
                , Image = {
                    Color = borderColor, Sprite = "assets/icons/examine.png"
                }
            }
            , Layer + ".search");
            container.Add(new CuiElement {
                Parent = Layer + ".search", Components = {
                    new CuiInputFieldComponent {
                        Text = "", FontSize = 15, Command = $"UI_OPENSHOP BUY {page} {type}", Align = TextAnchor.MiddleLeft, Color = "1 1 1 1", CharsLimit = 25, IsPassword = false
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "40 2", OffsetMax = "389 32"
                    }
                }
            }
            );
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "58 -725", OffsetMax = "391 -182"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".findResults");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "0 -48", OffsetMax = "333 -46"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".findResults");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "0 -48", OffsetMax = "333 0"
                }
                , Text = {
                    Text = GetMsg(player, "CHOSECATEGORY", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 30, Align = TextAnchor.MiddleCenter, Color = "0.82 0.78 0.76 1.00"
                }
            }
            , Layer + ".findResults");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "413 -168", OffsetMax = "1212 -134"
                }
                , Image = {
                    Color = _config.color.colorHeaderPanelLots
                }
            }
            , Layer, Layer + ".headerpanel");
            container.Add(new CuiButton {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "7 2", OffsetMax = "37 32"
                }
                , Button = {
                    Color = _config.color.colorRefreshButton, Command = $"UI_OPENSHOP BUY {page} {type} {search}", Sprite = "assets/icons/refresh.png"
                }
                , Text = {
                    Text = ""
                }
            }
            , Layer + ".headerpanel");
            container.Add(new CuiButton {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "630 7", OffsetMax = "649 27"
                }
                , Button = {
                    Color = "0 0 0 1", Command = canLast ? $"UI_OPENSHOP BUY {page - 1} {type} {search}" : ""
                }
                , Text = {
                    Text = "<", Font = "robotocondensed-bold.ttf", FontSize = 15, Align = TextAnchor.MiddleCenter, Color = canLast ? "0.96 0.93 0.71 1.00" : "0.40 0.36 0.26 1.00"
                }
            }
            , Layer + ".headerpanel", Layer + ".backbutton");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "651 7", OffsetMax = "756 27"
                }
                , Text = {
                    Text = GetMsg(player, "PAGEUI", new[] {
                        (page + 1).ToString(), PageCount(lots.Count).ToString()
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "0.96 0.93 0.71 1.00"
                }
            }
            , Layer + ".headerpanel");
            container.Add(new CuiButton {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "758 7", OffsetMax = "777 27"
                }
                , Button = {
                    Color = "0 0 0 1", Command = canNext ? $"UI_OPENSHOP BUY {page + 1} {type} {search}" : ""
                }
                , Text = {
                    Text = ">", Font = "robotocondensed-bold.ttf", FontSize = 15, Align = TextAnchor.MiddleCenter, Color = canNext ? "0.96 0.93 0.71 1.00" : "0.40 0.36 0.26 1.00"
                }
            }
            , Layer + ".headerpanel", Layer + ".nextbutton");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "37 2", OffsetMax = "630 32"
                }
                , Text = {
                    Text = GetMsg(player, "OFFERSBYR", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-bold.ttf", FontSize = 25, Align = TextAnchor.MiddleCenter, Color = "0.82 0.78 0.76 1.00"
                }
            }
            , Layer + ".headerpanel");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 1", AnchorMax = "0 1", OffsetMin = "413 -213", OffsetMax = "1212 -185"
                }
                , Image = {
                    Color = "0 0 0 0"
                }
            }
            , Layer, Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "0 -2", OffsetMax = "799 0"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "158 0", OffsetMax = "160 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "278 0", OffsetMax = "280 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "328 0", OffsetMax = "330 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "381 0", OffsetMax = "383 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "478 0", OffsetMax = "480 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "598 0", OffsetMax = "600 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "648 0", OffsetMax = "650 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiPanel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "701 0", OffsetMax = "703 28"
                }
                , Image = {
                    Color = borderColor
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "5 0", OffsetMax = "160 28"
                }
                , Text = {
                    Text = GetMsg(player, "TRADER", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-regular.ttf", FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "0.82 0.78 0.76 1.00"
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "160 0", OffsetMax = "280 28"
                }
                , Text = {
                    Text = GetMsg(player, "LotName", new[] {
                        ""
                    }
                    ), Font = "robotocondensed-regular.ttf", FontSize = 13, Align = TextAnchor.MiddleCenter, Color = "0.82 0.78 0.76 1.00"
                }
            }
            , Layer + ".lotnamesheader");
            container.Add(new CuiLabel {
                RectTransform = {
                    AnchorMin = "0 0", AnchorMax = "0 0", OffsetMin = "280 0", OffsetMax = "330 2
// ... [Code preview truncated due to length. Full source in attached file] ...

Автор
15FPS
Скачиваний
0
Просмотры
1
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху