private static User GetUserById(BigInteger id) { byte[] userData = NuIO.GetStorageWithKeyPath(Global.keyAcc, id.AsByteArray().AsString()); User user = (User)userData.Deserialize(); return(user); }
// 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); } } }
/** * Comfirmation for the completion of the purchase */ public static BigInteger PostPurchaseDone(BigInteger buyerID, BigInteger purchaseId, BigInteger stars, String comment) { byte[] purData = NuIO.GetStorageWithKeyPath(Global.keyPurchase, purchaseId.AsByteArray().AsString()); byte[] buyerData = NuIO.GetStorageWithKeyPath(Global.keyAcc, buyerID.AsByteArray().AsString()); if (purData.Length == 0 || buyerData.Length == 0) { return(0); } else { Purchase purchase = (Purchase)purData.Deserialize(); if (purchase.finished) { return(0); } else { User escrow = GetEscrow(); User seller = GetUserById(purchase.sellerId); escrow.balance -= purchase.amount; seller.balance += purchase.amount; purchase.comment = comment; purchase.stars = stars; purchase.finished = true; NuIO.SetStorageWithKeyPath(escrow.Serialize(), Global.keyAcc, "0"); NuIO.SetStorageWithKeyPath(seller.Serialize(), Global.keyAcc, purchase.sellerId.AsByteArray().AsString()); NuIO.SetStorageWithKeyPath(purchase.Serialize(), Global.keyPurchase, purchaseId.AsByteArray().AsString()); return(purchaseId); } } }
//唯有特权帐户允许从NASDAQ收盘价格读取数据后更新新增Pimetal的布置 //在同一周期里对每种丕料pimetalId可以有invokeTime次调用或修改(如果错误的话) public static byte[] AllocatePimetal(BigInteger pimetalId, BigInteger invokeTime) { if (Runtime.CheckWitness(Owner)) { BigInteger yearNext = GetYear() + 1; Transaction tx = (Transaction)ExecutionEngine.ScriptContainer; byte[] thisData = tx.Hash; //使用TxId生成随机数 int startIndex = (int)invokeTime * thisData.Length; byte[] totalData = NuIO.GetStorageWithKeyPath(keyPimetal, Op.BigInt2String(pimetalId)); byte[] startData = Op.SubBytes(totalData, 0, startIndex); byte[] endData = Op.SubBytes(totalData, startIndex + thisData.Length, totalData.Length - startIndex - thisData.Length); byte[] newData = Op.JoinByteArray(startData, thisData, endData); NuIO.SetStorageWithKeyPath(newData, keyPimetal, Op.BigInt2String(pimetalId)); return(NuTP.RespDataSuccess()); } else { return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Unauthorized)); } }
public static User ReadUser(byte[] id) { return(Bytes2User(NuIO.GetStorageWithKeyPath("u", Op.Bytes2String(id)))); }
public static Card ReadCard(byte[] id) { byte[] data = NuIO.GetStorageWithKeyPath("c", Op.Bytes2String(id)); return(Bytes2Card(data)); }
public static Entry GetEntry(BigInteger gameId, BigInteger entryId) { return((Entry)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId), Global.keyEntry, Op.BigInt2String(entryId)).Deserialize()); }
public static Game GetGame(BigInteger gameId) { return((Game)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId)).Deserialize()); }
public static byte[] Collect(byte[] invoker, BigInteger type, byte[] location) { byte[] invalidLoc = new byte[4] { 0, 0, 0, 0 }; if (location == invalidLoc) { return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest)); } int typePimetal = 99; for (int i = 0; i < 3; i++) { byte[] locs = NuIO.GetStorageWithKeyPath(keyPimetal, Op.BigInt2String(type)); //byte[] locs = Storage.Get(Storage.CurrentContext, keyPimetal + i); for (int j = 0; j < locs.Length; j += 4) { if (locs[j] == location[0] && locs[j + 1] == location[1] && locs[j + 2] == location[2] && locs[j + 3] == location[3]) { typePimetal = i; //更新该处内存为00 byte[] newData = new byte[0]; for (int k = 0; k < locs.Length; k++) { if (k < j || k >= j + 3) { byte[] newVal = new byte[1] { locs[k] }; newData = Op.JoinTwoByteArray(newData, newVal); //newData = newData.Concat(newVal); } else if (k < j + 3) { byte[] newVal = new byte[1] { 0 }; newData = Op.JoinTwoByteArray(newData, newVal); } } NuIO.SetStorageWithKeyPath(newData, keyPimetal, Op.BigInt2String(typePimetal)); break; } } } Player player = FindPlayer(invoker); if (typePimetal == 99) { return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest)); //非法输入值 } if (typePimetal == Pimetal.Water) { player.water += 1; } else if (typePimetal == Pimetal.Soil) { player.soil += 1; } else if (typePimetal == Pimetal.Wind) { player.wind += 1; } else if (typePimetal == Pimetal.Fire) { player.fire += 1; } byte[] newPlayerData = Player2Bytes(player); NuIO.SetStorageWithKeyPath(newPlayerData, keyPimetal, Op.BigInt2String(typePimetal)); return(NuTP.RespDataSuccess()); }
public static Player FindPlayer(byte[] addr) { byte[] data = NuIO.GetStorageWithKeyPath(keyPlayer, Op.Bytes2String(addr)); return(Bytes2Player(data)); }
private static byte[] GetQuiz(BigInteger quizID) { byte[] qdata = NuIO.GetStorageWithKeyPath("quiz", Op.BigInt2String(quizID)); return(NuTP.RespDataSucWithBody(qdata)); }