karni-game
Games for Karni: guest players, cloud saves, leaderboards, a soft-currency wallet, daily content and achievements, with a realtime server contract.
The server side of a game, for any game a project hosts. The server decides every reward, every price and every balance: a client only reports and asks. It is experimental: its names may change in a minor release, with a CHANGELOG entry.
The guide is docs/game.md, and the HTTP contract a client, a realtime server and a portal code against is docs/game-api.md.
Install
pip install karni-game # karni-game[commerce] for real-money items
karni add game
A player is a person of the people contract, so set it above the settings call, and mount the API once:
# config/settings.py
KARNI_PEOPLE = True
# config/urls.py, before path("api/", include("karni.api.urls"))
path("api/game/", include("karni_game.urls")),
Then make migrate. karni new "Block Hopper" --kind game writes all of it, with a games app, the game declared in content/game.yaml, and the game itself in game/block-hopper/, served at /play/block-hopper/. --kind app+game puts the game on a page of an app instead, at /game.
What it adds
| Part | What |
|---|---|
| Players | Player on people.Person: a guest with a token the server issues, an account once its email is verified, an upgrade that keeps the guest's progress (people.merge with the module's merge hook). |
| Saves | Save: versioned JSON per player, game and slot, a server version counter (409 with the server's copy on a stale write), a size limit, save migrations per game. |
| Boards | Board, Score, Best: periods on the game's clock, plausible ranges, runs (seed and input log) kept for review, flagged runs, run checks. |
| Wallet | Currency, Wallet, LedgerEntry: atomic and capped, a ledger entry with a reason for every move. |
| Rewards and shop | Reward, Claim, Item, Ownership: rewards declared on the server (daily, ad with a signed portal callback, mission, achievement, match, staff), cosmetics owned and equipped, a shop in soft currency, real-money items as the karni-commerce kind game_item. |
| Daily | Streak: the server's date, a daily seed per game, streaks with a grace rule, a daily board. |
| Achievements | Achievement, Mission, Progress, Unlock, Completion: counters the game reports, unlocked and granted by the server. |
| Config | ConfigValue: remote config and feature flags with a rollout, in the one boot call. Analytics checked against the game's schema and published as game.analytics.tracked. |
| Multiplayer | Match: a short-lived HS256 player token for a realtime server of your own, and its signed results report. |
The admin ships explicit classes (karni_game/admin.py) for players, scores, wallets and the ledger, where staff act. The other models use the derived admin. Two roles come with it (game-manager, game-moderator, the karni.roles entry point).
Settings
| Setting | Env var | Default | Meaning |
|---|---|---|---|
KARNI_GAME_RATES |
the defaults of docs/game-api.md | rates of the limited writes, by scope (None turns one off) |
|
KARNI_GAME_TOKEN_HEADER |
KARNI_GAME_TOKEN_HEADER |
X-Player-Token |
the guest token's header |
KARNI_GAME_CORS_ORIGINS |
KARNI_GAME_CORS_ORIGINS |
[] |
origins a portal build calls /api/game/ from (tokens only) |
KARNI_GAME_SERVER_SECRET |
KARNI_GAME_SERVER_SECRET |
blank | the realtime server's shared secret (at least 32 bytes). Blank: no multiplayer |
KARNI_GAME_TOKEN_ISSUER |
KARNI_GAME_TOKEN_ISSUER |
karni-game |
the player token's iss |
KARNI_GAME_TOKEN_SECONDS |
KARNI_GAME_TOKEN_SECONDS |
300 |
the player token's life |
KARNI_GAME_PORTALS |
{} |
portals that call back after an ad: {name: {"secret", "verifier"}} |
|
KARNI_GAME_SIGNATURE_TOLERANCE |
KARNI_GAME_SIGNATURE_TOLERANCE |
300 |
seconds a signed request's timestamp may be off |
KARNI_GAME_AD_INTENT_SECONDS |
KARNI_GAME_AD_INTENT_SECONDS |
3600 |
an ad reward intent's life |
KARNI_GAME_CLIENT_CLAIMS_PER_DAY |
KARNI_GAME_CLIENT_CLAIMS_PER_DAY |
5 |
the cap of a client-claimed ad reward |
KARNI_GAME_INTENT_DAYS |
KARNI_GAME_INTENT_DAYS |
7 |
days a retry key is kept |
KARNI_GAME_MAX_SAVE_BYTES |
KARNI_GAME_MAX_SAVE_BYTES |
1048576 |
the largest save any game allows |
KARNI_GAME_MAX_RUN_BYTES |
KARNI_GAME_MAX_RUN_BYTES |
262144 |
the largest run record |
KARNI_GAME_MERGER |
KARNI_GAME_MERGER |
karni_game.merge.Merger |
the merge rules of two players |
KARNI_GAME_DAILY_SEED |
KARNI_GAME_DAILY_SEED |
karni_game.daily.daily_seed |
seed(game, day) -> int |
KARNI_GAME_AUTODISCOVER |
KARNI_GAME_AUTODISCOVER |
True |
import each app's game.py |
KARNI_GAME_ROLES |
KARNI_GAME_ROLES |
True |
declare the two roles |
Security
- A player is found from the request only (the account, else the guest token). No endpoint takes a player's id from a client.
- A guest token is stored as its SHA-256 and revoked on upgrade.
- Balances change only through
karni_game.wallet.change(row lock, conditional update, never below 0, capped). - Portal callbacks and realtime reports are HMAC-SHA256 signed over a timestamp and the raw body, and refused past
KARNI_GAME_SIGNATURE_TOLERANCE. - Writes are rate limited per player, and a retry with the same
intentnever counts twice.
Escape hatches
KARNI_GAME_MERGER, KARNI_GAME_DAILY_SEED, a run check and a save migration per game in <app>/game.py, a verifier per portal, the views of karni_game.api (subclass and mount your own), and the service modules the views call. The guide lists each.
Tests
cd modules/karni-game && python -m pytest
# the wallet under two requests at once, on PostgreSQL:
PYTHONPATH=../../.github/ci DATABASE_URL=postgres://... python -m pytest --ds=karni_postgres_settings tests/test_concurrency.py
Metadata
Release files for karni-game 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| karni_game-0.7.0.tar.gz | 337.8 kB | Without provenance | Details for karni_game-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| karni_game-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for karni_game-0.7.0-py3-none-any.whl |
Total release size: 490.1 kB
Release files / karni_game-0.7.0.tar.gz
| Download URL | karni_game-0.7.0.tar.gz |
|---|---|
| Size | 337.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bfd05b00d0bfd815e7ab340ad98901e99b3f60b3018928e9a58b9d24b71395ff
|
|
BLAKE2b-256 checksum How to use checksums |
095c41cee5ae4af22feffece40d924ef4d560a9194a77014f4b508fc8975ba24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / karni_game-0.7.0-py3-none-any.whl
| Download URL | karni_game-0.7.0-py3-none-any.whl |
|---|---|
| Size | 152.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60aefe966fc391b4cb7000b4e8d1765387a3b019f0c90992d87fe6dca061a49e
|
|
BLAKE2b-256 checksum How to use checksums |
e2ca4066940766f6bc6850f0523b0e6a12da8a5d02f9e06623e327f82bc6e085
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for karni-game 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.