add world parameter to router

pull/43/head
Alexander Bentkamp 4 years ago
parent 0eacf1339b
commit 894d2708d8

@ -31,6 +31,7 @@ function Level() {
const params = useParams(); const params = useParams();
const levelId = parseInt(params.levelId) const levelId = parseInt(params.levelId)
const worldId = params.worldId
const [tacticDocs, setTacticDocs] = useState([]) const [tacticDocs, setTacticDocs] = useState([])
const [lemmaDocs, setLemmaDocs] = useState([]) const [lemmaDocs, setLemmaDocs] = useState([])
@ -105,7 +106,7 @@ function Level() {
return () => { editor.dispose() } return () => { editor.dispose() }
}, []) }, [])
const uri = `file:///level${levelId}` const uri = `file:///${worldId}/${levelId}`
// The next function will be called when the level changes // The next function will be called when the level changes
useEffect(() => { useEffect(() => {
@ -122,7 +123,7 @@ function Level() {
new AbbreviationRewriter(new AbbreviationProvider(), model, editor) new AbbreviationRewriter(new AbbreviationProvider(), model, editor)
leanClient.sendRequest("loadLevel", {world: "TestWorld", level: levelId}).then((res) => { leanClient.sendRequest("loadLevel", {world: worldId, level: levelId}).then((res) => {
// setLevelTitle("Level " + res["index"] + ": " + res["title"]) // setLevelTitle("Level " + res["index"] + ": " + res["title"])
// setIndex(parseInt(res["index"])) // setIndex(parseInt(res["index"]))
setTacticDocs(res["tactics"]) setTacticDocs(res["tactics"])

@ -91,7 +91,7 @@ function Welcome() {
</Typography> </Typography>
</Box> </Box>
<Box textAlign='center' sx={{ m: 5 }}> <Box textAlign='center' sx={{ m: 5 }}>
<Button component={RouterLink} to="/level/1" variant="contained">Start rescue mission</Button> <Button component={RouterLink} to="/world/TestWorld/level/1" variant="contained">Start rescue mission</Button>
</Box> </Box>
<div ref={worldsRef} style={{"width": "100%","height": "50em"}} /> <div ref={worldsRef} style={{"width": "100%","height": "50em"}} />
</div> </div>

@ -29,7 +29,7 @@ const router = createHashRouter([
element: <Welcome />, element: <Welcome />,
}, },
{ {
path: "/level/:levelId", path: "/world/:worldId/level/:levelId",
element: <Level />, element: <Level />,
}, },
], ],

@ -58,18 +58,20 @@ structure PlainGoal where
goals : Array GameGoal goals : Array GameGoal
deriving FromJson, ToJson deriving FromJson, ToJson
#check String.split
-- TODO: Find a better way to pass on the file name? -- TODO: Find a better way to pass on the file name?
def levelIdFromFileName [Monad m] [MonadError m] [MonadEnv m] (fileName : String) : m Nat := do def levelIdFromFileName [Monad m] [MonadError m] [MonadEnv m] (fileName : String) : m LevelId := do
if fileName.startsWith "/level" then let fileParts := fileName.splitOn "/"
if let some id := (fileName.drop "/level".length).toNat? then if fileParts.length == 3 then
return id if let some level := fileParts[2]!.toNat? then
return {game := `TestGame, world := fileParts[1]!, level := level}
throwError s!"Could not find level ID in file name: {fileName}" throwError s!"Could not find level ID in file name: {fileName}"
return 1
def getLevelByFileName [Monad m] [MonadError m] [MonadEnv m] (fileName : String) : m GameLevel := do def getLevelByFileName [Monad m] [MonadError m] [MonadEnv m] (fileName : String) : m GameLevel := do
let levelId ← levelIdFromFileName fileName let levelId ← levelIdFromFileName fileName
-- TODO: make world and game configurable -- TODO: make world and game configurable
let some level ← getLevel? {game := `TestGame, world := `TestWorld, level := levelId} let some level ← getLevel? levelId
| throwError "Level not found" | throwError "Level not found"
return level return level

Loading…
Cancel
Save