Created
March 29, 2020 11:17
-
-
Save cipolleschi/cbe0eafffec4fc33a0a11d596f2a6d65 to your computer and use it in GitHub Desktop.
Logic for the Dice Roller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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