|
|
|
@ -142,7 +142,15 @@ def coreCtx : Core.Context := {
|
|
|
|
fileMap := { source := "", positions := #[0], lines := #[1] } }
|
|
|
|
fileMap := { source := "", positions := #[0], lines := #[1] } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
partial def runLevel (env : Environment) (GameName : Name) (levels : HashMap Nat GameLevel) (idx : Nat) : IO Unit := do
|
|
|
|
structure RunTacticParams where
|
|
|
|
|
|
|
|
tactic : String
|
|
|
|
|
|
|
|
deriving FromJson
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
structure LoadLevelParams where
|
|
|
|
|
|
|
|
number : Nat
|
|
|
|
|
|
|
|
deriving FromJson
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
partial def runLevel (requestId : JsonRpc.RequestID) (env : Environment) (GameName : Name) (levels : HashMap Nat GameLevel) (idx : Nat) : IO Unit := do
|
|
|
|
let levelName : Name := s!"Level{toString idx}"
|
|
|
|
let levelName : Name := s!"Level{toString idx}"
|
|
|
|
let termElabM : TermElabM Unit := do
|
|
|
|
let termElabM : TermElabM Unit := do
|
|
|
|
let some lvl := levels.find? idx | throwError s!"Cannot find level {idx}"
|
|
|
|
let some lvl := levels.find? idx | throwError s!"Cannot find level {idx}"
|
|
|
|
@ -160,7 +168,8 @@ partial def runLevel (env : Environment) (GameName : Name) (levels : HashMap Nat
|
|
|
|
errors := resp.errors,
|
|
|
|
errors := resp.errors,
|
|
|
|
goals := resp.goals,
|
|
|
|
goals := resp.goals,
|
|
|
|
message := resp.message }
|
|
|
|
message := resp.message }
|
|
|
|
output (← levelInfo.toJson)
|
|
|
|
let hOut ← IO.getStdout
|
|
|
|
|
|
|
|
hOut.writeGspResponse ⟨requestId, ← levelInfo.toJson⟩
|
|
|
|
mainLoop
|
|
|
|
mainLoop
|
|
|
|
levelM.run lvl |>.run' state
|
|
|
|
levelM.run lvl |>.run' state
|
|
|
|
let metaM : MetaM Unit := termElabM.run' (ctx := {})
|
|
|
|
let metaM : MetaM Unit := termElabM.run' (ctx := {})
|
|
|
|
@ -215,43 +224,66 @@ where
|
|
|
|
catch ex => mkResponse #[← ex.toMessageData.toString]
|
|
|
|
catch ex => mkResponse #[← ex.toMessageData.toString]
|
|
|
|
|
|
|
|
|
|
|
|
mainLoop : LevelM Unit := do
|
|
|
|
mainLoop : LevelM Unit := do
|
|
|
|
match ← Action.get with
|
|
|
|
let hIn ← IO.getStdin
|
|
|
|
| Action.runTactic tac => do let resp ← runTactic tac; output s!"{← resp.toJson}"
|
|
|
|
let hOut ← IO.getStdout
|
|
|
|
| Action.loadLevel n => runLevel env GameName levels n
|
|
|
|
let hLog ← IO.getStderr
|
|
|
|
| Action.undo => do modify fun s => s.pop; output s!"{← (← mkResponse).toJson}"
|
|
|
|
let m ← hIn.readGspMessage
|
|
|
|
| Action.restartLevel => runLevel env GameName levels idx
|
|
|
|
match m with
|
|
|
|
| Action.prev => runLevel env GameName levels idx.pred
|
|
|
|
| .request id "runTactic" params => do
|
|
|
|
| Action.next => runLevel env GameName levels idx.succ
|
|
|
|
match fromJson? (toJson (params)) with
|
|
|
|
| Action.quit => IO.Process.exit 0
|
|
|
|
| Except.ok (v : RunTacticParams) =>
|
|
|
|
| Action.restartGame => output "Can't restart game now"
|
|
|
|
let resp ← runTactic v.tactic
|
|
|
|
| Action.info => output "Can't get info now"
|
|
|
|
hOut.writeGspResponse ⟨id, ← resp.toJson⟩
|
|
|
|
| Action.invalid s => output s!"{← { ← mkResponse with errors := #[s!"Invalid action: {s}"] : Response}.toJson}"
|
|
|
|
| Except.error inner =>
|
|
|
|
|
|
|
|
hLog.putStr s!"Invalid params: {inner}"
|
|
|
|
|
|
|
|
hLog.flush
|
|
|
|
|
|
|
|
| .request id "loadLevel" (some params) =>
|
|
|
|
|
|
|
|
match fromJson? (toJson (params)) with
|
|
|
|
|
|
|
|
| Except.ok (v : LoadLevelParams) =>
|
|
|
|
|
|
|
|
runLevel id env GameName levels v.number
|
|
|
|
|
|
|
|
| Except.error inner =>
|
|
|
|
|
|
|
|
hLog.putStr s!"Invalid params: {inner}"
|
|
|
|
|
|
|
|
hLog.flush
|
|
|
|
|
|
|
|
| .request id "undo" _ => do
|
|
|
|
|
|
|
|
modify fun s => s.pop
|
|
|
|
|
|
|
|
hOut.writeGspResponse ⟨id, ← (← mkResponse).toJson⟩
|
|
|
|
|
|
|
|
| _ =>
|
|
|
|
|
|
|
|
hLog.putStr s!"Invalid action: {toJson m}"
|
|
|
|
|
|
|
|
hLog.flush
|
|
|
|
mainLoop
|
|
|
|
mainLoop
|
|
|
|
|
|
|
|
|
|
|
|
#check (toJson "").compress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open System Lean Std in
|
|
|
|
open System Lean Std in
|
|
|
|
partial def runGame (GameName : Name) : IO Unit := do
|
|
|
|
partial def runGame (GameName : Name) : IO Unit := do
|
|
|
|
let hIn ← IO.getStdin
|
|
|
|
let env ← importModules [{ module := `Init : Import }, { module := GameName : Import }] {} 0
|
|
|
|
let hOut ← IO.getStdout
|
|
|
|
let termElabM : TermElabM Unit := do
|
|
|
|
let hLog ← IO.getStderr
|
|
|
|
let levels := levelsExt.getState env
|
|
|
|
hLog.putStr s!"{toJson $ ← hIn.readGspMessage}"
|
|
|
|
let game := {← gameExt.get with nb_levels := levels.size }
|
|
|
|
hOut.writeGspNotification ⟨"Hello!", "s"⟩
|
|
|
|
mainLoop env game levels
|
|
|
|
-- let env ← importModules [{ module := `Init : Import }, { module := GameName : Import }] {} 0
|
|
|
|
let metaM : MetaM Unit := termElabM.run' (ctx := {})
|
|
|
|
-- let termElabM : TermElabM Unit := do
|
|
|
|
discard <| metaM.run'.toIO coreCtx { env := env }
|
|
|
|
-- let levels := levelsExt.getState env
|
|
|
|
where
|
|
|
|
-- let game := {← gameExt.get with nb_levels := levels.size }
|
|
|
|
mainLoop (env : Environment) (game : Game) (levels : HashMap Nat GameLevel): IO Unit := do
|
|
|
|
-- mainLoop env game levels
|
|
|
|
let hIn ← IO.getStdin
|
|
|
|
-- let metaM : MetaM Unit := termElabM.run' (ctx := {})
|
|
|
|
let hOut ← IO.getStdout
|
|
|
|
-- discard <| metaM.run'.toIO coreCtx { env := env }
|
|
|
|
let hLog ← IO.getStderr
|
|
|
|
-- where
|
|
|
|
let m ← hIn.readGspMessage
|
|
|
|
-- mainLoop (env : Environment) (game : Game) (levels : HashMap Nat GameLevel): IO Unit := do
|
|
|
|
match m with
|
|
|
|
-- match ← Action.get with
|
|
|
|
| .request id "info" params =>
|
|
|
|
-- | Action.info => output (toJson game)
|
|
|
|
hOut.writeGspResponse ⟨id, game⟩
|
|
|
|
-- | Action.loadLevel n => runLevel env GameName levels n
|
|
|
|
| .request id "loadLevel" (some params) =>
|
|
|
|
-- | Action.quit => IO.Process.exit 0
|
|
|
|
match fromJson? (toJson (params)) with
|
|
|
|
-- | Action.invalid s => output s!"Invalid action: {s}"
|
|
|
|
| Except.ok (v : LoadLevelParams) =>
|
|
|
|
-- | _ => output "Invalid action"
|
|
|
|
runLevel id env GameName levels v.number
|
|
|
|
-- mainLoop env game levels
|
|
|
|
| Except.error inner =>
|
|
|
|
|
|
|
|
hLog.putStr s!"Invalid params: {inner}"
|
|
|
|
|
|
|
|
hLog.flush
|
|
|
|
|
|
|
|
| .request id m _ =>
|
|
|
|
|
|
|
|
hOut.writeGspResponseError
|
|
|
|
|
|
|
|
⟨id, JsonRpc.ErrorCode.methodNotFound, s!"Method not found: {m}", none⟩
|
|
|
|
|
|
|
|
| _ =>
|
|
|
|
|
|
|
|
hLog.putStr s!"Invalid action: {toJson m}"
|
|
|
|
|
|
|
|
hLog.flush
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainLoop env game levels
|
|
|
|
|
|
|
|
|
|
|
|
end Server
|
|
|
|
end Server
|
|
|
|
|