-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
138 lines (112 loc) · 4.43 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*jslint esversion: 6*/
let CharacterLines = new Map();
let lineIndex = 0;
$(document).ready(() => {
saveObject = data;
FindCharacterLines(saveObject);
let url = new URL(window.location.href);
let defaultCharacter = url.searchParams.get("c") || "Cynthia";
$("select#characters").val(defaultCharacter);
UpdateDialogueBox();
// When we pick a character from the dropdown.
$("select#characters").change(UpdateDialogueBox);
$("select#context-depth").change(UpdateDialogueBox);
$("input#director-mode").change(UpdateDialogueBox);
});
function UpdateDialogueBox(){
lineIndex = 0;
let dialogueBox = $("div#dialogue");
let contextDepth = $("select#context-depth").val();
dialogueBox.empty();
for (let node of CharacterLines.get($("select#characters").val())) {
dialogueBox.append(GenerateDialgueBlock(node, saveObject.nodes, contextDepth));
}
}
function GenerateDialgueBlock(node, nodelist, contextDepth) {
let returnValue = $("<table/>");
let parentNodes = getPreviousNodes(node, nodelist, contextDepth);
//console.log("Parent nodes: ", parentNodes);
lineIndex++;
console.log("Generating table");
for (let path of parentNodes) {
console.log("\t Looping");
if (contextDepth >= 3) {
let lastNode = path[path.length-1];
let nextNode = nodelist.find(node => node.id === lastNode.outbound[0].id);
path.push(nextNode);
path.forEach((pathNode, index) => {
returnValue.append(printNode(pathNode, lineIndex, index === path.length - 2));
});
} else {
path.forEach((pathNode, index) =>{
returnValue.append(printNode(pathNode, lineIndex, index === path.length - 1));
});
}
}
returnValue = $("</p>").append(returnValue);
returnValue.addClass("alternate " + (lineIndex%2 ? "odd" : "even"));
return returnValue;
}
function getPreviousNodes(node, nodelist, depth = 1) {
if (depth < 1) return [[node]];
let parentNodes = getParentNodes(node, nodelist);
let newArray = [];
if (depth > 1) {
let returnArray = [];
for (let parentNode of parentNodes) {
returnArray.push(...getPreviousNodes(parentNode, nodelist, depth - 1).map(nodeArray => {
nodeArray.push(node);
return nodeArray;
}));
}
return returnArray;
}
return parentNodes.map(parentNode => [parentNode, node]);
}
function getParentNodes(node, nodelist) {
return nodelist.filter(
preceedingNode => preceedingNode.outbound.some(
link => link.id === node.id
)
);
}
function printNode(node, lineIndex, primary = false) {
let returnValue = "";
if (node.type === "dialogue.Text") {
let text = node.text;
if ($("#director-mode").prop('checked')) {
lineIndex = node.id;
}
// Filter out lookup markup. Example: {the school|RevolverAcademy}
text = text.replace(/\{([^\{\}\|]*)(\|.*?)?\}/g, '$1');
// Filter out emotion markup. Example: [Aaaarghh!|s]
text = text.replace(/\[([^\[\]\|]*)(\|.*?)?\]/g, '$1');
let textSegments = text.split("|");
let firstNumber = true;
for (let textSegment of textSegments) {
if (primary) {
returnValue += `<tr><td style="width:100px">${firstNumber ? lineIndex : ""}</td ><td><span class="actor-name">${node.actor}:</span></td><td>${textSegment}</td></tr>`;
} else {
returnValue += `<tr class="context"><td></td><td><span class="actor-name">${node.actor}:</span></td><td>${textSegment}</td></tr>`;
}
firstNumber = false;
}
}
return returnValue;
}
function FindCharacterLines(saveObject){
for (let node of saveObject.nodes) {
if (node.type !== "dialogue.Text") {
continue;
}
if (!CharacterLines.has(node.actor)) {
CharacterLines.set(node.actor, []);
}
let characterDialogueList = CharacterLines.get(node.actor);
characterDialogueList.push(node);
}
for (let characterName of CharacterLines.keys()) {
let option = $(`<option value="${characterName}">${characterName}</option>`);
$("select#characters").append(option);
}
}