Exemple #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));
            }
        }
Exemple #2
0
 public static byte[] SetWeather(byte[] weathers)
 {
     if (Runtime.CheckWitness(Owner))
     {
         RW.SaveWeathers(weathers);
         return(NuTP.RespDataSuccess());
     }
     else
     {
         return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Forbidden));
     }
 }
Exemple #3
0
 public static byte[] Genesis(BigInteger serverID)
 {
     if (Runtime.CheckWitness(Owner))
     {
         Alg.GenesisCreateCities(serverID);
         //RW.GenesisCreateFake
         return(NuTP.RespDataSuccess());
     }
     else
     {
         return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.AuthFail));
     }
 }
Exemple #4
0
        private static byte[] FakeOccupies(BigInteger serverId, string email, BigInteger cityID)
        {
            if (Runtime.CheckWitness(Owner))
            {
                if (cityID > Const.numCities || cityID <= 0)
                {
                    return(NuTP.RespDataWithCode(ErrCate.City, ErrType.NotExist));
                }
                else
                {
                    User user = RW.FindUser(email);
                    if (user != null)
                    {
                        City city = RW.FindCity(serverId, cityID);
                        if (city.ownerID.Length == 0 || city.ownerID.Length == 1) //something wrong with bytes2string/string2bytes
                        {                                                         //not work when it's not occupied
                            city.ownerID = email;
                            RW.SaveCity(serverId, city);

                            user.city = cityID;
                            RW.SaveUser(user);
                            return(NuTP.RespDataSuccess());
                        }
                        else
                        {
                            return(NuTP.RespDataWithCode(ErrCate.City, ErrType.Duplicated));
                        }
                    }
                    else
                    {
                        return(NuTP.RespDataWithCode(ErrCate.User, ErrType.NotExist));
                    }
                }
            }
            else
            {
                return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.AuthFail));
            }
        }
Exemple #5
0
        /**
         *  In the 2nd stage of each game, players submit the hidden number(prove), nobody can fake it
         *  since it must match the hash every player submitted in during first round.
         *  If a user failed to submit the prove, s/he will be elimiated from this game.
         */
        public static byte[] PutProve(BigInteger gameId, BigInteger entryId, byte[] hidden)
        {
            Entry entry = GetEntry(gameId, entryId);

            Game       game   = GetGame(gameId);
            BigInteger height = Blockchain.GetHeight();

            if (height < game.heightStage1 || height > game.heightStage2)
            {
                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Forbidden));
            }
            else
            {
                if (Hash256(hidden) == entry.hiddenHash)
                {
                    entry.hidden = hidden;
                    return(NuTP.RespDataSuccess());
                }
                else
                {
                    return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
                }
            }
        }
Exemple #6
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));
            }
        }
Exemple #7
0
        public static byte[] Upgrade(byte[] invoker, BigInteger feature)
        {
            Player     player = FindPlayer(invoker);
            BigInteger lvl    = -1;

            if (feature == 0)
            {
                lvl = player.lvlAtk;
            }
            else if (feature == 1)
            {
                lvl = player.lvlDef;
            }
            else if (feature == 2)
            {
                lvl = player.lvlSpd;
            }
            else if (feature == 3)
            {
                lvl = player.lvlHP;
            }
            else if (feature == 4)
            {
                lvl = player.lvlRev;
            }
            int f = Op.BigInt2Int(feature);

            BigInteger[] pimetals = AmountUpgrade(f, lvl + 1);
            if (player.water >= pimetals[0] && player.soil >= pimetals[1] &&
                player.wind >= pimetals[2] && player.fire >= pimetals[3])
            {
                player.water -= pimetals[0];
                player.soil  -= pimetals[1];
                player.wind  -= pimetals[2];
                player.fire  -= pimetals[3];

                if (feature == 0)
                {
                    player.lvlAtk += 1;
                }
                else if (feature == 1)
                {
                    player.lvlDef += 1;
                }
                else if (feature == 2)
                {
                    player.lvlSpd += 1;
                }
                else if (feature == 3)
                {
                    player.lvlHP += 1;
                }
                else if (feature == 4)
                {
                    player.lvlRev += 1;
                }
                return(NuTP.RespDataSuccess());
            }
            else
            {
                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
            }
        }
Exemple #8
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());
        }
 public static object Main(string operation, params object[] args)
 {
     return(NuTP.RespDataSuccess());
 }