-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.js
234 lines (222 loc) · 6.82 KB
/
play.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Generated by IcedCoffeeScript 1.8.0-d
(function() {
var $, assert, choiceChars, compile, elem, hashchange, newGame, normalize, prepare, quote, restore, stdlib, turn, _ref;
choiceChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
assert = require('assert');
$ = require('jquery');
_ref = require('./compiler'), quote = _ref.quote, stdlib = _ref.stdlib, prepare = _ref.prepare;
elem = stdlib().elem;
normalize = function(s) {
return s.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/, ' ');
};
exports.compile = compile = function(src, opts) {
var firstPassage, handleError, passages, render;
if (opts == null) {
opts = {};
}
handleError = opts.handleError || function(msg) {
return console.log(msg);
};
passages = {};
firstPassage = null;
(function() {
var lastPassage, m, re;
re = /(?:^\s*\n?|\n\n)#(?!\{)\s*([^\n]*\S)\s*\n/g;
lastPassage = null;
while (m = re.exec(src)) {
if (lastPassage) {
lastPassage.endIndex = m.index;
}
lastPassage = {
name: normalize(m[1]),
startIndex: re.lastIndex
};
assert(!(lastPassage.name in passages), lastPassage.name);
passages[lastPassage.name] = lastPassage;
firstPassage || (firstPassage = lastPassage);
}
if (!lastPassage) {
handleError('no passages found');
}
lastPassage.endIndex = src.length;
})();
(function() {
var k, v, _fn;
_fn = function() {
v.src = src.substring(v.startIndex, v.endIndex);
v.mungedSrc = v.src.replace(/\[\[([^\]]*)\]\]/g, function(outer, inner, index) {
var m, offset, target, text;
offset = index + v.startIndex;
text = target = inner;
if (m = /^(.*)->\s*([^<>]*[^\s<>])\s*$/.exec(inner)) {
text = m[1];
target = m[2];
} else if (m = /^\s*([^<>]*[^\s<>])\s*<-(.*)$/.exec(inner)) {
target = m[1];
text = m[2];
}
target = normalize(target);
if (!(target in passages)) {
handleError("bad link target '" + target + "' at " + outer + ", passage '" + k + "', offset " + offset);
}
return "#\{imbroglio.mkLink " + (quote(target)) + ", " + (quote(text)) + "}";
});
v.prepared = prepare(v.mungedSrc, {
argNames: ['imbroglio'],
thisVar: 'imbroglio.state',
handleError: function(e) {
console.log(e);
if (e.error instanceof Error) {
throw e.error;
} else if (e.error) {
throw new Error(e.error);
} else {
throw new Error(e);
}
}
});
};
for (k in passages) {
v = passages[k];
_fn();
}
})();
render = function(passage, result) {
var linkCount, links, mkLink, moves, state;
if (result == null) {
result = {};
}
state = result.state || {};
delete result.state;
moves = result.moves || (result.moves = '');
links = {};
linkCount = 0;
mkLink = function(target, text) {
var choiceChar, el;
if (!(target in passages)) {
return elem('span', {
"class": 'error'
}, "ERROR: bad link target '" + target + "' in passage '" + passage.name + "': " + text);
} else if (linkCount >= choiceChars.length) {
return elem('span', {
"class": 'error'
}, "ERROR: too many links, passage '" + passage.name + "', target '" + target + "': " + text);
}
choiceChar = choiceChars[linkCount++];
el = elem('a', {
"class": 'choice',
href: "#!" + moves + choiceChar
}, text);
links[choiceChar] = {
el: el,
target: target
};
return el;
};
result.passageElem = passage.prepared(stdlib({
mkLink: mkLink,
state: state
}));
result.choose = function(ch) {
var link;
if (!(link = links[ch])) {
console.log("invalid move " + ch + " from passage " + passage.name);
return null;
}
$(link.el).addClass('chosen');
return render(passages[link.target], {
moves: "" + moves + ch,
state: state
});
};
return result;
};
return function() {
return render(firstPassage);
};
};
newGame = turn = null;
restore = function(moves) {
var $chosen, $output, $p, ch, last, prevTurn, scrollPos, _i, _len, _ref1;
if ((turn != null ? turn.moves : void 0) === moves) {
return;
}
$('#loading').show();
$('.pane').hide();
$('#game').hide();
$output = $('#output');
last = function() {
var children;
children = $output.children();
if (children.length) {
return $(children.get(children.length - 1));
} else {
return children;
}
};
if (!turn || turn.moves !== moves.slice(0, turn.moves.length)) {
turn = newGame();
$output.empty();
$output.append(turn.passageElem);
}
_ref1 = moves.slice(turn.moves.length);
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
ch = _ref1[_i];
prevTurn = turn;
if (!(turn = turn.choose(ch))) {
$('#404-pane').show();
$('#loading').hide();
return;
}
turn.prevTurn = prevTurn;
$output.append(turn.passageElem);
}
$output.children().removeClass('current');
last().addClass('current');
$('#game').show();
$('#loading').hide();
$p = $(turn.passageElem);
$chosen = $('.chosen');
scrollPos = !$chosen.length ? 0 : $($chosen.get($chosen.length - 1)).offset().top;
console.log("XXX scrollPos = " + scrollPos);
window.scrollTo(0, scrollPos);
};
hashchange = function() {
var hash, m, target;
hash = window.location.hash.replace(/^#/, '');
if (m = /^!(.*)$/.exec(hash)) {
return restore(m[1]);
}
turn = target = null;
if (!hash) {
target = $('#home');
} else if (m = /^\/([a-z][a-z0-9-]*)$/.exec(hash)) {
target = $("#" + m[1] + "-pane");
}
if (!(target != null ? target.length : void 0)) {
target = $('#404-pane');
}
$('#game').hide();
$('#output').empty();
$('.pane').hide();
target.show();
$('#loading').hide();
window.scrollTo(0, 0);
};
exports.start = function(src) {
newGame = compile(src);
return $(function() {
(function() {
var m, _ref1;
m = /([^\/]+)$/.exec(((_ref1 = window.location) != null ? _ref1.pathname : void 0) || '');
$('a[href="#"], a[href="#/"]').attr('href', m ? m[1] : '.');
})();
$(window).on('hashchange', function(e) {
e.preventDefault();
hashchange();
return true;
});
hashchange();
});
};
}).call(this);