You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lean4game/server/leanserver/GameServer/Game.lean

82 lines
2.1 KiB
Plaintext

import Lean
2 years ago
import GameServer.EnvExtensions
2 years ago
open Lean
2 years ago
structure GameServerState :=
(env : Lean.Environment)
2 years ago
(game : Name)
2 years ago
abbrev GameServerM := StateT GameServerState Server.Watchdog.ServerM
instance : MonadEnv GameServerM := {
getEnv := do return (← get).env
modifyEnv := fun f => do
let s ← get
set {s with env := f s.env}
}
namespace Game
open Server
open Watchdog
open Lsp
open JsonRpc
2 years ago
open IO
2 years ago
def getGame (game : Name): GameServerM Game := do
let some game ← getGame? game
| throwServerError "Game not found"
return game
/--
Fields:
- description: Lemma in mathematical language.
- descriptionGoal: Lemma printed as Lean-Code.
-/
2 years ago
structure LevelInfo where
index : Nat
title : String
tactics: Array TacticDocEntry
lemmas: Array LemmaDocEntry
introduction : String
descrText : String := ""
descrFormat : String := ""
2 years ago
deriving ToJson
2 years ago
structure LoadLevelParams where
2 years ago
world : Name
level : Nat
deriving ToJson, FromJson
2 years ago
partial def handleServerEvent (ev : ServerEvent) : GameServerM Bool := do
match ev with
| ServerEvent.clientMsg msg =>
match msg with
| Message.request id "info" _ =>
2 years ago
let s ← get
let c ← read
2 years ago
c.hOut.writeLspResponse ⟨id, (← getGame s.game)⟩
2 years ago
return true
| Message.request id "loadLevel" params =>
let p ← parseParams LoadLevelParams (toJson params)
2 years ago
let s ← get
let c ← read
2 years ago
let some lvl ← getLevel? {game := s.game, world := p.world, level := p.level}
| throwServerError s!"Level not found {(← getGame s.game).name} {p.world} {p.level}"
2 years ago
let levelInfo : LevelInfo :=
{ index := lvl.index,
title := lvl.title,
tactics := lvl.tactics,
lemmas := lvl.lemmas,
descrText := lvl.descrText,
descrFormat := lvl.descrFormat --toExpr <| format (lvl.goal.raw) --toString <| Syntax.formatStx (lvl.goal.raw) --Syntax.formatStx (lvl.goal.raw) , -- TODO
introduction := lvl.introduction }
2 years ago
c.hOut.writeLspResponse ⟨id, ToJson.toJson levelInfo⟩
return true
| _ => return false
| _ => return false
end Game