/// <summary> /// Method is invoked by Game Owner. /// </summary> /// <param name="id">stronghold ID</param> /// <returns></returns> public static void Stronghold(BigInteger id) { // Invoker has permission to execute this function? if (!Runtime.CheckWitness(GeneralContract.GameOwner)) { throw new System.Exception(); } if (id <= 0) { throw new System.Exception(); } string key = GeneralContract.STRONGHOLD_MAP + id.ToByteArray(); byte[] bytes = Storage.Get(Storage.CurrentContext, key); if (bytes.Length > 0) { throw new System.Exception(); } Stronghold stronghold = new Stronghold(); stronghold.ID = id; stronghold.Hero = 0; stronghold.CreatedBlock = Blockchain.GetHeight(); bytes = Neo.SmartContract.Framework.Helper.Serialize(stronghold); Storage.Put(Storage.CurrentContext, key, bytes); IncrementStrongholdAmount(id); }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="hero"></param> /// <param name="attackerNum"></param> /// <param name="cofferSize"></param> /// <param name="defenderObject"></param> public static void Battle(BattleLog log, Hero hero, BigInteger attackerNum, object cofferSize, BigInteger defenderObject) { // Get Hero of Defender string key; byte[] bytes; if (log.BattleType == GeneralContract.PVC) { Runtime.Log("Attack to city"); key = GeneralContract.CITY_MAP + log.DefenderObject; bytes = Storage.Get(Storage.CurrentContext, key); if (bytes.Length <= 0) { Runtime.Notify(7005); throw new Exception(); } else { City city = (City)Neo.SmartContract.Framework.Helper.Deserialize(bytes); if (city.Hero > 0 && city.Hero == attackerNum) { Runtime.Notify(7006); throw new System.Exception(); } else { byte[] feeBytes = Storage.Get(Storage.CurrentContext, GeneralContract.FEE_PVC); if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner)) { if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner2)) { Runtime.Notify(7007); throw new Exception(); } } // Increase city coffer byte[] pvcCofferAdditionBytes = Storage.Get(Storage.CurrentContext, GeneralContract.PVC_COFFER_ADDITION_AMOUNT); byte[] cofferSizeBytes = (byte[])cofferSize; if (pvcCofferAdditionBytes.Length > 0) { if (!pvcCofferAdditionBytes.Equals(cofferSizeBytes)) { Runtime.Notify(7020, cofferSizeBytes, pvcCofferAdditionBytes); throw new Exception(); } } BigInteger cofferSizeNum = (BigInteger)cofferSize; BigInteger cityCoffer = Helper.GetCoffer(defenderObject); cityCoffer = BigInteger.Add(cityCoffer, cofferSizeNum); if (log.BattleResult == GeneralContract.ATTACKER_WON) { // change city owner city.Hero = attackerNum; } else if (log.BattleResult != GeneralContract.ATTACKER_LOSE) { Runtime.Notify(7008); throw new Exception(); } Helper.SetCoffer(defenderObject, cityCoffer); key = GeneralContract.CITY_MAP + log.DefenderObject; bytes = Neo.SmartContract.Framework.Helper.Serialize(city); Storage.Put(Storage.CurrentContext, key, bytes); } } } else if (log.BattleType == GeneralContract.PVP) { key = GeneralContract.STRONGHOLD_MAP + log.DefenderObject; bytes = Storage.Get(Storage.CurrentContext, key); if (bytes.Length <= 0) { Runtime.Notify(7009); throw new Exception(); } else { Runtime.Log("Stronghold is on blockchain"); if (hero.StrongholdsAmount > 0) { Runtime.Notify(7010); throw new Exception(); } Stronghold stronghold = (Stronghold)Neo.SmartContract.Framework.Helper.Deserialize(bytes); if (stronghold.Hero > 0 && stronghold.Hero == attackerNum) { Runtime.Notify(7010); throw new Exception(); } else { Runtime.Log("Stronghold is not owned by player"); byte[] feeBytes = Storage.Get(Storage.CurrentContext, GeneralContract.FEE_PVP); if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner)) { if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner2)) { Runtime.Notify(7012); throw new Exception(); } } Runtime.Log("Attachment is included"); if (log.BattleResult == GeneralContract.ATTACKER_WON) { // Can not compile // byte[] oldHeroIdBytes = stronghold.Hero.ToByteArray(); BigInteger oldHeroKeyInt = stronghold.Hero; byte[] oldHeroIdBytes = oldHeroKeyInt.ToByteArray(); string oldHeroKey = GeneralContract.HERO_MAP + oldHeroIdBytes; byte[] oldHeroBytes = Storage.Get(oldHeroKey); if (oldHeroKeyInt > 0 && oldHeroIdBytes.Length <= 0) { Runtime.Notify(7022); throw new Exception(); } else if (oldHeroKeyInt > 0) { Hero oldHero = (Hero)Neo.SmartContract.Framework.Helper.Deserialize(oldHeroBytes); oldHero.StrongholdsAmount = BigInteger.Subtract(oldHero.StrongholdsAmount, 1); oldHeroBytes = Neo.SmartContract.Framework.Helper.Serialize(oldHero); Storage.Put(oldHeroKey, oldHeroBytes); } // change stronghold owner stronghold.Hero = attackerNum; stronghold.CreatedBlock = Blockchain.GetHeight(); hero.StrongholdsAmount = BigInteger.Add(hero.StrongholdsAmount, 1); string heroKey = GeneralContract.HERO_MAP + log.Attacker; byte[] heroBytes = Neo.SmartContract.Framework.Helper.Serialize(hero); Storage.Put(Storage.CurrentContext, heroKey, heroBytes); } else if (log.BattleResult != GeneralContract.ATTACKER_LOSE) { Runtime.Notify(7013); throw new Exception(); } Runtime.Log("Stronghold attack data prepared"); key = GeneralContract.STRONGHOLD_MAP + log.DefenderObject; bytes = Neo.SmartContract.Framework.Helper.Serialize(stronghold); Storage.Put(Storage.CurrentContext, key, bytes); Runtime.Log("Stronghold data inserted"); } } } else if (log.BattleType == GeneralContract.PVE) { Runtime.Log("Bandit camp attack"); key = GeneralContract.BANDIT_CAMP_MAP + log.DefenderObject; bytes = Storage.Get(Storage.CurrentContext, key); if (bytes.Length <= 0) { Runtime.Notify(7014); throw new System.Exception(); } else { Runtime.Log("Bandit camp on blockchain"); byte[] feeBytes = Storage.Get(Storage.CurrentContext, GeneralContract.FEE_PVE); if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner)) { if (!GeneralContract.AttachmentExistAB(feeBytes, GeneralContract.GameOwner2)) { Runtime.Notify(7015); throw new Exception(); } } BigInteger exp = (BigInteger)cofferSize; if (log.BattleResult == GeneralContract.ATTACKER_WON) { Runtime.Log("Bandit camp attacker won"); UpdateItemStats(log.AttackerItem1, log.AttackerItem2, log.AttackerItem3, log.AttackerItem4, log.AttackerItem5, log.BattleId, exp); } else if (log.BattleResult == GeneralContract.ATTACKER_LOSE) { Runtime.Log("Bandit camp attacker Lose"); UpdateItemStats(log.AttackerItem1, log.AttackerItem2, log.AttackerItem3, log.AttackerItem4, log.AttackerItem5, log.BattleId, exp); } else { Runtime.Notify(7016); throw new System.Exception(); } } } else { Runtime.Notify(7017); throw new Exception(); } Runtime.Log("Battle typ specific data change finished"); log.Time = Blockchain.GetHeader(Blockchain.GetHeight()).Timestamp; Runtime.Log("Battle data returned"); string battleIdKey = GeneralContract.BATTLE_LOG_MAP + log.BattleId; byte[] battleLogBytes = Storage.Get(Storage.CurrentContext, battleIdKey); battleLogBytes = Neo.SmartContract.Framework.Helper.Serialize(log); Storage.Put(Storage.CurrentContext, battleIdKey, battleLogBytes); Runtime.Log("Battle Data on blockchain"); Runtime.Notify(7000, log.BattleId, log.BattleResult, log.BattleType, log.Attacker, log.AttackerTroops, log.AttackerRemained, log.AttackerItem1, log.AttackerItem2, log.AttackerItem3, log.AttackerItem4, log.AttackerItem5, log.DefenderObject, log.DefenderTroops, log.DefenderRemained); }