Example #1
0
        //创世初始化
        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));
            }
        }
Example #2
0
        /**
         *  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);
        }
Example #3
0
        private static User GetUserById(BigInteger id)
        {
            byte[] userData = NuIO.GetStorageWithKeyPath(Global.keyAcc, id.AsByteArray().AsString());
            User   user     = (User)userData.Deserialize();

            return(user);
        }
Example #4
0
        /**
         *  In the 1st stage of each game, players are only allowed to submit the
         *  hidden number's hash, rather than it hidden value itself.
         */
        public static byte[] PutEntry(BigInteger gameId, byte[] address, byte[] pick, byte[] hiddenHash)
        {
            Game game = GetGame(gameId);

            if (game == null)
            {
                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
            }
            else
            {
                if (Blockchain.GetHeight() >= game.heightStage1)
                {
                    return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Forbidden));;
                }
                else
                {
                    Entry entry = new Entry();
                    entry.gameId     = gameId;
                    entry.address    = address;
                    entry.pick       = pick;
                    entry.hiddenHash = hiddenHash;
                    byte[]     data = entry.Serialize();
                    BigInteger id   = NumEntries(gameId);
                    NuIO.SetStorageWithKeyPath(data, Global.keyGame, Op.BigInt2String(gameId), Global.keyEntry, Op.BigInt2String(id));

                    game.numEntries += 1;
                    NuIO.SetStorageWithKeyPath(game.Serialize(), Global.keyGame, Op.BigInt2String(gameId));

                    return(NuTP.RespDataSucWithBody(data));
                }
            }
        }
Example #5
0
    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);
    }
Example #6
0
        //
        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);
                }
            }
        }
Example #7
0
        //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);
        }
Example #8
0
        //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);
        }
Example #9
0
        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);
        }
Example #10
0
        /**
         *  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);
        }
Example #11
0
        /**
         *  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);
                }
            }
        }
Example #12
0
        //唯有特权帐户允许从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));
            }
        }
Example #13
0
 public static Player FindPlayer(byte[] addr)
 {
     byte[] data = NuIO.GetStorageWithKeyPath(keyPlayer, Op.Bytes2String(addr));
     return(Bytes2Player(data));
 }
Example #14
0
 public static Entry GetEntry(BigInteger gameId, BigInteger entryId)
 {
     return((Entry)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId), Global.keyEntry, Op.BigInt2String(entryId)).Deserialize());
 }
Example #15
0
 public static Game GetGame(BigInteger gameId)
 {
     return((Game)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId)).Deserialize());
 }
Example #16
0
 public static BigInteger NumGames()
 {
     return(Op.Bytes2BigInt(NuIO.GetStorageWithKey(Global.keyNumGames)));
 }
Example #17
0
        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());
        }
Example #18
0
 private static byte[] GetQuiz(BigInteger quizID)
 {
     byte[] qdata = NuIO.GetStorageWithKeyPath("quiz", Op.BigInt2String(quizID));
     return(NuTP.RespDataSucWithBody(qdata));
 }
Example #19
0
 public static byte SaveUser(User user)
 {
     //KeyPath of card: /u/{user.Id}
     return(NuIO.SetStorageWithKeyPath(User2Bytes(user), "u", Op.Bytes2String(user.id)));
 }
Example #20
0
 public static byte SaveCard(Card card)
 {
     //KeyPath of card: /c/{card.Id}
     return(NuIO.SetStorageWithKeyPath(Card2Bytes(card), "c", Op.Bytes2String(card.id)));
 }
Example #21
0
 public static Card ReadCard(byte[] id)
 {
     byte[] data = NuIO.GetStorageWithKeyPath("c", Op.Bytes2String(id));
     return(Bytes2Card(data));
 }
Example #22
0
 public static BigInteger NumAccounts()
 {
     return(NuIO.GetStorageWithKey(Global.keyNumAccounts).AsBigInteger());
 }
Example #23
0
 public static User ReadUser(byte[] id)
 {
     return(Bytes2User(NuIO.GetStorageWithKeyPath("u", Op.Bytes2String(id))));
 }
Example #24
0
 public static BigInteger NumPurchase()
 {
     return(NuIO.GetStorageWithKey(Global.keyNumProducts).AsBigInteger());
 }