Пример #1
0
        /// <summary>
        /// Given the proper parameters, it runs the correct method in our BoggleServer and sends the response message.
        /// This is essentially covering the functionality of IBoggleService from before.
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="GameID"></param>
        /// <param name="IsBrief"></param>
        /// <param name="content"></param>
        private void ParseMessage(string Type, string Url, string GameID, string IsBrief, dynamic content)
        {
            HttpStatusCode status;

            // Each method we call should return the object we to JSON encoded

            if (Type == "POST")
            {
                // CreateUser
                if (Url == "users")
                {
                    UserID ReturnID = server.CreateUser(content, out status);
                    CompileMessage(status, ReturnID);
                }
                // JoinGame
                else if (Url == "games")
                {
                    GameIDReturn IDReturn = server.JoinGame(content, out status);
                    CompileMessage(status, IDReturn);
                }
            }
            else if (Type == "PUT")
            {
                // CancelJoinRequest
                if (Url == "games" && GameID == string.Empty)
                {
                    server.CancelJoinRequest(content, out status);
                    CompileMessage(status, null);
                }
                // PlayWord
                else
                {
                    ScoreReturn Score = server.PlayWord(content, GameID, out status);
                    CompileMessage(status, Score);
                }
            }
            // GetGameStatus
            else
            {
                Game CurrentGame = new Game();
                CurrentGame = server.GetGameStatus(GameID, IsBrief, out status);
                CompileMessage(status, CurrentGame);
            }
        }
        /// <summary>
        /// Invokes a user token to join the game
        /// </summary>
        public GameIDReturn JoinGame(GameInfo Info)
        {
            lock (sync)
            {
                GameIDReturn ReturnInfo = new GameIDReturn();
                //string ReturnInfo = string.Empty;

                string nickname;
                //!UserIDs.ContainsKey(Info.UserToken) ||
                if (Info.TimeLimit < 5 || Info.TimeLimit > 120)
                {
                    SetStatus(Forbidden);
                    return(ReturnInfo);
                }
                else if (!UserIDs.TryGetValue(Info.UserToken, out nickname))
                {
                    SetStatus(Forbidden);
                    return(ReturnInfo);
                }
                //If the same player tries to join the pending game against himself.
                else if (CurrentPendingGame.Player1Token == Info.UserToken)
                {
                    SetStatus(Conflict);
                    return(ReturnInfo);
                }
                //If the pending game has a player 1 waiting.
                else if (CurrentPendingGame.Player1Token != null)
                {
                    Game NewGame = new Game();
                    NewGame.Player1 = new Player();
                    NewGame.Player2 = new Player();
                    string P1Nickname;
                    UserIDs.TryGetValue(CurrentPendingGame.Player1Token, out P1Nickname);

                    //Game NewPendingGame = new Game();
                    BoggleBoard Board         = new BoggleBoard();
                    string      PendingGameID = GameList.Keys.Count.ToString();

                    //Start new active game.
                    NewGame.GameState           = "active";
                    NewGame.Board               = Board.ToString();
                    NewGame.Player1Token        = CurrentPendingGame.Player1Token;
                    NewGame.Player2Token        = Info.UserToken;
                    NewGame.TimeLimit           = (Info.TimeLimit + CurrentPendingGame.TimeLimit) / 2;
                    NewGame.TimeLeft            = NewGame.TimeLimit;
                    NewGame.Player1.Nickname    = P1Nickname;
                    NewGame.Player1.Score       = 0;
                    NewGame.Player1.WordsPlayed = new List <WordScore>();
                    NewGame.Player2.Nickname    = UserIDs[Info.UserToken];
                    NewGame.Player2.Score       = 0;
                    NewGame.Player2.WordsPlayed = new List <WordScore>();

                    //Add an empty pending game.
                    CurrentPendingGame.Player1Token = null;
                    CurrentPendingGame.TimeLimit    = 0;


                    //Send back information to client.
                    GameList.Add(GameList.Keys.Count + 1, NewGame);
                    //GameList.Add(GameList.Keys.Count + 1, NewPendingGame);

                    //ReturnInfo.GameID = (GameList.Keys.Count - 1).ToString();
                    ReturnInfo.GameID = (GameList.Keys.Count).ToString();
                    SetStatus(Created);
                    return(ReturnInfo);
                }
                //If the pending game is empty.
                else if (CurrentPendingGame.Player1Token == null)
                {
                    //Inputs user data into the pending game.
                    CurrentPendingGame.GameState    = "pending";
                    CurrentPendingGame.Player1Token = Info.UserToken;
                    CurrentPendingGame.TimeLimit    = Info.TimeLimit;


                    //Returns info back to the user.
                    //ReturnInfo.GameID = GameList.Keys.Count.ToString();
                    ReturnInfo.GameID = (GameList.Keys.Count + 1).ToString();
                    SetStatus(Accepted);
                    return(ReturnInfo);
                }
                return(ReturnInfo);
            }
        }