Skip to content

Instantly share code, notes, and snippets.

@cjbrooks12
Created March 1, 2023 15:41
Show Gist options
  • Save cjbrooks12/f54e9f0ff10f8c6c9d7d553b5e8e9ce7 to your computer and use it in GitHub Desktop.
Save cjbrooks12/f54e9f0ff10f8c6c9d7d553b5e8e9ce7 to your computer and use it in GitHub Desktop.
Ballast Clicker
package com.copperleaf.ballast.clicker
import com.copperleaf.ballast.InputHandler
import com.copperleaf.ballast.InputHandlerScope
import kotlinx.coroutines.delay
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
public object ClickerComponentContract {
public data class State(
val lastUpdate: Instant,
val currentValue: Long,
)
public sealed class Inputs {
public object Click : Inputs()
public object StartAutoClick : Inputs()
public object StopAutoClick : Inputs()
public data class AutoClick(val updateTime: Instant) : Inputs()
}
public sealed class Events {}
}
public class ClickerComponentInputHandler : InputHandler<
ClickerComponentContract.Inputs,
ClickerComponentContract.Events,
ClickerComponentContract.State,
> {
override suspend fun InputHandlerScope<
ClickerComponentContract.Inputs,
ClickerComponentContract.Events,
ClickerComponentContract.State>.handleInput(
input: ClickerComponentContract.Inputs
): Unit = when (input) {
is ClickerComponentContract.Inputs.Click -> {
updateState { it.copy(currentValue = it.currentValue + 100) }
}
is ClickerComponentContract.Inputs.StartAutoClick -> {
// update the time so the first AutoClick Input sent only adds
// the amount since the auto-click started
updateState { it.copy(lastUpdate = Clock.System.now()) }
sideJob("auto-click") {
while (true) {
// auto-click 60 times per second. This is roughly once
// per frame, but not necessarily synchronized with
// the actual frame clock.
//
// You could potentially do some other logic instead of
// a basic loop to control the actual dispatch of the
// auto-click Inputs (collecting from a SharedFlow shared
// with other VMs, for example)
delay(1000.milliseconds / 60)
// Post an AutoClick with the current Instant. The delta
// will be computed based on when the Input was sent, not
// when it will be processed.
postInput(ClickerComponentContract.Inputs.AutoClick(Clock.System.now()))
}
}
}
is ClickerComponentContract.Inputs.StopAutoClick -> {
sideJob("auto-click") {
// keep this block empty to cancel the side-job.
// There will be an explicit API for this in v3
}
}
is ClickerComponentContract.Inputs.AutoClick -> {
// compute the delta based on what's in the current state
// and what was in the Input. Notice that this does not use
// any time functions here, so it's OK if an Input gets
// dropped or things process a bit slowly. As long as they
// get processed one at a time (LIFO or FIFO InputStrategy)
// things will still always be computed correctly, regardless
// of how long it actually takes for an Input to make it here
val currentState = getCurrentState()
val delta: Duration = input.updateTime - currentState.lastUpdate
// Based on that delta, compute the point value and update the
// state.
val amountToAutoIncrement: Long = delta.inWholeMilliseconds / 10
updateState {
it.copy(
lastUpdate = input.updateTime,
currentValue = it.currentValue + amountToAutoIncrement,
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment