Пример #1
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));
                }
            }
        }
Пример #2
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);
                }
            }
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
                }
            }
        }
Пример #8
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));
            }
        }
Пример #9
0
 public static byte SaveUser(User user)
 {
     //KeyPath of card: /u/{user.Id}
     return(NuIO.SetStorageWithKeyPath(User2Bytes(user), "u", Op.Bytes2String(user.id)));
 }
Пример #10
0
 public static byte SaveCard(Card card)
 {
     //KeyPath of card: /c/{card.Id}
     return(NuIO.SetStorageWithKeyPath(Card2Bytes(card), "c", Op.Bytes2String(card.id)));
 }
Пример #11
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());
        }