示例#1
0
 public GameInfo(int Gameid)
 {
     GameState = "pending";
     GameID    = Gameid.ToString();
     //Player1.Score = 0;
     // Player2.Score = 0;
     GameBoard = new BoggleBoard();
     Board     = GameBoard.ToString();
 }
示例#2
0
        public GameInfo JoinGame(UserInfo user, out HttpStatusCode status)
        {
            status = OK;
            lock (sync)
            {
                if (user.UserToken == null || user.UserToken.Length == 0 || user.TimeLimit < 5 || user.TimeLimit > 120)
                {
                    status = Forbidden;
                    return(null);
                }
                string gameID = "";
                using (SqlConnection conn = new SqlConnection(BoggleDB))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        //check if the user is in the user table.
                        //if not set the status to forbidden and return null.
                        using (SqlCommand command = new SqlCommand("select UserID from Users where UserID = @UserID", conn, trans))
                        {
                            command.Parameters.AddWithValue("@UserID", user.UserToken);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                //if the user is not registered, forbidden
                                if (!reader.HasRows)
                                {
                                    reader.Close();
                                    status = Forbidden;
                                    trans.Commit();
                                    return(null);
                                }
                            }
                        }

                        // create a pending game with no player if needed
                        using (SqlCommand command = new SqlCommand("select GameID from Games", conn, trans))
                        {
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                //if there is no games, add a new pending game.
                                if (!reader.HasRows)
                                {
                                    reader.Close();
                                    using (SqlCommand command4 = new SqlCommand("insert into Games (GameState) values (@GameState)", conn, trans))
                                    {
                                        command4.Parameters.AddWithValue("@GameState", "pending");
                                        command4.ExecuteNonQuery();
                                    }
                                }
                            }
                        }

                        // check if user is already a player in a pending game
                        using (SqlCommand command = new SqlCommand("select Player1, Player2 "

                                                                   + "from Games where Player1 = @Player1 and Player2 is NULL", conn, trans))
                        {
                            command.Parameters.AddWithValue("@Player1", user.UserToken);
                            //command.Parameters.AddWithValue("@Player2", null);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                //if user is already Player1 in pending game
                                if (reader.HasRows)
                                {
                                    reader.Close();
                                    status = Conflict;
                                    trans.Commit();
                                    return(null);
                                }
                            }
                        }
                        // get pending gameID
                        using (SqlCommand command = new SqlCommand("select GameID from Games where "

                                                                   + "Player2 is NULL", conn, trans))
                        {
                            //command.Parameters.AddWithValue("@Player2", null);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    gameID = reader["GameID"].ToString();
                                }
                            }
                        }

                        // determine if Player1 of the pending game is null
                        using (SqlCommand command = new SqlCommand("select Player1 from Games where "

                                                                   + "GameID = @GameID", conn, trans))
                        {
                            command.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    if (reader.IsDBNull(0))                                    //)//||
                                    {
                                        reader.Close();
                                        //add user as player1 to pending game
                                        using (SqlCommand command2 = new SqlCommand("update Games set Player1 = @Player1, TimeLimit = @TimeLimit, GameState = @GameState where GameID = @GameID", conn, trans))
                                        {
                                            command2.Parameters.AddWithValue("@GameID", gameID);
                                            command2.Parameters.AddWithValue("@Player1", user.UserToken);
                                            command2.Parameters.AddWithValue("@TimeLimit", user.TimeLimit);
                                            command2.Parameters.AddWithValue("@GameState", "pending");

                                            command2.ExecuteNonQuery();
                                            status = Accepted;
                                            GameInfo temp = new GameInfo();
                                            temp.GameID = gameID;
                                            trans.Commit();
                                            return(temp);
                                        }
                                    }
                                    else
                                    {
                                        reader.Close();
                                        //otherwise, add user as player2 to pending game and make gamestate active
                                        using (SqlCommand command3 = new SqlCommand("update Games set Player2 = @Player2, "

                                                                                    + "Board = @Board, TimeLimit = (TimeLimit + @TimeLimit) / 2, "

                                                                                    + "StartTime = @StartTime, GameState = @GameState where GameID = @GameID", conn, trans))
                                        {
                                            command3.Parameters.AddWithValue("@GameID", gameID);
                                            command3.Parameters.AddWithValue("@Player2", user.UserToken);
                                            command3.Parameters.AddWithValue("@TimeLimit", user.TimeLimit);
                                            command3.Parameters.AddWithValue("@GameState", "active");

                                            BoggleBoard board = new BoggleBoard();
                                            command3.Parameters.AddWithValue("@Board", board.ToString());
                                            command3.Parameters.AddWithValue("@StartTime", DateTime.UtcNow);


                                            command3.ExecuteNonQuery();
                                            status = Created;
                                        }
                                        //adding a new pending game without any players
                                        using (SqlCommand command4 = new SqlCommand("insert into Games (GameState) values (@GameState)", conn, trans))
                                        {
                                            command4.Parameters.AddWithValue("@GameState", "pending");
                                            command4.ExecuteNonQuery();
                                            GameInfo temp = new GameInfo();
                                            temp.GameID = gameID;
                                            trans.Commit();
                                            return(temp);
                                        }
                                    }
                                }
                                return(null);
                            }
                        }
                    }
                }
            }
        }
        /// <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);
            }
        }
示例#4
0
        /// <summary>
        /// Join a game.
        /// If UserToken is invalid, TimeLimit < 5, or TimeLimit > 120, responds with status 403 (Forbidden).
        /// Otherwise, if UserToken is already a player in the pending game, responds with status 409 (Conflict).
        /// Otherwise, if there is already one player in the pending game, adds UserToken as the second player. The pending game becomes active and
        /// a new pending game with no players is created. The active game's time limit is the integer average of the time limits requested by the
        /// two players. Returns the new active game's GameID (which should be the same as the old pending game's GameID). Responds with status 201 (Created).
        /// Otherwise, adds UserToken as the first player of the pending game, and the TimeLimit as the pending game's requested time limit. Returns the
        /// pending game's GameID. Responds with status 202 (Accepted).
        /// </summary>
        public GameRoom JoinGame(GameInfo g)
        {
            lock (sync)
            {
                // Create pending game
                if (g.UserToken == null || !players.ContainsKey(g.UserToken) || g.TimeLimit < 5 || g.TimeLimit > 120)
                {
                    SetStatus(Forbidden);
                    return(null);
                }
                else if (games.TryGetValue(pendingGame, out GameStatus game) && players.TryGetValue(g.UserToken, out Player player))
                {
                    // Conflict if same user joins game
                    if (game.Player1 != null && g.UserToken.Equals(game.Player1.UserToken))
                    {
                        SetStatus(Conflict);
                        return(null);
                    }
                    else if (game.Player1 == null)
                    {
                        // Create new game
                        GameRoom room = new GameRoom();
                        room.GameID = pendingGame;

                        games.TryGetValue(pendingGame, out GameStatus newGame);
                        players.TryGetValue(g.UserToken, out Player player1);

                        games.Remove(pendingGame);

                        // Add information to first player in pending game
                        newGame.Player1   = player1;
                        newGame.TimeLimit = g.TimeLimit;
                        newGame.GameState = "pending";

                        games.Add(pendingGame, newGame);

                        SetStatus(Accepted);
                        return(room);
                    }
                    else
                    {
                        GameRoom room = new GameRoom();
                        room.GameID = pendingGame;

                        games.TryGetValue(pendingGame, out GameStatus currentGame);
                        games.Remove(pendingGame);

                        // Add first and second player, and time information to GameStatus
                        players.TryGetValue(g.UserToken, out Player player2);

                        currentGame.TimeLimit = (game.TimeLimit + g.TimeLimit) / 2;
                        currentGame.TimeLeft  = currentGame.TimeLimit;
                        times.Add(pendingGame, System.DateTime.UtcNow.Ticks);

                        currentGame.GameState = "active";
                        currentGame.Player2   = player2;

                        currentGame.Player1.WordsPlayed = new List <Words>();
                        currentGame.Player2.WordsPlayed = new List <Words>();

                        board             = new BoggleBoard();
                        currentGame.Board = board.ToString();

                        games.Add(pendingGame, currentGame);

                        // Create new pending game.
                        games.Add(pendingGame = uniqueGameID(), new GameStatus());

                        SetStatus(Created);
                        return(room);
                    }
                }
                return(null);
            }
        }