-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
110 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { GameSettings, QA } from "./types.ts"; | ||
import { PCG } from "./pcg.ts"; | ||
|
||
function parseCsv(csv: string, separator = ","): QA[] { | ||
return csv.split("\n") | ||
.map((x) => x.split(separator)) | ||
.map(([q, a]) => ({ q, a })); | ||
} | ||
function parseSettings(problems: QA[]) { | ||
const settings: GameSettings = { | ||
title: "", | ||
timelimit: 60, | ||
shuffle: true, | ||
voice: false, | ||
}; | ||
problems.forEach(({ q, a }) => { | ||
if (!q.startsWith(":")) return; | ||
if(a === null) return; | ||
switch (q) { | ||
case ":title": | ||
settings.title = a; | ||
break; | ||
case ":timelimit": | ||
settings.timelimit = parseInt(a); | ||
break; | ||
case ":shuffle": | ||
settings.shuffle = a !== "false"; | ||
break; | ||
case ":voice": | ||
settings.voice = a === "true"; | ||
break; | ||
} | ||
}); | ||
return settings; | ||
} | ||
function shuffleArray(array: unknown[], rng: PCG) { | ||
for (let i = array.length - 1; i > 1; i--) { | ||
const j = rng.nextInt(i); | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
} | ||
|
||
export async function fetchProblems(url: string) { | ||
const errors: string[] = []; | ||
const separator = url.endsWith(".tsv") ? "\t" : ","; | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
return { ok: false as const, errors: [`Failed to fetch ${url}: ${response.statusText}`] }; | ||
} | ||
const data = parseCsv(await response.text(), separator); | ||
const settings = parseSettings(data); | ||
const problems = data.filter(({ q }) => !q.startsWith(":")); | ||
|
||
if (settings.shuffle) { | ||
const today = Math.floor(Date.now() / 86400000); | ||
const rng = new PCG(BigInt(today)); | ||
shuffleArray(problems, rng); | ||
} | ||
if (errors.length === 0) return { ok: true as const, problems, settings } | ||
return { ok: false as const, errors }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
:title typescript | ||
let isDone=false | ||
Record<string,number> | ||
addEventListener('click',()=>{}) | ||
const list:number[]=[] | ||
interface Person{name:string;age:number} | ||
type StringOrNumber=string|number | ||
return`Hello, ${name}` | ||
enum Direction{Up,Down,Left,Right} | ||
const tuple:[string,number]=['hello',10] | ||
class Animal{constructor(public name:string){}} | ||
const readOnlyArray:ReadonlyArray<number>=[1,2,3] | ||
function greet(name:string):string{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,34 @@ | ||
import GameMain from "./_components/GameMain.tsx"; | ||
import Keyboard from "./_components/Keyboard.tsx"; | ||
import { GameSettings } from "./_components/_lib.ts"; | ||
import { fetchProblems } from "./_lib/fetchProblems.ts"; | ||
import { render } from "./_deps.ts"; | ||
import { PCG } from "./_lib/pcg.ts"; | ||
|
||
function parseCsv(csv: string) { | ||
return csv.split("\n") | ||
.map((x) => x.split(",")) | ||
.map(([q, a]) => ({ q, a: a ? a.trim() : q })); | ||
} | ||
|
||
(async () => { | ||
const hash = location.hash.slice(1); | ||
let problems_; | ||
if (hash.length > 0) { | ||
problems_ = await fetch(hash).then((x) => x.text()).then(parseCsv).catch( | ||
console.error, | ||
); | ||
} | ||
problems_ = problems_ || | ||
await fetch("/csv/default.csv").then((x) => x.text()).then(parseCsv); | ||
|
||
const settings: GameSettings = { | ||
title: "", | ||
timelimit: 60, | ||
shuffle: true, | ||
voice: false, | ||
}; | ||
problems_.forEach(({ q, a }) => { | ||
if (!q.startsWith(":")) return; | ||
switch (q) { | ||
case ":title": | ||
settings.title = a; | ||
break; | ||
case ":timelimit": | ||
settings.timelimit = parseInt(a); | ||
break; | ||
case ":shuffle": | ||
settings.shuffle = a !== "false"; | ||
break; | ||
case ":voice": | ||
settings.voice = a === "true"; | ||
break; | ||
} | ||
}); | ||
|
||
function shuffleArray(array: unknown[], rng: PCG) { | ||
for (let i = array.length - 1; i > 1; i--) { | ||
const j = rng.nextInt(i); | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
const hash = location.hash.slice(1); | ||
let csv = "/csv/default.csv" | ||
if (hash.length > 0) { | ||
if (!hash.includes("csv=")) { | ||
csv = hash | ||
} else { | ||
const params = new URLSearchParams(hash); | ||
csv = params.get("csv")! | ||
} | ||
} | ||
|
||
const problems = problems_.filter(({ q }) => !q.startsWith(":")); | ||
if (settings.shuffle) { | ||
const today = Math.floor(Date.now() / 86400000); | ||
const rng = new PCG(BigInt(today)); | ||
shuffleArray(problems, rng); | ||
} | ||
const App = ( | ||
<> | ||
<GameMain problems={problems} settings={settings} /> | ||
<details open> | ||
<summary>きーぼーど</summary> | ||
<Keyboard /> | ||
</details> | ||
</> | ||
); | ||
const appElem = document.getElementById("app")!; | ||
const probsSets = await fetchProblems(csv) | ||
if (!probsSets.ok) { | ||
appElem.insertAdjacentHTML("beforeend", probsSets.errors.join("<br>")); | ||
throw new Error('failed to init'); | ||
} | ||
|
||
render(App, document.getElementById("app")!); | ||
})(); | ||
const { problems, settings } = probsSets; | ||
const App = ( | ||
<> | ||
<GameMain problems={problems} settings={settings} /> | ||
<details open> | ||
<summary>きーぼーど</summary> | ||
<Keyboard /> | ||
</details> | ||
</> | ||
); | ||
render(App, appElem); |