public void Subscribe()
        {
            Player player = new Player() { id = Context.ClientId };
            //looking for room
            if (_GameManager.AnyOpenRooms())
            {
                //room found, joining room
                Caller.alertMsg("Room found, joining room...");

                GameRoom openRoom = _GameManager.JoinOpenRoom(player);

                // begin game
                Clients[openRoom.FirstUser.id].alertMsg("Game has begun, your 'X'");
                Clients[openRoom.FirstUser.id].beginGame('x');
                Clients[openRoom.SecondUser.id].alertMsg("Game has begun, your 'O'");
                Clients[openRoom.SecondUser.id].beginGame('o');
            }
            else
            {
                // no rooms found, waiting for another player
                Caller.alertMsg("No rooms found, creating room..");
                _GameManager.CreateRoom(player);
                Caller.alertMsg("Waiting for another player to join...");
            }
        }
        public void CreateRoom(Player player)
        {
            GameRoom newRoom = new GameRoom();
            newRoom.FirstUser = player;
            newRoom.GroupId = _GameRooms.Count().ToString();
            newRoom.GamesCurrentState = GameRoom.GameStates.LookingForAnotherPlayer;

            _GameRooms.Add(newRoom);
        }
        public void Disconnect()
        {
            Player player = new Player() { id = Context.ClientId };
            Player opponent = _GameManager.GetOpponent(player);

            if (opponent != null)
                Clients[opponent.id].opponentQuit();

            _GameManager.RemoveRoom(player);
        }
 public bool DidPlayerTie(Player player)
 {
     bool playerTied = false;
     Player currentPlayer = GetPlayer(player);
     Player opponent = GetOpponent(player);
     if (_GameLogic.PlayerHasTied(currentPlayer.Moves, opponent.Moves))
     {
         playerTied = true;
         RemoveRoom(player);
     }
     return playerTied;
 }
        public bool DidPlayerWin(Player player)
        {
            bool playerWon = false;
            Player currentPlayer = GetPlayer(player);

            if (_GameLogic.PlayerHasWon(currentPlayer.Moves))
            {
                playerWon = true;
                RemoveRoom(player);
            }

            return playerWon;
        }
        public Player GetOpponent(Player player)
        {
            GameRoom room = GetRoom(player);
            Player opponent = null;

            if (room != null)
            {
                if (room.FirstUser.id == player.id)
                {
                    opponent = room.SecondUser;
                }
                else
                {
                    opponent = room.FirstUser;
                }
            }

            return opponent;
        }
        public void MakeMove(string move)
        {
            Player player = new Player() { id = Context.ClientId };
            _GameManager.PlayerMove(player, move);
            Player opponent = _GameManager.GetOpponent(player);

            Clients[opponent.id].setOpponentsMove(move);

            if (_GameManager.DidPlayerWin(player))
            {
                Caller.youWin();
                Clients[opponent.id].youLose();
            }
            else if (_GameManager.DidPlayerTie(player))
            {
                Caller.youTied();
                Clients[opponent.id].youTied();
            }
        }
        public void RemoveRoom(Player player)
        {
            GameRoom room = GetRoom(player);

            if (room != null)
                _GameRooms.Remove(room);
        }
        public void PlayerMove(Player player, string move)
        {
            Player currentPlayer = GetPlayer(player);

            if (currentPlayer.Moves == null)
                currentPlayer.Moves = new List<string>();

            currentPlayer.Moves.Add(move);
        }
 public GameRoom JoinOpenRoom(Player player)
 {
     GameRoom openRoom = _GameRooms.First(room => room.GamesCurrentState == GameRoom.GameStates.LookingForAnotherPlayer);
     openRoom.SecondUser = player;
     openRoom.GamesCurrentState = GameRoom.GameStates.GameInProgress;
     return openRoom;
 }
        public GameRoom GetRoom(Player player)
        {
            return _GameRooms.FirstOrDefault(r =>
                {
                    bool isRoom = false;

                    if (r.FirstUser != null && r.FirstUser.id == player.id)
                        isRoom = true;

                    if (r.SecondUser != null && r.SecondUser.id == player.id)
                        isRoom = true;

                    return isRoom;

                });
        }
        public Player GetPlayer(Player player)
        {
            GameRoom room = GetRoom(player);
            Player currentPlayer = null;

            if (room != null)
            {
                if (room.FirstUser.id == player.id)
                {
                    currentPlayer = room.FirstUser;
                }
                else
                {
                    currentPlayer = room.SecondUser;
                }
            }
            return currentPlayer;
        }