Skip to content

Commit

Permalink
Autogenerate scenario schema doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 22, 2023
1 parent e06e04f commit d474d3a
Show file tree
Hide file tree
Showing 11 changed files with 535 additions and 105 deletions.
1 change: 1 addition & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ cliParser =
, Just Recipes <$ switch (long "recipes" <> help "Generate recipes page (uses data from recipes.yaml)")
, Just Capabilities <$ switch (long "capabilities" <> help "Generate capabilities page (uses entity map)")
, Just Commands <$ switch (long "commands" <> help "Generate commands page (uses constInfo, constCaps and inferConst)")
, Just Scenario <$ switch (long "scenario" <> help "Generate scenario schema page")
]
seed :: Parser (Maybe Int)
seed = optional $ option auto (long "seed" <> short 's' <> metavar "INT" <> help "Seed to use for world generation")
Expand Down
206 changes: 206 additions & 0 deletions data/scenarios/README_NEW.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions data/schema/display.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/swarm-game/swarm/main/data/schema/display.json",
"title": "Swarm entity display",
"description": "How to display an entity or robot in the Swarm game",
"description": "A display specifies how an entity or a robot (robots are essentially special kinds of entities) is displayed in the world. It consists of a key-value mapping described by the following table.",
"type": "object",
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -47,7 +47,7 @@
"blue",
"water"
],
"description": "The name of the attribute that should be used to style the robot or entity. A list of currently valid attributes can be found at https://github.com/swarm-game/swarm/blob/main/src/Swarm/TUI/Attr.hs."
"description": "The name of the attribute that should be used to style the robot or entity. A list of currently valid attributes can be found at https://github.com/swarm-game/swarm/blob/main/src/Swarm/TUI/View/Attribute/Attr.hs."
},
"priority": {
"default": 1,
Expand Down
102 changes: 1 addition & 101 deletions data/schema/entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,106 +5,6 @@
"description": "Description of entities in the Swarm game",
"type": "array",
"items": {
"description": "Description of an entity in the Swarm game",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"description": "The name of the entity. This is what will show up in the inventory and how the entity can be referred to."
},
"display": {
"type": "object",
"$ref": "./display.json",
"description": "Display information for the entity."
},
"plural": {
"default": "null",
"type": "string",
"description": "An explicit plural form of the name of the entity. If omitted, standard heuristics will be used for forming the English plural of its name."
},
"description": {
"type": "array",
"items": [
{
"type": "string"
}
],
"description": "A description of the entity, as a list of paragraphs."
},
"orientation": {
"default": "null",
"type": "array",
"items": [
{
"name": "X coordinate",
"type": "number"
},
{
"name": "Y coordinate",
"type": "number"
}
],
"description": "A 2-tuple of integers specifying an orientation vector for the entity. Currently unused."
},
"growth": {
"default": "null",
"type": "array",
"items": [
{
"name": "minimum",
"type": "number"
},
{
"name": "maximum",
"type": "number"
}
],
"description": "For growable entities, a 2-tuple of integers specifying the minimum and maximum amount of time taken for one growth stage. The actual time for one growth stage will be chosen uniformly at random from this range; it takes two growth stages for an entity to be fully grown."
},
"combustion": {
"type": "object",
"$ref": "./combustion.json",
"description": "Properties of combustion."
},
"yields": {
"default": "null",
"type": "string",
"description": "The name of the entity which will be added to a robot's inventory when it executes grab or harvest on this entity. If omitted, the entity will simply yield itself."
},
"properties": {
"default": "[]",
"type": "array",
"items": [
{
"type": "string",
"examples": [
"unwalkable",
"portable",
"infinite",
"known",
"growable"
]
}
],
"description": "A list of properties of this entity. See Entity properties."
},
"capabilities": {
"default": "[]",
"type": "array",
"items": [
{
"type": "string"
}
],
"description": "A list of capabilities provided by entity, when it is equipped as a device. See Capabilities."
}
},
"required": [
"name",
"display",
"description"
]
"$ref": "./entity.json"
}

}
105 changes: 105 additions & 0 deletions data/schema/entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/swarm-game/swarm/main/data/schema/entity.json",
"title": "Entity",
"description": "Description of an entity in the Swarm game",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"description": "The name of the entity. This is what will show up in the inventory and how the entity can be referred to."
},
"display": {
"type": "object",
"$ref": "./display.json",
"description": "Display information for the entity."
},
"plural": {
"default": "null",
"type": "string",
"description": "An explicit plural form of the name of the entity. If omitted, standard heuristics will be used for forming the English plural of its name."
},
"description": {
"type": "array",
"items": [
{
"type": "string"
}
],
"description": "A description of the entity, as a list of paragraphs."
},
"orientation": {
"default": "null",
"type": "array",
"items": [
{
"name": "X coordinate",
"type": "number"
},
{
"name": "Y coordinate",
"type": "number"
}
],
"description": "A 2-tuple of integers specifying an orientation vector for the entity. Currently unused."
},
"growth": {
"default": "null",
"type": "array",
"items": [
{
"name": "minimum",
"type": "number"
},
{
"name": "maximum",
"type": "number"
}
],
"description": "For growable entities, a 2-tuple of integers specifying the minimum and maximum amount of time taken for one growth stage. The actual time for one growth stage will be chosen uniformly at random from this range; it takes two growth stages for an entity to be fully grown."
},
"combustion": {
"type": "object",
"$ref": "./combustion.json",
"description": "Properties of combustion."
},
"yields": {
"default": "null",
"type": "string",
"description": "The name of the entity which will be added to a robot's inventory when it executes grab or harvest on this entity. If omitted, the entity will simply yield itself."
},
"properties": {
"default": "[]",
"type": "array",
"items": [
{
"type": "string",
"examples": [
"unwalkable",
"portable",
"infinite",
"known",
"growable"
]
}
],
"description": "A list of properties of this entity. See Entity properties."
},
"capabilities": {
"default": "[]",
"type": "array",
"items": [
{
"type": "string"
}
],
"description": "A list of capabilities provided by entity, when it is equipped as a device. See Capabilities."
}
},
"required": [
"name",
"display",
"description"
]
}
4 changes: 3 additions & 1 deletion data/schema/objective.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"description": "A compact (2-3 word) summary of the goal",
"type": "string"
},
"prerequisite": {}
"prerequisite": {
"type": "object"
}
}
}
4 changes: 3 additions & 1 deletion src/Swarm/Doc/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Data.Text qualified as T
import Data.Text.IO qualified as T
import Data.Tuple (swap)
import Swarm.Doc.Pedagogy
import Swarm.Doc.Schema.Scenario
import Swarm.Game.Display (displayChar)
import Swarm.Game.Entity (Entity, EntityMap (entitiesByName), entityDisplay, entityName, loadEntities)
import Swarm.Game.Entity qualified as E
Expand Down Expand Up @@ -92,7 +93,7 @@ data EditorType = Emacs | VSCode | Vim
deriving (Eq, Show, Enum, Bounded)

-- | An enumeration of the kinds of cheat sheets we can produce.
data SheetType = Entities | Commands | Capabilities | Recipes
data SheetType = Entities | Commands | Capabilities | Recipes | Scenario
deriving (Eq, Show, Enum, Bounded)

-- | A configuration record holding the URLs of the various cheat
Expand Down Expand Up @@ -135,6 +136,7 @@ generateDocs = \case
entities <- loadEntities
recipes <- loadRecipes entities
sendIO $ T.putStrLn $ recipePage address recipes
Scenario -> genScenarioSchemaDocs
TutorialCoverage -> renderTutorialProgression >>= putStrLn . T.unpack

-- ----------------------------------------------------------------------------
Expand Down
36 changes: 36 additions & 0 deletions src/Swarm/Doc/Schema/Refined.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Refined JSON schema after converting
-- all JSON Value types to their specific sum types
module Swarm.Doc.Schema.Refined where

import Data.Aeson
import Control.Applicative ((<|>))
import Data.Text (Text)
import Text.Pandoc.Builder
import Data.Text qualified as T

newtype SingleOrList a = SingleOrList {
getList :: [a]
} deriving (Eq, Ord, Show)

instance (FromJSON a) => FromJSON (SingleOrList a) where
parseJSON x = SingleOrList <$> do
(pure <$> parseJSON x) <|> parseJSON x

type SchemaIdReference = Text

data SchemaType =
Simple (SingleOrList Text)
| Reference SchemaIdReference
| Alternates [Value]
deriving (Eq, Ord, Show)

listToText :: SchemaType -> Inlines
listToText = \case
Simple xs -> code $ T.intercalate " | " $ getList xs
Reference x -> link x "Object schema" $ text "Link to object properties"
Alternates xs -> code $ T.intercalate " | " $ map (T.pack . show) xs
Loading

0 comments on commit d474d3a

Please sign in to comment.