-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (118 loc) · 5.02 KB
/
index.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
/*
Copyright (C) 2023 Andrew Sillers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const https = require("https");
const http = require("http");
const fs = require("fs");
const express = require("express");
const cookieParser = require('cookie-parser');
const utils = require("./utils.js");
const stateHelper = require("./state.js");
var options = {
// key: fs.readFileSync('../stack/letsencrypt/certificates/tropemon.com.key'),
// cert: fs.readFileSync('../stack/letsencrypt/certificates/tropemon.com.crt')
};
const app = express();
app.use(cookieParser());
app.use(express.static('public'))
// read or initialize the state cookie on each request,
// by reading (or creating) a cookie with an "id" value
app.use(async function (req, res, next) {
var id = req.cookies.id;
//console.log("id is", id);
req.state = await stateHelper.createStateObjectFromID(id);
await stateHelper.saveState(res, req.state);
//console.log("state is ", req.state);
next();
});
var videoStreams = utils.videoStreams;
var pushNewFrame = utils.pushNewFrame;
// whenever the user asks for the /screen,
// create a new entry in `videoStream` object with this kept-open response,
// indexed by cookie ID, so we can push to it as needed.
// We will never willingly close this response,
// but it will close when the user navigates away or times out.
app.get("/screen", async function(req, res) {
const scenes = require("./scenes.js");
videoStreams[req.state.id] = res;
var [canvas, ctx] = utils.getCanvasAndCtx();
res.writeHead(200, {
'Cache-Control': 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0',
Pragma: 'no-cache',
Connection: 'close',
'Content-Type': 'multipart/x-mixed-replace; boundary=--endofsection'
});
res.write(`Content-Type:image/svg+xml\n\n`);
res.write(canvas.toBuffer());
res.write(`--endofsection\n`);
await scenes.find(req.state.scene).render(req, ctx, canvas);
// debug
//ctx.fillText(`c${req.state.cursorPos},d${req.state.dialogPos}`, 0, 10);
res.write(`Content-Type:image/svg+xml\n\n`);
pushNewFrame(res, canvas);
// Firefox kills the image feed upon getting any 204 (even a separate request!)
// so we have to use a redirect-and-reload approach
if(req.headers['user-agent'].match(/Firefox/)) {
res.end();
}
// when the HTTP client disconnnects, remove its screen stream
res.on("close", _=>{
console.log("closed");
delete videoStreams[req.state.id];
});
});
app.get(["/d","/u","/r","/l","/a","/b","/on"], async function controlInput(req, res) {
const scenes = require("./scenes.js");
var screenRes = videoStreams[req.state.id];
var [canvas, ctx] = utils.getCanvasAndCtx();
var controlMap = { "/d":"down", "/u":"up", "/r":"right", "/l":"left", "/a":"a", "/b":"b", "/on":"on" };
// look up the the state's current scene and process this input
await scenes.find(req.state.scene).process(controlMap[req.originalUrl], req, ctx);
// Firefox kills the image feed upon getting any 204 (even a separate request!)
// so we have to use a redirect-and-reload approach
if(!screenRes || req.headers['user-agent'].match(/Firefox/)) {
await stateHelper.saveState(res, req.state);
res.redirect(utils.homeURL);
return;
}
// look up the current state (which might have changed) and render state as visual output
await scenes.find(req.state.scene).render(req, ctx, canvas);
// save mutated state back into the database
await stateHelper.saveState(res, req.state);
// debug
//ctx.fillText(`c${req.state.cursorPos},d${req.state.dialogPos}`, 0, 10);
if(screenRes) { pushNewFrame(screenRes, canvas); }
res.status(204).end();
});
// hey, I coded this in like 4 weeks
// if your state is absolutely borked somehow, this endpoint will reset your state
app.get("/escapeHatch", async function controlInput(req, res) {
const scenes = require("./scenes.js");
// still in the intro? you can't possibly have messed up your state yet, right??
// and you might not even have your starter Trope
if(scenes.find(req.state.scene) == scenes.INTRO) {
res.redirect(utils.homeURL);
}
req.state.scene = scenes.MENU;
req.state.cursorPos = 0;
req.state.dialogPos = 0;
req.opponentId = "";
req.tropeOpponent = [0];
req.opponenetMove = 99;
req.opponentNextMove = 99;
await stateHelper.saveState(res, req.state);
res.redirect(utils.homeURL);
});
http.createServer(options, app).listen(process.env.PORT || 3000);
//https.createServer(options, app).listen(443);
//http.createServer(options, app).listen(3000);