2014-07-02 23:11:36 -05:00
|
|
|
|
namespace Ficdown.Parser.Player
|
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
using Model.Story;
|
2014-07-26 23:54:50 -05:00
|
|
|
|
using Model.Traverser;
|
|
|
|
|
using Parser;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-08-09 15:18:31 -05:00
|
|
|
|
internal class GameTraverser : IGameTraverser
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
private StateManager _manager;
|
|
|
|
|
private Queue<StateQueueItem> _processingQueue;
|
|
|
|
|
private IDictionary<string, PageState> _processed;
|
|
|
|
|
private IDictionary<string, PageState> _compressed;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-08-09 15:18:31 -05:00
|
|
|
|
private Story _story;
|
|
|
|
|
public Story Story
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-08-09 15:18:31 -05:00
|
|
|
|
get { return _story; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_story = value;
|
|
|
|
|
_manager = new StateManager(_story);
|
|
|
|
|
_processingQueue = new Queue<StateQueueItem>();
|
|
|
|
|
_processed = new Dictionary<string, PageState>();
|
|
|
|
|
_compressed = new Dictionary<string, PageState>();
|
|
|
|
|
}
|
2014-07-26 23:54:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PageState> Enumerate()
|
|
|
|
|
{
|
|
|
|
|
// generate comprehensive enumeration
|
|
|
|
|
|
2014-07-27 00:43:32 -05:00
|
|
|
|
var initial = _manager.InitialState;
|
|
|
|
|
_processingQueue.Enqueue(new StateQueueItem
|
|
|
|
|
{
|
|
|
|
|
Page = initial,
|
|
|
|
|
AffectedStates = new List<State> {initial.AffectedState}
|
|
|
|
|
});
|
2014-07-26 23:54:50 -05:00
|
|
|
|
while (_processingQueue.Count > 0)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var state = _processingQueue.Dequeue();
|
2014-07-27 00:43:32 -05:00
|
|
|
|
if (!_processed.ContainsKey(state.Page.UniqueHash))
|
2014-07-26 23:54:50 -05:00
|
|
|
|
{
|
2014-07-27 00:43:32 -05:00
|
|
|
|
_processed.Add(state.Page.UniqueHash, state.Page);
|
2014-07-26 23:54:50 -05:00
|
|
|
|
ProcessState(state);
|
|
|
|
|
}
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
// compress redundancies
|
2014-08-09 15:18:31 -05:00
|
|
|
|
foreach (var row in _processed)
|
|
|
|
|
{
|
|
|
|
|
if (!_compressed.ContainsKey(row.Value.CompressedHash))
|
|
|
|
|
{
|
|
|
|
|
var scene = row.Value;
|
|
|
|
|
var links = scene.Links.Keys.ToArray();
|
|
|
|
|
foreach (var link in links)
|
|
|
|
|
{
|
|
|
|
|
scene.Links[link] = _processed[scene.Links[link]].CompressedHash;
|
|
|
|
|
}
|
|
|
|
|
_compressed.Add(row.Value.CompressedHash, row.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-08-09 15:18:31 -05:00
|
|
|
|
return _compressed.Values;
|
2014-07-26 23:54:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 00:43:32 -05:00
|
|
|
|
private void ProcessState(StateQueueItem currentState)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var states = new HashSet<string>();
|
2014-07-27 00:43:32 -05:00
|
|
|
|
|
|
|
|
|
var anchors = Utilities.ParseAnchors(currentState.Page.Scene.Description);
|
|
|
|
|
var conditionals =
|
|
|
|
|
anchors.SelectMany(
|
|
|
|
|
a => a.Href.Conditions != null ? a.Href.Conditions.Select(c => c.Key) : new string[] {})
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToArray();
|
|
|
|
|
var hasFirstSeen = RegexLib.BlockQuotes.IsMatch(currentState.Page.Scene.Description);
|
|
|
|
|
|
|
|
|
|
foreach (var affected in currentState.AffectedStates)
|
|
|
|
|
{
|
|
|
|
|
// signal to previous scenes that this scene's used conditionals are important
|
|
|
|
|
foreach (var conditional in conditionals) _manager.ToggleStateOn(affected, conditional);
|
|
|
|
|
|
|
|
|
|
// signal to previous scenes if this scene has first-seen text
|
|
|
|
|
if (hasFirstSeen) _manager.ToggleSeenSceneOn(affected, currentState.Page.Scene.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var anchor in anchors.Where(a => a.Href.Target != null || a.Href.Toggles != null))
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-27 00:43:32 -05:00
|
|
|
|
var newState = _manager.ResolveNewState(anchor, currentState.Page);
|
|
|
|
|
if (!currentState.Page.Links.ContainsKey(anchor.Original))
|
|
|
|
|
currentState.Page.Links.Add(anchor.Original, newState.UniqueHash);
|
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
if (!states.Contains(newState.UniqueHash) && !_processed.ContainsKey(newState.UniqueHash))
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
states.Add(newState.UniqueHash);
|
2014-07-27 00:43:32 -05:00
|
|
|
|
var newAffected = new List<State>(currentState.AffectedStates);
|
|
|
|
|
newAffected.Add(newState.AffectedState);
|
|
|
|
|
_processingQueue.Enqueue(new StateQueueItem {Page = newState, AffectedStates = newAffected});
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-26 23:54:50 -05:00
|
|
|
|
}
|