Ejemplo n.º 1
0
        protected override void Handle(BotPlayerJoin data, PlayerConnection connection)
        {
            logger.LogDebug($"Player join Request from {connection.Player.Id}. User: {data.Username}, Character: {data.CharacterIndex}");

            var user = userManager.GetByTwitchId(data.TwitchId) ?? userManager.GetByYouTubeId(data.TwitchId) ?? userManager.Get(data.Username);

            if (user == null)
            {
                user = userManager.Create(data.Username, data.TwitchId, data.YouTubeId);
            }
            else
            {
                userManager.LinkSreamerId(user, data.TwitchId, data.YouTubeId);
            }

            var player = playerProvider.Get(user, data.CharacterIndex);

            if (player == null)
            {
                // this user doesnt have any players yet.
                // create one for them.
                player = playerProvider.CreateRandom(user, user.Username);
            }

            var session = sessionManager.Get(data.Session);

            if (session != null)
            {
                if (sessionManager.InSession(user, session))
                {
                    // we cannot join with another player as we are already in
                    // this session.

                    logger.LogDebug($"Cannot add User: {data.Username}, Character: {data.CharacterIndex} to this session as the user already have a player in this session.");
                    return;
                }

                // check if this player is in an active session already.
                // if it is, we cannot join IF that session is hosted by the same user.
                // otherwise remove it from the existing session
                var activePlayerSession = sessionManager.Get(player);
                if (activePlayerSession != null)
                {
                    if (activePlayerSession.Host != null && activePlayerSession.Host.Player != null && activePlayerSession.Host.Player.Id == player.Id)
                    {
                        logger.LogDebug($"Cannot add User: {data.Username}, Character: {data.CharacterIndex} to this session as the character is being used in a hosted session.");
                        return;
                    }

                    var playerConnection = connectionProvider.GetPlayerConnection(player);
                    if (playerConnection != null && playerConnection.User?.Id == user.Id)
                    {
                        logger.LogDebug($"Cannot add User: {data.Username}, Character: {data.CharacterIndex} to this session as the character is being used in a hosted session.");
                        return;
                    }

                    worldProcessor.RemovePlayer(player);
                }

                session.AddPlayer(player);
                worldProcessor.AddPlayer(player);
            }
        }