Exemplo n.º 1
0
 public static Task <IInventoryItem> GiveItemAsync(this IItemSpawner spawner,
                                                   IInventory inventory,
                                                   IItemAsset asset,
                                                   [CanBeNull] IItemState state = null)
 {
     return(spawner.GiveItemAsync(inventory, asset.ItemAssetId, state));
 }
Exemplo n.º 2
0
        /// See <see cref="IItemSpawner.GiveItemAsync"/>.
        public static Task <IItemInstance?> GiveItemAsync(this IItemSpawner spawner,
                                                          IInventory inventory,
                                                          IItemAsset asset,
                                                          IItemState?state = null)
        {
            if (spawner == null)
            {
                throw new ArgumentNullException(nameof(spawner));
            }

            return(spawner.GiveItemAsync(inventory, asset.ItemAssetId, state));
        }
Exemplo n.º 3
0
        public async Task <decimal> Buy(IPlayerUser user, int amount = 1)
        {
            if (!CanBuy())
            {
                throw new InvalidOperationException("No buying at this shop");
            }

            var inventory = user.Player.GetInventory() ??
                            throw new UserFriendlyException(_stringLocalizer["errors:no_inventory"]);

            VerifyAmount(amount);

            var itemAsset = await GetItemAsset();

            var price = ShopData.BuyPrice !.Value * amount;

            var balance = await _economyProvider.UpdateBalanceAsync(user.Id, user.Type, -price,
                                                                    _stringLocalizer["transactions:items:bought",
                                                                                     new
                                                                                     {
                                                                                         ItemAsset = itemAsset,
                                                                                         Amount = amount,
                                                                                         Price = price,
                                                                                         _economyProvider.CurrencySymbol,
                                                                                         _economyProvider.CurrencyName
                                                                                     }]);

            for (var i = 0; i < amount; i++)
            {
                var item = await _itemSpawner.GiveItemAsync(inventory, itemAsset);

                if (item == null)
                {
                    throw new Exception($"Could not give item with id '{itemAsset.ItemAssetId}' to player");
                }
            }

            return(balance);
        }
Exemplo n.º 4
0
        protected override async UniTask OnExecuteAsync()
        {
            if (Context.Parameters.Length < 1 || Context.Parameters.Length > 2)
            {
                throw new CommandWrongUsageException(Context);
            }

            // If the console is trying to spawn itself a kit, remind to specify a player
            if (Context.Parameters.Length == 1 && Context.Actor.Type == KnownActorTypes.Console)
            {
                throw new CommandWrongUsageException(Context);
            }

            string kitName = Context.Parameters[0].ToUpperInvariant();

            KitsData kitsData = await m_DataStore.LoadAsync <KitsData>(KitsKey);

            if (!kitsData.Kits.ContainsKey(kitName))
            {
                throw new UserFriendlyException(m_StringLocalizer["kits:spawn:none", new { Kit = kitName }]);
            }

            if (await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.{kitName}") == PermissionGrantResult.Deny)
            {
                throw new UserFriendlyException(m_StringLocalizer["kits:spawn:denied", new { Kit = kitName }]);
            }

            if (Context.Parameters.Length == 2 &&
                await m_PermissionChecker.CheckPermissionAsync(Context.Actor, $"kits.kit.give.{kitName}") == PermissionGrantResult.Deny)
            {
                throw new UserFriendlyException(m_StringLocalizer["kits:give:deny", new { Kit = kitName }]);
            }

            UnturnedUser recipient;

            if (Context.Parameters.Length == 2)
            {
                recipient = await Context.Parameters.GetAsync <UnturnedUser>(1);
            }
            else
            {
                recipient = Context.Actor as UnturnedUser;
            }

            if (recipient == null)
            {
                throw new UserFriendlyException(m_StringLocalizer["commands:failed_player", new { Player = Context.Parameters[1] }]);
            }

            SerializableKit kit = kitsData.Kits[kitName];

            double?cooldown = await m_CooldownManager.OnCooldownAsync(recipient, "kits", kitName, kit.Cooldown);

            if (cooldown.HasValue)
            {
                throw new UserFriendlyException(m_StringLocalizer["kits:spawn:cooldown", new { Kit = kitName, Time = cooldown }]);
            }

            foreach (var item in kit.SerializableItems)
            {
                await m_ItemSpawner.GiveItemAsync(
                    recipient.Player.Inventory,
                    item.ID,
                    new SerializedItemState(item.Quality, item.Durability, item.Amount, item.State));
            }

            await Context.Actor.PrintMessageAsync(m_StringLocalizer["kits:spawn:success",
                                                                    new { Kit = kitName, Player = recipient.DisplayName }]);
        }