game_characters
Per-user character persistence for S.N.E.S. game sessions.
Characters are stored per-user (not per-game) and automatically loaded into every game session. The last created character is the default. Character images are persisted to the filesystem at /home/star/large_files/assets/ for permanence.
# 🎭💀 POSSESSED AVATAR REGISTRY
- async game_characters.create_character(user_id, name, description, redis, image_data=None, image_url=None)[source]
Create a new character for a user.
Images are saved to the web-accessible game assets directory so img2img can reference them via public URL.
- async game_characters.list_characters(user_id, redis)[source]
List a user’s saved characters, most-recently-used first.
Reads the per-user Redis hash
game:characters:<user_id>(field =char_id, value = JSON), decodes every entry (handling bothstrandbytesfrom the client), stamps each dict with itschar_id, and returns them sorted by thelast_usedtimestamp descending so the active/default character sorts first. Read-only; any decode or connection error is logged and reported as an empty list.Called by
get_active_character()as a fallback (most-recent character) and bygame_ui/components.pyto render the character roster.
- async game_characters.get_character(user_id, char_id, redis)[source]
Fetch one of a user’s characters by its identifier.
Reads a single field from the per-user Redis hash
game:characters:<user_id>keyed bychar_idand JSON-decodes it (handling bothstrandbytesvalues). ReturnsNoneif the field is missing. Read-only; any decode/connection error is logged and surfaced asNone.Called by
get_active_character()to resolve the stored active id; no external callers were found via grep.
- async game_characters.get_active_character(user_id, redis)[source]
Resolve a user’s active character, falling back to most-recent.
Reads the active-pointer string
game:active_char:<user_id>from Redis; if it is set, resolves the character viaget_character(). If no active pointer exists, it falls back to the most-recently-used character returned bylist_characters()so a user who never explicitly chose one still gets a sensible default. Read-only; any error is logged and surfaced asNone.This is the most widely used accessor in the module: it is invoked across the message pipeline and platforms – e.g.
message_processor/context_injections.py,message_processor/processor.py,message_processor/generate_and_send.py,platforms/discord.py,background_agents/game_art_agent.py,tools/game_controls.py, andopenrouter_client/executor.py– to inject the player’s character into prompts and image generation.
- async game_characters.set_active_character(user_id, char_id, redis)[source]
Mark a character active for a user and refresh its last-used time.
Writes the
char_idinto the active-pointer stringgame:active_char:<user_id>, then reads that character back from the per-user hashgame:characters:<user_id>, updates itslast_usedtimestamp, and persists the modified JSON. Keepinglast_usedcurrent also influences the most-recent fallback inget_active_character()and the ordering inlist_characters(). Errors are logged and swallowed (best-effort write).Called by
create_character()(to auto-activate a freshly created character) and by the UI/platform layersgame_ui/modals/lobby.pyandplatforms/discord.pywhen a user selects a character.
- async game_characters.delete_character(user_id, char_id, redis)[source]
Delete a user’s character, its image file, and any active pointer.
Reads the character from the per-user hash
game:characters:<user_id>to find its persistedimage_pathand best-effort removes that file from disk, then deletes the character field from the hash withHDEL. If the deleted character was the one recorded in the active-pointer stringgame:active_char:<user_id>, that pointer is cleared so no dangling active id remains. Touches both Redis and the filesystem; errors are logged and treated as a failed delete.No external callers were found via grep; this is the public teardown counterpart to
create_character().
- async game_characters.get_character_image_data(char)[source]
Read a character’s persisted image bytes from disk, if present.
Pulls the
image_pathrecorded on the character dict (written bycreate_character()when an image was supplied) and, if that path exists, reads and returns the raw file bytes – e.g. for re-use as an img2img reference. ReturnsNonewhen the character has no stored image or the file is missing, and logs and swallows any read error. Touches the filesystem only (no Redis).No callers were found via grep; intended for consumers needing the original image bytes rather than the public URL stored on the character.
- Parameters:
char (
dict[str,Any]) – A character dict, typically fromget_active_character()orget_character(), expected to carry animage_pathkey.- Returns:
The image file contents, or
Noneif absent/unreadable.- Return type: