2014-07-02 23:11:36 -05:00
|
|
|
|
namespace Ficdown.Parser.Parser
|
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
using System;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-09 15:18:31 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Model.Parser;
|
2014-08-10 11:10:21 -05:00
|
|
|
|
using Model.Player;
|
|
|
|
|
using Model.Story;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-08-09 15:18:31 -05:00
|
|
|
|
internal class StateResolver : IStateResolver
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
private static readonly Random _random = new Random((int) DateTime.Now.Ticks);
|
|
|
|
|
private readonly IDictionary<string, string> _pageNames;
|
|
|
|
|
private readonly HashSet<string> _usedNames;
|
2014-08-10 11:10:21 -05:00
|
|
|
|
private Story _story;
|
2014-08-09 15:18:31 -05:00
|
|
|
|
|
|
|
|
|
public StateResolver()
|
|
|
|
|
{
|
|
|
|
|
_pageNames = new Dictionary<string, string>();
|
|
|
|
|
_usedNames = new HashSet<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 17:32:13 -05:00
|
|
|
|
public ResolvedStory Resolve(IEnumerable<PageState> pages, Story story)
|
2014-08-09 15:18:31 -05:00
|
|
|
|
{
|
2014-08-10 11:10:21 -05:00
|
|
|
|
_story = story;
|
2014-08-10 17:32:13 -05:00
|
|
|
|
return new ResolvedStory
|
|
|
|
|
{
|
|
|
|
|
Name = story.Name,
|
|
|
|
|
Description = story.Description,
|
|
|
|
|
FirstPage = GetPageNameForHash(pages.Single(p => p.Id.Equals(Guid.Empty)).CompressedHash),
|
|
|
|
|
Pages = pages.Select(
|
2014-08-09 15:18:31 -05:00
|
|
|
|
page =>
|
|
|
|
|
new ResolvedPage
|
|
|
|
|
{
|
|
|
|
|
Name = GetPageNameForHash(page.CompressedHash),
|
2014-08-10 15:25:20 -05:00
|
|
|
|
Content = ResolveDescription(page),
|
|
|
|
|
ActiveToggles = GetStateDictionary(page).Where(t => t.Value).Select(t => t.Key)
|
2014-08-10 17:32:13 -05:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
2014-08-09 15:18:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
private string ResolveAnchor(string blockName, int lineNumber, Anchor anchor, IDictionary<string, bool> playerState, string targetHash)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
var text = anchor.Text;
|
|
|
|
|
if (anchor.Href.Conditions != null)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2015-07-19 15:51:10 -05:00
|
|
|
|
var satisfied = Utilities.GetInstance(blockName, lineNumber).ConditionsMet(playerState, anchor.Href.Conditions);
|
|
|
|
|
var alts = Utilities.GetInstance(blockName, lineNumber).ParseConditionalText(text);
|
2014-08-09 15:18:31 -05:00
|
|
|
|
var replace = alts[satisfied];
|
|
|
|
|
text = RegexLib.EscapeChar.Replace(replace, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
return !string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(targetHash)
|
|
|
|
|
? string.Format("[{0}](/{1})", text, GetPageNameForHash(targetHash))
|
|
|
|
|
: text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ResolveDescription(PageState page)
|
|
|
|
|
{
|
2014-08-10 11:10:21 -05:00
|
|
|
|
var resolved = new StringBuilder();
|
2017-12-28 17:42:33 -06:00
|
|
|
|
if(!string.IsNullOrEmpty(page.Scene.Name))
|
|
|
|
|
resolved.AppendFormat("## {0}\n\n", page.Scene.Name);
|
2014-08-09 15:18:31 -05:00
|
|
|
|
|
2014-08-10 15:25:20 -05:00
|
|
|
|
var firstToggleCounter = 0;
|
2014-08-10 11:10:21 -05:00
|
|
|
|
for (var i = 0; i < page.State.ActionsToShow.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (page.State.ActionsToShow[i])
|
|
|
|
|
{
|
2014-08-10 15:25:20 -05:00
|
|
|
|
var actionTuple = _story.Actions.Single(a => a.Value.Id == i + 1);
|
2015-07-19 15:51:10 -05:00
|
|
|
|
var actionAnchors = Utilities.GetInstance(page.Scene.Name, page.Scene.LineNumber).ParseAnchors(actionTuple.Value.Description);
|
2014-08-10 15:25:20 -05:00
|
|
|
|
var anchorDict = GetStateDictionary(page);
|
2014-08-10 17:32:13 -05:00
|
|
|
|
if (
|
|
|
|
|
actionAnchors.Any(
|
|
|
|
|
a => a.Href.Conditions != null && a.Href.Conditions.ContainsKey(actionTuple.Key)))
|
2014-08-10 15:25:20 -05:00
|
|
|
|
{
|
|
|
|
|
if (page.State.ActionFirstToggles[firstToggleCounter++])
|
|
|
|
|
{
|
|
|
|
|
anchorDict[actionTuple.Key] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resolved.AppendFormat("{0}\n\n", actionAnchors.Aggregate(actionTuple.Value.Description,
|
|
|
|
|
(current, anchor) =>
|
|
|
|
|
current.Replace(anchor.Original,
|
2015-07-19 15:51:10 -05:00
|
|
|
|
ResolveAnchor(page.Scene.Name, page.Scene.LineNumber, anchor, anchorDict,
|
2014-08-10 15:25:20 -05:00
|
|
|
|
page.Links.ContainsKey(anchor.Original) ? page.Links[anchor.Original] : null))));
|
2014-08-10 11:10:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
var anchors = Utilities.GetInstance(page.Scene.Name, page.Scene.LineNumber).ParseAnchors(page.Scene.Description);
|
2014-08-10 15:25:20 -05:00
|
|
|
|
var stateDict = GetStateDictionary(page);
|
|
|
|
|
var text =
|
|
|
|
|
RegexLib.EmptyListItem.Replace(
|
|
|
|
|
anchors.Aggregate(page.Scene.Description,
|
|
|
|
|
(current, anchor) =>
|
|
|
|
|
current.Replace(anchor.Original,
|
2015-07-19 15:51:10 -05:00
|
|
|
|
ResolveAnchor(page.Scene.Name, page.Scene.LineNumber, anchor, stateDict,
|
2014-08-10 15:25:20 -05:00
|
|
|
|
page.Links.ContainsKey(anchor.Original) ? page.Links[anchor.Original] : null))),
|
|
|
|
|
string.Empty);
|
2014-08-09 15:18:31 -05:00
|
|
|
|
var seen = page.State.ScenesSeen[page.Scene.Id - 1];
|
2014-08-10 15:25:20 -05:00
|
|
|
|
resolved.Append(!seen
|
2014-08-10 11:10:21 -05:00
|
|
|
|
? RegexLib.BlockQuoteToken.Replace(text, string.Empty)
|
2014-08-10 15:25:20 -05:00
|
|
|
|
: RegexLib.BlockQuotes.Replace(text, string.Empty));
|
|
|
|
|
return resolved.ToString();
|
2014-08-09 15:18:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 17:32:13 -05:00
|
|
|
|
public static IDictionary<string, bool> GetStateDictionary(PageState page)
|
2014-08-09 15:18:31 -05:00
|
|
|
|
{
|
|
|
|
|
return page.StateMatrix.Where(matrix => page.State.PlayerState[matrix.Value])
|
|
|
|
|
.ToDictionary(m => m.Key, m => true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPageNameForHash(string hash)
|
|
|
|
|
{
|
|
|
|
|
if (!_pageNames.ContainsKey(hash))
|
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
do
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
name = RandomString(8);
|
|
|
|
|
} while (_usedNames.Contains(name));
|
|
|
|
|
_pageNames.Add(hash, name);
|
|
|
|
|
_usedNames.Add(name);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
2014-08-09 15:18:31 -05:00
|
|
|
|
return _pageNames[hash];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string RandomString(int size)
|
|
|
|
|
{
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26*_random.NextDouble() + 65)));
|
|
|
|
|
builder.Append(ch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-19 15:51:10 -05:00
|
|
|
|
}
|