Beispiel #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));
                }
            }
        }
 public static byte[] FindDataSiege(BigInteger serverId, byte[] seigeId)
 {
     return(IO.GetStorageWithKeyPath(
                Const.preServer,
                Op.BigInt2String(serverId),
                Const.preSeige,
                Op.Bytes2String(seigeId)
                ));
 }
 public static byte[] FindDataCity(BigInteger serverId, BigInteger cityId)
 {
     return(IO.GetStorageWithKeyPath(
                Const.preServer,
                Op.BigInt2String(serverId),
                Const.preCity,
                Op.BigInt2String(cityId)
                ));
 }
 public static byte SaveCity(BigInteger serverID, City city)
 {
     return(IO.SetStorageWithKeyPath(City2Bytes(city)
                                     , Const.preServer
                                     , Op.BigInt2String(serverID)
                                     , Const.preCity
                                     , Op.BigInt2String(city.cityId)
                                     ));
 }
 public static BigInteger NumCitySiegeHistory(BigInteger serverId, BigInteger cityId)
 {
     return(Op.Bytes2BigInt(
                IO.GetStorageWithKeyPath(
                    Const.preServer, Op.BigInt2String(serverId),
                    Const.preCity, Op.BigInt2String(cityId),
                    Const.keyTotSiege
                    )
                ));
 }
 public static byte SaveSiege(Siege siege)
 {
     byte[] data = Seige2Bytes(siege);
     return(IO.SetStorageWithKeyPath(data,
                                     Const.preServer,
                                     Op.BigInt2String(siege.serverID),
                                     Const.preCity,
                                     Op.BigInt2String(siege.cityID)
                                     ));
 }
        public static byte[] FindSiegeDataByCityHistId(BigInteger serverId, BigInteger cityId, BigInteger histId)
        {
            byte[] siegeId = IO.GetStorageWithKeyPath(
                Const.preServer, Op.BigInt2String(serverId),
                Const.preCity, Op.BigInt2String(cityId),
                Const.preSeige, Op.BigInt2String(histId)
                );

            return(FindDataSiege(serverId, siegeId));
        }
        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);
        }
Beispiel #9
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);
        }
Beispiel #10
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));
            }
        }
Beispiel #11
0
 public static Entry GetEntry(BigInteger gameId, BigInteger entryId)
 {
     return((Entry)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId), Global.keyEntry, Op.BigInt2String(entryId)).Deserialize());
 }
Beispiel #12
0
 public static Game GetGame(BigInteger gameId)
 {
     return((Game)NuIO.GetStorageWithKeyPath(Global.keyGame, Op.BigInt2String(gameId)).Deserialize());
 }
Beispiel #13
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());
        }
Beispiel #14
0
 private static byte[] GetQuiz(BigInteger quizID)
 {
     byte[] qdata = NuIO.GetStorageWithKeyPath("quiz", Op.BigInt2String(quizID));
     return(NuTP.RespDataSucWithBody(qdata));
 }