Skip to content

Commit

Permalink
Merge pull request #22 from ntnui-darts/add-rtc-fast-mode
Browse files Browse the repository at this point in the history
Add RTC fast mode. Fix multiplier not being reset on segment selection.
  • Loading branch information
magnetenstad authored Oct 8, 2023
2 parents d7091ca + d3935fd commit 537bff7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 44 deletions.
57 changes: 54 additions & 3 deletions src/components/RtcGameInput.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,68 @@
<template>
<div
v-if="
gameStore.game
? getTypeAttribute < Boolean > (gameStore.game, 'fast', false)
: false
"
div
class="row"
style="justify-content: space-between"
>
<button
v-for="i in [1, 2, 3]"
@click="selectedMultiplier = i"
:class="{
selected: selectedMultiplier == i,
}"
>
{{ multiplierToString(i) }}
</button>
</div>
<div class="row" style="height: 12em">
<button @click="emit('miss')">&#10008;</button>
<button @click="emit('hit')">&#10004;</button>
<button @click="registerMiss()">&#10008;</button>
<button @click="registerHit()">&#10004;</button>
</div>
<button @click="emit('undo')">&#x232B;</button>
</template>

<script lang="ts" setup>
import { Multiplier, Segment, multiplierToString } from '@/types/game'
import { ref } from 'vue'
import { useGameStore } from '@/stores/game'
import { getTypeAttribute } from '@/types/game'
import { RtcController, getRtcLegScore } from '@/games/rtc'
const gameStore = useGameStore()
const emit = defineEmits<{
hit: []
hit: [segment: Segment]
miss: []
undo: []
}>()
const getDefaultMultiplier = () =>
gameStore.game ? getTypeAttribute<Multiplier>(gameStore.game, 'mode', 1) : 1
const selectedMultiplier = ref(getDefaultMultiplier())
const registerMiss = () => {
emit('miss')
selectedMultiplier.value = 1
}
const registerHit = () => {
if (!gameStore.game) throw Error()
const sequence = (gameStore.getController() as RtcController).sequence
const score = getRtcLegScore(gameStore.game, gameStore.getCurrentVisits)
const sector = sequence.at(score)
if (!sector) throw Error()
emit('hit', {
multiplier: selectedMultiplier.value,
sector,
})
selectedMultiplier.value = getDefaultMultiplier()
}
</script>

<style scoped>
Expand Down
24 changes: 23 additions & 1 deletion src/components/RtcOptionsInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,42 @@
Random order
</button>
</div>
<div class="row">
<button
:class="{ selected: fast }"
:disabled="mode != 1"
@click="
() => {
fast = !fast
update()
}
"
>
Fast
</button>
</div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
const random = ref(false)
const fast = ref(false)
const mode = ref<1 | 2 | 3>(1)
const emit = defineEmits<{
update: [typeAttributes: string[]]
}>()
const update = () => {
emit('update', [`mode:${mode.value}`, `random:${random.value}`])
if (mode.value != 1) {
fast.value = false
}
emit('update', [
`mode:${mode.value}`,
`random:${random.value}`,
`fast:${fast.value}`,
])
}
onMounted(() => {
Expand Down
1 change: 1 addition & 0 deletions src/components/X01GameInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const selectSector = (sector: number) => {
sector: sector,
})
}
selectedMultiplier.value = 1
}
</script>

Expand Down
28 changes: 4 additions & 24 deletions src/games/rtc-random.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import { useGameStore } from '@/stores/game'
import {
Game,
GameController,
Multiplier,
getTypeAttribute,
getVisitsOfUser,
} from '@/types/game'
import { getRtcController, getRtcLegScore } from './rtc'
import { Game } from '@/types/game'
import { RtcController, getRtcController } from './rtc'

export const getRtcRandomController = (game: Game): GameController => {
const gameStore = useGameStore()
export const getRtcRandomController = (game: Game): RtcController => {
const sequence = Array(20)
.fill(undefined)
.map((_, i) => i + 1)
.sort(() => Math.random() - 0.5)

return {
...getRtcController(game),
getUserDisplayText(userId) {
const score = getRtcLegScore(getVisitsOfUser(game, userId))
return `${sequence.at(score)}`
},
recordHit() {
const score = getRtcLegScore(gameStore.getCurrentVisits)
const sector = sequence.at(score)
if (!sector) return
gameStore.saveScore({
multiplier: getTypeAttribute<Multiplier>(game, 'mode', 1),
sector,
})
},
sequence,
}
}
37 changes: 22 additions & 15 deletions src/games/rtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,56 @@ import {
getTypeAttribute,
} from '@/types/game'

export const getRtcController = (game: Game): GameController => {
export type RtcController = GameController & { sequence: number[] }

export const getRtcController = (game: Game): RtcController => {
const gameStore = useGameStore()
const sequence = Array(20)
.fill(undefined)
.map((_, i) => i + 1)

return {
game,
sequence,
getCurrentLegScore() {
return getRtcLegScore(getVisitsOfUser(game, gameStore.userId))
return getRtcLegScore(game, getVisitsOfUser(game, gameStore.userId))
},
getUserResultText(userId) {
const name = useUsersStore().getUser(userId)?.name ?? 'Unknown'
const visits = game.legs.find((leg) => leg.userId == userId)?.visits
return `${name}, ${visits?.length} visits`
},
getUserDisplayText(userId) {
const currentSector = getCurrentSector(getVisitsOfUser(game, userId))
return `${currentSector}`
const score = getRtcLegScore(game, getVisitsOfUser(game, userId))
return `${sequence.at(score)}`
},
getSegmentText(segment) {
return segment ? `${segment.sector}` : '-'
},
recordHit() {
gameStore.saveScore({
multiplier: getTypeAttribute<Multiplier>(game, 'mode', 1),
sector: getCurrentSector(getVisitsOfUser(game, gameStore.userId)),
})
recordHit(segment) {
gameStore.saveScore(segment)
},
recordMiss() {
gameStore.saveScore({ multiplier: Multiplier.None, sector: 0 })
},
}
}

const getCurrentSector = (visits: Visit[]) => {
return Math.max(1, ...visits.flat().map((s) => (s ? s.sector + 1 : 0)))
export const getCurrentSector = (game: Game, visits: Visit[]) => {
return getRtcLegScore(game, visits) + 1
}

const sumNumbers = (numbers: number[]) => {
return numbers.reduce((prev, current) => prev + current, 0)
}

export const getRtcLegScore = (visits: Visit[]) => {
return sumNumbers(visits.map(getVisitScore))
export const getRtcLegScore = (game: Game, visits: Visit[]) => {
return sumNumbers(visits.map((v) => getVisitScore(game, v)))
}

const getVisitScore = (visit: Visit) => {
return visit.filter((seg) => seg != null && seg.sector != 0).length
const getVisitScore = (game: Game, visit: Visit) => {
const isFast = getTypeAttribute<Boolean>(game, 'fast', false)
return sumNumbers(
visit.map((seg) => (seg ? (isFast ? seg.multiplier : 1) : 0))
)
}
2 changes: 1 addition & 1 deletion src/types/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface GameController {
getUserResultText(userId: string): string
getUserDisplayText(userId: string): string
getSegmentText(segment?: Segment | null): string
recordHit(segment?: Segment): void
recordHit(segment: Segment): void
recordMiss(): void
}

Expand Down

0 comments on commit 537bff7

Please sign in to comment.