Docs: datapackage typing (#1229)
* Docs: add ClassVar marker to World class * Docs: add typing to network_data_package
This commit is contained in:
		
							parent
							
								
									ff1f5569e7
								
							
						
					
					
						commit
						67be80e59d
					
				| 
						 | 
					@ -92,7 +92,7 @@ def generate(race=False):
 | 
				
			||||||
    return render_template("generate.html", race=race, version=__version__)
 | 
					    return render_template("generate.html", race=race, version=__version__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def gen_game(gen_options, meta: Optional[Dict[str, Any]] = None, owner=None, sid=None):
 | 
					def gen_game(gen_options: dict, meta: Optional[Dict[str, Any]] = None, owner=None, sid=None):
 | 
				
			||||||
    if not meta:
 | 
					    if not meta:
 | 
				
			||||||
        meta: Dict[str, Any] = {}
 | 
					        meta: Dict[str, Any] = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,7 @@ def get_db_data(known_games: typing.Set[str]) -> typing.Tuple[typing.Counter[str
 | 
				
			||||||
                                                              typing.DefaultDict[datetime.date, typing.Dict[str, int]]]:
 | 
					                                                              typing.DefaultDict[datetime.date, typing.Dict[str, int]]]:
 | 
				
			||||||
    games_played = defaultdict(Counter)
 | 
					    games_played = defaultdict(Counter)
 | 
				
			||||||
    total_games = Counter()
 | 
					    total_games = Counter()
 | 
				
			||||||
    cutoff = date.today()-timedelta(days=30)
 | 
					    cutoff = date.today() - timedelta(days=30)
 | 
				
			||||||
    room: Room
 | 
					    room: Room
 | 
				
			||||||
    for room in select(room for room in Room if room.creation_time >= cutoff):
 | 
					    for room in select(room for room in Room if room.creation_time >= cutoff):
 | 
				
			||||||
        for slot in room.seed.slots:
 | 
					        for slot in room.seed.slots:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,8 @@ from __future__ import annotations
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import pathlib
 | 
					import pathlib
 | 
				
			||||||
from typing import Dict, FrozenSet, Set, Tuple, List, Optional, TextIO, Any, Callable, Type, Union, TYPE_CHECKING
 | 
					from typing import Dict, FrozenSet, Set, Tuple, List, Optional, TextIO, Any, Callable, Type, Union, TYPE_CHECKING, \
 | 
				
			||||||
 | 
					    ClassVar
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from Options import AssembleOptions
 | 
					from Options import AssembleOptions
 | 
				
			||||||
from BaseClasses import CollectionState
 | 
					from BaseClasses import CollectionState
 | 
				
			||||||
| 
						 | 
					@ -130,24 +131,24 @@ class World(metaclass=AutoWorldRegister):
 | 
				
			||||||
    """A World object encompasses a game's Items, Locations, Rules and additional data or functionality required.
 | 
					    """A World object encompasses a game's Items, Locations, Rules and additional data or functionality required.
 | 
				
			||||||
    A Game should have its own subclass of World in which it defines the required data structures."""
 | 
					    A Game should have its own subclass of World in which it defines the required data structures."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    option_definitions: Dict[str, AssembleOptions] = {}  # link your Options mapping
 | 
					    option_definitions: ClassVar[Dict[str, AssembleOptions]] = {}  # link your Options mapping
 | 
				
			||||||
    game: str  # name the game
 | 
					    game: ClassVar[str]  # name the game
 | 
				
			||||||
    topology_present: bool = False  # indicate if world type has any meaningful layout/pathing
 | 
					    topology_present: ClassVar[bool] = False  # indicate if world type has any meaningful layout/pathing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # gets automatically populated with all item and item group names
 | 
					    # gets automatically populated with all item and item group names
 | 
				
			||||||
    all_item_and_group_names: FrozenSet[str] = frozenset()
 | 
					    all_item_and_group_names: ClassVar[FrozenSet[str]] = frozenset()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # map names to their IDs
 | 
					    # map names to their IDs
 | 
				
			||||||
    item_name_to_id: Dict[str, int] = {}
 | 
					    item_name_to_id: ClassVar[Dict[str, int]] = {}
 | 
				
			||||||
    location_name_to_id: Dict[str, int] = {}
 | 
					    location_name_to_id: ClassVar[Dict[str, int]] = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # maps item group names to sets of items. Example: "Weapons" -> {"Sword", "Bow"}
 | 
					    # maps item group names to sets of items. Example: "Weapons" -> {"Sword", "Bow"}
 | 
				
			||||||
    item_name_groups: Dict[str, Set[str]] = {}
 | 
					    item_name_groups: ClassVar[Dict[str, Set[str]]] = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # increment this every time something in your world's names/id mappings changes.
 | 
					    # increment this every time something in your world's names/id mappings changes.
 | 
				
			||||||
    # While this is set to 0 in *any* AutoWorld, the entire DataPackage is considered in testing mode and will be
 | 
					    # While this is set to 0 in *any* AutoWorld, the entire DataPackage is considered in testing mode and will be
 | 
				
			||||||
    # retrieved by clients on every connection.
 | 
					    # retrieved by clients on every connection.
 | 
				
			||||||
    data_version: int = 1
 | 
					    data_version: ClassVar[int] = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # override this if changes to a world break forward-compatibility of the client
 | 
					    # override this if changes to a world break forward-compatibility of the client
 | 
				
			||||||
    # The base version of (0, 1, 6) is provided for backwards compatibility and does *not* need to be updated in the
 | 
					    # The base version of (0, 1, 6) is provided for backwards compatibility and does *not* need to be updated in the
 | 
				
			||||||
| 
						 | 
					@ -157,7 +158,7 @@ class World(metaclass=AutoWorldRegister):
 | 
				
			||||||
    # update this if the resulting multidata breaks forward-compatibility of the server
 | 
					    # update this if the resulting multidata breaks forward-compatibility of the server
 | 
				
			||||||
    required_server_version: Tuple[int, int, int] = (0, 2, 4)
 | 
					    required_server_version: Tuple[int, int, int] = (0, 2, 4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    hint_blacklist: FrozenSet[str] = frozenset()  # any names that should not be hintable
 | 
					    hint_blacklist: ClassVar[FrozenSet[str]] = frozenset()  # any names that should not be hintable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # NOTE: remote_items and remote_start_inventory are now available in the network protocol for the client to set.
 | 
					    # NOTE: remote_items and remote_start_inventory are now available in the network protocol for the client to set.
 | 
				
			||||||
    # These values will be removed.
 | 
					    # These values will be removed.
 | 
				
			||||||
| 
						 | 
					@ -176,24 +177,24 @@ class World(metaclass=AutoWorldRegister):
 | 
				
			||||||
    forced_auto_forfeit: bool = False
 | 
					    forced_auto_forfeit: bool = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Hide World Type from various views. Does not remove functionality.
 | 
					    # Hide World Type from various views. Does not remove functionality.
 | 
				
			||||||
    hidden: bool = False
 | 
					    hidden: ClassVar[bool] = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # see WebWorld for options
 | 
					    # see WebWorld for options
 | 
				
			||||||
    web: WebWorld = WebWorld()
 | 
					    web: ClassVar[WebWorld] = WebWorld()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # autoset on creation:
 | 
					    # autoset on creation:
 | 
				
			||||||
    multiworld: "MultiWorld"
 | 
					    multiworld: "MultiWorld"
 | 
				
			||||||
    player: int
 | 
					    player: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # automatically generated
 | 
					    # automatically generated
 | 
				
			||||||
    item_id_to_name: Dict[int, str]
 | 
					    item_id_to_name: ClassVar[Dict[int, str]]
 | 
				
			||||||
    location_id_to_name: Dict[int, str]
 | 
					    location_id_to_name: ClassVar[Dict[int, str]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    item_names: Set[str]  # set of all potential item names
 | 
					    item_names: ClassVar[Set[str]]  # set of all potential item names
 | 
				
			||||||
    location_names: Set[str]  # set of all potential location names
 | 
					    location_names: ClassVar[Set[str]]  # set of all potential location names
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    zip_path: Optional[pathlib.Path] = None  # If loaded from a .apworld, this is the Path to it.
 | 
					    zip_path: ClassVar[Optional[pathlib.Path]] = None  # If loaded from a .apworld, this is the Path to it.
 | 
				
			||||||
    __file__: str  # path it was loaded from
 | 
					    __file__: ClassVar[str]  # path it was loaded from
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, world: "MultiWorld", player: int):
 | 
					    def __init__(self, world: "MultiWorld", player: int):
 | 
				
			||||||
        self.multiworld = world
 | 
					        self.multiworld = world
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,17 @@ if typing.TYPE_CHECKING:
 | 
				
			||||||
    from .AutoWorld import World
 | 
					    from .AutoWorld import World
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GamesPackage(typing.TypedDict):
 | 
				
			||||||
 | 
					    item_name_to_id: typing.Dict[str, int]
 | 
				
			||||||
 | 
					    location_name_to_id: typing.Dict[str, int]
 | 
				
			||||||
 | 
					    version: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class DataPackage(typing.TypedDict):
 | 
				
			||||||
 | 
					    version: int
 | 
				
			||||||
 | 
					    games: typing.Dict[str, GamesPackage]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class WorldSource(typing.NamedTuple):
 | 
					class WorldSource(typing.NamedTuple):
 | 
				
			||||||
    path: str  # typically relative path from this module
 | 
					    path: str  # typically relative path from this module
 | 
				
			||||||
    is_zip: bool = False
 | 
					    is_zip: bool = False
 | 
				
			||||||
| 
						 | 
					@ -54,7 +65,7 @@ for world_source in world_sources:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lookup_any_item_id_to_name = {}
 | 
					lookup_any_item_id_to_name = {}
 | 
				
			||||||
lookup_any_location_id_to_name = {}
 | 
					lookup_any_location_id_to_name = {}
 | 
				
			||||||
games = {}
 | 
					games: typing.Dict[str, GamesPackage] = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .AutoWorld import AutoWorldRegister
 | 
					from .AutoWorld import AutoWorldRegister
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +80,7 @@ for world_name, world in AutoWorldRegister.world_types.items():
 | 
				
			||||||
    lookup_any_item_id_to_name.update(world.item_id_to_name)
 | 
					    lookup_any_item_id_to_name.update(world.item_id_to_name)
 | 
				
			||||||
    lookup_any_location_id_to_name.update(world.location_id_to_name)
 | 
					    lookup_any_location_id_to_name.update(world.location_id_to_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
network_data_package = {
 | 
					network_data_package: DataPackage = {
 | 
				
			||||||
    "version": sum(world.data_version for world in AutoWorldRegister.world_types.values()),
 | 
					    "version": sum(world.data_version for world in AutoWorldRegister.world_types.values()),
 | 
				
			||||||
    "games": games,
 | 
					    "games": games,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue