Skip to content

Commit

Permalink
show result
Browse files Browse the repository at this point in the history
  • Loading branch information
TeemuKoivisto committed Jan 18, 2024
1 parent 85f6162 commit ce0f7f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions packages/client/src/components/MidiInfo.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script lang="ts">
import { onMount } from 'svelte'
import { midiActions, midiInput, midiRange } from '$stores/midi'
import { getNote } from '$utils/midi'
import { midiActions, midiInput } from '$stores/midi'
let rangeMin = getNote($midiRange[0]).absolute
let rangeMax = getNote($midiRange[1]).absolute
function handleSetRange() {
// prompt -> press the lowest note in your MIDI device
Expand All @@ -23,9 +25,9 @@
<div class="flex flex-col">
<label class="font-bold" for="range_min">Range</label>
<div class="my-1 flex">
<input class="w-10" id="range_min" value={'C4'} />
<input class="w-10" id="range_min" bind:value={rangeMin} />
<span class="mx-2">-</span>
<input class="w-10" id="range_max" value={'C5'} />
<input class="w-10" id="range_max" bind:value={rangeMax} />
</div>
<div>
<button class="btn primary" on:click={handleSetRange}>Use MIDI</button>
Expand Down
13 changes: 8 additions & 5 deletions packages/client/src/routes/(site)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let targetNote = 0
let playedNote = 0
let timeout: ReturnType<typeof setTimeout> | undefined
let guessState: 'waiting' | 'correct' | 'wrong'
let guessState: 'waiting' | 'correct' | 'wrong' | 'ended'
onMount(() => {
handlePromptMIDI()
Expand Down Expand Up @@ -44,15 +44,15 @@
setPlayedNote(value, correct)
timeout = setTimeout(() => {
if ($currentGame?.ended) {
gameActions.endGame()
setTargetNote()
guessState = 'ended'
} else if ($currentGame) {
setTargetNote($currentGame.current)
guessState = 'waiting'
}
setPlayedNote()
guessState = 'waiting'
timeout = undefined
}, 20000000)
}, 2000)
}
}
Expand Down Expand Up @@ -82,7 +82,6 @@
function setTargetNote(value?: number) {
if (value === undefined) {
gameActions.endGame()
targetEl.style.display = 'none'
targetNote = 0
} else {
Expand Down Expand Up @@ -173,6 +172,10 @@
{#if guessState === 'correct' || guessState === 'wrong'}
<div>Target: {getNote(targetNote).absolute}</div>
<div class="ml-8">Played: {getNote(playedNote).absolute}</div>
{:else if guessState === 'ended'}
<div>
Result: {$currentGame.correct} / {$currentGame.notes.length}
</div>
{/if}
</div>
{:else}
Expand Down
8 changes: 6 additions & 2 deletions packages/client/src/stores/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { persist } from './persist'

class GuessGame {
notes: number[]
correct = 0
idx = 0
constructor(notes: number[]) {
this.notes = notes
Expand All @@ -14,10 +15,13 @@ class GuessGame {
return this.notes[this.idx]
}
get ended() {
return this.notes.length === this.idx - 1
return this.notes.length === this.idx + 1
}
guess(note: number) {
const result = this.current === note
if (result) {
this.correct += 1
}
this.idx += 1
return result
}
Expand All @@ -42,7 +46,7 @@ export const gameActions = {
currentGame.set(game)
return game
},
endGame() {
clearGame() {
currentGame.set(undefined)
}
}

0 comments on commit ce0f7f4

Please sign in to comment.