예제 #1
0
        public static byte[] User2Bytes(User user)
        {
            if (user == null)
            {
                return(Op.Void());
            }
            byte[] segCardIds = Op.Void();
            if (user.cards != null && user.cards.Length > 0)
            {
                for (int i = 0; i < user.cards.Length; i++)
                {
                    //no cards data
                    segCardIds = segCardIds.AddSeg(user.cards[i].cardID);
                }
            }

            byte[] ret = NuSD.SegString(user.email)
                         .AddSeg(user.pswHash)
                         .AddSeg(user.address)
                         .AddSegStr(user.nickName)
                         .AddSegInt(user.icon)
                         .AddSegInt(user.score)
                         .AddSegInt(user.serverID)
                         .AddSegInt(user.city)
                         .AddSeg(user.warID)
                         .AddSeg(segCardIds);

            return(ret);
        }
예제 #2
0
        public void TestDataDesegTableComplex()
        {
            byte[] out1 =
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8
            };

            Assert.AreEqual(out1, NuSD.DesegFromTableFromData(table531, 0));

            byte[] out2 =
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6
            };
            Assert.AreEqual(out2, NuSD.DesegFromTableFromData(table531, NuSD.SegLenOfBody(out1)));

            byte[] out3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };
            Assert.AreEqual(out3, NuSD.DesegFromTableFromData(table531, NuSD.SegLenOfBody(out1) + NuSD.SegLenOfBody(out2)));
        }
예제 #3
0
        /** Get basic information of a list of users
         *
         *  获取一系列玩家信息, 内容包括所在服务器、所占城池、
         * 返回:该用户所有卡牌具体信息。
         *
         * For each user, the information includes:
         *     -  User email and address,
         *     -  User current server and city
         *     -  User score and unclaimed score
         *     -  User's cards (full cards data)
         *
         * Return: Array of the User objects and cards
         *
         *
         */

        /* * NuTP:
         * <emails> = S[S<email>*]
         *
         * <getUsers> =  [S<header>,
         *             S[([S<user>,
         *                  S[S<card>*10]]
         *             )*]
         */

        private static byte[] GetUsers(Credential credential, byte[] emails)
        {
            // This method does not require credential

            int numEmails = emails.SizeTable();

            byte[] retBody = Op.Void();

            for (int i = 0; i < numEmails; i++)
            {
                string email = emails.SplitTblStr(i);
                User   user  = RW.FindUser(email);
                if (user != null)
                {
                    byte[] body = NuSD.Seg(RW.User2Bytes(user)).AddSeg(RW.UserCards2Table(user));
                    retBody.AddSeg(body);
                }
                else
                {
                    return(NuTP.RespDataWithDetail(ErrCate.User, ErrType.NotExist, email, Op.Void()));
                }
            }

            NuTP.Response response = new NuTP.Response
            {
                header = new NuTP.Header
                {
                    domain = NuTP.SysDom,
                    code   = NuTP.Code.Success
                },

                body = retBody
            };
            return(NuTP.Response2Bytes(response));
        }
예제 #4
0
 public void TestLengthBody()
 {
     Assert.AreEqual(NuSD.PreLenOfBody(body5), 1);
     Assert.AreEqual(NuSD.PreLenOfBody(body300), 2);
     Assert.AreEqual(NuSD.SegLenOfBody(body5), 6);
     Assert.AreEqual(NuSD.SegLenOfBody(body300), 302);
 }
예제 #5
0
        // <emails> = [S<email>*]
        //<FakeUsers> = [S<user>*]
        private static byte[] FakeUsers(BigInteger serverId, byte[] emails)
        {
            byte[] body = Op.Void();

            if (Runtime.CheckWitness(Owner))
            {
                int numEmails = emails.SizeTable();
                for (int i = 0; i < numEmails; i++)
                {
                    string email = NuSD.SplitTblStr(emails, i);
                    User   user  = new User
                    {
                        address  = Op.Void(),
                        email    = email,
                        pswHash  = Op.Void(),
                        nickName = "Fake",
                        icon     = 0,
                        serverID = serverId,
                        warID    = Op.Void(),
                        city     = Const.numCities
                    };
                    user.cards = GenerateRandomCards(user, 10);
                    RW.SaveUser(user);
                    body.AddSeg(RW.User2Bytes(user));
                }

                return(NuTP.RespDataSucWithBody(body));
            }
            else
            {
                return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.AuthFail));
            }
        }
예제 #6
0
        public void TestSegEmpty()
        {
            byte[] n = new byte[0];

            byte[] bData = NuSD.Seg(n);
            Assert.AreEqual(bData.Length, 1);
            Assert.AreEqual(bData[0], 0x00);
        }
예제 #7
0
 public void TestDeseg()
 {
     Assert.AreEqual(NuSD.Deseg(seg5), body5);
     Assert.AreEqual(NuSD.Deseg(seg128), body127);
     Assert.AreEqual(NuSD.Deseg(seg129), body128);
     Assert.AreEqual(NuSD.Deseg(seg131), body130);
     Assert.AreEqual(NuSD.Deseg(seg257), body255);
     Assert.AreEqual(NuSD.Deseg(seg302), body300);
 }
예제 #8
0
 public void TestSeg()
 {
     Assert.AreEqual(NuSD.Seg(body5), seg5);
     Assert.AreEqual(NuSD.Seg(body127), seg128);
     Assert.AreEqual(NuSD.Seg(body128), seg129);
     Assert.AreEqual(NuSD.Seg(body130), seg131);
     Assert.AreEqual(NuSD.Seg(body255), seg257);
     Assert.AreEqual(NuSD.Seg(body300), seg302);
 }
예제 #9
0
        public void TestLengthFromSegData()
        {
            Assert.AreEqual(NuSD.PreLenOfFirstSegFromTable(tableThree), 1);
            Assert.AreEqual(NuSD.PreLenOfFirstSegFromData(tableThree, 6), 1);
            Assert.AreEqual(NuSD.PreLenOfFirstSegFromData(tableThree, 9), 1);

            Assert.AreEqual(NuSD.BodyLenOfFirstSegFromTable(tableThree), 5);
            Assert.AreEqual(NuSD.BodyLenOfFirstSegFromData(tableThree, 6), 2);
            Assert.AreEqual(NuSD.BodyLenOfFirstSegFromData(tableThree, 9), 1);
        }
예제 #10
0
 public static byte[] Quiz2Bytes(Quiz quiz)
 {
     byte[] ret  = NuSD.Seg(quiz.nameInquirer).AddSeg(quiz.nameAnswerer).AddSegStr(quiz.message);
     byte[] qids = Op.Void();
     for (int i = 0; i < quiz.questions.Length; i++)
     {
         qids = qids.AddSeg(Question2Bytes(quiz.questions[i]));
     }
     return(ret.AddSeg(qids));
 }
예제 #11
0
        public static Card[] Table2Cards(byte[] table)
        {
            int num = table.SizeTable();

            Card[] cards = new Card[num];
            for (int i = 0; i < num; i++)
            {
                cards[i] = Bytes2Card(NuSD.SplitTbl(table, i));
            }
            return(cards);
        }
예제 #12
0
        public void TestDataDesegTable()
        {
            byte[] out1 = { 1, 2, 3, 4, 5 };
            Assert.AreEqual(NuSD.DesegFromTable(tableThree), out1);

            byte[] out2 = { 12, 2 };
            Assert.AreEqual(NuSD.DesegFromTableFromData(tableThree, NuSD.SegLenOfBody(out1)), out2);

            byte[] out3 = { 0 };
            Assert.AreEqual(NuSD.DesegFromTableFromData(tableThree, NuSD.SegLenOfBody(out1) + NuSD.SegLenOfBody(out2)), out3);
        }
예제 #13
0
        public static War ByteArrayToWar(byte[] data)
        {
            War war = new War();

            war.id          = Op.Bytes2String(NuSD.DesegWithIdFromSeg(data, 0));
            war.regEndBlock = Op.Bytes2BigInt(NuSD.DesegWithIdFromSeg(data, 1));
            BigInteger height = Blockchain.GetHeight();

            war.regLeftBlocks = war.regEndBlock - height;
            return(war);
        }
예제 #14
0
        public static Card[] Table2CardIDs(byte[] table)
        {
            int num = table.SizeTable();

            Card[] cards = new Card[num];
            for (int i = 0; i < num; i++)
            {
                byte[] cid = NuSD.SplitTbl(table, i);
                cards[i] = FindCard(cid);
            }
            return(cards);
        }
예제 #15
0
 public static byte[] Player2Bytes(Player player)
 {
     return(NuSD.Seg(player.address)
            .AddSegInt(player.lvlAtk)
            .AddSegInt(player.lvlDef)
            .AddSegInt(player.lvlSpd)
            .AddSegInt(player.lvlHP)
            .AddSegInt(player.lvlRev)
            .AddSegInt(player.lastUpdateYear)
            .AddSegInt(player.water)
            .AddSegInt(player.soil)
            .AddSegInt(player.wind)
            .AddSegInt(player.fire));
 }
예제 #16
0
        public static byte[] User2Bytes(User user)
        {
            byte[] dataCardIds = Op.Void();
            if (user.cards.Length > 0)
            {
                for (int i = 0; i < user.cards.Length; i++)
                {
                    //no cards data
                    dataCardIds = dataCardIds.AddSeg(user.cards[i].id);
                }
            }

            return(NuSD.Seg(user.id).AddSeg(dataCardIds));
        }
예제 #17
0
 public static byte[] Card2Bytes(Card card)
 {
     if (card == null)
     {
         return(Op.Void());
     }
     else
     {
         return(NuSD.Seg(card.cardID)
                .AddSegInt(card.type)
                .AddSeg(card.lvls)
                .AddSegStr(card.ownerEmail)
                .AddSegInt(card.warPos));
     }
 }
예제 #18
0
        public void TestDataDesegWithID()
        {
            //public static byte[] tableThree = { 5, 1, 2, 3, 4, 5, 2, 12, 2, 1, 0 };
            byte[] b1 = NuSD.DesegWithIdFromTable(tableThree, 0);
            byte[] b2 = NuSD.DesegWithIdFromTable(tableThree, 1);
            byte[] b3 = NuSD.DesegWithIdFromTable(tableThree, 2);

            byte[] seg = NuSD.Seg(tableThree);
            byte[] b4  = NuSD.DesegWithIdFromSeg(seg, 1);

            Assert.AreEqual(b1, new byte[] { 1, 2, 3, 4, 5 });
            Assert.AreEqual(b2, new byte[] { 12, 2 });
            Assert.AreEqual(b3, new byte[] { 0 });
            Assert.AreEqual(b4, new byte[] { 12, 2 });
        }
예제 #19
0
 // Customized Serialization for Card.
 // The Class Neunity.Tools.NuSD Manages Serialization/Deserialization.
 // NuSD: <Card> = [S<id>,S<name>,S<birthBlock>,S<level>,S<ownerId>,S<isFighting#1>]
 public static byte[] Card2Bytes(Card card)
 {
     if (card == null)
     {
         return(Op.Void());
     }
     else
     {
         return(NuSD.Seg(card.id)
                .AddSegStr(card.name)
                .AddSegInt(card.birthBlock)
                .AddSegInt(card.level)
                .AddSeg(card.ownerId)
                .AddSegBool(card.isLive));
     }
 }
예제 #20
0
        /** Get basic information of an user
         *
         *  获取一个玩家信息, 内容包括所在服务器、所占城池、
         * 返回:该用户所有卡牌具体信息。
         *
         * The information includes:
         *     -  User email and address,
         *     -  User current server and city
         *     -  User score and unclaimed score
         *     -  User's cards (full cards data)
         *
         * Return: Array of the User objects and cards
         *
         *
         */

        /* * NuTP:
         * <GetUsers> =  [S<header>, S<user>, S[S<card>*]]
         *
         */

        private static byte[] GetUser(Credential credential, string email)
        {
            // This method does not require credential

            User user = RW.FindUser(email);

            if (user != null)
            {
                byte[] body = NuSD.Seg(RW.User2Bytes(user)).AddSeg(RW.UserCards2Table(user));
                return(NuTP.RespDataSucWithBody(body));
            }
            else
            {
                return(NuTP.RespDataWithDetail(ErrCate.User, ErrType.NotExist, email, Op.Void()));
            }
        }
예제 #21
0
        public static byte[] City2Bytes(City city)
        {
            if (city == null)
            {
                return(Op.Void());
            }

            byte[] data = NuSD.SegInt(city.serverID)
                          .AddSegInt(city.cityId)
                          .AddSegStr(city.ownerID)
                          .AddSeg(city.siegeID);
            if (city.ownerID != "")
            {
                data = data.AddSeg(CardIDs2Table(city.ownerCards));
            }
            return(data);
        }
예제 #22
0
 public static byte[] Cred2Bytes(Credential credential)
 {
     return(NuSD.SegString(credential.email)
            .AddSeg(credential.address)
            .AddSeg(Hash256(Op.String2Bytes(credential.psw))));
 }
예제 #23
0
 public void TestSeg()
 {
     Assert.AreEqual(NuSD.Seg(body5), seg5);
     Assert.AreEqual(NuSD.Seg(body300), seg302);
     Assert.AreEqual(NuSD.Seg(body255), seg257);
 }
예제 #24
0
 public void TestLengthSeg()
 {
     Assert.AreEqual(NuSD.PreLenOfSeg(seg5), 1);
     Assert.AreEqual(NuSD.BodyLenOfSeg(seg5), body5.Length);
     Assert.AreEqual(NuSD.BodyLenOfSeg(seg302), body300.Length);
 }
예제 #25
0
 public void TestNumSegs()
 {
     Assert.AreEqual(NuSD.NumSegsOfTable(tableThree), 3);
     Assert.AreEqual(NuSD.NumSegsOfTableFromData(tableThree, 6), 2);
 }
예제 #26
0
 public static byte[] Question2Bytes(Question question)
 {
     return(NuSD.SegString(question.title).AddSegStr(question.answer));
 }
예제 #27
0
 public static byte[] WarToByteArray(War war)
 {
     return(NuSD.JoinSegs2Seg(NuSD.SegString(war.id), NuSD.SegInt(war.regEndBlock)));
 }