Skip to content

Commit

Permalink
Moves PRNG state handling to action (Separations of concerns)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschirrmacher authored and mdrie committed Oct 14, 2021
1 parent 554b38f commit 21e1f21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/model/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Game, GameId, initGame } from "../game"
import { API } from "./api"
import { allLaws, Law, LawId } from "../laws"
import { LawId } from "../laws"
import { Event } from "../events"
import { getState } from "../lib/random"

Expand Down Expand Up @@ -52,7 +52,6 @@ export default function RepositoryFactory({
},

async saveGame(game: Game): Promise<void> {
game.prngState = getState()
storage.setItem("game", JSON.stringify(game))
try {
api.saveGame(game) // We don't await here, b/c saving could take place in the background and can even be retried later
Expand Down
10 changes: 8 additions & 2 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Event } from "../events"
import { LawId } from "../laws"
import { Change, applyEffects, createBaseValues } from "../params"
import { steps } from "../tourSteps"
import { seedWithGame } from "../lib/random"
import { getState, seedWithGame } from "../lib/random"

interface Router {
push: (path: string) => void
Expand All @@ -29,6 +29,7 @@ export function ActionFactory(router: Router, repository: Repository) {
try {
const game = await repository.loadGame(payload.gameId)
seedWithGame(game)
game.prngState = getState()
await repository.saveGame(game)
router.push("/games/" + game.id)
context.commit("setGameState", { game })
Expand Down Expand Up @@ -56,6 +57,7 @@ export function ActionFactory(router: Router, repository: Repository) {
.map((law) => ({ lawId: law.id, effectiveSince: law.effectiveSince }))
game.acceptedLaws = [...filteredLawRefs, newLawRef]
const event = prepareNextStep(game)
game.prngState = getState()
await repository.saveGame(game)
context.commit("setGameState", { game })
context.dispatch("applyEvent", { event })
Expand All @@ -66,6 +68,7 @@ export function ActionFactory(router: Router, repository: Repository) {
const game = { ...(context.state.game as Game) }
game.rejectedLaws = [...game.rejectedLaws, payload.lawId]
await repository.decisionMade(game, payload.lawId, false)
game.prngState = getState()
await repository.saveGame(game)
context.commit("setGameState", { game })
},
Expand All @@ -74,6 +77,7 @@ export function ActionFactory(router: Router, repository: Repository) {
const game = { ...(context.state.game as Game) }
await repository.decisionMade(game, "sitOut", true)
const event = prepareNextStep(game)
game.prngState = getState()
await repository.saveGame(game)
context.commit("setGameState", { game })
context.dispatch("applyEvent", { event })
Expand All @@ -84,6 +88,7 @@ export function ActionFactory(router: Router, repository: Repository) {
const laws = game.acceptedLaws.map(getAcceptedLaw)
game.currentYear++
game.values = Calculator.calculateNextYear(game, laws, game.currentYear)
game.prngState = getState()
await repository.saveGame(game)
context.commit("setGameState", { game })
},
Expand All @@ -99,14 +104,15 @@ export function ActionFactory(router: Router, repository: Repository) {
acknowledgeEvent(context: Context, event: Event) {
const game = { ...(context.state.game as Game) }
game.events.find((e) => e.id === event.id)!.acknowledged = true
game.prngState = getState()
repository.saveGame(game)
context.commit("setGameState", { game })
},

applyEffects(context: Context, payload: { changes: Change[] }) {
const affectedContext = { dispatch: context.dispatch, values: createBaseValues(context.state.game!.values) }
applyEffects(affectedContext, payload.changes)
const game = { ...context.state.game, values: affectedContext.values } as Game
const game = { ...context.state.game, values: affectedContext.values, prngState: getState() } as Game
repository.saveGame(game)
context.commit("setGameState", { game })
},
Expand Down

0 comments on commit 21e1f21

Please sign in to comment.