Ejemplo n.º 1
0
        public ExtensionPlayerJoinResult PlayerJoin(string broadcasterId, Guid characterId)
        {
            var result = new ExtensionPlayerJoinResult();

            if (string.IsNullOrEmpty(broadcasterId) || characterId == Guid.Empty)
            {
                result.ErrorMessage = "Invalid broadcaster or bad character id";
                return(result);
            }

            var activeSession = gameData.GetOwnedSessionByUserId(broadcasterId);

            if (activeSession == null)
            {
                result.ErrorMessage = "Broadcaster does not have an active game session";
                return(result);
            }

            if (TryGetSession(out var sessionInfo))
            {
                var c = gameData.GetCharacter(characterId);
                if (c == null)
                {
                    result.ErrorMessage = "No such character.";
                    return(result);
                }

                var myUser = gameData.GetUser(c.UserId);
                if (myUser == null || myUser.UserId != sessionInfo.UserId)
                {
                    result.ErrorMessage = "No such user.";
                    return(result);
                }

                if (c.UserId != myUser.Id)
                {
                    result.ErrorMessage = "Well, that is not your character.";
                    return(result);
                }

                // We are just going to assume that it was successeful
                // since, if it "fails" as the character is already in game. well. Then what harm done?
                result.Success = true;
                result.Player  = c.MapForWebsite(gameData, myUser);


                sessionInfoProvider.SetActiveCharacter(sessionInfo, characterId);

                var gameEvent = gameData.CreateSessionEvent(
                    GameEventType.PlayerAdd,
                    activeSession,
                    new PlayerAdd()
                {
                    CharacterId = characterId,
                    Identifier  = c.Identifier,
                    UserId      = sessionInfo.UserId,
                    UserName    = sessionInfo.UserName
                });

                gameData.Add(gameEvent);
            }
            else
            {
                result.ErrorMessage = "You don't seem to be authenticated.";
                return(result);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ExtensionPlayerJoinResult> PlayerJoin(string broadcasterId)
        {
            var result = new ExtensionPlayerJoinResult();

            if (string.IsNullOrEmpty(broadcasterId))
            {
                result.ErrorMessage = "Invalid broadcaster or bad character id";
                return(result);
            }

            var activeSession = gameData.GetOwnedSessionByUserId(broadcasterId);

            if (activeSession == null)
            {
                result.ErrorMessage = "Broadcaster does not have an active game session";
                return(result);
            }

            if (TryGetSession(out var sessionInfo))
            {
                var user = gameData.GetUser(sessionInfo.AccountId);
                if (user == null)
                {
                    result.ErrorMessage = "No such user.";
                    return(result);
                }

                var existingCharacters = gameData.GetCharactersByUserId(user.Id);
                if (existingCharacters.Count >= 3)
                {
                    result.ErrorMessage = "You already have 3 characters.";
                    return(result);
                }

                var index  = existingCharacters.Count + 1;
                var player = await playerManager.CreatePlayer(user.UserId, user.UserName, index.ToString());

                if (player == null)
                {
                    result.ErrorMessage = "Failed to create a new character.";
                    return(result);
                }

                var c = gameData.GetCharacter(player.Id);
                if (c == null)
                {
                    result.ErrorMessage = "Okay. We don't know what went wrong here. Very sorry!";
                    return(result);
                }

                // We are just going to assume that it was successeful
                // since, if it "fails" as the character is already in game. well. Then what harm done?
                result.Success = true;
                result.Player  = c.MapForWebsite(gameData, user);

                sessionInfoProvider.SetActiveCharacter(sessionInfo, c.Id);

                var gameEvent = gameData.CreateSessionEvent(
                    GameEventType.PlayerAdd,
                    activeSession,
                    new PlayerAdd()
                {
                    CharacterId = c.Id,
                    Identifier  = c.Identifier,
                    UserId      = sessionInfo.UserId,
                    UserName    = sessionInfo.UserName
                });

                gameData.Add(gameEvent);
            }
            else
            {
                result.ErrorMessage = "You don't seem to be authenticated.";
                return(result);
            }

            return(result);
        }