add support for multiple games in redux state
This commit is contained in:
@@ -3,7 +3,7 @@ import type { PayloadAction } from '@reduxjs/toolkit'
|
||||
import { loadState } from "./localStorage";
|
||||
|
||||
interface ProgressState {
|
||||
level: {[world: string]: {[level: number]: LevelProgressState}}
|
||||
level: {[game: string]: {[world: string]: {[level: number]: LevelProgressState}}}
|
||||
}
|
||||
interface Selection {
|
||||
selectionStartLineNumber: number,
|
||||
@@ -20,12 +20,15 @@ interface LevelProgressState {
|
||||
const initialProgressState = loadState() ?? { level: {} } as ProgressState
|
||||
const initalLevelProgressState = {code: "", completed: false} as LevelProgressState
|
||||
|
||||
function addLevelProgress(state, action: PayloadAction<{world: string, level: number}>) {
|
||||
if (!state.level[action.payload.world]) {
|
||||
state.level[action.payload.world] = {}
|
||||
function addLevelProgress(state, action: PayloadAction<{game: string, world: string, level: number}>) {
|
||||
if (!state.level[action.payload.game]) {
|
||||
state.level[action.payload.game] = {}
|
||||
}
|
||||
if (!state.level[action.payload.world][action.payload.level]) {
|
||||
state.level[action.payload.world][action.payload.level] = {...initalLevelProgressState}
|
||||
if (!state.level[action.payload.game][action.payload.world]) {
|
||||
state.level[action.payload.game][action.payload.world] = {}
|
||||
}
|
||||
if (!state.level[action.payload.game][action.payload.world][action.payload.level]) {
|
||||
state.level[action.payload.game][action.payload.world][action.payload.level] = {...initalLevelProgressState}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,45 +36,46 @@ export const progressSlice = createSlice({
|
||||
name: 'progress',
|
||||
initialState: initialProgressState,
|
||||
reducers: {
|
||||
codeEdited(state, action: PayloadAction<{world: string, level: number, code: string}>) {
|
||||
codeEdited(state, action: PayloadAction<{game: string, world: string, level: number, code: string}>) {
|
||||
addLevelProgress(state, action)
|
||||
state.level[action.payload.world][action.payload.level].code = action.payload.code
|
||||
state.level[action.payload.world][action.payload.level].completed = false
|
||||
state.level[action.payload.game][action.payload.world][action.payload.level].code = action.payload.code
|
||||
state.level[action.payload.game][action.payload.world][action.payload.level].completed = false
|
||||
},
|
||||
changedSelection(state, action: PayloadAction<{world: string, level: number, selections: Selection[]}>) {
|
||||
changedSelection(state, action: PayloadAction<{game: string, world: string, level: number, selections: Selection[]}>) {
|
||||
addLevelProgress(state, action)
|
||||
state.level[action.payload.world][action.payload.level].selections = action.payload.selections
|
||||
state.level[action.payload.game][action.payload.world][action.payload.level].selections = action.payload.selections
|
||||
},
|
||||
levelCompleted(state, action: PayloadAction<{world: string, level: number}>) {
|
||||
levelCompleted(state, action: PayloadAction<{game: string, world: string, level: number}>) {
|
||||
addLevelProgress(state, action)
|
||||
state.level[action.payload.world][action.payload.level].completed = true
|
||||
state.level[action.payload.game][action.payload.world][action.payload.level].completed = true
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
export function selectLevel(world: string, level: number) {
|
||||
export function selectLevel(game: string, world: string, level: number) {
|
||||
return (state) =>{
|
||||
if (!state.progress.level[world]) { return initalLevelProgressState }
|
||||
if (!state.progress.level[world][level]) { return initalLevelProgressState }
|
||||
return state.progress.level[world][level]
|
||||
if (!state.progress.level[game]) { return initalLevelProgressState }
|
||||
if (!state.progress.level[game][world]) { return initalLevelProgressState }
|
||||
if (!state.progress.level[game][world][level]) { return initalLevelProgressState }
|
||||
return state.progress.level[game][world][level]
|
||||
}
|
||||
}
|
||||
|
||||
export function selectCode(world: string, level: number) {
|
||||
export function selectCode(game: string, world: string, level: number) {
|
||||
return (state) => {
|
||||
return selectLevel(world, level)(state).code
|
||||
return selectLevel(game, world, level)(state).code
|
||||
}
|
||||
}
|
||||
|
||||
export function selectSelections(world: string, level: number) {
|
||||
export function selectSelections(game: string, world: string, level: number) {
|
||||
return (state) => {
|
||||
return selectLevel(world, level)(state).selections
|
||||
return selectLevel(game, world, level)(state).selections
|
||||
}
|
||||
}
|
||||
|
||||
export function selectCompleted(world: string, level: number) {
|
||||
export function selectCompleted(game: string, world: string, level: number) {
|
||||
return (state) => {
|
||||
return selectLevel(world, level)(state).completed
|
||||
return selectLevel(game, world, level)(state).completed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user