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
|
|
|
|
|
|
|
|
|
internal class GameTraverser
|
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
private readonly StateManager _manager;
|
|
|
|
|
private readonly Queue<PageState> _processingQueue;
|
|
|
|
|
private readonly IDictionary<string, PageState> _processed;
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
public GameTraverser(Story story)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
_manager = new StateManager(story);
|
|
|
|
|
_processingQueue = new Queue<PageState>();
|
|
|
|
|
_processed = new Dictionary<string, PageState>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PageState> Enumerate()
|
|
|
|
|
{
|
|
|
|
|
// generate comprehensive enumeration
|
|
|
|
|
|
|
|
|
|
_processingQueue.Enqueue(_manager.InitialState);
|
|
|
|
|
while (_processingQueue.Count > 0)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var state = _processingQueue.Dequeue();
|
|
|
|
|
if (!_processed.ContainsKey(state.UniqueHash))
|
|
|
|
|
{
|
|
|
|
|
_processed.Add(state.UniqueHash, state);
|
|
|
|
|
ProcessState(state);
|
|
|
|
|
}
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
// compress redundancies
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
return _processed.Values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessState(PageState currentState)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var states = new HashSet<string>();
|
|
|
|
|
foreach (
|
|
|
|
|
var anchor in
|
|
|
|
|
Utilities.ParseAnchors(currentState.Scene.Description)
|
|
|
|
|
.Where(a => a.Href.Target != null || a.Href.Toggles != null))
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var newState = _manager.ResolveNewState(anchor, currentState);
|
|
|
|
|
if (!currentState.Links.ContainsKey(anchor.Original))
|
|
|
|
|
currentState.Links.Add(anchor.Original, newState.UniqueHash);
|
|
|
|
|
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);
|
|
|
|
|
_processingQueue.Enqueue(newState);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-26 23:54:50 -05:00
|
|
|
|
}
|