예제 #1
0
        /**Generate a random card*/
        public static byte[] RandomCard(byte[] ownerId, byte[] cardId, string name)
        {
            Card cardOrig = ReadCard(cardId);

            if (cardOrig == null)
            {
                byte[] newLvData = Hash256(Op.JoinTwoByteArray(ownerId, Op.BigInt2Bytes(Blockchain.GetHeight())));


                Card card = new Card
                {
                    id         = cardId,
                    name       = name,
                    birthBlock = Blockchain.GetHeight(),
                    level      = Op.Bytes2BigInt(newLvData) % 3,
                    ownerId    = ownerId,
                    isLive     = true
                };
                SaveCard(card);
                return(NuTP.RespDataSucWithBody(Card2Bytes(card)));
            }
            else
            {
                return(NuTP.RespDataWithCode(Error.Dom, Error.AlreadyExist));
            }
        }
예제 #2
0
 // 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)),
 };
예제 #3
0
        /// <summary>
        /// 给指定用户产生随机初始卡
        /// 返回实际生成的卡牌数量
        /// </summary>
        public static Card[] GenerateRandomCards(User user, int num)
        {
            Card[] cards           = new Card[num];
            int    numCardsAlready = 0;

            if (user.cards != null && user.cards.Length > 0)
            {
                numCardsAlready = user.cards.Length;
            }

            byte[] dHeight = Op.BigInt2Bytes(Blockchain.GetHeight());
            byte[] salt    = Rand();
            for (int i = 0; i < num; i++)
            {
                Card   cardResult = new Card();
                byte[] dEmail     = Op.String2Bytes(user.email);
                byte[] dNum       = Op.BigInt2Bytes(i + numCardsAlready);

                cardResult.cardID = Random(Op.JoinByteArray(dEmail, dNum, dHeight), 10);

                cardResult.type = Op.Bytes2BigInt(Hash160(cardResult.cardID)) % TypeArmy.TypeCount;
                cardResult.lvls = Random(salt, Const.numCellsOfCard);

                cardResult.ownerEmail = user.email;
                cardResult.warPos     = 0;
                cards[i] = cardResult;
                RW.SaveCard(cardResult);
            }

            return(cards);
        }
예제 #4
0
 public static BigInteger NumUserSiegeHistory(User user)
 {
     return(Op.Bytes2BigInt(
                IO.GetStorageWithKeyPath(
                    Const.preUser, user.email,
                    Const.keyTotSiege
                    )
                ));
 }
예제 #5
0
 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
                    )
                ));
 }
예제 #6
0
        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);
        }
예제 #7
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));
            }
        }
예제 #8
0
파일: Class1.cs 프로젝트: llenroc/neogame
        /**
         *  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);
        }
예제 #9
0
파일: Class1.cs 프로젝트: llenroc/neogame
        /**
         *  After 2nd stage finished, anybody can query the winnerPick.
         */
        public static byte[] CalcResult(BigInteger gameId)
        {
            Game game = GetGame(gameId);

            if (game == null)
            {
                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
            }
            else
            {
                if (game.winnerPick[0] == 1)
                {
                    return(NuTP.RespDataSucWithBody(game.winnerPick));
                }
                else
                {
                    BigInteger height = Blockchain.GetHeight();
                    if (height < game.heightStage2)
                    {
                        return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Forbidden));
                    }
                    else
                    {
                        BigInteger salt = 0;
                        for (int i = 0; i < game.numEntries; i++)
                        {
                            Entry entry = GetEntry(gameId, i);
                            if (entry.hidden.Length != 0)
                            {
                                salt += Op.Bytes2BigInt(entry.hidden);
                            }
                        }
                        byte[] winnerPick = Op.SubBytes(Hash256(salt.ToByteArray()), 0, 1);
                        game.winnerPick = winnerPick;
                        return(NuTP.RespDataSucWithBody(winnerPick));
                    }
                }
            }
        }
예제 #10
0
        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);
        }
예제 #11
0
 public static BigInteger SplitTblInt(this byte[] table, int index) => Op.Bytes2BigInt(DesegWithIdFromData(table, 0, index));
예제 #12
0
 public static BigInteger SplitSegInt(this byte[] data, int startID) => Op.Bytes2BigInt(DesegFromTableFromData(data, startID));
예제 #13
0
 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))
 };
예제 #14
0
파일: Class1.cs 프로젝트: llenroc/neogame
 public static BigInteger NumGames()
 {
     return(Op.Bytes2BigInt(NuIO.GetStorageWithKey(Global.keyNumGames)));
 }