Exemplo n.º 1
0
        /// <summary>
        /// Sends response to provided number
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="number"></param>
        public void ReplyToNumber(long userId, int number)
        {
            // Try-catch block: if something goes wrong replies with "Index not found"
            try
            {
                // Getting save
                var save = saves.Get(userId);

                if (save.Current.Story != null)       // IF story already selected
                {
                    if (save.Current.Episode != null) // IF episode already selected
                    {
                        // Getting story from DB
                        StoryDocument story = stories.GetEpisode(save.Current.Story.Value, save.Current.Episode.Value);

                        // Finding selected option
                        StoryOption selectedOption = story.GetStoryline(save.Current.Storyline)
                                                     .Elements[save.Current.Position]
                                                     .Options[number - 1];

                        if (selectedOption.Storyline == "Ending") // If selected option is ending...
                        {
                            // Add ending to progress
                            save.AddEnding(story.StoryId, story.Episode, selectedOption.Position.Value);

                            // Send messages
                            messageSender.Send(userId, messageBuilder.BuildEnding(story, selectedOption.Position.Value));
                            var episodes = stories.GetStoryEpisodes(story.StoryId);
                            var progress = save.GetStoryStats(story.StoryId);
                            messageSender.Send(userId, messageBuilder.BuildEpisodeSelectDialog(episodes, progress));
                        }
                        else // Default case
                        {
                            // Updating save progress
                            if (selectedOption.Storyline != null)
                            {
                                save.Current.Storyline = selectedOption.Storyline;
                            }
                            if (selectedOption.Position != null)
                            {
                                save.Current.Position = selectedOption.Position.Value;
                            }

                            // "Needed" check
                            if (selectedOption.Needed != null && !selectedOption.Needed.All(save.Current.Unlockables.Contains))
                            {
                                goto IndexNotFound;
                            }

                            // "Unlocks" handling
                            if (!string.IsNullOrEmpty(selectedOption.Unlocks))
                            {
                                if (!save.Current.Unlockables.Contains(selectedOption.Unlocks))
                                {
                                    save.AddUnlockable(selectedOption.Unlocks);
                                }
                                else
                                {
                                    goto IndexNotFound;
                                }
                            }

                            // If selected option contains an achievement
                            if (selectedOption.Achievement != null)
                            {
                                // Add achievement to save stats
                                save.AddAchievement(story.StoryId, story.Episode, selectedOption.Achievement.Value);
                                // Edit response message
                                messageSender.Send(userId, messageBuilder.BuildAchievement(story.Achievements[selectedOption.Achievement.Value]));
                            }

                            // Send created message
                            var storylineElement = story.GetStoryline(save.Current.Storyline).Elements[save.Current.Position];
                            messageSender.Send(userId, messageBuilder.BuildContent(storylineElement, save.Current.Unlockables));
                        }
                    }
                    else // From episode selection
                    {
                        // Check that previous episode's canonical ending completed
                        if (number == 0 || save.GetStoryStats(save.Current.Story.Value).Episodes[number - 1].ObtainedEndings.Contains(0))
                        {
                            // Get episode by provided number
                            StoryDocument story = stories.GetEpisode(save.Current.Story.Value, number);

                            // Update current progress
                            save.Current.Episode   = number;
                            save.Current.Storyline = story.Storylines[0].Tag;
                            save.Current.Position  = 0;

                            // Send message
                            messageSender.Send(userId, messageBuilder.BuildContent(story.Storylines[0].Elements[0], save.Current.Unlockables));
                        }
                        else
                        {
                            goto IndexNotFound;
                        }
                    }
                }
                else // From story selection
                {
                    // Find story progress
                    var stats = save.GetStoryStats(number);

                    // Get all story episodes
                    var episodes = stories.GetStoryEpisodes(number);
                    save.Current.Story = number;

                    // Send episode select dialog
                    messageSender.Send(userId, messageBuilder.BuildEpisodeSelectDialog(episodes, stats));
                }

                // Finally update save
                saves.Update(save);
                return;
            }
            catch (Exception e)
            {
                if (e is IndexOutOfRangeException || e is ArgumentOutOfRangeException)
                {
                    goto IndexNotFound;
                }
                else
                {
                    throw;
                }
            }

IndexNotFound:         // If something went wrong...
            messageSender.Send(userId, messageBuilder.BuildIndexOutOfRangeMessage());
        }