Skip to content

Commit

Permalink
Start on perma play feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattConrad committed May 18, 2016
1 parent 5b89534 commit 8bc61a5
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 17 deletions.
50 changes: 33 additions & 17 deletions Controllers/HomeController.cs
Expand Up @@ -13,6 +13,7 @@ public class HomeController : Controller
public static readonly string _inkJsonsDirectory = "/AppData/InkJsons/";
public static readonly string _rawInksDirectory = "/AppData/RawInks/";
private static readonly string _gameStatesDirectory = "/AppData/GameStates/";
private static readonly string _permaplaysDirectory = "/Permaplays/";

//_rootPath is a filesystem path, for writing .ink/.jsons. _webAppPath is a URL modifier that is used instead of ~ (tilde, ofc, doesn't work with nginx)
// tilde handling is under discussion: https://github.com/aspnet/Announcements/issues/57 doesn't quite look like this is speaking to my issue, though.
Expand All @@ -25,16 +26,32 @@ public HomeController(Microsoft.Extensions.PlatformAbstractions.IApplicationEnvi
_webAppPath = config["WebAppPath"];
}

public IActionResult Index()
public ViewResult Index()
{
ViewBag.SessionGuid = Guid.NewGuid();
ViewBag.WebAppPath = _webAppPath;
return View();
}

public IActionResult ContinueStory(Guid sessionGuid, int? choiceIndex)
public ViewResult PlayOnly(string playId)
{
string inkJsonPath = _rootPath + _inkJsonsDirectory + sessionGuid + ".json";
ViewBag.SessionGuid = Guid.NewGuid();
ViewBag.WebAppPath = _webAppPath;
ViewBag.PlayId = playId;

//make sure it's a valid path.
string inkPath = _rootPath + _permaplaysDirectory + playId + ".json";
ViewBag.InkFileExists = System.IO.File.Exists(inkPath);

return View();
}

public JsonResult ContinueStory(Guid sessionGuid, string playId, int? choiceIndex)
{
//if we have a playId, this is a permaplay story, and we load it from permaplays insetad of inkJsons.
string inkJsonPath = string.IsNullOrEmpty(playId)
? _rootPath + _inkJsonsDirectory + sessionGuid + ".json"
: _rootPath + _permaplaysDirectory + playId + ".json";
string gameStatePath = _rootPath + _gameStatesDirectory + sessionGuid + ".json";

//if no choices at all, this means we're starting a new story.
Expand All @@ -53,7 +70,18 @@ public IActionResult ContinueStory(Guid sessionGuid, int? choiceIndex)
return Json(outputs);
}

public IActionResult PlayInk(string inktext, Guid sessionGuid)
private JsonResult StartNewStory(string inkJsonPath, string gameStatePath)
{
var story = Models.InkMethods.LoadEmptyStory(inkJsonPath);

List<InkOutputMessage> outputs = InkMethods.GetStoryOutputMessages(story);

InkMethods.SaveStory(gameStatePath, story);

return Json(outputs);
}

public JsonResult PlayInk(string inktext, Guid sessionGuid)
{
try
{
Expand Down Expand Up @@ -91,7 +119,7 @@ public IActionResult PlayInk(string inktext, Guid sessionGuid)
{
try
{
var errors = GetInklecateErrors(x.Message);
var errors = GetInklecateErrors(x.Message);
return Json(new { errors = errors });
}
//MWCTODO: this means GetInklecateErrors() threw a new exception, should also write to the internal log (figure out the RC2 way of doing this)
Expand Down Expand Up @@ -153,17 +181,5 @@ private void AddCateError(string errorMessage, int start, int end, Regex re, ref
errs.Add(new CateError() { Message = msg, LineNumber = line });
}

private IActionResult StartNewStory(string inkJsonPath, string gameStatePath)
{
var story = Models.InkMethods.LoadEmptyStory(inkJsonPath);

List<InkOutputMessage> outputs = InkMethods.GetStoryOutputMessages(story);

InkMethods.SaveStory(gameStatePath, story);

return Json(outputs);
}


}
}
3 changes: 3 additions & 0 deletions Models/InkModels.cs
Expand Up @@ -42,7 +42,10 @@ public static List<InkOutputMessage> GetStoryOutputMessages(Story story)
{
outputs.Add(new InkOutputMessage() { MessageType = InkOutputMessageTypes.Text, OutputText = story.Continue() });
}

outputs.AddRange(story.currentChoices.Select(c => new InkOutputMessage() { MessageType = InkOutputMessageTypes.Choice, ChoiceIndex = c.index, OutputText = c.text }));

outputs = outputs.Select(o => { o.OutputText = System.Net.WebUtility.HtmlEncode(o.OutputText); return o; }).ToList();

return outputs;
}
Expand Down
5 changes: 5 additions & 0 deletions Startup.cs
Expand Up @@ -49,6 +49,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

app.UseMvc(routes =>
{
routes.MapRoute(
"perma",
"play/{playId}",
new { controller = "Home", action = "PlayOnly" }
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
Expand Down
89 changes: 89 additions & 0 deletions Views/Home/PlayOnly.cshtml
@@ -0,0 +1,89 @@
@{
string webAppPath = ViewBag.WebAppPath == null ? "/" : (string)ViewBag.WebAppPath;
}

<form>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<p>Welcome! Enjoy playing the story. If you'd like to try writing one of your own, see the main Quill page.</p>
</div>
</div>
<hr>
<div class="row" style="padding-top: 10px;" >
<div class="col-md-6 col-md-offset-3 col-xs-12">
<div id="story-main-div" class="col-md-12"></div>
<div id="story-choices-div" class="col-md-12"></div>
<div id="story-status-div" class="col-md-12"></div>
<!--reset-story link somewhere down in here?-->
</div>
</div>
</form>

@section scripts {

<script type="text/javascript">
var continueStoryUrl = '@(webAppPath)Home/ContinueStory';
var sessionGuid = '@(((Guid)ViewBag.SessionGuid).ToString())';
var playId = '@ViewBag.PlayId.ToString()';
var $storyMainDiv;
var $storyChoicesDiv;
var $storyStatusDiv;
$(document).ready(function () {
$storyMainDiv = $("#story-main-div");
$storyChoicesDiv = $("#story-choices-div");
$storyStatusDiv = $("#story-status-div");
$storyChoicesDiv.on('click', '.story-choice-link', function () {
$storyChoicesDiv.empty();
continueStory($(this).data('cindex'), null);
return false;
})
continueStory(null);
});
function emptyAll() {
$storyMainDiv.empty();
$storyChoicesDiv.empty();
$storyStatusDiv.empty();
}
function continueStory(choiceIndex) {
$("#story-main-div p.new-text").removeClass("new-text");
$.getJSON(
continueStoryUrl,
{ "sessionGuid": sessionGuid, "playId": playId, "choiceIndex": choiceIndex },
function (messages) {
var currentChoices = processStoryMessages(messages);
}
);
}
//displays results from the current message set, and returns the set of valid choices for the present halt (for possibly choice replaying).
function processStoryMessages(messages) {
var currentChoices = [];
for (var i = 0; i < messages.length; i++) {
if (messages[i].messageType == 'text') {
$storyMainDiv.append($('<p class="new-text"></p>').html(messages[i].outputText));
} else if (messages[i].messageType == 'choice') {
currentChoices.push(messages[i].choiceIndex);
$storyChoicesDiv.append($('<p><a href="#" class="story-choice-link" data-cindex="' + messages[i].choiceIndex + '">' + messages[i].outputText + '</a></p>'));
} else if (messages[i].messageType == 'error') {
$storyMainDiv.append($('<p class="alert alert-danger"></p>').html(messages[i].outputText));
}
}
if (messages.length > 0 && currentChoices.length > 0) $storyMainDiv.append('<hr />');
if (currentChoices.length == 0) $storyChoicesDiv.append($('<p></p>').html('<em>(story complete)</em>'));
$storyMainDiv.scrollTop($storyMainDiv.prop('scrollHeight') - $storyMainDiv.innerHeight());
$storyChoicesDiv.scrollTop($storyChoicesDiv.prop('scrollHeight') - $storyChoicesDiv.innerHeight());
return currentChoices;
}
</script>
}

0 comments on commit 8bc61a5

Please sign in to comment.