/** * Deploy or reset the game world. Only the owner account can do it. */ private static bool Deploy() { BigInteger i = 0; NuIO.SetStorageWithKey(Global.keyNumGames, Op.BigInt2Bytes(i)); return(true); }
//创世初始化 public static byte[] Genesis() { if (Runtime.CheckWitness(Owner)) { //年份实际从1开始。第一区块无丕料 byte[] startYear = new byte[1] { 1 }; NuIO.SetStorageWithKey(keyYear, startYear); //Storage.Put(Storage.CurrentContext, keyYear, 0); //年份为0 BigInteger water = 100; BigInteger soil = 100; BigInteger wind = 100; BigInteger fire = 100; AllocatePimetal(Pimetal.Water, water); AllocatePimetal(Pimetal.Soil, soil); AllocatePimetal(Pimetal.Wind, wind); AllocatePimetal(Pimetal.Fire, fire); return(NuTP.RespDataSuccess()); } else { return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Unauthorized)); } }
void TestLocalStorage() { byte[] key = Encoding.UTF8.GetBytes("name"); byte[] name = Encoding.UTF8.GetBytes("terrence"); NuIO.SetStorageWithKey(key, name); byte[] nameResult = NuIO.GetStorageWithKey(key); string strName = Encoding.UTF8.GetString(nameResult); Debug.Log(strName); }
// public static BigInteger PostPurchase(BigInteger buyerId, BigInteger sellerId, BigInteger prodID, BigInteger num) { byte[] buyerData = NuIO.GetStorageWithKeyPath(Global.keyAcc, buyerId.AsByteArray().AsString()); byte[] sellerData = NuIO.GetStorageWithKeyPath(Global.keyAcc, sellerId.AsByteArray().AsString()); byte[] prodData = NuIO.GetStorageWithKeyPath(Global.keyProd, prodID.AsByteArray().AsString()); if (buyerData.Length == 0 || sellerData.Length == 0 || prodData.Length == 0) { return(0); } else { BigInteger nowNum = NuIO.GetStorageWithKey(Global.keyNumPurchases).AsBigInteger() + 1; Product product = (Product)prodData.Deserialize(); User buyer = (User)buyerData.Deserialize(); BigInteger cost = product.price * num; if (cost > buyer.balance) { return(0); } else { User escrow = GetEscrow(); escrow.balance += cost; buyer.balance -= cost; Purchase purchase = new Purchase() { index = nowNum, buyerId = buyerId, sellerId = sellerId, prodId = prodID, number = num, finished = false, amount = cost }; byte[] purData = purchase.Serialize(); NuIO.SetStorageWithKeyPath(purData.Serialize(), Global.keyAcc, NumPurchase().AsByteArray().AsString()); NuIO.SetStorageWithKeyPath(escrow.Serialize(), Global.keyAcc, "0"); NuIO.SetStorageWithKey(Global.keyNumPurchases, nowNum.AsByteArray()); return(nowNum); } } }
//Create prod information public static bool PostProduct(BigInteger prodId, string desc, BigInteger price, BigInteger sellerID) { BigInteger nowNum = NuIO.GetStorageWithKey(Global.keyNumProducts).AsBigInteger() + 1; Product listing = new Product() { index = nowNum, idSeller = sellerID, description = desc, price = price }; NuIO.SetStorageWithKeyPath(listing.Serialize(), Global.keyProd, NumProducts().AsByteArray().AsString()); NuIO.SetStorageWithKey(Global.keyNumProducts, nowNum.AsByteArray()); return(true); }
//Create account information public static bool PostAccount(byte[] addr, string name, BigInteger balance) { BigInteger nowNum = NuIO.GetStorageWithKey(Global.keyNumAccounts).AsBigInteger() + 1; User user = new User() { index = nowNum, name = name, balance = balance, address = addr }; NuIO.SetStorageWithKeyPath(user.Serialize(), Global.keyAcc, NumAccounts().AsByteArray().AsString()); NuIO.SetStorageWithKey(Global.keyNumAccounts, nowNum.AsByteArray()); return(true); }
/** * Start a new game. Only the owner account can do it. */ private static bool StartGame() { BigInteger num = Op.Bytes2BigInt(NuIO.GetStorageWithKey(Global.keyNumGames)); Game game = new Game(); BigInteger currentHeight = Blockchain.GetHeight(); game.heightStage1 = currentHeight + Global.Stage1Height; game.heightStage2 = currentHeight + Global.Stage2Height; game.numEntries = 0; game.isFinalized = false; byte[] data = game.Serialize(); BigInteger gameid = NumGames(); NuIO.SetStorageWithKeyPath(data, Global.keyGame, Op.BigInt2String(gameid)); NuIO.SetStorageWithKey(Global.keyNumGames, Op.BigInt2Bytes(gameid + 1)); return(true); }