示例#1
0
        private SetGame CreatePendingGame(SetGame setGame, out HttpStatusCode status)
        {
            using (SqlConnection connection = new SqlConnection(BoggleDB))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    using (SqlCommand addCommand = new SqlCommand(
                               "insert into Games(Player1, TimeLimit) output inserted.GameID  values (@Player1, @TimeLimit)",
                               connection,
                               transaction))
                    {
                        addCommand.Parameters.AddWithValue("@Player1", setGame.UserToken);
                        addCommand.Parameters.AddWithValue("@TimeLimit", setGame.TimeLimit);

                        string ID = addCommand.ExecuteScalar().ToString();
                        if (ID == null)
                        {
                            throw new Exception("Query failed unexpectedly");
                        }

                        status = Accepted;
                        SetGame sg = new SetGame(ID);
                        transaction.Commit();
                        return(sg);
                    }
                }
            }
        }
示例#2
0
            private void JoinGameRequest(string line)
            {
                SetGame sg      = JsonConvert.DeserializeObject <SetGame>(line);
                SetGame setGame = new BoggleService().JoinGame(sg, out HttpStatusCode status);
                string  result  = CreateResult(setGame, status);

                ss.BeginSend(result, (x, y) => { ss.Shutdown(System.Net.Sockets.SocketShutdown.Both); }, null);
            }
示例#3
0
        public SetGame JoinGame(SetGame setGame, out HttpStatusCode status)
        {
            if (setGame.UserToken == null)
            {
                status = Forbidden;
                return(null);
            }

            if (setGame.TimeLimit < 5 || setGame.TimeLimit > 120)
            {
                status = Forbidden;
                return(null);
            }

            using (SqlConnection connection = new SqlConnection(BoggleDB))
            {
                connection.Open();

                // Transaction for databse commands
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    // SQL command to run
                    // To check if user ID is valid
                    using (SqlCommand command = new SqlCommand(
                               "select UserID from Users where UserID = @UserID",
                               connection,
                               transaction))
                    {
                        command.Parameters.AddWithValue("@UserID", setGame.UserToken);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            // User ID is invalid
                            if (!reader.HasRows)
                            {
                                status = Forbidden;
                                reader.Close();
                                transaction.Commit();
                                return(null);
                            }
                        }
                    }

                    // SQL command to run
                    // To check if user is in game
                    using (SqlCommand command = new SqlCommand(
                               "select * from Games where Games.Player1 = @UserID or Games.Player2 = @UserID",
                               connection,
                               transaction))
                    {
                        command.Parameters.AddWithValue("@UserID", setGame.UserToken);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                status = Conflict;
                                reader.Close();
                                transaction.Commit();
                                return(null);
                            }
                        }
                    }

                    // If there is a user waiting with the same time limit, add current user to game
                    using (SqlCommand command = new SqlCommand(
                               "select * from Games where Games.Player2 is NULL",
                               connection,
                               transaction))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            // No pending games to join, make a new pending game
                            if (!reader.HasRows)
                            {
                                reader.Close();
                                transaction.Commit();
                                return(CreatePendingGame(setGame, out status));
                            }
                            else
                            {
                                // reader consists of gameID and player1
                                // need to set Player2, Board, TimeLimit, StartTime
                                using (SqlCommand joinGameCommand = new SqlCommand(
                                           "update Games set Player2 = @Player2, Board = @Board, TimeLimit = @TimeLimit," +
                                           " StartTime = @StartTime where GameID = @GameID",
                                           connection,
                                           transaction))
                                {
                                    reader.Read();
                                    Game game         = new Game();
                                    int? newTimeLimit = setGame.TimeLimit;
                                    if (reader["TimeLimit"] != null)
                                    {
                                        newTimeLimit = ((int)reader["TimeLimit"] + newTimeLimit) / 2;
                                    }

                                    string id = reader["GameID"].ToString();

                                    joinGameCommand.Parameters.AddWithValue("@Player2", setGame.UserToken);
                                    joinGameCommand.Parameters.AddWithValue("@Board", game.Board);
                                    joinGameCommand.Parameters.AddWithValue("@TimeLimit", newTimeLimit);
                                    joinGameCommand.Parameters.AddWithValue("@StartTime", DateTime.Now);
                                    joinGameCommand.Parameters.AddWithValue("@GameID", id);

                                    reader.Close();

                                    SetGame sg = new SetGame(id);
                                    if (joinGameCommand.ExecuteNonQuery() == 0)
                                    {
                                        status = Forbidden;
                                    }
                                    else
                                    {
                                        status = Created;
                                    }
                                    transaction.Commit();
                                    return(sg);
                                }
                            }
                        }
                    }
                }
            }
        }