create-ifloom 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/bin.js +4 -0
- package/dist/bin.js +391 -0
- package/dist/bin.js.map +1 -0
- package/dist/examples/cafe/index.html +12 -0
- package/dist/examples/cafe/package.json +26 -0
- package/dist/examples/cafe/src/game.ts +41 -0
- package/dist/examples/cafe/src/layout.tsx +49 -0
- package/dist/examples/cafe/src/main.ts +7 -0
- package/dist/examples/cafe/src/rules.ts +38 -0
- package/dist/examples/cafe/src/story/hiring.ts +25 -0
- package/dist/examples/cafe/src/story/opening.ts +19 -0
- package/dist/examples/cafe/src/styles.css +7 -0
- package/dist/examples/cafe/src/world/actions.ts +40 -0
- package/dist/examples/cafe/src/world/menu.ts +14 -0
- package/dist/examples/cafe/src/world/staff.ts +12 -0
- package/dist/examples/cafe/src/world/stats.ts +10 -0
- package/dist/examples/cafe/src/world/triggers.ts +18 -0
- package/dist/examples/cafe/tsconfig.json +9 -0
- package/dist/examples/cafe/vite.config.ts +7 -0
- package/dist/examples/novel/index.html +12 -0
- package/dist/examples/novel/package.json +26 -0
- package/dist/examples/novel/src/game.ts +49 -0
- package/dist/examples/novel/src/layout.tsx +25 -0
- package/dist/examples/novel/src/main.ts +8 -0
- package/dist/examples/novel/src/story/ch1.ts +36 -0
- package/dist/examples/novel/src/story/ch2.ts +42 -0
- package/dist/examples/novel/src/story/ch3.ts +41 -0
- package/dist/examples/novel/src/styles.css +7 -0
- package/dist/examples/novel/src/world/stats.ts +6 -0
- package/dist/examples/novel/tsconfig.json +9 -0
- package/dist/examples/novel/vite.config.ts +7 -0
- package/dist/examples/showcase/.env +4 -0
- package/dist/examples/showcase/index.html +12 -0
- package/dist/examples/showcase/package.json +44 -0
- package/dist/examples/showcase/public/art/character/ava.svg +1 -0
- package/dist/examples/showcase/public/art/character/sam.svg +1 -0
- package/dist/examples/showcase/public/art/photo/ava.svg +1 -0
- package/dist/examples/showcase/public/art/photo/sam.svg +1 -0
- package/dist/examples/showcase/src/game.ts +168 -0
- package/dist/examples/showcase/src/layout.tsx +84 -0
- package/dist/examples/showcase/src/main.tsx +55 -0
- package/dist/examples/showcase/src/place.ts +8 -0
- package/dist/examples/showcase/src/rules.ts +51 -0
- package/dist/examples/showcase/src/story/ava.prose.ts +12 -0
- package/dist/examples/showcase/src/story/ava.ts +199 -0
- package/dist/examples/showcase/src/story/cafe.ts +157 -0
- package/dist/examples/showcase/src/story/flat.ts +90 -0
- package/dist/examples/showcase/src/story/high-street.ts +37 -0
- package/dist/examples/showcase/src/story/moving-day.ts +74 -0
- package/dist/examples/showcase/src/story/office.ts +26 -0
- package/dist/examples/showcase/src/story/park.ts +172 -0
- package/dist/examples/showcase/src/story/sam.ts +27 -0
- package/dist/examples/showcase/src/styles.css +8 -0
- package/dist/examples/showcase/src/ui.ts +44 -0
- package/dist/examples/showcase/src/unlocks/supporter.ts +40 -0
- package/dist/examples/showcase/src/world/achievements.ts +13 -0
- package/dist/examples/showcase/src/world/actions.ts +13 -0
- package/dist/examples/showcase/src/world/cast.ts +46 -0
- package/dist/examples/showcase/src/world/cheats.ts +33 -0
- package/dist/examples/showcase/src/world/phone.ts +88 -0
- package/dist/examples/showcase/src/world/settings.ts +24 -0
- package/dist/examples/showcase/src/world/stats.ts +24 -0
- package/dist/examples/showcase/src/world/strings.ts +7 -0
- package/dist/examples/showcase/src/world/things.ts +59 -0
- package/dist/examples/showcase/tsconfig.json +9 -0
- package/dist/examples/showcase/vite.config.ts +7 -0
- package/dist/templates/next/AGENTS.md +60 -0
- package/dist/templates/next/README.md +33 -0
- package/dist/templates/next/app/Game.tsx +46 -0
- package/dist/templates/next/app/layout.tsx +13 -0
- package/dist/templates/next/app/page.tsx +6 -0
- package/dist/templates/next/gitignore +6 -0
- package/dist/templates/next/next.config.ts +6 -0
- package/dist/templates/next/package.json +33 -0
- package/dist/templates/next/postcss.config.mjs +6 -0
- package/dist/templates/next/src/layout.tsx +73 -0
- package/dist/templates/next/src/styles.css +16 -0
- package/dist/templates/next/tsconfig.json +21 -0
- package/dist/templates/plugin/README.md +81 -0
- package/dist/templates/plugin/gitignore +2 -0
- package/dist/templates/plugin/package.json +66 -0
- package/dist/templates/plugin/src/index.ts +162 -0
- package/dist/templates/plugin/src/react/index.tsx +95 -0
- package/dist/templates/plugin/tests/contracts.test.ts +28 -0
- package/dist/templates/plugin/tests/plugin.test.ts +59 -0
- package/dist/templates/plugin/tests/react.test.tsx +47 -0
- package/dist/templates/plugin/tsconfig.json +21 -0
- package/dist/templates/plugin/tsdown.config.ts +14 -0
- package/dist/templates/react/package.json +30 -0
- package/dist/templates/react/src/layout.tsx +69 -0
- package/dist/templates/react/src/main.ts +11 -0
- package/dist/templates/react/src/styles.css +16 -0
- package/dist/templates/react/vite.config.ts +7 -0
- package/dist/templates/shared/AGENTS.md +58 -0
- package/dist/templates/shared/gitignore +3 -0
- package/dist/templates/shared/index.html +12 -0
- package/dist/templates/shared/src/game.ts +23 -0
- package/dist/templates/shared/src/story/opening.ts +34 -0
- package/dist/templates/shared/src/styles.css +228 -0
- package/dist/templates/shared/src/world/stats.ts +6 -0
- package/dist/templates/shared/tsconfig.json +9 -0
- package/dist/templates/shared/vite.config.ts +5 -0
- package/dist/templates/svelte/package.json +25 -0
- package/dist/templates/svelte/src/App.svelte +11 -0
- package/dist/templates/svelte/src/Beat.svelte +31 -0
- package/dist/templates/svelte/src/InputField.svelte +36 -0
- package/dist/templates/svelte/src/Layout.svelte +84 -0
- package/dist/templates/svelte/src/Line.svelte +26 -0
- package/dist/templates/svelte/src/Minigame.svelte +21 -0
- package/dist/templates/svelte/src/Panel.svelte +27 -0
- package/dist/templates/svelte/src/main.ts +5 -0
- package/dist/templates/svelte/src/runner.ts +29 -0
- package/dist/templates/svelte/vite.config.ts +5 -0
- package/dist/templates/vanilla/package.json +22 -0
- package/dist/templates/vanilla/src/layout.ts +35 -0
- package/dist/templates/vanilla/src/main.ts +253 -0
- package/dist/templates/vue/package.json +25 -0
- package/dist/templates/vue/src/App.vue +12 -0
- package/dist/templates/vue/src/Beat.vue +28 -0
- package/dist/templates/vue/src/InputField.vue +34 -0
- package/dist/templates/vue/src/Layout.vue +84 -0
- package/dist/templates/vue/src/Line.vue +41 -0
- package/dist/templates/vue/src/Minigame.vue +19 -0
- package/dist/templates/vue/src/Panel.vue +30 -0
- package/dist/templates/vue/src/main.ts +5 -0
- package/dist/templates/vue/src/runner.ts +32 -0
- package/dist/templates/vue/vite.config.ts +5 -0
- package/package.json +48 -4
- package/src/bin.ts +18 -0
- package/src/create.ts +89 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* The page: what goes in the side column, what goes in the story, and what
|
|
4
|
+
* floats over them. This is a copy of the React UI's own `DefaultLayout`, so
|
|
5
|
+
* change anything. Delete a part, move it, wrap it, or show it only in some
|
|
6
|
+
* rooms with `when`. Each section hides itself when the game has nothing for
|
|
7
|
+
* it.
|
|
8
|
+
*
|
|
9
|
+
* A plugin's own UI (`shopUi` from `@ifloom/plugin-shop/react`, say), listed
|
|
10
|
+
* in `plugins` in `app/Game.tsx`, goes where each `<Plugins at="…" />` is. Or draw
|
|
11
|
+
* a plugin's components yourself, wherever you like.
|
|
12
|
+
*
|
|
13
|
+
* On a phone the side column becomes a drawer behind a menu button.
|
|
14
|
+
*
|
|
15
|
+
* Not to be confused with Next's `app/layout.tsx`, the HTML around the app:
|
|
16
|
+
* this is the game's page inside it, which `app/Game.tsx` passes to `GameShell`.
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
Clock,
|
|
20
|
+
Game,
|
|
21
|
+
GameMenu,
|
|
22
|
+
InputFields,
|
|
23
|
+
Inventory,
|
|
24
|
+
Notices,
|
|
25
|
+
Overlay,
|
|
26
|
+
PanelHost,
|
|
27
|
+
Plugins,
|
|
28
|
+
RoomActions,
|
|
29
|
+
Scene,
|
|
30
|
+
Section,
|
|
31
|
+
Sidebar,
|
|
32
|
+
StatList,
|
|
33
|
+
Story,
|
|
34
|
+
Title,
|
|
35
|
+
useWords,
|
|
36
|
+
} from '@ifloom/react'
|
|
37
|
+
|
|
38
|
+
export function Layout() {
|
|
39
|
+
// The section titles are UI words, so `ui.stats` in the game's strings renames one.
|
|
40
|
+
const words = useWords()
|
|
41
|
+
return (
|
|
42
|
+
<Game>
|
|
43
|
+
<Sidebar>
|
|
44
|
+
<Title />
|
|
45
|
+
<Clock />
|
|
46
|
+
<GameMenu />
|
|
47
|
+
{/* `collapsible` folds in place, and the player's choice is remembered. */}
|
|
48
|
+
<Section id="stats" title={words('stats')} collapsible open>
|
|
49
|
+
<StatList />
|
|
50
|
+
<Plugins at="stats" />
|
|
51
|
+
</Section>
|
|
52
|
+
<Section id="inventory" title={words('inventory')} collapsible>
|
|
53
|
+
<Inventory title={false} />
|
|
54
|
+
</Section>
|
|
55
|
+
{/* What the plugins' UIs bring to the side column: a Quests button, the People, ... */}
|
|
56
|
+
<Plugins at="sidebar" />
|
|
57
|
+
</Sidebar>
|
|
58
|
+
<Story>
|
|
59
|
+
<Scene />
|
|
60
|
+
<InputFields />
|
|
61
|
+
{/* The bottom of the story: where you are and the way out, what to do, then who's here. */}
|
|
62
|
+
<Plugins at="room" />
|
|
63
|
+
<RoomActions />
|
|
64
|
+
<Plugins at="story" />
|
|
65
|
+
</Story>
|
|
66
|
+
{/* A panel content opens, such as a shop's counter, in a dialog. */}
|
|
67
|
+
<PanelHost />
|
|
68
|
+
<Overlay>
|
|
69
|
+
<Notices />
|
|
70
|
+
</Overlay>
|
|
71
|
+
</Game>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
@import "@ifloom/react/styles.css";
|
|
2
|
+
@import "@ifloom/react/themes/paper.css";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Make it yours. Set any colour or token here, or style any part of the page
|
|
6
|
+
* by its data-part attribute, for example:
|
|
7
|
+
*
|
|
8
|
+
* :root { --font-body: Georgia, serif; --primary: oklch(0.5 0.15 30); }
|
|
9
|
+
* [data-part="choice"] { border-radius: 0; }
|
|
10
|
+
*
|
|
11
|
+
* See https://ifloom.dev/docs/how-to/react/theme-the-react-ui#style-parts-with-css
|
|
12
|
+
*
|
|
13
|
+
* These are Tailwind source, built with your own classes. A game without
|
|
14
|
+
* Tailwind imports @ifloom/react's prebuilt styles.min.css instead, and a
|
|
15
|
+
* preset's .min.css: see "Use the prebuilt stylesheet" on the same page.
|
|
16
|
+
*/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2023",
|
|
4
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"allowImportingTsExtensions": true,
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"isolatedModules": true,
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"jsx": "react-jsx",
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"incremental": true,
|
|
17
|
+
"plugins": [{ "name": "next" }]
|
|
18
|
+
},
|
|
19
|
+
"include": ["next-env.d.ts", "app", "src", ".next/types/**/*.ts", ".next/dev/types/**/*.ts"],
|
|
20
|
+
"exclude": ["node_modules"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# {{title}}
|
|
2
|
+
|
|
3
|
+
A plugin for [IFLoom](https://ifloom.dev). TODO: say what it adds, in a sentence or two.
|
|
4
|
+
|
|
5
|
+
## Use it in a game
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install {{name}}
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Add it to the game's `plugins`, and give it content in `data.__id__.entries`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { loadGame, type Content } from '@ifloom/engine'
|
|
15
|
+
import { __camel__ } from '{{name}}'
|
|
16
|
+
|
|
17
|
+
const town: Content = {
|
|
18
|
+
actions: [{ id: 'shop', label: 'Buy bread', effects: [__camel__.mark('bread')] }],
|
|
19
|
+
data: { '__id__': { entries: [{ id: 'bread', label: 'A loaf of bread' }] } },
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { game } = loadGame([town], { id: 'my-game', title: 'My Game', plugins: [__camel__()] })
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Name | Used as | What it does |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| `__id__/marked` (`__camel__.marked`) | condition | `{ '__id__/marked': { entry } }` holds once the entry is marked. |
|
|
28
|
+
| `__id__/mark` (`__camel__.mark`) | effect | `{ '__id__/mark': { entry } }` marks the entry. |
|
|
29
|
+
| `__id__/mark` (`__camel__.markCommand`) | command | `{ '__id__/mark': { id } }` marks the entry `id`, or says why not. |
|
|
30
|
+
|
|
31
|
+
A UI reads `view.plugins.__id__`, typed as `__Pascal__View` through the `ViewRegistry` entry in `src/index.ts`.
|
|
32
|
+
|
|
33
|
+
## Its React UI
|
|
34
|
+
|
|
35
|
+
A game drawn with [`@ifloom/react`](https://ifloom.dev/docs/reference/react) lists the plugin's React UI, from the `{{name}}/react` subpath:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { mountGame } from '@ifloom/react'
|
|
39
|
+
import { __camel__Ui } from '{{name}}/react'
|
|
40
|
+
|
|
41
|
+
mountGame(game, { plugins: [__camel__Ui] })
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
It puts the entries in the side column. A game can draw them itself instead, from `use__Pascal__()` or `view.plugins.__id__`, and a game with another UI never loads `{{name}}/react`: React and `@ifloom/react` are optional peers. It's built only from `@ifloom/react`'s exports, declared once with `pluginKit` and read with `usePlugin`, and styled by the kit's parts and theme tokens, so it looks right in every preset with no stylesheet. [Plugin UIs and the kit](https://ifloom.dev/docs/reference/react/kit) explains each part.
|
|
45
|
+
|
|
46
|
+
## What it exports
|
|
47
|
+
|
|
48
|
+
| Export | What it is |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `__camel__` | The plugin's factory, with its helpers: `__camel__.marked`, `__camel__.mark` and `__camel__.markCommand`. |
|
|
51
|
+
| `entriesOf`, `entryOf` | The game's entries, and one by id. |
|
|
52
|
+
| `__Pascal__Entry`, `__Pascal__State`, `__Pascal__View` | An entry as content writes it, what the plugin keeps in a save, and what a UI reads. |
|
|
53
|
+
| `__camel__Ui` | From `{{name}}/react`: the React UI, for `mountGame`'s `plugins`. |
|
|
54
|
+
| `use__Pascal__`, `__Pascal__Controls` | From `{{name}}/react`: the hook that reads the view in a React UI, and what it returns. |
|
|
55
|
+
| `__Pascal__List`, `__Pascal__Section` | From `{{name}}/react`: the list of entries, which a game can replace, and the section it sits in. |
|
|
56
|
+
| `__CONSTANT___WORDS` | From `{{name}}/react`: the React UI's words, for a game to reword. |
|
|
57
|
+
|
|
58
|
+
## Work on it
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
npm install
|
|
62
|
+
npm test # vitest: the plugin's test, its React UI's, and its contract test
|
|
63
|
+
npm run typecheck # tsc --noEmit
|
|
64
|
+
npm run build # dist/, what npm publish packs
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
[Writing a plugin](https://ifloom.dev/docs/reference/plugin-api) explains every part a plugin can have: hooks, guards, prices, the view, migrations, the profile and checks.
|
|
68
|
+
|
|
69
|
+
## Getting it listed
|
|
70
|
+
|
|
71
|
+
The docs list plugins other people have written on [Community plugins](https://ifloom.dev/docs/reference/plugins/community). A plugin is listed after someone has read it, when:
|
|
72
|
+
|
|
73
|
+
- it imports only the public API of `@ifloom/engine`, and reaches other plugins only by name (`cooperates`, their conditions, effects and `query`);
|
|
74
|
+
- its engine side needs no UI: it gives a view, and works in a game with no UI package. A React UI, if it has one, is its `/react` subpath, built only from `@ifloom/react`'s public exports;
|
|
75
|
+
- `@ifloom/engine` is a peer dependency, named with `^` and a version;
|
|
76
|
+
- it has tests that play it in a small game, and they pass;
|
|
77
|
+
- content it reads lives in `data`, and its conditions, effects and commands follow the [naming rules](https://ifloom.dev/docs/reference/plugin-api#id);
|
|
78
|
+
- it has an open-source licence, in its repository and `package.json`;
|
|
79
|
+
- it's on npm, with this README saying what it adds and how to use it.
|
|
80
|
+
|
|
81
|
+
To submit it, open a [plugin submission](https://github.com/IFLoom/ifloom/issues/new?template=plugin-submission.yml).
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{name}}",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "{{title}}: a plugin for IFLoom.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"ifloom",
|
|
8
|
+
"ifloom-plugin",
|
|
9
|
+
"interactive-fiction"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./react": {
|
|
18
|
+
"types": "./dist/react/index.d.ts",
|
|
19
|
+
"default": "./dist/react/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsdown",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@ifloom/engine": "^0.1.1",
|
|
33
|
+
"@ifloom/react": "^0.1.1",
|
|
34
|
+
"react": "^19.0.0",
|
|
35
|
+
"react-dom": "^19.0.0",
|
|
36
|
+
"@types/react": "^19.0.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"@ifloom/react": {
|
|
40
|
+
"optional": true
|
|
41
|
+
},
|
|
42
|
+
"react": {
|
|
43
|
+
"optional": true
|
|
44
|
+
},
|
|
45
|
+
"react-dom": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"@types/react": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@ifloom/audio": "^0.1.1",
|
|
54
|
+
"@ifloom/browser": "^0.1.1",
|
|
55
|
+
"@ifloom/engine": "^0.1.1",
|
|
56
|
+
"@ifloom/react": "^0.1.1",
|
|
57
|
+
"@types/node": "^26.6.4",
|
|
58
|
+
"@types/react": "^19.3.0",
|
|
59
|
+
"@types/react-dom": "^19.3.0",
|
|
60
|
+
"react": "19.3.0",
|
|
61
|
+
"react-dom": "19.3.0",
|
|
62
|
+
"tsdown": "0.23.0",
|
|
63
|
+
"typescript": "6.0.3",
|
|
64
|
+
"vitest": "^4.1.11"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{title}}, a plugin for IFLoom. TODO: what it adds, in a sentence or two.
|
|
3
|
+
*
|
|
4
|
+
* It imports only `@ifloom/engine` and requires no other plugin. To work with
|
|
5
|
+
* one the game may have, list it in `cooperates` and ask it through its
|
|
6
|
+
* conditions, effects and `query`, which answer nothing when the game lacks it.
|
|
7
|
+
* Any UI draws its view from `view.plugins.__id__`. Its own React UI is in
|
|
8
|
+
* `src/react/`, the `/react` subpath, which a game drawn another way never loads.
|
|
9
|
+
*
|
|
10
|
+
* How each part is named: https://ifloom.dev/docs/reference/plugin-api#id
|
|
11
|
+
*/
|
|
12
|
+
import { stringOf, type CommandRef, type Condition, type EffectRef, type Game } from '@ifloom/engine'
|
|
13
|
+
import {
|
|
14
|
+
arg,
|
|
15
|
+
defineCalls,
|
|
16
|
+
defineData,
|
|
17
|
+
definePlugin,
|
|
18
|
+
list,
|
|
19
|
+
type CommandsOf,
|
|
20
|
+
type ConditionsOf,
|
|
21
|
+
type DefinedPlugin,
|
|
22
|
+
type EffectsOf,
|
|
23
|
+
type PluginProblem,
|
|
24
|
+
} from '@ifloom/engine/plugin'
|
|
25
|
+
|
|
26
|
+
const ID = '__id__'
|
|
27
|
+
|
|
28
|
+
/** One entry of the plugin's content, from the game's `data.__id__.entries`. TODO: the real fields. */
|
|
29
|
+
export type __Pascal__Entry = {
|
|
30
|
+
id: string
|
|
31
|
+
label: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What the plugin keeps in a save. Bump `version` and add a migration when this changes shape. */
|
|
35
|
+
export type __Pascal__State = {
|
|
36
|
+
/** Entries marked, with the turn each was marked on. */
|
|
37
|
+
marked: Record<string, number>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a UI reads at `view.plugins.__id__`. */
|
|
41
|
+
export type __Pascal__View = {
|
|
42
|
+
entries: { id: string; label: string; marked: boolean }[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The lists of the plugin's content, each declared once: `data.__id__.entries`,
|
|
47
|
+
* what one entry is called in a problem, and which of its fields are words a
|
|
48
|
+
* player reads, so a translation's `words` table reaches them
|
|
49
|
+
* (`__id__/entries.<id>.label`, https://ifloom.dev/docs/reference/words).
|
|
50
|
+
*/
|
|
51
|
+
const DATA = defineData(ID, { entries: list<__Pascal__Entry>({ noun: 'entry', words: { label: true } }) })
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The plugin's conditions, effects and commands, each declared once with what
|
|
55
|
+
* it takes. The implementations below are handed their arguments typed and
|
|
56
|
+
* read from what content wrote; the content checker reports an argument left
|
|
57
|
+
* out, of the wrong shape or that names an entry the game lacks; and the
|
|
58
|
+
* helpers write calls with these writers.
|
|
59
|
+
*/
|
|
60
|
+
const CALLS = defineCalls(ID, {
|
|
61
|
+
conditions: {
|
|
62
|
+
/** The entry has been marked. */
|
|
63
|
+
marked: { entry: arg.entry(DATA.entries) },
|
|
64
|
+
},
|
|
65
|
+
effects: {
|
|
66
|
+
/** Mark the entry now. */
|
|
67
|
+
mark: { entry: arg.entry(DATA.entries) },
|
|
68
|
+
},
|
|
69
|
+
commands: {
|
|
70
|
+
/** Mark the entry `id`. */
|
|
71
|
+
mark: { id: arg.entry(DATA.entries) },
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* What content writes for each call, from `CALLS`, and what the view holds,
|
|
77
|
+
* so a game that imports the plugin has its calls checked:
|
|
78
|
+
* `{ '__id__/marked': { entyr } }` fails to compile.
|
|
79
|
+
*/
|
|
80
|
+
declare module '@ifloom/engine' {
|
|
81
|
+
interface ConditionRegistry extends ConditionsOf<typeof CALLS> {}
|
|
82
|
+
interface EffectRegistry extends EffectsOf<typeof CALLS> {}
|
|
83
|
+
interface CommandRegistry extends CommandsOf<typeof CALLS> {}
|
|
84
|
+
interface ViewRegistry {
|
|
85
|
+
'__id__': __Pascal__View
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The game's entries: `data.__id__.entries`, from its content and packs. A list is read with `<list>Of(game)`, one by id with `<one>Of(game, id)`. */
|
|
90
|
+
export function entriesOf(game: Game): __Pascal__Entry[] {
|
|
91
|
+
return DATA.entries.all(game)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** One of the game's entries, by id. */
|
|
95
|
+
export function entryOf(game: Game, id: string): __Pascal__Entry | undefined {
|
|
96
|
+
return DATA.entries.get(game, id)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The plugin. Add it to the game's `plugins`, and put entries in its content's
|
|
101
|
+
* `data.__id__.entries`. Anything a mod could add or change is content in
|
|
102
|
+
* `data`, never an option. Hooks are flat members named `onX` (`onTurn`,
|
|
103
|
+
* `onCommand`) and return the next state; providers are nouns (`view`,
|
|
104
|
+
* `label`) and change nothing. The helpers content is written with go on the
|
|
105
|
+
* factory, below.
|
|
106
|
+
*/
|
|
107
|
+
function __camel__Plugin(): DefinedPlugin<__Pascal__State, __Pascal__View> {
|
|
108
|
+
return definePlugin<__Pascal__State, __Pascal__View, typeof CALLS>({
|
|
109
|
+
id: ID,
|
|
110
|
+
version: 1,
|
|
111
|
+
calls: CALLS,
|
|
112
|
+
data: DATA,
|
|
113
|
+
start: () => ({ marked: {} }),
|
|
114
|
+
// The plugin's own lines, which a game rewords by their full key, `__id__/noSuchEntry`.
|
|
115
|
+
strings: { noSuchEntry: 'There is nothing like that.' },
|
|
116
|
+
conditions: {
|
|
117
|
+
/** `{ '__id__/marked': { entry } }`: the entry has been marked. */
|
|
118
|
+
marked: (_state, { entry }, { slice }) => slice.marked[entry] !== undefined,
|
|
119
|
+
},
|
|
120
|
+
effects: {
|
|
121
|
+
/** `{ '__id__/mark': { entry } }`: mark the entry now. */
|
|
122
|
+
mark: (state, { entry }, { slice, set, game }) => {
|
|
123
|
+
if (slice.marked[entry] !== undefined || !entryOf(game, entry)) return state
|
|
124
|
+
return set({ ...slice, marked: { ...slice.marked, [entry]: state.turn } })
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
commands: {
|
|
128
|
+
/** `{ '__id__/mark': { id } }`: mark the entry `id`. */
|
|
129
|
+
mark: {
|
|
130
|
+
whyNot: (_state, { id }, { game }) => (entryOf(game, id) ? undefined : stringOf(game, '__id__/noSuchEntry')),
|
|
131
|
+
run: (state, { id }, { slice, set }) => set({ ...slice, marked: { ...slice.marked, [id]: state.turn } }),
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
view: (_state, { slice, game }): __Pascal__View => ({
|
|
135
|
+
entries: entriesOf(game).map((entry) => ({ id: entry.id, label: entry.label, marked: slice.marked[entry.id] !== undefined })),
|
|
136
|
+
}),
|
|
137
|
+
// What a call names comes from `CALLS`. What the content names would go in `references.content`, `DATA.entries.content(game, ...)`.
|
|
138
|
+
check: (game) => {
|
|
139
|
+
const problems: PluginProblem[] = []
|
|
140
|
+
const seen = new Set<string>()
|
|
141
|
+
for (const entry of entriesOf(game)) {
|
|
142
|
+
if (seen.has(entry.id)) problems.push(DATA.entries.problem(entry.id, 'two entries share this id'))
|
|
143
|
+
seen.add(entry.id)
|
|
144
|
+
}
|
|
145
|
+
return problems
|
|
146
|
+
},
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** A condition: the entry has been marked. */
|
|
151
|
+
const marked = (entry: string): Condition => CALLS.conditions.marked({ entry })
|
|
152
|
+
/** An effect: mark the entry. */
|
|
153
|
+
const mark = (entry: string): EffectRef => CALLS.effects.mark({ entry })
|
|
154
|
+
/** A command: mark the entry. */
|
|
155
|
+
const markCommand = (entry: string): CommandRef => CALLS.commands.mark({ id: entry })
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The plugin, with its helpers on it: `__camel__()` makes the plugin for the
|
|
159
|
+
* game's `plugins`, and content is written with `__camel__.mark('bread')`,
|
|
160
|
+
* so the helpers' names never clash with another plugin's.
|
|
161
|
+
*/
|
|
162
|
+
export const __camel__ = Object.assign(__camel__Plugin, { marked, mark, markCommand })
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {{title}}'s React UI, for a game drawn with `@ifloom/react`: the
|
|
3
|
+
* `{{name}}/react` subpath. A game lists `__camel__Ui` in its `plugins` to
|
|
4
|
+
* have the entries in the side column, or draws them itself from
|
|
5
|
+
* `use__Pascal__`. A game with another UI never loads this file.
|
|
6
|
+
*
|
|
7
|
+
* It's built only from `@ifloom/react` and `@ifloom/react/kit`, the same
|
|
8
|
+
* pieces a game's own UI uses, and styled by the kit's parts (each with its own look),
|
|
9
|
+
* the kit's layout classes and the theme's tokens, so it looks right in
|
|
10
|
+
* every preset with no stylesheet of its own.
|
|
11
|
+
*
|
|
12
|
+
* How a plugin UI is made: https://ifloom.dev/docs/reference/react/kit
|
|
13
|
+
*/
|
|
14
|
+
import type { GameState, Result } from '@ifloom/engine'
|
|
15
|
+
import type { ComponentProps } from 'react'
|
|
16
|
+
import { Section, usePart } from '@ifloom/react'
|
|
17
|
+
import { overridable, pluginKit, useKitPart, usePlugin, type Override, type PluginUi } from '@ifloom/react/kit'
|
|
18
|
+
import { __camel__, type __Pascal__View } from '../index.ts'
|
|
19
|
+
|
|
20
|
+
/** The plugin's words, in English. A game rewords any with `ui.<key>` in its strings: `{ 'ui.__camel__Marked': 'Done' }`. */
|
|
21
|
+
export const __CONSTANT___WORDS = {
|
|
22
|
+
__camel__Title: '{{title}}',
|
|
23
|
+
__camel__Marked: 'Marked',
|
|
24
|
+
} as const
|
|
25
|
+
|
|
26
|
+
// The UI declared once, its id and its words: `usePlugin` reads the plugin's view, typed, and says its words; `__camel__Ui` spreads it.
|
|
27
|
+
const __CONSTANT__ = pluginKit('__id__', __CONSTANT___WORDS)
|
|
28
|
+
|
|
29
|
+
// The component and its parts, for a game's `components` and `classes`.
|
|
30
|
+
declare module '@ifloom/react' {
|
|
31
|
+
interface OverridableComponents {
|
|
32
|
+
__Pascal__List: Override<ComponentProps<typeof __Pascal__List>>
|
|
33
|
+
}
|
|
34
|
+
interface PartNames {
|
|
35
|
+
'__id__': true
|
|
36
|
+
'__id__-entry': true
|
|
37
|
+
'__id__-marked': true
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What `use__Pascal__` returns: the plugin's view, and its command. */
|
|
42
|
+
export type __Pascal__Controls = __Pascal__View & {
|
|
43
|
+
/** Mark an entry: sends `__id__/mark`, refused with its reason. */
|
|
44
|
+
mark: (entry: string) => Result<GameState>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The plugin's view with its command, or null in a game without the plugin. `__Pascal__List` is drawn from it. */
|
|
48
|
+
export function use__Pascal__(): __Pascal__Controls | null {
|
|
49
|
+
const { view, send } = usePlugin(__CONSTANT__)
|
|
50
|
+
if (!view) return null
|
|
51
|
+
return { ...view, mark: (entry) => send(__camel__.markCommand(entry)) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function __Pascal__ListImpl({ className }: { className?: string }) {
|
|
55
|
+
const { words } = usePlugin(__CONSTANT__)
|
|
56
|
+
const data = use__Pascal__()
|
|
57
|
+
const list = usePart('__id__', 'flex flex-col gap-2 text-sm', className)
|
|
58
|
+
// Kit parts bring their own look, so a preset styles them as it does every plugin's: add only layout.
|
|
59
|
+
const row = useKitPart('item', '__id__-entry', 'flex-row items-center gap-2')
|
|
60
|
+
const badge = useKitPart('badge', '__id__-marked')
|
|
61
|
+
if (!data?.entries.length) return null
|
|
62
|
+
return (
|
|
63
|
+
<ul {...list}>
|
|
64
|
+
{data.entries.map((entry) => (
|
|
65
|
+
<li key={entry.id} {...row} data-entry={entry.id}>
|
|
66
|
+
{entry.label}
|
|
67
|
+
{entry.marked ? <span {...badge}>{words('__camel__Marked')}</span> : null}
|
|
68
|
+
</li>
|
|
69
|
+
))}
|
|
70
|
+
</ul>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The entries, each with a badge once it's marked. Replace it by name in `components`. */
|
|
75
|
+
export const __Pascal__List = overridable('__Pascal__List', __Pascal__ListImpl)
|
|
76
|
+
|
|
77
|
+
/** The default layout's section for it: the list, folding in place, hidden while there's nothing in it. */
|
|
78
|
+
export function __Pascal__Section() {
|
|
79
|
+
const { words } = usePlugin(__CONSTANT__)
|
|
80
|
+
return (
|
|
81
|
+
<Section id="__id__" title={words('__camel__Title')} collapsible>
|
|
82
|
+
<__Pascal__List />
|
|
83
|
+
</Section>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The plugin's React UI, for `plugins` on `mountGame` or `GameProvider`: its
|
|
89
|
+
* section in the side column, and its words. It can also bring panels by
|
|
90
|
+
* name, pages, settings and inline pieces: see `PluginUi`.
|
|
91
|
+
*/
|
|
92
|
+
export const __camel__Ui: PluginUi = {
|
|
93
|
+
...__CONSTANT__,
|
|
94
|
+
layout: { sidebar: [__Pascal__Section] },
|
|
95
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { readFileSync, readdirSync } from 'node:fs'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { boundaryProblems, undocumentedExports } from '@ifloom/engine/testing'
|
|
5
|
+
|
|
6
|
+
const root = new URL('../', import.meta.url)
|
|
7
|
+
const read = (path: string) => readFileSync(new URL(path, root), 'utf8')
|
|
8
|
+
|
|
9
|
+
/** Every source file's text, by its path from the package's folder: `src/index.ts`, `src/react/index.tsx`. */
|
|
10
|
+
const sources = Object.fromEntries(
|
|
11
|
+
readdirSync(new URL('src/', root), { recursive: true, encoding: 'utf8' })
|
|
12
|
+
.filter((path) => /\.tsx?$/.test(path))
|
|
13
|
+
.map((path) => [`src/${path.replaceAll('\\', '/')}`, read(`src/${path}`)]),
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
// The rules a plugin keeps to be listed on Community plugins, read from the source as written.
|
|
17
|
+
describe('the plugin package', () => {
|
|
18
|
+
// It imports only @ifloom/engine and its own files, uses no browser global outside its React UI, and its React UI
|
|
19
|
+
// (src/react/, the /react subpath) imports only the engine, React, @ifloom/react's front door and its own files.
|
|
20
|
+
it('keeps to the plugin boundary', () => {
|
|
21
|
+
expect(boundaryProblems(sources)).toEqual([])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// A game author learns the plugin from its README, so an export the README doesn't name is one nobody finds.
|
|
25
|
+
it('names every export, its React UI’s too, in the README', () => {
|
|
26
|
+
expect(undocumentedExports([read('src/index.ts'), read('src/react/index.tsx')], read('README.md'))).toEqual([])
|
|
27
|
+
})
|
|
28
|
+
})
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { apply, checkGame, holds, loadGame, view, type Content, type Game } from '@ifloom/engine'
|
|
3
|
+
import type { Plugin } from '@ifloom/engine/plugin'
|
|
4
|
+
import { freshState, loadChecked, partnerCases, play } from '@ifloom/engine/testing'
|
|
5
|
+
import { __camel__, type __Pascal__Entry } from '../src/index.ts'
|
|
6
|
+
|
|
7
|
+
const entries: __Pascal__Entry[] = [
|
|
8
|
+
{ id: 'bread', label: 'A loaf of bread' },
|
|
9
|
+
{ id: 'milk', label: 'A pint of milk' },
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
const town: Content = {
|
|
13
|
+
name: 'town',
|
|
14
|
+
actions: [{ id: 'shop', effects: [__camel__.mark('bread')] }],
|
|
15
|
+
data: { '__id__': { entries } },
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The town with these plugins, failing on any problem the content checker finds. */
|
|
19
|
+
const game = (plugins: Plugin[] = [__camel__()]): Game => loadChecked([town], { id: '__id__-town', title: '{{title}} town', plugins })
|
|
20
|
+
|
|
21
|
+
const shown = (g: Game, state = freshState(g)) => view(state, g).plugins['__id__']!
|
|
22
|
+
|
|
23
|
+
// TODO: replace these with the plugin's real behaviour.
|
|
24
|
+
describe('__id__', () => {
|
|
25
|
+
it('refuses to mark what is not there', () => {
|
|
26
|
+
const g = game()
|
|
27
|
+
expect(apply(freshState(g), g, __camel__.markCommand('cheese')).reason).toBe('There is nothing like that.')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('reports an entry content names that the game lacks', () => {
|
|
31
|
+
const loaded = loadGame([{ ...town, actions: [{ id: 'shop', effects: [__camel__.mark('cheese')] }] }], {
|
|
32
|
+
id: '__id__-town',
|
|
33
|
+
title: '{{title}} town',
|
|
34
|
+
plugins: [__camel__()],
|
|
35
|
+
})
|
|
36
|
+
expect(loaded.problems.map((problem) => problem.what)).toEqual(['unknown entry "cheese"'])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('reports two entries with one id in a hand-built game (loading merges entries by id)', () => {
|
|
40
|
+
const g = game()
|
|
41
|
+
const doubled = { ...g, data: { '__id__': { entries: [...entries, entries[0]] } } }
|
|
42
|
+
expect(checkGame(doubled).problems.map((problem) => problem.what)).toContain('two entries share this id')
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// The plugin alone, then with each plugin it cooperates with: list a partner here, such as
|
|
47
|
+
// `places({ start: 'street' })`, once it's in the plugin's `cooperates`, and test what it changes in its own block.
|
|
48
|
+
for (const { name, plugins } of partnerCases(__camel__(), [])) {
|
|
49
|
+
describe(`__id__, ${name}`, () => {
|
|
50
|
+
it('marks an entry through its effect and its command, and shows it', () => {
|
|
51
|
+
const g = game(plugins)
|
|
52
|
+
expect(shown(g).entries.map((entry) => entry.marked)).toEqual([false, false])
|
|
53
|
+
const shopped = play(freshState(g), g, { type: 'action', id: 'shop' })
|
|
54
|
+
expect(holds(shopped, g, __camel__.marked('bread'))).toBe(true)
|
|
55
|
+
const both = play(shopped, g, __camel__.markCommand('milk'))
|
|
56
|
+
expect(shown(g, both).entries.map((entry) => entry.marked)).toEqual([true, true])
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { renderToString } from 'react-dom/server'
|
|
3
|
+
import { createRunner, loadGame, type Content } from '@ifloom/engine'
|
|
4
|
+
import { freshState } from '@ifloom/engine/testing'
|
|
5
|
+
import { GameProvider } from '@ifloom/react'
|
|
6
|
+
import { __camel__ } from '../src/index.ts'
|
|
7
|
+
import { __camel__Ui, __Pascal__List } from '../src/react/index.tsx'
|
|
8
|
+
|
|
9
|
+
/** A town with two entries, the first marked by the `shop` action. */
|
|
10
|
+
function runner() {
|
|
11
|
+
const town: Content = {
|
|
12
|
+
name: 'town',
|
|
13
|
+
actions: [{ id: 'shop', effects: [__camel__.mark('bread')] }],
|
|
14
|
+
data: {
|
|
15
|
+
'__id__': {
|
|
16
|
+
entries: [
|
|
17
|
+
{ id: 'bread', label: 'A loaf of bread' },
|
|
18
|
+
{ id: 'milk', label: 'A pint of milk' },
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
const { game } = loadGame([town], { id: '__id__-town', title: '{{title}} town', plugins: [__camel__()] })
|
|
24
|
+
return createRunner(game, freshState(game))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// TODO: test what your UI draws and sends. Drawn to a string here, so it needs no browser;
|
|
28
|
+
// @testing-library/react with jsdom can click it too.
|
|
29
|
+
describe('the React UI', () => {
|
|
30
|
+
it('lists the entries, marked ones with a badge', () => {
|
|
31
|
+
const playing = runner()
|
|
32
|
+
playing.apply({ type: 'action', id: 'shop' })
|
|
33
|
+
const html = renderToString(
|
|
34
|
+
<GameProvider runner={playing} plugins={[__camel__Ui]}>
|
|
35
|
+
<__Pascal__List />
|
|
36
|
+
</GameProvider>,
|
|
37
|
+
)
|
|
38
|
+
expect(html).toContain('A loaf of bread')
|
|
39
|
+
expect(html).toContain('A pint of milk')
|
|
40
|
+
expect(html.match(/Marked/g)).toHaveLength(1)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('is a module for the game’s plugins list, named for the plugin', () => {
|
|
44
|
+
expect(__camel__Ui.id).toBe('__id__')
|
|
45
|
+
expect(__camel__Ui.layout?.sidebar).toHaveLength(1)
|
|
46
|
+
})
|
|
47
|
+
})
|