private void AddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            bool insertRequired;

            using (QueryResult reader = this.DbConnection.QueryReader(
                       "SELECT COUNT(UserId) AS Count FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
                       key.UserId, key.BankChestIndex
                       )) {
                if (!reader.Read())
                {
                    throw new InvalidOperationException("Unexpected data were returned.");
                }

                insertRequired = (reader.Get <int>("Count") == 0);
            }

            if (insertRequired)
            {
                this.DbConnection.Query(
                    "INSERT INTO Protector_BankChests (UserId, ChestIndex, Content) VALUES (@0, @1, @2);",
                    key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
                    );
            }
            else
            {
                this.DbConnection.Query(
                    "UPDATE Protector_BankChests SET Content = @2 WHERE UserId = @0 AND ChestIndex = @1;",
                    key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
                    );
            }
        }
 public bool Equals(BankChestDataKey other)
 {
     return(
         this.UserId == other.UserId &&
         this.BankChestIndex == other.BankChestIndex
         );
 }
Пример #3
0
 public bool Equals(BankChestDataKey other)
 {
     return (
     this.UserId == other.UserId &&
     this.BankChestIndex == other.BankChestIndex
       );
 }
        private BankChestMetadata GetBankChestMetadata(BankChestDataKey key)
        {
            using (QueryResult reader = this.DbConnection.QueryReader(
                       "SELECT Content FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
                       key.UserId, key.BankChestIndex
                       )) {
                if (!reader.Read())
                {
                    return(null);
                }

                ItemData[] itemDataFromDB = this.StringToItemMetadata(reader.Get <string>("Content"));
                ItemData[] itemData       = itemDataFromDB;
                // Backward compatibility in case chests can now hold more items than before.
                if (itemDataFromDB.Length < Chest.maxItems)
                {
                    itemData = new ItemData[Chest.maxItems];
                    for (int i = 0; i < itemDataFromDB.Length; i++)
                    {
                        itemData[i] = itemDataFromDB[i];
                    }
                }

                return(new BankChestMetadata {
                    Items = itemData
                });
            };
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            Contract.Requires<ObjectDisposedException>(!this.IsDisposed);

              lock (this.workQueueLock) {
            return this.WorkQueue.EnqueueTask(() => {
              this.AddOrUpdateBankChest(key, bankChest);
            });
              }
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.AddOrUpdateBankChest(key, bankChest);
                }));
            }
        }
        public Task EnqueueUpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.UpdateBankChestItem(key, slotIndex, newItem);
                }));
            }
        }
        public Task <BankChestMetadata> EnqueueGetBankChestMetadata(BankChestDataKey key)
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);
            Contract.Requires <ArgumentException>(key != BankChestDataKey.Invalid);

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask((keyLocal) => {
                    return this.GetBankChestMetadata(keyLocal);
                }, key));
            }
        }
        public Task<BankChestMetadata> EnqueueGetBankChestMetadata(BankChestDataKey key)
        {
            Contract.Requires<ObjectDisposedException>(!this.IsDisposed);
              Contract.Requires<ArgumentException>(key != BankChestDataKey.Invalid);

              lock (this.workQueueLock) {
            return this.WorkQueue.EnqueueTask((keyLocal) => {
              return this.GetBankChestMetadata(keyLocal);
            }, key);
              }
        }
Пример #10
0
        private void UpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            BankChestMetadata bankChest = this.GetBankChestMetadata(key);

            if (bankChest == null)
            {
                throw new ArgumentException("No bank chest with the given key found.", "key");
            }

            bankChest.Items[slotIndex] = newItem;
            this.AddOrUpdateBankChest(key, bankChest);
        }
        public Task EnqueueUpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            if (base.IsDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.UpdateBankChestItem(key, slotIndex, newItem);
                }));
            }
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            if (base.IsDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.AddOrUpdateBankChest(key, bankChest);
                }));
            }
        }
        public Task <BankChestMetadata> EnqueueGetBankChestMetadata(BankChestDataKey key)
        {
            if (base.IsDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            if (!(key != BankChestDataKey.Invalid))
            {
                throw new ArgumentException();
            }

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask((keyLocal) => {
                    return this.GetBankChestMetadata(keyLocal);
                }, key));
            }
        }
Пример #14
0
        public void SetUpBankChest(TSPlayer player, DPoint tileLocation, int bankChestIndex, bool checkPermissions = false)
        {
            Contract.Requires<ArgumentNullException>(player != null);
              Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation");
              Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
              Contract.Requires<ArgumentOutOfRangeException>(bankChestIndex >= 1, "bankChestIndex");

              Tile tile = TerrariaUtils.Tiles[tileLocation];
              BlockType blockType = (BlockType)tile.type;
              if (blockType != BlockType.Chest && blockType != BlockType.Dresser)
            throw new InvalidBlockTypeException(blockType);

              if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.SetBankChests_Permission))
            throw new MissingPermissionException(ProtectorPlugin.SetBankChests_Permission);

              if (
            checkPermissions && !player.Group.HasPermission(ProtectorPlugin.NoBankChestLimits_Permision)
              ) {
            if (bankChestIndex > this.Config.MaxBankChestsPerPlayer)
              throw new ArgumentOutOfRangeException("bankChestIndex", this.Config.MaxBankChestsPerPlayer, "Global bank chest limit reached.");

            int byGroupLimit;
            if (
              this.Config.MaxBankChests.TryGetValue(player.Group.Name, out byGroupLimit) &&
              bankChestIndex > byGroupLimit
            ) {
              throw new ArgumentOutOfRangeException("bankChestIndex", byGroupLimit, "Group bank chest limit reached.");
            }
              }

              DPoint chestLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
              ProtectionEntry protection;
              lock (this.WorldMetadata.Protections)
            if (!this.WorldMetadata.Protections.TryGetValue(chestLocation, out protection))
              throw new NoProtectionException(chestLocation);

              if (!this.CheckBlockAccess(player, chestLocation, true))
            throw new TileProtectedException(chestLocation);

              if (protection.RefillChestData != null)
            throw new ChestIncompatibilityException();

              int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y);
              if (tChestIndex == -1 || Main.chest[tChestIndex] == null)
            throw new NoChestDataException(chestLocation);

              if (protection.BankChestKey != BankChestDataKey.Invalid)
            throw new ChestTypeAlreadyDefinedException();

              BankChestDataKey bankChestKey = new BankChestDataKey(player.User.ID, bankChestIndex);
              lock (this.WorldMetadata.Protections) {
            if (this.WorldMetadata.Protections.Values.Count(p => p.BankChestKey == bankChestKey) > 0)
              throw new BankChestAlreadyInstancedException();
              }

              if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.BankChestShare_Permission))
            protection.Unshare();

              Chest tChest = Main.chest[tChestIndex];
              BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(bankChestKey).Result;
              if (bankChest == null) {
            bankChest = new BankChestMetadata();
            for (int i = 0; i < Chest.maxItems; i++)
              bankChest.Items[i] = ItemData.FromItem(tChest.item[i]);

            this.ServerMetadataHandler.EnqueueAddOrUpdateBankChest(bankChestKey, bankChest);
              } else {
            for (int i = 0; i < tChest.item.Length; i++) {
              if (tChest.item[i].stack > 0)
            throw new ChestNotEmptyException(chestLocation);
            }

            for (int i = 0; i < Chest.maxItems; i++)
              tChest.item[i] = bankChest.Items[i].ToItem();
              }

              protection.BankChestKey = bankChestKey;
        }
Пример #15
0
        public void SetUpBankChest(TSPlayer player, DPoint tileLocation, int bankChestIndex, bool checkPermissions = false)
        {
            Contract.Requires <ArgumentNullException>(player != null);
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation");
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
            Contract.Requires <ArgumentOutOfRangeException>(bankChestIndex >= 1, "bankChestIndex");

            Tile      tile      = TerrariaUtils.Tiles[tileLocation];
            BlockType blockType = (BlockType)tile.type;

            if (blockType != BlockType.Chest && blockType != BlockType.Dresser)
            {
                throw new InvalidBlockTypeException(blockType);
            }

            if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.SetBankChests_Permission))
            {
                throw new MissingPermissionException(ProtectorPlugin.SetBankChests_Permission);
            }

            if (
                checkPermissions && !player.Group.HasPermission(ProtectorPlugin.NoBankChestLimits_Permision)
                )
            {
                if (bankChestIndex > this.Config.MaxBankChestsPerPlayer)
                {
                    throw new ArgumentOutOfRangeException("bankChestIndex", this.Config.MaxBankChestsPerPlayer, "Global bank chest limit reached.");
                }

                int byGroupLimit;
                if (
                    this.Config.MaxBankChests.TryGetValue(player.Group.Name, out byGroupLimit) &&
                    bankChestIndex > byGroupLimit
                    )
                {
                    throw new ArgumentOutOfRangeException("bankChestIndex", byGroupLimit, "Group bank chest limit reached.");
                }
            }

            DPoint          chestLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
            ProtectionEntry protection;

            lock (this.WorldMetadata.Protections)
                if (!this.WorldMetadata.Protections.TryGetValue(chestLocation, out protection))
                {
                    throw new NoProtectionException(chestLocation);
                }

            if (protection.RefillChestData != null)
            {
                throw new ChestIncompatibilityException();
            }

            IChest chest = this.ChestFromLocation(chestLocation);

            if (chest == null)
            {
                throw new NoChestDataException(chestLocation);
            }

            if (protection.BankChestKey != BankChestDataKey.Invalid)
            {
                throw new ChestTypeAlreadyDefinedException();
            }

            BankChestDataKey bankChestKey = new BankChestDataKey(player.User.ID, bankChestIndex);

            lock (this.WorldMetadata.Protections) {
                if (this.WorldMetadata.Protections.Values.Count(p => p.BankChestKey == bankChestKey) > 0)
                {
                    throw new BankChestAlreadyInstancedException();
                }
            }

            if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.BankChestShare_Permission))
            {
                protection.Unshare();
            }

            BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(bankChestKey).Result;

            if (bankChest == null)
            {
                bankChest = new BankChestMetadata();
                for (int i = 0; i < Chest.maxItems; i++)
                {
                    bankChest.Items[i] = chest[i];
                }

                this.ServerMetadataHandler.EnqueueAddOrUpdateBankChest(bankChestKey, bankChest);
            }
            else
            {
                for (int i = 0; i < Chest.maxItems; i++)
                {
                    if (chest[i].StackSize > 0)
                    {
                        throw new ChestNotEmptyException(chestLocation);
                    }
                }

                for (int i = 0; i < Chest.maxItems; i++)
                {
                    chest.SetItem(i, bankChest.Items[i]);
                }
            }

            protection.BankChestKey = bankChestKey;
        }
        private BankChestMetadata GetBankChestMetadata(BankChestDataKey key)
        {
            using (QueryResult reader = this.DbConnection.QueryReader(
            "SELECT Content FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
            key.UserId, key.BankChestIndex
              )) {
            if (!reader.Read())
              return null;

            ItemData[] itemDataFromDB = this.StringToItemMetadata(reader.Get<string>("Content"));
            ItemData[] itemData = itemDataFromDB;
            // Backward compatibility in case chests can now hold more items than before.
            if (itemDataFromDB.Length < Chest.maxItems) {
              itemData = new ItemData[Chest.maxItems];
              for (int i = 0; i < itemDataFromDB.Length; i++)
            itemData[i] = itemDataFromDB[i];
            }

            return new BankChestMetadata { Items = itemData };
              };
        }
        public Task EnqueueUpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            Contract.Requires<ObjectDisposedException>(!this.IsDisposed);

              lock (this.workQueueLock) {
            return this.WorkQueue.EnqueueTask(() => {
              this.UpdateBankChestItem(key, slotIndex, newItem);
            });
              }
        }
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                BankChestDataKey key = (BankChestDataKey)value;

                writer.WriteValue(string.Format("{0}, {1}", key.UserId, key.BankChestIndex));
            }
        private void AddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            bool insertRequired;
              using (QueryResult reader = this.DbConnection.QueryReader(
            "SELECT COUNT(UserId) AS Count FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
            key.UserId, key.BankChestIndex
              )) {
            if (!reader.Read())
              throw new InvalidOperationException("Unexpected data were returned.");

            insertRequired = (reader.Get<int>("Count") == 0);
              }

              if (insertRequired) {
            this.DbConnection.Query(
              "INSERT INTO Protector_BankChests (UserId, ChestIndex, Content) VALUES (@0, @1, @2);",
              key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
            );
              } else {
            this.DbConnection.Query(
              "UPDATE Protector_BankChests SET Content = @2 WHERE UserId = @0 AND ChestIndex = @1;",
              key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
            );
              }
        }
        private void UpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            BankChestMetadata bankChest = this.GetBankChestMetadata(key);
              if (bankChest == null)
            throw new ArgumentException("No bank chest with the given key found.", "key");

              bankChest.Items[slotIndex] = newItem;
              this.AddOrUpdateBankChest(key, bankChest);
        }