From 7f76db7af73759179d5746c7bd53b28ab0a65bc8 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 25 Apr 2020 15:37:41 -0700 Subject: [PATCH] Add two getter methods to lex.gameloop --- amanuensis/lexicon/__init__.py | 7 ++++++- amanuensis/lexicon/gameloop.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/amanuensis/lexicon/__init__.py b/amanuensis/lexicon/__init__.py index 415eec7..4757828 100644 --- a/amanuensis/lexicon/__init__.py +++ b/amanuensis/lexicon/__init__.py @@ -2,7 +2,10 @@ from .admin import ( valid_name, create_lexicon, load_all_lexicons) -from .gameloop import attempt_publish +from .gameloop import ( + get_player_characters, + get_player_drafts, + attempt_publish) from .setup import ( player_can_join_lexicon, add_player_to_lexicon, @@ -12,6 +15,8 @@ __all__ = [member.__name__ for member in [ valid_name, create_lexicon, load_all_lexicons, + get_player_characters, + get_player_drafts, attempt_publish, player_can_join_lexicon, add_player_to_lexicon, diff --git a/amanuensis/lexicon/gameloop.py b/amanuensis/lexicon/gameloop.py index 5dd6644..ea854dc 100644 --- a/amanuensis/lexicon/gameloop.py +++ b/amanuensis/lexicon/gameloop.py @@ -2,8 +2,9 @@ Submodule of functions for managing lexicon games during the core game loop of writing and publishing articles. """ -from typing import Iterable, Any +from typing import Iterable, Any, List +from amanuensis.config import ReadOnlyOrderedDict from amanuensis.models import LexiconModel from amanuensis.parser import ( parse_raw_markdown, @@ -13,6 +14,37 @@ from amanuensis.parser import ( filesafe_title) +def get_player_characters( + lexicon: LexiconModel, + uid: str) -> Iterable[ReadOnlyOrderedDict]: + """ + Returns each character in the lexicon owned by the given player + """ + for character in lexicon.cfg.character.values(): + if character.player == uid: + yield character + + +def get_player_drafts( + lexicon: LexiconModel, + uid: str) -> Iterable[ReadOnlyOrderedDict]: + """ + Returns each draft in the lexicon by a character owned by the + given player. + """ + characters = list(get_player_characters(lexicon, uid)) + drafts: List[Any] = [] + for filename in lexicon.ctx.draft.ls(): + for character in characters: + if filename.startswith(character.cid): + drafts.append(filename) + break + for i in range(len(drafts)): + with lexicon.ctx.draft.read(drafts[i]) as draft: + drafts[i] = draft + return drafts + + def attempt_publish(lexicon: LexiconModel) -> None: """ If the lexicon's publsh policy allows the current set of approved