Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
Created March 29, 2020 11:17
Show Gist options
  • Select an option

  • Save cipolleschi/cbe0eafffec4fc33a0a11d596f2a6d65 to your computer and use it in GitHub Desktop.

Select an option

Save cipolleschi/cbe0eafffec4fc33a0a11d596f2a6d65 to your computer and use it in GitHub Desktop.
Logic for the Dice Roller
import Katana
enum DiceRollerLogic {
struct RollDice: AnySideEffect {
let die: AppState.DiceState.Die
func sideEffect(_ context: AnySideEffectContext) throws {
let extreme = die.rawValue
let result = Int.random(in: 1...extreme)
context.dispatch(UpdateCurrentResult(die: die, result: result))
}
}
fileprivate struct UpdateCurrentResult: StateUpdater {
let die: AppState.DiceState.Die
let result: Int
func updateState(_ state: inout AppState) {
// Update the history
if let latestResult = state.diceState.currentResults[self.die] {
if state.diceState.rollHistory[self.die] != nil {
state.diceState.rollHistory[self.die]?.append(latestResult)
} else {
state.diceState.rollHistory[self.die] = [latestResult]
}
}
// update the current result
state.diceState.currentResults[self.die] = self.result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment