|
| 1 | +# Working in this repository |
| 2 | + |
| 3 | +Rollful is a hosted dice-rolling API built on the `opendice` npm package. This file is the |
| 4 | +single source of agent guidance. Setup, style and tests are in |
| 5 | +[CONTRIBUTING.md](CONTRIBUTING.md). |
| 6 | + |
| 7 | +## What this repository is, and is not |
| 8 | + |
| 9 | +Rollful hosts OpenDice. It does not reimplement it. Parsing, rolling, keep rules, |
| 10 | +explosions and the randomness all live in the package, and the API is a thin layer that |
| 11 | +adds what a public endpoint needs: validation, limits, CORS, rate limiting and an OpenAPI |
| 12 | +document. |
| 13 | + |
| 14 | +If you find yourself writing dice logic here, it belongs in `opendice` instead. |
| 15 | + |
| 16 | +Rollful is not tied to any game system. It knows what `4d6kh3` means and nothing about what |
| 17 | +anyone uses it for. Do not add rules, character sheets, system presets or named rolls. |
| 18 | + |
| 19 | +## The line that must not be crossed |
| 20 | + |
| 21 | +`RollContext.rand` lets a caller supply their own random source. It is not exposed through |
| 22 | +the API and must never be. A roll result records what the dice showed, never where the |
| 23 | +numbers came from, so a rigged source produces a result indistinguishable from a fair one. |
| 24 | +Exposing it would let anyone choose their own results while the response still looked |
| 25 | +honest. |
| 26 | + |
| 27 | +## Layout |
| 28 | + |
| 29 | +| Path | Contents | |
| 30 | +| ---------------------- | ------------------------------------------------ | |
| 31 | +| `apps/api` | the Worker: routes, guards, error mapping | |
| 32 | +| `apps/api/src/dice.ts` | the only place that calls OpenDice | |
| 33 | +| `packages/schema` | Zod schemas and the limits, shared with the site | |
| 34 | + |
| 35 | +`packages/schema` is consumed as TypeScript source, not built. Both the Worker bundler and |
| 36 | +Astro compile it, so it has no build step. |
| 37 | + |
| 38 | +## Where limits live |
| 39 | + |
| 40 | +Every limit the API adds is in `packages/schema/src/limits.ts`, and nowhere else. The site |
| 41 | +reads the same file to document them, so a number changed there changes the guard, the |
| 42 | +OpenAPI document and the docs together. Do not hardcode a limit in a route or a test. |
| 43 | + |
| 44 | +The limits OpenDice enforces itself are not repeated here. It already caps formula length, |
| 45 | +dice per roll, explosions per die, bonuses and the total's precision, and its parse errors |
| 46 | +are already safe to return: `excerpt()` replaces any character a formula cannot contain |
| 47 | +before quoting input back. |
| 48 | + |
| 49 | +## The CPU budget |
| 50 | + |
| 51 | +The Workers free plan allows 10ms of CPU per request. The worst single roll OpenDice will |
| 52 | +accept, `1000d2!`, costs about 1.7ms, so single rolls are safe by a wide margin. |
| 53 | + |
| 54 | +Batches are not, which is why `assertWithinDiceBudget` exists. Twenty rolls of `1000d2!` |
| 55 | +would cost about 34ms. Parsing costs roughly 1% of rolling, so the handler parses every |
| 56 | +formula first, sums the dice, and refuses the request before rolling anything. Any new |
| 57 | +endpoint that rolls more than once must go through the same function. |
| 58 | + |
| 59 | +## Node APIs |
| 60 | + |
| 61 | +OpenDice uses none, and `wrangler.jsonc` deliberately has no `compatibility_flags` and no |
| 62 | +`nodejs_compat`. Randomness is `crypto.getRandomValues`, which workerd provides. The test |
| 63 | +"the Worker runs OpenDice without Node APIs" exists to catch a regression at deploy rather |
| 64 | +than in production. Do not add `nodejs_compat` to make a dependency work — pick a |
| 65 | +dependency that does not need it. |
| 66 | + |
| 67 | +## Traps |
| 68 | + |
| 69 | +- `roll()` returns objects made with `Object.create(null)`. They serialise fine, but they |
| 70 | + have no `hasOwnProperty` and no prototype. `dice.ts` copies the fields it needs rather |
| 71 | + than passing the object straight through. |
| 72 | +- `parseFormula()` throws on a string bonus that contains no dice, such as `'3'`. Numeric |
| 73 | + bonuses must be sent as numbers. This matches what OpenDice does internally. |
| 74 | +- The rate limit binding is per Cloudflare location, and `period` accepts only 10 or 60. |
| 75 | + Do not document it as a global quota. |
| 76 | +- A trailing word in a formula is a parse error unless the request lists it in `tags`. That |
| 77 | + is deliberate: an unrecognised word is far more often a typo than a tag. |
| 78 | + |
| 79 | +## Style |
| 80 | + |
| 81 | +Prose is neutral and direct, readable at high school level. It documents the product; it |
| 82 | +does not narrate. |
| 83 | + |
| 84 | +Comments are kept to a minimum. Code should be self-explanatory, and a comment is only worth |
| 85 | +adding where the code cannot express the point. If the code could express it, change the |
| 86 | +code instead. |
| 87 | + |
| 88 | +## Before you finish |
| 89 | + |
| 90 | +```bash |
| 91 | +npm run typecheck && npm test && npm run format:check |
| 92 | +``` |
0 commit comments