Esempio n. 1
0
        private static void UpdateItemStats(byte[] id1, byte[] id2, byte[] id3, byte[] id4, byte[] id5,
                                            byte[] battleId, BigInteger exp)
        {
            byte[][]   upgradableItem   = new byte[5][];
            BigInteger upgradableAmount = 0;

            int checkedIndex = 0;

            if (IsUpgradableItem(id1))
            {
                upgradableItem[checkedIndex] = id1; checkedIndex++;
                upgradableAmount             = BigInteger.Add(upgradableAmount, 1);
            }
            if (IsUpgradableItem(id2))
            {
                upgradableItem[checkedIndex] = id2; checkedIndex++;
                upgradableAmount             = BigInteger.Add(upgradableAmount, 1);
            }
            if (IsUpgradableItem(id3))
            {
                upgradableItem[checkedIndex] = id3; checkedIndex++;
                upgradableAmount             = BigInteger.Add(upgradableAmount, 1);
            }
            if (IsUpgradableItem(id4))
            {
                upgradableItem[checkedIndex] = id4; checkedIndex++;
                upgradableAmount             = BigInteger.Add(upgradableAmount, 1);
            }
            if (IsUpgradableItem(id5))
            {
                upgradableItem[checkedIndex] = id5; checkedIndex++;
                upgradableAmount             = BigInteger.Add(upgradableAmount, 1);
            }

            if (upgradableAmount == 0)
            {
                return;
            }

            BigInteger randomUpgradableIndex = GeneralContract.GetRandomNumber(0, (ulong)upgradableAmount);

            byte[] itemId = Helper.GetIdByIndex(upgradableItem, upgradableAmount, randomUpgradableIndex);
            if (itemId.Length <= 0)
            {
                Runtime.Log("Random generated is 0");
                return;
            }

            string key = GeneralContract.ITEM_MAP + itemId;

            Item item = (Item)Neo.SmartContract.Framework.Helper.Deserialize(Storage.Get(Storage.CurrentContext, key));

            // Increase XP that represents on how many items the Item was involved
            item.XP = BigInteger.Add(item.XP, exp);

            // Increase Level
            if (item.LEVEL == 0 && item.XP >= 2 ||
                item.LEVEL == 1 && item.XP >= 6 ||
                item.LEVEL == 2 && item.XP >= 20 ||
                item.LEVEL == 3 && item.XP >= 48 ||
                item.LEVEL == 4 && item.XP >= 92 ||
                item.LEVEL == 5 && item.XP >= 152 ||
                item.LEVEL == 6 && item.XP >= 228 ||
                item.LEVEL == 7 && item.XP >= 318 ||
                item.LEVEL == 8 && item.XP >= 434 ||
                item.LEVEL == 9 && item.XP >= 580
                )
            {
                item.LEVEL      = item.LEVEL + 1;
                item.STAT_VALUE = item.STAT_VALUE + 1;
            }

            // Put back On Storage the Item with increased values
            byte[] bytes = Neo.SmartContract.Framework.Helper.Serialize(item);
            Storage.Put(Storage.CurrentContext, key, bytes);

            Runtime.Log("Item exp increased");
            Runtime.Notify(7019, itemId, battleId, exp, item.LEVEL, item.STAT_VALUE, item.XP);
        }
Esempio n. 2
0
        /**
         * Function records item drop
         *
         * Function drops item in every 120 blocks. Usually called by Server Side of Blocklords.
         *
         * Has 0 argument
         */
        public static void SimpleDropItem(byte[] itemId, object strongholdAmountObj, object dropIntervalObj)
        {
            if (!Runtime.CheckWitness(GeneralContract.GameOwner))
            {
                Runtime.Notify(16);
                throw new System.Exception();
            }

            DropData lastDrop = new DropData();

            lastDrop.Block        = 0;
            lastDrop.StrongholdId = 0;

            byte[] lastDropBytes = Storage.Get(Storage.CurrentContext, GeneralContract.LAST_ITEM_DROP);
            if (lastDropBytes.Length > 0)
            {
                lastDrop = (DropData)Neo.SmartContract.Framework.Helper.Deserialize(lastDropBytes);
            }

            byte[] dropIntervalSettingBytes = Storage.Get(Storage.CurrentContext, GeneralContract.INTERVAL_DROP);
            byte[] dropIntervalBytes        = (byte[])dropIntervalObj;

            if (dropIntervalSettingBytes.Length > 0)
            {
                if (!dropIntervalSettingBytes.Equals(dropIntervalBytes))
                {
                    Runtime.Notify(7);
                    throw new System.Exception();
                }
            }

            BigInteger dropInterval = (BigInteger)dropIntervalObj;

            if (Blockchain.GetHeight() <= dropInterval + lastDrop.Block)
            {
                Runtime.Notify(5001);
                throw new System.Exception();
            }

            byte[] strongholdsAmountSettingBytes = Storage.Get(Storage.CurrentContext, GeneralContract.AMOUNT_STRONGHOLDS);
            byte[] strongholdsAmountBytes        = (byte[])strongholdAmountObj;

            if (!strongholdsAmountSettingBytes.Equals(strongholdsAmountBytes))
            {
                Runtime.Notify(8);
                throw new System.Exception();
            }

            BigInteger strongholdsAmount = (BigInteger)strongholdAmountObj;

            string     key;
            Stronghold stronghold;

            byte[] bytes;


            // Check that Item has no owner and that is is on stronghold reward batch
            string itemKey = GeneralContract.ITEM_MAP + itemId;

            bytes = Storage.Get(Storage.CurrentContext, itemKey);
            if (bytes.Length <= 0)
            {
                Runtime.Notify(1005);
                throw new System.Exception();
            }
            Item item = (Item)Neo.SmartContract.Framework.Helper.Deserialize(bytes);

            if (item.BATCH != GeneralContract.STRONGHOLD_REWARD_BATCH)
            {
                Runtime.Notify(5002);
                throw new System.Exception();
            }

            // returned an index on list of available strongholds ids
            BigInteger random = GeneralContract.GetRandomNumber(0, strongholdsAmount);

            random = BigInteger.Add(random, 1);

            key   = GeneralContract.STRONGHOLD_MAP + random.ToByteArray();
            bytes = Storage.Get(Storage.CurrentContext, key);
            if (bytes.Length <= 0)
            {
                // Delete Item
                //Storage.Delete(Storage.CurrentContext, itemKey);
                Runtime.Notify(5003);//, itemId, random, 0, Blockchain.GetHeight());
                throw new System.Exception();
            }
            else
            {
                stronghold = (Stronghold)Neo.SmartContract.Framework.Helper.Deserialize(bytes);

                BigInteger lordId = stronghold.Hero;
                if (lordId <= 0)
                {
                    // Delete Item
                    Storage.Delete(Storage.CurrentContext, itemKey);

                    // Record the match
                    lastDrop.Block        = Blockchain.GetHeight();
                    lastDrop.HeroId       = 0;
                    lastDrop.ItemId       = itemId;
                    lastDrop.StrongholdId = random;

                    lastDropBytes = Neo.SmartContract.Framework.Helper.Serialize(lastDrop);

                    Storage.Put(Storage.CurrentContext, GeneralContract.LAST_ITEM_DROP, lastDropBytes);

                    Runtime.Notify(5004, itemId, random, 0, Blockchain.GetHeight());
                }
                else
                {
                    string heroKey = GeneralContract.HERO_MAP + lordId.ToByteArray();
                    Hero   hero    = (Hero)Neo.SmartContract.Framework.Helper.Deserialize(Storage.Get(Storage.CurrentContext, heroKey));

                    // Change owner of Item.
                    item.HERO  = hero.ID;
                    item.BATCH = GeneralContract.NO_BATCH;
                    byte[] itemBytes = Neo.SmartContract.Framework.Helper.Serialize(item);

                    // Save Item after change of ownership
                    Storage.Put(Storage.CurrentContext, itemKey, itemBytes);

                    // Kick out a lord from stronghold.
                    stronghold.Hero         = 0;
                    stronghold.CreatedBlock = Blockchain.GetHeight();
                    byte[] strongholdBytes = Neo.SmartContract.Framework.Helper.Serialize(stronghold);
                    Storage.Put(Storage.CurrentContext, key, strongholdBytes);

                    lastDrop.Block        = Blockchain.GetHeight();
                    lastDrop.HeroId       = hero.ID;
                    lastDrop.ItemId       = itemId;
                    lastDrop.StrongholdId = random;

                    lastDropBytes = Neo.SmartContract.Framework.Helper.Serialize(lastDrop);

                    Storage.Put(Storage.CurrentContext, GeneralContract.LAST_ITEM_DROP, lastDropBytes);

                    // Save hero with the mark that he lost his stronghold
                    hero.StrongholdsAmount = BigInteger.Subtract(hero.StrongholdsAmount, 1);
                    byte[] heroBytes = Neo.SmartContract.Framework.Helper.Serialize(hero);
                    Storage.Put(heroKey, heroBytes);

                    Runtime.Notify(5000, itemId, lastDrop.StrongholdId, lastDrop.HeroId, lastDrop.Block);
                }
            }
        }