/**Generate a random card*/ public static byte[] RandomCard(byte[] ownerId, byte[] cardId, string name) { Card cardOrig = ReadCard(cardId); if (cardOrig == null) { byte[] newLvData = Hash256(Op.JoinTwoByteArray(ownerId, Op.BigInt2Bytes(Blockchain.GetHeight()))); Card card = new Card { id = cardId, name = name, birthBlock = Blockchain.GetHeight(), level = Op.Bytes2BigInt(newLvData) % 3, ownerId = ownerId, isLive = true }; SaveCard(card); return(NuTP.RespDataSucWithBody(Card2Bytes(card))); } else { return(NuTP.RespDataWithCode(Error.Dom, Error.AlreadyExist)); } }
/** * 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); }
/// <summary> /// 给指定用户产生随机初始卡 /// 返回实际生成的卡牌数量 /// </summary> public static Card[] GenerateRandomCards(User user, int num) { Card[] cards = new Card[num]; int numCardsAlready = 0; if (user.cards != null && user.cards.Length > 0) { numCardsAlready = user.cards.Length; } byte[] dHeight = Op.BigInt2Bytes(Blockchain.GetHeight()); byte[] salt = Rand(); for (int i = 0; i < num; i++) { Card cardResult = new Card(); byte[] dEmail = Op.String2Bytes(user.email); byte[] dNum = Op.BigInt2Bytes(i + numCardsAlready); cardResult.cardID = Random(Op.JoinByteArray(dEmail, dNum, dHeight), 10); cardResult.type = Op.Bytes2BigInt(Hash160(cardResult.cardID)) % TypeArmy.TypeCount; cardResult.lvls = Random(salt, Const.numCellsOfCard); cardResult.ownerEmail = user.email; cardResult.warPos = 0; cards[i] = cardResult; RW.SaveCard(cardResult); } return(cards); }
public static object Main(string operation, params object[] args) { BigInteger a = (BigInteger)args[0]; BigInteger b = (BigInteger)args[1]; return(NuTP.RespDataSucWithBody(Op.BigInt2Bytes(a + b))); }
//public static byte[] SegByte(byte b) => new byte[2] { 1, b }; public static byte[] Seg(byte[] body) { BigInteger rem = (body.Length) % 255; byte[] r = new byte[0]; for (int i = 0; i < body.Length / 255; i++) { r = Op.JoinTwoByteArray(r, new byte[1] { 255 }); } return(Op.JoinTwoByteArray(Op.JoinTwoByteArray(r, Op.BigInt2Bytes(rem)), body)); }
public static BigInteger AddSiegeCityHist(BigInteger serverId, BigInteger cityId, byte[] siegeId) { BigInteger histId = NumCitySiegeHistory(serverId, cityId); IO.SetStorageWithKeyPath(siegeId, Const.preServer, Op.BigInt2String(serverId), Const.preCity, Op.BigInt2String(cityId), Const.preSeige, Op.BigInt2String(histId) ); IO.SetStorageWithKeyPath(Op.BigInt2Bytes(histId + 1), Const.preServer, Op.BigInt2String(serverId), Const.preCity, Op.BigInt2String(cityId), Const.keyTotSiege ); return(histId); }
public void TestOp1() { BigInteger i1 = 1; BigInteger i2 = 12102159; bool b1 = true; bool b2 = false; string s1 = ""; string s2 = "adfeav"; byte[] bi1 = Op.BigInt2Bytes(i1); byte[] bi2 = Op.BigInt2Bytes(i2); byte[] bb1 = Op.Bool2Bytes(b1); byte[] bb2 = Op.Bool2Bytes(b2); byte[] bs1 = Op.String2Bytes(s1); byte[] bs2 = Op.String2Bytes(s2); Assert.AreNotEqual(bi1, bi2); }
/** * 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); }
//public static byte[] SegByte(byte b) => new byte[2] { 1, b }; public static byte[] Seg(byte[] body) { //if body = Op.Void: rem = 0; BigInteger rem = (body.Length) % 255; byte[] r = Op.Void; for (int i = 0; i < body.Length / 255; i++) { r = Op.JoinTwoByteArray(r, new byte[1] { 255 }); } byte[] remBA = Op.BigInt2Bytes(rem); if (rem >= 128 && rem < 256) { remBA = Op.SubBytes(remBA, 0, 1); } return(Op.JoinByteArray(r, remBA, body)); }
public static bool TestOp() { bool result = true; BigInteger integer = 15; byte b = Op.BigInt2Bytes(integer)[0]; result = Op.And(result, b == 15); BigInteger integer1 = 255; byte b1 = Op.BigInt2Bytes(integer1)[0]; result = Op.And(result, b == 255); int i = 2; string s = "c"; string o = s + i.ToString(); result = Op.And(result, o == "c2"); return(result); }
public void TestOpNull() { /** * Default Values: * For the type BigInteger and String, if the value is empty (zero byte), the logic should convert them to the default value. * Specifically, * - the default value of bool is false * - the default value of BigInteger is 0 * - the default value of String is "\0" * * To convert the default value back to byte[], the result would be "0x00" rather than null. */ byte[] nullBA = new byte[0]; byte[] zBA = new byte[1] { 0x00 }; //----- Bool ------- bool b1 = false; Assert.AreEqual(b1, Op.Bytes2Bool(nullBA)); Assert.AreEqual(Op.Bool2Bytes(b1), zBA); //----- BigInt ------- BigInteger big = Op.Bytes2BigInt(nullBA); BigInteger bp1 = big + 1; Assert.AreEqual(big, new BigInteger(0)); Assert.AreEqual(bp1, new BigInteger(1)); Assert.AreEqual(Op.BigInt2Bytes(big), zBA); //----- String ------- String str = Op.Bytes2String(nullBA); Assert.AreEqual(str, "\0"); Assert.AreEqual(Op.String2Bytes(str), zBA); }
public static byte[] SegInt(BigInteger body) => Seg(Op.BigInt2Bytes(body));