// Customized Deserialization for Card // The class Neunity.Adapter.Op manages type conversation for different platforms public static Card Bytes2Card(byte[] data) => new Card { type = Op.Bytes2BigInt(SD.DesegWithIdFromSeg(data, 0)), lvls = SD.DesegWithIdFromSeg(data, 1), birthBlock = Op.Bytes2BigInt(SD.DesegWithIdFromSeg(data, 2)), name = Op.Bytes2String(SD.DesegWithIdFromSeg(data, 3)), };
public static byte[] FindDataSiege(BigInteger serverId, byte[] seigeId) { return(IO.GetStorageWithKeyPath( Const.preServer, Op.BigInt2String(serverId), Const.preSeige, Op.Bytes2String(seigeId) )); }
public static War ByteArrayToWar(byte[] data) { War war = new War(); war.id = Op.Bytes2String(SD.DesegWithIdFromSeg(data, 0)); war.regEndBlock = Op.Bytes2BigInt(SD.DesegWithIdFromSeg(data, 1)); BigInteger height = Blockchain.GetHeight(); war.regLeftBlocks = war.regEndBlock - height; return(war); }
private static BigInteger Post(byte[] quizBytes) { if (GetTransReceiver() == Owner) { if (GetGASAttached() >= gasRequired) { Quiz quiz = Bytes2Quiz(quizBytes); byte[] id = Hash160(quizBytes); quiz.quizId = id; NuIO.SetStorageWithKeyPath(Quiz2Bytes(quiz), "quiz", Op.Bytes2String(id)); } } return(0); }
/** The Logic of merging the cards. The rule is: * 0. Two cards has to share the same owner * 1. Card1 has higher level than Card2 * 2. Card1 and Card2 should have the same */ public static byte[] CardMerge(byte[] cardID1, byte[] cardID2, string name) { Card card1 = ReadCard(cardID1); if (card1 == null) { return(NuTP.RespDataWithDetail(Error.Dom, Error.NoExist, Op.Bytes2String(cardID1), Op.Void())); } Card card2 = ReadCard(cardID2); if (card2 == null) { return(NuTP.RespDataWithDetail(Error.Dom, Error.NoExist, Op.Bytes2String(cardID2), Op.Void())); } if (Op.Bytes2BigInt(card1.ownerId) != Op.Bytes2BigInt(card2.ownerId)) { return(NuTP.RespDataWithCode(Error.Dom, Error.DiffOwner)); } if (card1.level >= card2.level) { BigInteger newLevel = card1.level + card2.level; Card newCard = new Card { id = Hash256(Op.JoinTwoByteArray(card1.id, card2.id)), name = name, level = newLevel, birthBlock = Blockchain.GetHeight(), ownerId = card1.ownerId, isLive = true }; SaveCard(newCard); card1.isLive = false; card2.isLive = false; SaveCard(card1); SaveCard(card2); return(NuTP.RespDataSucWithBody(Card2Bytes(newCard))); } else { return(NuTP.RespDataWithCode(Error.Dom, Error.LvInvalid)); } }
// Use this for initialization void Start() { Op.SetStoragePath(Application.persistentDataPath + "/smartcontract_data.jsn"); //MergeCards(); //TestLocalStorage(); string a = ""; string b = "1"; string c = "advda"; byte[] ba = Op.String2Bytes(a); byte[] bb = Op.String2Bytes(b); byte[] bc = Op.String2Bytes(c); string sba = Op.Bytes2String(ba); string sbb = Op.Bytes2String(bb); string sbc = Op.Bytes2String(bc); Debug.Log(""); }
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 string SplitTblStr(this byte[] table, int index) => Op.Bytes2String(DesegWithIdFromData(table, 0, index));
public static string SplitSegStr(this byte[] data, int startID) => Op.Bytes2String(DesegFromTableFromData(data, startID));
public static User ReadUser(byte[] id) { return(Bytes2User(NuIO.GetStorageWithKeyPath("u", Op.Bytes2String(id)))); }
public static byte SaveUser(User user) { //KeyPath of card: /u/{user.Id} return(NuIO.SetStorageWithKeyPath(User2Bytes(user), "u", Op.Bytes2String(user.id))); }
public static Card ReadCard(byte[] id) { byte[] data = NuIO.GetStorageWithKeyPath("c", Op.Bytes2String(id)); return(Bytes2Card(data)); }
public static byte SaveCard(Card card) { //KeyPath of card: /c/{card.Id} return(NuIO.SetStorageWithKeyPath(Card2Bytes(card), "c", Op.Bytes2String(card.id))); }
public static byte[] FindDataCard(byte[] cardID) { return(IO.GetStorageWithKeyPath(Const.preCard, Op.Bytes2String(cardID))); }
public static byte SaveCard(Card card) { return(IO.SetStorageWithKeyPath(Card2Bytes(card), Const.preCard, Op.Bytes2String(card.cardID))); }
public static Student Bytes2Student(byte[] data) => new Student { id = Op.Bytes2BigInt(SD.DesegWithIdFromSeg(data, 0)), name = Op.Bytes2String(SD.DesegWithIdFromSeg(data, 1)), balance = Op.Bytes2BigInt(SD.DesegWithIdFromSeg(data, 2)) };
public static Player FindPlayer(byte[] addr) { byte[] data = NuIO.GetStorageWithKeyPath(keyPlayer, Op.Bytes2String(addr)); return(Bytes2Player(data)); }