-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.tsx
73 lines (62 loc) · 1.93 KB
/
util.tsx
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
import { renderToReadableStream } from 'react-dom/server'
import { loadSessions, type SessionRequest } from "./session"
import { createClient } from 'redis'
import { getInitialGameData, type GameData } from './data'
async function initDB() {
console.log(process.env.REDIS_URL)
const client = createClient({
url: process.env.REDIS_URL
})
client.on('error', (err) => {
console.error(err)
})
await client.connect()
return client
}
export const db = await initDB()
loadSessions()
export async function renderPage(
content: JSX.Element,
full = true,
status: number = 200
) {
let page = content
if (full) {
page = await getWrapper(content)
}
const stream = await renderToReadableStream(page)
return new Response(stream, {
headers: {
'Content-Type': 'text/html'
},
status
})
}
export async function getWrapper(content: JSX.Element) {
return (
<html>
<head>
<title>Pestovanie</title>
<link rel="stylesheet" href="/static/main.css" />
<link href="https://fonts.googleapis.com/css2?family=Playpen+Sans:[email protected]&family=Ubuntu&display=swap" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charSet="utf-8" />
</head>
<body className='p-0 m-0 bg-yellow-950 h-screen overflow-hidden text-yellow-50'>
{content}
<script src="/static/client.js"></script>
</body>
</html>
)
}
export async function getGameData(req: SessionRequest) {
if (!req.session) throw new Error('No session')
const user = req.session.user
return JSON.parse(await db.get(`game:${user}`) || JSON.stringify(getInitialGameData())) as GameData
}
export async function setGameData(req: SessionRequest, data: GameData) {
if (!req.session) throw new Error('No session')
const user = req.session.user
await db.set(`game:${user}`, JSON.stringify(data))
}
export const TIME_MULTIPLIER = 1000 * 60 * 60