-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patheditor.html
360 lines (244 loc) · 10.7 KB
/
editor.html
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>body { max-width: 600px; margin: auto; color: #444444 } textarea { width: 100%; height: 80vh }</style>
<script>
const dream = `
#1
あなたの名前を入力してください
主人公 <=
決定 #2
#2
ここに小説を書くと、文中に現れた「主人公」という文字は、読み手が最初に入力したものに置き換わります。
`.trim();
const quiz = `
#1
(選択肢は、行末にスペースを入れて「#2」のように移動先のページを書くことで作れます)
問題.日本で一番高い山は?
・富士山 #2
・北岳 #3
#2
(3行下に書いてある「正解数 +1」という命令により、コンピューターが正解数をカウントアップしてくれます)
正解!
正解数 +1
・最終問題へ #4
#3
ざんねん、それは日本で二番目です…
・最終問題へ #4
#4
手首につけるアクセサリーといえば?
・ネックレス #5
・ブレスレット #6
#5
ざんねん、それは首につけます…
・結果発表へ #7
#6
正解!
正解数 +1
・結果発表へ #7
#7
(選択肢の「#数字」の後にスペースを入れて、条件付きの選択肢を作れます。=は値と等しい、>=は以上、<=は以下、>は値より大きい、<は値より小さいとき、という意味)
全問終了!さてあなたの評価は…
・Sランク #8 正解数>=2
・Aランク #9 正解数=1
・Bランク #10
#8
すごい!
#9
ふつう!
#10
しょぼい!!
`.trim();
const adventure = `
#1
(値を設定する命令は、一行で「名前 :数字」と書きます。:と数字は半角じゃないとダメ)
私は宝とか平和とかを求め、たいまつを片手に洞窟の入り口に立った。
【ステータスと持ち物】
HP:10
たいまつ:1
さて、どうしようか。
・洞窟に入る #2
・洞窟に入らない #3
#2
(たいまつを持っている場合は使う。クイズの例のランク付けとは違い、たいまつを1個以上持っていれば使ってもよいし、使わなくてもよい、という場合には条件付き選択肢を無条件選択肢よりも「下」に書く)
入ったら暗かった。
たいまつに灯をともして明るくすると安全かもしれない。
どうしようかな?
・このまま進む #5
・たいまつに灯をともす #6 たいまつ >= 1
#3
(値を増やす命令は一行で「名前 +数字」、値を減らす命令は「名前 -数字」と書きます。ここではHPを減らしてみます)
洞窟の上から落石だ!
ぎゃー!!!
HP -3
まだまだたくさん降ってくる、ヤバい!
洞窟の中へ逃げ込む #2
#4
死。
#5
(さいころを振るように、値をランダムに決めるには「名前 ~数字」と書きます。数字は何面のさいころなのかを表すので、~6なら1~6の目が出ます)
【ランダムイベント】
暗くて足元がよく見えない…
たいまつに灯をつけていない場合、ひょっとすると転んでしまうかもしれないし、幸運にも転ばないかもしれない。
もっと露骨に言うと、6面のさいころを振って1,2,3,4が出たら転んでしまうかもしれない!!
さいころの目 ~6
・明るいので絶対転ばない #8 火のついたたいまつ >= 1
・転ぶ #7 さいころの目 <= 4
・転ばない #8
#6
たいまつに灯をつけた。明るい!転んだりしなくて済みそうだ。
たいまつ -1
火のついたたいまつ +1
・次へ #2
#7
(変数同士も足したり引いたりできる。ランダムと足し合わせて、狙ったダメージを与える例)
転んだ!
ぎゃー!!!
7~12ダメージだ!!!
ダメージ :7
ブレ ~5
ダメージ + ブレ
HP - ダメージ
・死亡 #4 HP<=0
・無事 #8
#8
俺たちの冒険はこれからだ!(打ち切り)
`.trim();
function create() {
const downloadStr = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>div { max-width: 600px; padding: 1rem; margin: auto; color: #444444 } div:nth-last-child(2n+3) { background-color: #eeeeee }</style>
<script>
const scenes = {};
let variables = {};
let replacements = {};
function createScene(no, event) {
if (event) {
event.preventDefault();
if (!document.body.lastChild.style.height) document.body.lastChild.style.height = "100vh";
if (event.target.parentNode !== document.body.lastChild.previousSibling) return;
}
let choiceState = "initial";
const replaceRe = new RegExp(\`(\${Object.keys(replacements).join("|") || "0^"})\`, "g");
const replaceFn = text => text.replace(replaceRe, (_, key) => replacements[key]);
const text = scenes[no].split("\\n").map(line => {
let match = null;
if (match = line.match(/^\\s*(\\S+)\\s*<=\\s*$/)) {
const onchange = \`if (event.target.parentNode === document.body.lastChild.previousSibling) replacements['\${match[1]}'] = event.target.value\`;
return \`<input type="text" value="\${replacements[match[1]] ?? match[1]}" onchange="\${onchange}">\`;
}
if (match = line.match(/^\\s*(\\S+)\\s*(:|\\+|-|~)\\s*(\\S+)\\s*$/)) {
const [, variable, operator, value] = match;
const isVariant = /\\D+/.test(value);
if (isVariant && variables[value] === undefined) return replaceFn(line);
variables[variable] ??= 0;
const operand = isVariant ? variables[value] : +value;
if (operator === ":") variables[variable] = operand;
if (operator === "+") variables[variable] += operand;
if (operator === "-") variables[variable] -= operand;
if (operator === "~") variables[variable] = Math.floor(Math.random() * operand + 1);
return \`\${replaceFn(line)} (\${isVariant ? \`\${operator}\${operand} \` : ""}=> \${variables[variable]})\`;
}
if (match = line.match(/^.*\\S+\\s*#(\\d+)\\s*(\\S+?)\\s*(=|<|>|<=|>=)\\s*(-?\\d+)\\s*$/)) {
const [, no, variable, operator, value] = match;
let meet = false;
if ((operator[0] === "=" || operator[1] === "=") && variables[variable] === +value) meet = true;
if (operator[0] === "<" && variables[variable] < +value) meet = true;
if (operator[0] === ">" && variables[variable] > +value) meet = true;
if (choiceState === "forced" || !meet) return replaceFn(line) + "<br>";
if (choiceState === "initial") choiceState = "forced";
return \`<a href="#\${no}" onclick="createScene(\${no}, event)">\${replaceFn(line)}</a><br>\`;
}
if (match = line.match(/^.*\\S+\\s*#(\\d+)\\s*$/)) {
if (choiceState === "forced") return replaceFn(line) + "<br>";
if (choiceState === "initial") choiceState = "selectable";
return \`<a href="#\${match[1]}" onclick="createScene(\${match[1]}, event)">\${replaceFn(line)}</a><br>\`;
}
return replaceFn(line);
}).join("<br>");
const reset = choiceState === "initial" ? \`<br><br><a href="#1" onclick="variables = {};replacements = {};createScene(1, event)">最初から #1</a><br>\` : "";
document.body.lastChild.insertAdjacentHTML("beforebegin", \`<div>\${text}\${reset}</div>\`);
scrollBy(0, document.body.lastChild.previousSibling.getBoundingClientRect().top);
}
document.addEventListener("DOMContentLoaded", () => {
let key = 0;
document.body.textContent.split("\\n").forEach(line => {
let match = null;
if (match = line.match(/^\\s*#(\\d+)\\s*$/)) {
if (scenes[key]) scenes[key] = scenes[key].trim();
key = match[1];
scenes[key] = "";
}
scenes[key] += line + "\\n";
});
scenes[key] = scenes[key].trim();
if (location.protocol === "file:") {
document.body.innerHTML = "<div></div>";
} else {
const downloadStr = \`<!DOCTYPE html>\\n<html>\\n<head>\${document.head.innerHTML}</head>\\n<\${""}body>\${document.body.textContent.slice(0, -1)}</body>\\n</html>\`;
const downloadFile = new Blob([downloadStr], { type: "text/html" });
document.body.innerHTML = \`<div><a href="\${URL.createObjectURL(downloadFile)}" download="\${document.title}.html">この作品をダウンロード</a></div><div></div>\`;
}
createScene(1);
});
<${""}/script>
<title>${document.forms.upload.elements.title.value}</title>
</head>
<body>
${document.forms.upload.elements.content.value}
</body>
</html>
`.trim();
const downloadFile = new Blob([downloadStr], { type: "text/html" });
return URL.createObjectURL(downloadFile);
}
function play() {
const url = create();
open(url, "TestPlay");
URL.revokeObjectURL(url);
}
function save() {
const url = create();
const a = document.createElement("a");
a.href = url;
a.download = `${document.forms.upload.elements.title.value}.html`;
a.click();
URL.revokeObjectURL(url);
}
function load(file) {
const r = new FileReader();
r.onload = () => {
let match;
if (match = r.result.match(/<title.*?>(.*)<\/title.*?>/i)) document.forms.upload.elements.title.value = match[1];
match = r.result.match(/<body.*?>([^]*)<\/body.*?>/i);
document.forms.upload.elements.content.value = match ? match[1].trim() : r.result;
};
r.readAsText(file);
}
</script>
<title>ゲームブックエディター</title>
</head>
<body>
<form action="upload.php" method="post" name="upload">
タイトル:
<input type="text" name="title" required>
サンプル:
<input type="button" value="夢小説" onclick="content.value += dream">
<input type="button" value="クイズ" onclick="content.value += quiz">
<input type="button" value="冒険" onclick="content.value += adventure">
<br>
ファイルから読み込み:
<input type="file" accept=".html" onchange="load(event.target.files[0])">
<textarea name="content" required></textarea>
<input type="button" value="テストプレイ" onclick="play()">
<input type="button" value="保存" onclick="save()">
<input type="submit" value="投稿" disabled>
</form>
</body>
</html>