-
Notifications
You must be signed in to change notification settings - Fork 7
/
web_template.html
681 lines (612 loc) · 23.5 KB
/
web_template.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dante's Cowboy</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css2?family=Palanquin+Dark&display=swap" rel="stylesheet">
<style type="text/css">
body {
margin: 0;
background-color: black;
}
.game {
position: absolute;
top: 0px;
left: 0px;
margin: 0px;
border: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: block;
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
image-rendering: crisp-edges;
image-rendering: pixelated;
-ms-interpolation-mode: nearest-neighbor;
}
#inputdiv,
#buy_menu {
position: absolute;
top: 0px;
left: 0px;
margin: 0px;
border: 0;
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
}
#inputdiv {
z-index: 10;
display: none;
/* set to flex by javascript */
overflow: hidden;
background: #00000090;
}
#inputtext {
position: absolute;
top: 0;
left: 0;
background: none;
width: 90%;
height: 50%;
margin-left: 5%;
margin-right: 5%;
border-top-style: hidden;
border-right-style: hidden;
border-left-style: hidden;
border-bottom-style: hidden;
transform: translateY(100%);
text-align: center;
font-size: 2em;
font-family: Arial;
color: white;
}
#inputtext:focus {
outline: none;
}
#inputbuttondiv {
position: absolute;
display: flex;
width: 100vw;
justify-content: center;
flex-wrap: wrap;
height: 170px;
margin-top: 40vh;
}
#inputbutton {
font-family: Arial;
font-size: 3em;
color: white;
background: none;
border-radius: 10px;
border: 2px solid;
border-color: white;
padding: 10px;
margin: 30px;
}
#buy_menu {
display: none;
/* set in javascript when shown when error code */
flex-direction: column;
justify-content: center;
word-wrap: break-word;
align-items: center;
text-align: center;
height: 100vh;
background: #000000D0;
font-family: arial;
gap: 20px;
color: #fff;
z-index: 20;
}
#buy_menu h1,
#daypass {
margin-left: 30px;
margin-right: 30px;
}
/* Style the payment screen title */
#buy_menu h1 {
font-size: 32px;
}
/* Style the payment button */
#payment-button,
#submit-button {
background-color: #fff;
color: #000;
font-size: 24px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
#daypass {
border: 4px solid white;
border-radius: 25px;
padding: 10px;
display: flex;
gap: 20px;
}
#codebox {
border: 4px solid white;
border-radius: 10px;
padding: 10px;
margin: 30px;
}
#visibility-toggle,
#closebutton {
border: none;
font-size: 30px;
background: none;
color: white;
}
#closebutton {
float: right;
}
/* Media queries for smaller screens */
@media screen and (max-width: 480px) {
#buy_menu h1 {
font-size: 24px;
margin-bottom: 20px;
}
#payment-button {
font-size: 20px;
padding: 8px 16px;
}
}
/* Hamburger Button Style */
#hamburgerBtn {
z-index: 999;
position: fixed;
/* Fixed position */
top: 10px;
/* 10 pixels from the top */
left: 10px;
/* 10 pixels from the left */
background: none;
/* White with 50% opacity */
border: none;
/* No border */
cursor: pointer;
/* Change mouse pointer on hover */
padding: 10px;
/* Some padding around the image */
}
#hamburgerBtn img {
display: block;
/* Removes any default spacing around image */
width: 30px;
/* Width of the hamburger icon */
height: auto;
/* Maintain aspect ratio */
}
/* Modal Styles */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
}
.modal-content {
height: 80%;
color: white;
background: rgba(0, 0, 0, 0.6);
margin: auto;
padding: 20px;
width: 80%;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 30px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* New Font Family */
font-family: 'Palanquin Dark', sans-serif;
}
.modal-content a {
text-decoration: none;
/* Optional: removes underline from links */
color: inherit;
/* Optional: retains text color of parent */
display: block;
width: min(90vw, 400px);
padding: 20px;
/* Makes the link block-level for better control */
}
.modal-content img {
z-index: 10;
max-width: 100%;
/* Ensure the image is not larger than the container */
height: auto;
/* Maintain aspect ratio */
}
</style>
<link rel="icon" type="image/x-icon" href="/favicon.ico">
</head>
<body style="background:black">
<!-- Hamburger Button -->
<button id="hamburgerBtn">
<img src="/Hamburger.png" alt="Hamburger Menu">
</button>
<!-- Modal -->
<div id="modal" class="modal">
<div id="modalContent" class="modal-content">
<a href="https://discord.gg/eTGwPteMXr">
<p>Join the discord</p>
</a>
<a href="https://discord.gg/eTGwPteMXr"><img src="discord.svg" alt="Discord logo"></a>
</div>
</div>
<div id="buy_menu">
<h1>AI is expensive so you need to pay for it</h1>
<p>You can play free 10 minutes a day. If you're seeing this you must buy a day pass to continue. If you already
bought one, but are still seeing this, check that the code matches up. If you're unsatisfied with your
service, you can get a refund via the email stripe sent you.</p>
<div id="daypass">
<button id="closebutton" onclick="closepayment()"><i class="fa-regular fa-xmark"></i></button>
<p>24 Hour Pass</p>
<button id="payment-button" onclick="do_checkout()">Buy Now</button>
</div>
<div id="codebox">
<label for="pass">Day pass code:</label>
<input type="password" name="pass" id="codeinput" onkeyup='on_codeinput_key(event);' required>
<button id="visibility-toggle" onclick="visibility_toggle()"><i class="fa fa-eye-slash"
id="visibility-toggle-eye"></i></button>
</div>
</div>
<div id="inputdiv" class="inputdiv">
<textarea onkeyup='on_textarea_key(event);' id="inputtext">
</textarea>
<div id="inputbuttondiv">
<button id="inputbutton" onclick="end_dialog()"> Submit </button>
<button id="inputbutton" onclick="end_without_saying()"> Cancel </button>
</div>
</div>
<canvas class="game" id="canvas" oncontextmenu="event.preventDefault()" width="1504" height="864"></canvas>
<script type="text/javascript">
var Module = {
preRun: [],
postRun: [],
print: (function () {
return function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
};
})(),
printErr: function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
canvas: (function () {
var canvas = document.getElementById('canvas');
canvas.addEventListener("webglcontextlost", function (e) { alert('FIXME: WebGL context lost, please reload the page'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function (text) { },
monitorRunDependencies: function (left) { },
};
window.onerror = function (event) {
console.log("onerror: " + event.message);
//for(;;) {}
};
</script>
<script type="text/javascript">
// Get the modal
var modal = document.getElementById("modal");
var modalContent = document.getElementById("modalContent");
var open = false;
// Get the button that opens the modal
var btn = document.getElementById("hamburgerBtn");
// When the user clicks on the button, open the modal
function updateFromOpen() {
if (open) {
modal.style.display = "block";
} else {
modal.style.display = "none";
}
}
btn.addEventListener('click', function (event) {
// Consume the event here
open = !open;
updateFromOpen();
// Prevent the default button action
event.preventDefault();
// Stop the event from bubbling up to parent elements
event.stopPropagation();
});
// When the user clicks anywhere outside of the modal, close it
const disable_func = function (event) {
open = false;
updateFromOpen();
}
window.onclick = disable_func;
modal.onclick = disable_func;
modalContent.onclick = disable_func;
let game_doing_input = true;
let server_url = "";
let codeinput = document.getElementById("codeinput");
function set_my_code(new_code) {
let final_codeinput_string = "";
for (let i = 0; i < new_code.length; i++) {
let cur = new_code[i];
let cur_charcode = cur.charCodeAt(0);
if (cur_charcode >= "a".charCodeAt(0) && cur_charcode <= "z".charCodeAt(0)) {
final_codeinput_string += String.fromCharCode(cur_charcode + ("A".charCodeAt(0) - "a".charCodeAt(0)));
} else {
if ((cur_charcode >= "A".charCodeAt(0) && cur_charcode <= "Z".charCodeAt(0)) || (cur_charcode >= "0".charCodeAt(0) && cur_charcode <= "9".charCodeAt(0))) {
final_codeinput_string += cur;
}
}
}
codeinput.value = final_codeinput_string;
}
function get_my_code() {
return codeinput.value;
}
let code_visible = false;
let input_modal = document.getElementById("inputdiv");
let pause_modal = document.getElementById("buy_menu");
let save_game_data = null;
function visibility_toggle() {
if (code_visible) {
code_visible = false;
document.getElementById("codeinput").type = "password";
document.getElementById("visibility-toggle-eye").className = "fa fa-eye-slash";
} else {
code_visible = true;
document.getElementById("codeinput").type = "text";
document.getElementById("visibility-toggle-eye").className = "fa fa-eye";
}
}
function frame(delta) {
let input_visible = input_modal.style.display === "flex";
let pause_visible = pause_modal.style.display === "flex";
if (Module.ccall('is_receiving_text_input', 'bool', [], [])) {
if (!input_visible) {
document.getElementById("inputtext").value = "";
setTimeout(function () { document.getElementById("inputtext").focus(); }, 50); // for some reason focus doesn't work immediately here
document.getElementById("inputdiv").style.display = "flex";
}
}
if ((input_visible || pause_visible) && game_doing_input) {
Module.ccall('stop_controlling_input', 'void', [], []);
game_doing_input = false;
}
if (!input_visible && !pause_visible && !game_doing_input) {
Module.ccall('start_controlling_input', 'void', [], []);
game_doing_input = true;
}
window.requestAnimationFrame(frame);
}
Module.onRuntimeInitialized = () => {
window.requestAnimationFrame(frame);
}
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
// Use a lookup table to find the index.
const lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
const encode = (arraybuffer) => {
let bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = '';
for (i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63];
}
if (len % 3 === 2) {
base64 = base64.substring(0, base64.length - 1) + '=';
}
else if (len % 3 === 1) {
base64 = base64.substring(0, base64.length - 2) + '==';
}
return base64;
};
const decode = (base64) => {
let bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
if (base64[base64.length - 1] === '=') {
bufferLength--;
if (base64[base64.length - 2] === '=') {
bufferLength--;
}
}
const arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
};
function end_dialog() {
document.getElementById("inputdiv").style.display = "none";
Module.ccall('end_text_input', 'void', ['string'], [document.getElementById("inputtext").value]);
}
function show_paywall() {
document.getElementById('buy_menu').style.display = "flex";
}
function hide_paywall() {
document.getElementById('buy_menu').style.display = "none";
}
function on_codeinput_key(event) {
set_my_code(get_my_code());
}
function save_all() {
document.cookie = get_my_code();
if (typeof (Storage) !== "undefined") {
console.log("Saving full game");
Module.ccall('dump_save_data', 'void', [], []);
localStorage.setItem("game", encode(save_game_data));
}
}
function load_all() {
set_my_code(document.cookie);
if (typeof (Storage) !== "undefined") {
console.log("Attempting read of full game...");
let read_data = localStorage.getItem("game");
if (read_data != null) {
console.log("Reading full game");
let decoded = decode(read_data);
Module.ccall('read_from_save_data', 'void', ['array', 'number'], [new Uint8Array(decoded), decoded.byteLength]);
} else {
console.log("Couldn't find full game");
}
}
}
function end_without_saying() {
document.getElementById("inputtext").value = "";
end_dialog();
}
function on_textarea_key(event) {
let final_textarea_string = "";
let cur_textarea_string = document.getElementById("inputtext").value;
let should_end = false;
for (let i = 0; i < Math.min(cur_textarea_string.length, 800); i++) {
let cur = cur_textarea_string[i];
if (cur === "\n") {
should_end = true;
continue;
}
if (cur.charCodeAt(0) >= 255) continue; // non ascii gtfo
if (cur === "|") continue; // used for splitting
final_textarea_string += cur_textarea_string[i];
}
document.getElementById("inputtext").value = final_textarea_string;
if (event.key === "Enter") should_end = true;
if (event.key === "Escape") {
document.getElementById("inputtext").value = "";
should_end = true;
}
// have a boolean flag here and call it once so the game's end dialog function isn't called twice
if (should_end) end_dialog();
}
const max_retries = 5;
let cur_id = 1; // zero is not a valid id, the zero value means no async request currently active
let generation_requests = []; // array of dictionaries with structure:
/*
{ id: number,
request: XMLHTTPRequest,
request_info: { url: string, body: string }
retries_remaining: number, // on failure, retries over and over until out of retries
}
*/
function do_checkout() {
fetch(server_url + "/checkout").then((response) => response.text()).then((text) => {
split_up = text.split("|");
if (split_up.length !== 2) {
console.log("Weird response from server, length isn't 2");
} else {
set_my_code(split_up[0])
save_all();
let url_to_go_to = split_up[1];
window.location.href = url_to_go_to;
}
}).catch((error) => {
console.log("Error doing checkout: " + error);
});
}
function resend_request(r) {
r.failed = false;
r.request = new XMLHttpRequest();
r.request.onerror = function (e) {
r.failed = true;
};
r.request.open("POST", r.request_info.url, true);
r.request.send(r.request_info.body);
}
// server URL is decided in C
function set_server_url(p) {
server_url = p;
}
// Returns an integer, a "handle" into the generation request. Takes in the string prompt, and api URL
// the api url must start with `http://` or https.
function make_generation_request(p, api) {
cur_id += 1;
let to_push = {
"id": cur_id,
"request": new XMLHttpRequest(),
"retries_remaining": max_retries,
"request_info": { "url": (' ' + api).slice(1), "body": p },
"failed": false,
}
console.log("Making generation request with id " + to_push.id);
generation_requests.push(to_push)
resend_request(to_push)
return cur_id;
}
// returns 0 if not done yet, 1 if succeeded, 2 if failed after too many retries (i.e server down, or no internet)
// -1 if it doesn't exist
function get_generation_request_status(id) {
for (let i = 0; i < generation_requests.length; i++) {
if (generation_requests[i].id == id) {
if (generation_requests[i].failed) {
}
else {
let http_status = generation_requests[i].request.status;
if (http_status == 200) {
return 1; // done, everything ok
}
else if (http_status == 0) { // not done yet
return 0;
}
}
{ // errored
if (generation_requests[i].retries_remaining > 0) {
console.log("Retrying request");
generation_requests[i].retries_remaining -= 1;
resend_request(generation_requests[i]);
return 0;
} else {
return 2; // too many retries, failed
}
}
}
}
return -1;
}
function done_with_generation_request(id) {
console.log("Removing request with id " + id);
let new_generation_requests = [];
for (let i = 0; i < generation_requests.length; i++) {
if (generation_requests[i].id == id) {
} else {
new_generation_requests.push(generation_requests[i])
}
}
generation_requests = new_generation_requests;
}
// doesn't fill string if not done yet, or the id doesn't exist
function get_generation_request_content(id) {
let to_return = "";
for (let i = 0; i < generation_requests.length; i++) {
if (generation_requests[i].id == id) {
to_return = generation_requests[i].request.responseText;
break;
}
}
return to_return;
}
</script>
{{{ SCRIPT }}}
</body>
</html>