Beispiel #1
0
        //Main Entrance
        public static Object Main(string operation, params object[] args)
        {
            if (operation == "randomCard")
            {
                byte[] ownerId  = (byte[])args[0];
                byte[] cardId   = (byte[])args[1];
                string cardName = (string)args[2];

                return(RandomCard(ownerId, cardId, cardName));
            }

            if (operation == "getCard")
            {   //Used Internally Only
                byte[] cardId   = (byte[])args[0];
                Card   card     = ReadCard(cardId);
                byte[] cardData = Card2Bytes(card);
                return(NuTP.RespDataSucWithBody(cardData));
            }

            if (operation == "cardMerge")
            {
                byte[] cardID1 = (byte[])args[0];
                byte[] cardID2 = (byte[])args[1];
                string name    = (string)args[2];

                return(CardMerge(cardID1, cardID2, name));
            }


            //... Other operations

            return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.NotFound));
        }
Beispiel #2
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));
            }
        }
Beispiel #3
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));
                }
            }
        }
Beispiel #4
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));
            }
        }
Beispiel #5
0
        /** Register the user onto a server.
         *
         * 用户注册/登录 (由于钱包功能尚不支持,Demo阶段暂时使用用户邮箱/密码作为认证方法。若有变动将在Global.VerifyUser()方法里变更实现)
         * 返回:该用户所有卡牌具体信息。若在该服务器上新账户,为用户新建若干卡牌。
         *
         * Check psw/signature first, then
         * If the user does exist already,
         *      If user exist on current server, consider as login in.
         *      If user does exist on another server, transfer user to this server.
         * Else, create cards for this user. Need attach some GAS...
         *
         * Return: S<header>
         *
         */
        private static byte[] Register(Credential credential, BigInteger serverId, string nickName, BigInteger iconID)
        {
            byte[] userData = RW.FindDataUser(credential.email);
            if (userData.Length != 0)
            {   //Exists
                return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.Duplicated));
            }
            else
            {
                byte[] psdHash = Hash256(Op.String2Bytes(credential.psw));
                User   user    = new User();
                user.address  = credential.address;
                user.email    = credential.email;
                user.pswHash  = psdHash;
                user.nickName = nickName;
                user.icon     = iconID;
                user.serverID = serverId;
                user.warID    = Op.Void();
                user.city     = Const.numCities;
                //user.cardIDs = new byte[0];

                RW.SaveUser(user);
                return(NuTP.RespDataSucWithBody(RW.User2Bytes(user)));
            }
        }
Beispiel #6
0
        public static object Main(string operation, params object[] args)
        {
            BigInteger a = (BigInteger)args[0];
            BigInteger b = (BigInteger)args[1];

            return(NuTP.RespDataSucWithBody(Op.BigInt2Bytes(a + b)));
        }
Beispiel #7
0
 /** All cities in a server with "maximum" recent items.
  *
  * 获取城市状态列表
  *
  * Return: Array of War Object
  */
 private static byte[] GetCities(Credential credential, BigInteger serverId)
 {
     byte[] body = Op.Void();
     for (int i = 0; i < Const.numCities; i++)
     {
         body.AddSeg(RW.FindDataCity(serverId, i));
     }
     return(NuTP.RespDataSucWithBody(body));
 }
Beispiel #8
0
 private static byte[] GetQuizMessage(BigInteger quizID)
 {
     byte[] quizData = GetQuiz(quizID);
     if (quizData.Length != 0)
     {
         Quiz   quiz    = Bytes2Quiz(quizData);
         byte[] message = Op.String2Bytes(quiz.message);
         return(NuTP.RespDataSucWithBody(message));
     }
     return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.NotFound));
 }
Beispiel #9
0
        /** User starts a war against a city. If the city is empty now, it's gonna occupy it directly.
         * 参战某城市,
         * Return: An object of War. If failed to create, return null
         */
        /** User starts a war against a city. If the city is empty now, it's gonna occupy it directly.
         * 参战某城市,
         * Return: An object of War. If failed to create, return null
         */
        public static byte[] StartSiege(Credential credential, BigInteger serverId, BigInteger cityId, byte[] cardIds)
        {
            if (Global.VerifyUser(credential))
            {
                if (cityId >= Const.numCities)
                {
                    return(NuTP.RespDataWithCode(ErrCate.City, ErrType.NotExist));
                }
                else
                {   //City does exist
                    City       city   = RW.FindCity(serverId, cityId);
                    BigInteger status = RW.GetStatusCity(city);
                    if (status == StatusCity.Sieging)
                    {
                        return(NuTP.RespDataWithCode(ErrCate.City, ErrType.Duplicated));
                    }
                    else
                    {
                        User user = RW.FindUser(credential.email);
                        if (user.warID != Op.Void())
                        {  //User does not allow to participate into multiple wars at the same time
                            return(NuTP.RespDataWithCode(ErrCate.War, ErrType.Duplicated));
                        }
                        else
                        {
                            CarryBattleSC.Card[] cards = RW.Table2Cards(cardIds);
                            if (cards.Length != Const.numCardsSiege)
                            {  //Format of card array must be wrong
                                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
                            }

                            for (int i = 0; i < Const.numCardsSiege; i++)
                            {
                                if (cards[i].ownerEmail != user.email)
                                {
                                    return(NuTP.RespDataWithCode(ErrCate.Card, ErrType.NotExist));
                                }
                            }

                            Siege siege = RW.CreateSiege(serverId, city, user, cards, Blockchain.GetHeight());

                            return(NuTP.RespDataSucWithBody(RW.Seige2Bytes(siege)));
                        }
                    }
                }
            }
            else
            {
                return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.AuthFail));
            }
        }
Beispiel #10
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()));
            }
        }
Beispiel #11
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));
            }
        }
Beispiel #12
0
        /* * NuTP:
         *
         * <RegCards> =  [S<header>, S[S<card>*]]
         *
         */
        private static byte[] RegCards(Credential credential, int num)
        {
            byte[] userData = RW.FindDataUser(credential.email);
            if (userData.Length == 0)
            {   //Account Not exist
                return(NuTP.RespDataWithCode(ErrCate.Account, ErrType.NotExist));
            }
            else
            {   //Account does exist
                User user       = RW.Bytes2User(userData);
                int  numAlready = RW.NumCardsOfUser(user);
                int  numPending = Const.numCardsTotalReg - numAlready;
                if (numPending <= 0)
                {
                    return(NuTP.RespDataWithCode(ErrCate.Card, ErrType.Duplicated));
                }
                else
                {
                    CarryBattleSC.Card[] cardsNew  = GenerateRandomCards(user, (numPending < Const.numCardsPerReg) ? numPending : Const.numCardsPerReg);
                    CarryBattleSC.Card[] cardsOrig = user.cards;
                    user.cards = new CarryBattleSC.Card[cardsOrig.Length + cardsNew.Length];


                    for (int i = 0; i < cardsOrig.Length; i++)
                    {
                        user.cards[i] = cardsOrig[i];
                    }

                    for (int j = 0; j < cardsNew.Length; j++)
                    {
                        user.cards[j + cardsOrig.Length] = cardsNew[j];
                    }
                    //更新玩家数据
                    RW.SaveUser(user);

                    byte[] body = RW.UserCards2Table(user);
                    return(NuTP.RespDataSucWithBody(body));
                }
            }
        }
Beispiel #13
0
        /**
         *  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));
                    }
                }
            }
        }
Beispiel #14
0
        public static byte[] IsWinner(BigInteger gameId, BigInteger entryId)
        {
            Game game = GetGame(gameId);

            if (game == null)
            {
                return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.BadRequest));
            }
            else
            {
                BigInteger height = Blockchain.GetHeight();
                if (height < game.heightStage2)
                {
                    return(NuTP.RespDataWithCode(NuTP.SysDom, NuTP.Code.Forbidden));
                }
                else
                {
                    Entry entry = GetEntry(gameId, entryId);
                    bool  ret   = entry.pick == game.winnerPick;
                    return(NuTP.RespDataSucWithBody(Op.Bool2Bytes(ret)));
                }
            }
        }
Beispiel #15
0
 /** All the completed wars related with particular user with "maximum" recent items.
  *
  * 获取某城市已完成战役列表(最近的maximum个)
  *
  *
  * Return: Array of War Object
  */
 private static byte[] GetCitySeiges(Credential credential, BigInteger serverId, BigInteger cityId, BigInteger maximum)
 {
     byte[] siegesData = RW.FindSiegesByCityHistory(serverId, cityId, maximum);
     return(NuTP.RespDataSucWithBody(siegesData));
 }
Beispiel #16
0
 private static byte[] GetQuiz(BigInteger quizID)
 {
     byte[] qdata = NuIO.GetStorageWithKeyPath("quiz", Op.BigInt2String(quizID));
     return(NuTP.RespDataSucWithBody(qdata));
 }