public static ExecutionResult ReturnToGame(Player player, IGameRetrieverService _gameStoreService, ITextParsing textParsing, IGameProcessor gameProcessor)
        {
            player.PlayerFlag = PlayerFlag.IN_GAME.ToString();
            var games       = _gameStoreService.ListGames();
            var playerState = player.ActiveGameSave;
            var gameFound   = games.Where(a => a.GameName == playerState.GameName).FirstOrDefault();
            var state       = gameFound.FindStateById(playerState.StateId);
            var execResult  = new ExecutionResult
            {
                MessagesToShow = new List <MessageResult> {
                    new MessageResult {
                        Message = textParsing.ParseText(playerState, state.StateText)
                    }
                },
                OptionsToShow = gameProcessor.GetCurrentOptions(playerState, gameFound, state)
            };

            execResult.OptionsToShow.Add("-Menu-");
            return(execResult);
        }
Example #2
0
        public (List <MessageResult> Messages, DrawState EndingState, List <string> StatesVisited) RecursivelyHandleStates(DrawState currentState, PlayerGameSave gameSave, Player player, DrawGame game, bool withDataChanges = true, bool ignoreFrameShift = false)
        {
            if (withDataChanges)
            {
                HandleAnyAttachments(currentState, gameSave, player, false);
            }

            List <MessageResult> messages = new List <MessageResult>();
            var message = _textParsing.ParseText(gameSave, currentState.StateText);

            if (message != null && message.Trim().ToLower().StartsWith("#showbuiltimage"))
            {
                var imageUrl = _imageService.CreateImageAsync(_imageBuildDataTracker.GetParams()).Result;
                messages.Add(new MessageResult
                {
                    ImageUrl = imageUrl
                });
                message = "";
            }
            if (message != null && message.Trim().ToLower().StartsWith("#function"))
            {
                if (ignoreFrameShift)
                {
                    message = "";
                }
                else
                {
                    return(HandleFunction(message, currentState, gameSave, player, game, withDataChanges));
                }
            }
            if (message != null && message.Trim().ToLower().Equals("#return"))
            {
                return(HandleFunctionReturn(message, currentState, gameSave, player, game, withDataChanges));
            }

            if (!string.IsNullOrEmpty(message))
            {
                messages.Add(new MessageResult
                {
                    Message  = message,
                    ImageUrl = currentState.IsImage ? currentState.StateText : null,
                });
            }

            DrawState newState             = null;
            var       directTransitions    = currentState.StateOptions.Where(a => a.IsDirectTransition).ToList();
            var       nondirectTransitions = currentState.StateOptions.Where(a => !a.IsDirectTransition).ToList();
            var       resolvedTransitions  = nondirectTransitions.Select(a => (a.ResultState, _textParsing.ResolveOption(gameSave, a.StateText)));

            var validTransitions    = resolvedTransitions.Where(a => a.Item2.IsDirectTransition && a.Item2.DirectTransitionCommandResult && a.Item2.OptionType != OptionType.Fallback).ToList();
            var fallbackTransitions = resolvedTransitions.Where(a => a.Item2.IsDirectTransition && a.Item2.DirectTransitionCommandResult && a.Item2.OptionType == OptionType.Fallback).ToList();

            if (validTransitions.Count > 1)
            {
                throw new AdventureGameException("Found more than 1 applicable conditional direct transition");
            }
            else if (validTransitions.Count == 1)
            {
                newState = validTransitions.First().ResultState;
            }
            else if (directTransitions.Count > 1)
            {
                throw new AdventureGameException("Found more than 1 applicable direct transition");
            }
            else if (directTransitions.Count == 1)
            {
                newState = directTransitions.First().ResultState;
            }
            else if (fallbackTransitions.Count > 1)
            {
                throw new AdventureGameException("Found more than 1 applicable conditional direct transition");
            }
            else if (fallbackTransitions.Count == 1)
            {
                newState = fallbackTransitions.First().ResultState;
            }

            if (withDataChanges)
            {
                HandleAnyAttachments(currentState, gameSave, player, true);
            }

            if (newState == null)
            {
                return(messages, currentState, new List <string> {
                    currentState.Id
                });
Example #3
0
        public ExecutionResult HandleMessage(string message, Player player)
        {
            if (player.PlayerFlag != PlayerFlag.LOAD_GAME.ToString())
            {
                player.PlayerMenuContext = player.PlayerFlag.ToString();
                player.PlayerFlag        = PlayerFlag.LOAD_GAME.ToString();
                return(ExecutionResultHelper.SingleMessage("Select a game save to load", GetOptions(player)));
            }
            else if (message == Messages.Return)
            {
                player.PlayerMenuContext2 = "0";
                if (player.PlayerMenuContext == PlayerFlag.GAME_MENU.ToString())
                {
                    return(MessageHandlerHelpers.ReturnToGameMenu(player, ""));
                }
                return(MessageHandlerHelpers.ReturnToMainMenu(player));
            }
            else if (message.StartsWith("<"))
            {
                if (int.TryParse(player.PlayerMenuContext2, out int pageNumber))
                {
                    if (pageNumber <= 0)
                    {
                        pageNumber = 1;
                    }
                    player.PlayerMenuContext2 = "" + (pageNumber - 1);
                }
                else
                {
                    player.PlayerMenuContext2 = "0";
                }
                return(ExecutionResultHelper.SingleMessage("Select a game save to load", GetOptions(player)));
            }
            else if (message.StartsWith(">"))
            {
                if (int.TryParse(player.PlayerMenuContext2, out int pageNumber))
                {
                    if (pageNumber < 0)
                    {
                        pageNumber = 0;
                    }
                    player.PlayerMenuContext2 = "" + (pageNumber + 1);
                }
                else
                {
                    player.PlayerMenuContext2 = "" + player.GameSaves.Count / PageSize;
                }
                return(ExecutionResultHelper.SingleMessage("Select a game save to load", GetOptions(player)));
            }
            else
            {
                var saveIdString = message.Split(' ')[0];
                if (!int.TryParse(saveIdString, out var saveId))
                {
                    return(ExecutionResultHelper.SingleMessage("Invalid save specified", GetOptions(player)));
                }
                var gameSave = _gameSaveService.GetGameSaveById(saveId, player.PlayerId)?.PlayerGameSave;
                if (gameSave == null)
                {
                    return(ExecutionResultHelper.SingleMessage($"Save with ID {saveId} is invalid. Either it doesn't exist, or it doesn't belong to you!", GetOptions(player)));
                }

                player.PlayerMenuContext2 = "0";

                player.ActiveGameSave.GameName     = gameSave.GameName;
                player.ActiveGameSave.GameSaveData = gameSave.GameSaveData.Select(a => new PlayerGameSaveData {
                    Name = a.Name, Value = a.Value
                }).ToList();
                player.ActiveGameSave.StateId    = gameSave.StateId;
                player.ActiveGameSave.FrameStack = gameSave.FrameStack.Select(a => new PlayerFrameStack
                {
                    CreatedDate   = a.CreatedDate,
                    FunctionName  = a.FunctionName,
                    ReturnStateId = a.ReturnStateId,
                    Save          = player.ActiveGameSave,
                    SaveId        = player.ActiveGameSave.SaveId
                }).ToList();

                player.PlayerFlag = PlayerFlag.IN_GAME.ToString();
                var games       = _gameStore.ListGames();
                var playerState = player.ActiveGameSave;
                var gameFound   = games.Where(a => a.GameName == playerState.GameName).FirstOrDefault();
                var state       = gameFound.FindStateById(playerState.StateId);
                var execResult  = new ExecutionResult
                {
                    MessagesToShow = new List <MessageResult> {
                        new MessageResult {
                            Message = _textParsing.ParseText(playerState, state.StateText)
                        }
                    },
                    OptionsToShow = _gameProcessor.GetCurrentOptions(playerState, gameFound, state)
                };

                execResult.OptionsToShow.Add("-Menu-");
                return(execResult);
            }
        }