Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file player to fix khrj's workflow #981

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/pages/deprecated-file-player.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
import '../global.css'
import StandardHead from '../components/standard-head.astro'

const file = Astro.url.searchParams.get('file') ?? ''
const codeRes = await fetch(file)
const code = await codeRes.text()
---

<html lang='en'>
<head>
<StandardHead title='(Deprecated) File Player' />
<style>
#screen-container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}

#screen {
max-width: 100%;
max-height: 100%;
background: black;
}
</style>
</head>
<body>
<div id='screen-container'>
<canvas id='screen' width='1000' height='800' tabindex='-1' />
</div>

<script define:vars={{ code }}>
window.__sprig_intitial_code__ = code
</script>
<script>
import { runGame } from '../lib/engine/3-editor'

const screen = document.getElementById('screen') as HTMLCanvasElement
const { __sprig_intitial_code__ } = window as unknown as { __sprig_intitial_code__: string }

let runRes = runGame(__sprig_intitial_code__, screen, (_) => {})
if (runRes.error) console.error(runRes.error.raw)

const params = new URLSearchParams(window.location.search)
if (params.has('watch')) {
let oldCode = __sprig_intitial_code__
setInterval(async () => {
const codeRes = await fetch(params.get('file') || '')
const code = await codeRes.text()

if (oldCode !== code) {
oldCode = code
runRes.cleanup()
runRes = runGame(code, screen, (_) => {})
if (runRes.error) console.error(runRes.error.raw)
}
}, Number(params.get('watch') || 1000))
}
</script>
</body>
</html>