Skip to main content

karni-game

Games for Karni: guest players, cloud saves, leaderboards, a soft-currency wallet, daily content and achievements, with a realtime server contract.

PyPI License: Apache-2.0 Docs

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 in content/game.yaml and a page 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 intent never 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.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-game 0.6.0
File Size Uploaded Provenance Actions
karni_game-0.6.0.tar.gz 199.3 kB Without provenance Details for karni_game-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-game 0.6.0
File Interpreter ABI Platform Actions
karni_game-0.6.0-py3-none-any.whl Python 3 none any Details for karni_game-0.6.0-py3-none-any.whl

Total release size: 307.4 kB

Release files / karni_game-0.6.0.tar.gz

Download URL karni_game-0.6.0.tar.gz
Size 199.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc1e83eff0212484334df63bb31490c29ae317d4db653fe7160e06484d7516b2
BLAKE2b-256 checksum
How to use checksums
d9a98f8491a5966b1af7412f8384e07fc311c364427208b815de8a6ea506c9b4
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.6.0-py3-none-any.whl

Download URL karni_game-0.6.0-py3-none-any.whl
Size 108.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3300901baa2781adadc1e4e9714671132c8ac03ea8d847d20f59ab5603d3eea9
BLAKE2b-256 checksum
How to use checksums
04eb4509d3929aec531778757b586c0e0566c1c3adab6f4cad5ce80f64db4f33
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.6.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.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page