Stian Fredrik Aune
2 years ago
25 changed files with 266 additions and 128 deletions
-
9webui-react/src/contexts/RuntimeContext.tsx
-
12webui-react/src/hooks/milestones.ts
-
2webui-react/src/models/Shared.ts
-
29webui-react/src/pages/PlayPage.tsx
-
23webui-react/src/pages/runtime/ControlsBoi.tsx
-
2webui-react/src/pages/runtime/MilestoneBoi.tsx
-
5webui-react/src/pages/runtime/ProgramBoi.sass
-
2webui-react/src/pages/runtime/ProgramBoi.tsx
-
11webui-react/src/primitives/Shared.tsx
-
4webui-react/src/primitives/misc/Misc.sass
-
37webui-react/src/primitives/misc/Misc.tsx
-
89ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/domain/models/Milestones.kt
-
2ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/domain/models/Workout.kt
-
6ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/domain/models/WorkoutState.kt
-
11ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/domain/runtime/FlowBus.kt
-
4ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/domain/runtime/Value.kt
-
69ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/infrastructure/drivers/MilestoneChecker.kt
-
12ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/infrastructure/drivers/ProgramEnforcer.kt
-
14ykonsole-core/src/main/kotlin/net/aiterp/git/ykonsole2/infrastructure/testing/TestDriver.kt
-
4ykonsole-iconsole/src/main/kotlin/net/aiterp/git/ykonsole2/infrastructure/IConsole.kt
-
8ykonsole-ktor/src/main/kotlin/net/aiterp/git/ykonsole2/application/routes/Shared.kt
-
11ykonsole-ktor/src/main/kotlin/net/aiterp/git/ykonsole2/application/routes/ws/Connection.kt
-
1ykonsole-ktor/src/main/kotlin/net/aiterp/git/ykonsole2/application/routes/ws/SocketOutput.kt
-
17ykonsole-mysql/src/main/kotlin/net/aiterp/git/ykonsole2/infrastructure/repositories/MySqlWorkoutStateRepository.kt
-
10ykonsole-mysql/src/main/resources/migrations/tables/workout_state.xml
@ -0,0 +1,89 @@ |
|||
package net.aiterp.git.ykonsole2.domain.models |
|||
|
|||
import net.aiterp.git.ykonsole2.domain.runtime.* |
|||
|
|||
fun MutableMap<Value, List<Value>>.tryMilestone(event: ValuesReceived): MilestoneReached? = tryMilestone(event.values) |
|||
|
|||
fun List<WorkoutState>.makeMilestoneReachedEvents(): List<MilestoneReached> { |
|||
val cache = mutableMapOf<Value, List<Value>>() |
|||
|
|||
return mapNotNull { cache.tryMilestone(it) } |
|||
} |
|||
|
|||
private fun MutableMap<Value, List<Value>>.tryMilestone(state: WorkoutState): MilestoneReached? = |
|||
tryMilestone(state.asValueList()) |
|||
|
|||
private fun MutableMap<Value, List<Value>>.tryMilestone(newValues: List<Value>): MilestoneReached? { |
|||
val time = newValues.find<Time>() ?: return null |
|||
val calories = newValues.find<Calories>() ?: return null |
|||
val distance = newValues.find<Distance>() ?: return null |
|||
|
|||
var next: MilestoneReached? = null |
|||
|
|||
val roundDistance = distance.roundedDown |
|||
if (roundDistance.isMilestone && !containsKey(roundDistance)) { |
|||
val last = getOrDefault(distance.lastMilestone, default) |
|||
|
|||
this[roundDistance] = listOf(time, calories) |
|||
next = MilestoneReached( |
|||
roundDistance, listOf(time, calories), listOf( |
|||
Time(time.seconds - last.findInt<Time>()), |
|||
Calories(calories.kcal - last.findInt<Calories>()), |
|||
) |
|||
) |
|||
} |
|||
|
|||
val roundedCals = calories.roundedDown |
|||
if (roundedCals.isMilestone && !containsKey(roundedCals)) { |
|||
val last = getOrDefault(roundedCals.lastMilestone, default) |
|||
|
|||
this[roundedCals] = listOf(time, distance) |
|||
next = MilestoneReached( |
|||
roundedCals, listOf(time, distance), listOf( |
|||
Time(time.seconds - last.findInt<Time>()), |
|||
Distance(distance.meters - last.findInt<Distance>()), |
|||
) |
|||
) |
|||
} |
|||
|
|||
val roundedTime = time.roundedDown |
|||
if (roundedTime.isMilestone && !containsKey(roundedTime)) { |
|||
val last = this.getOrDefault(time.lastMilestone, default) |
|||
|
|||
this[roundedTime] = listOf(calories, distance) |
|||
next = MilestoneReached( |
|||
roundedTime, listOf(calories, distance), listOf( |
|||
Calories(calories.kcal - last.findInt<Calories>()), |
|||
Distance(distance.meters - last.findInt<Distance>()), |
|||
) |
|||
) |
|||
} |
|||
|
|||
return next |
|||
} |
|||
|
|||
private val default = listOf(Time(0), Calories(0), Distance(0)) |
|||
|
|||
private val Value.roundedDown |
|||
get() = when (this) { |
|||
is Time -> Time(seconds - (seconds % 2)) |
|||
is Calories -> Calories(kcal - (kcal % 4)) |
|||
is Distance -> Distance(meters - (meters % 10)) |
|||
else -> error("Not milestone-able") |
|||
} |
|||
|
|||
private val Value.isMilestone |
|||
get() = when (this) { |
|||
is Time -> seconds > 0 && seconds % 60 == 0 |
|||
is Calories -> kcal > 0 && kcal % 100 == 0 |
|||
is Distance -> meters > 0 && meters % 1000 == 0 |
|||
else -> error("Not milestone-able") |
|||
} |
|||
|
|||
private val Value.lastMilestone |
|||
get() = when (this) { |
|||
is Time -> Time(maxOf(0, (seconds - 1) - ((seconds - 1) % 60))) |
|||
is Calories -> Calories(maxOf(0, (kcal - 1) - ((kcal - 1) % 100))) |
|||
is Distance -> Distance(maxOf(0, (meters - 1) - ((meters - 1) % 1000))) |
|||
else -> error("Not milestone-able") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue