Skip to content

icecap.infrastructure.repository

This module provides repository classes for accessing different types of game entities.

The repositories abstract the data access layer and provide a clean interface for retrieving and manipulating game entities.

Classes:

GameObjectRepository

GameObjectRepository(driver: GameDriver)

Repository for game object entities.

This class provides methods to access game object entities in the game.

Source code in icecap/infrastructure/repository/game_object_repository.py
def __init__(self, driver: GameDriver):
    self.driver = driver

get_game_object_from_entity

get_game_object_from_entity(
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> GameObject

Extends an Entity object to a GameObject instance.

This method takes an Entity object, extracts all the necessary information and creates a GameObject object from it.

You can bring your own name resolver and object manager.

Source code in icecap/infrastructure/repository/game_object_repository.py
def get_game_object_from_entity(
    self,
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> GameObject:
    """Extends an Entity object to a GameObject instance.

    This method takes an Entity object, extracts all the necessary information
    and creates a GameObject object from it.

    You can bring your own name resolver and object manager.
    """
    object_manager = object_manager or self.driver.object_manager
    name_resolver = name_resolver or self.driver.name_resolver

    position = object_manager.get_entity_position(entity)
    game_object_fields = object_manager.get_game_object_fields(entity)
    name = name_resolver.resolve_game_object_name_by_entry_id(game_object_fields.entry)

    game_object = GameObject(
        guid=entity.guid,
        object_address=entity.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=name,
        entity_type=EntityType.GAME_OBJECT,
        game_object_fields=GameObjectFields(
            entry_id=game_object_fields.entry,
            display_id=game_object_fields.display_id,
            owner_guid=game_object_fields.created_by,
            state=game_object_fields.bytes1_state,
        ),
    )
    return game_object

refresh_game_object

refresh_game_object(game_object: GameObject) -> GameObject

Refresh the game object data with the latest information from the game.

This method retrieves the latest data for a game object from the game and returns a new GameObject instance with the updated data. The original GameObject instance is not modified.

Source code in icecap/infrastructure/repository/game_object_repository.py
def refresh_game_object(self, game_object: GameObject) -> GameObject:
    """Refresh the game object data with the latest information from the game.

    This method retrieves the latest data for a game object from the game and
    returns a new GameObject instance with the updated data. The original GameObject
    instance is not modified.
    """
    object_manager = self.driver.object_manager

    position = object_manager.get_entity_position(game_object)
    game_object_fields = object_manager.get_game_object_fields(game_object)

    return GameObject(
        guid=game_object.guid,
        object_address=game_object.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=game_object.name,
        entity_type=EntityType.GAME_OBJECT,
        game_object_fields=GameObjectFields(
            entry_id=game_object_fields.entry,
            display_id=game_object_fields.display_id,
            owner_guid=game_object_fields.created_by,
            state=game_object_fields.bytes1_state,
        ),
    )

yield_game_objects

yield_game_objects() -> Generator[GameObject, None, None]

Yield all game object entities around the local player.

This method iterates through all objects around the player and yields only those that are of type GAME_OBJECT. Each entity is extended to a GameObject object before being yielded.

Source code in icecap/infrastructure/repository/game_object_repository.py
def yield_game_objects(self) -> Generator[GameObject, None, None]:
    """Yield all game object entities around the local player.

    This method iterates through all objects around the player and yields
    only those that are of type GAME_OBJECT. Each entity is extended to a
    GameObject object before being yielded.
    """
    object_manager = self.driver.object_manager
    name_resolver = self.driver.name_resolver

    for entity in object_manager.yield_objects():
        if entity.entity_type != EntityType.GAME_OBJECT:
            continue

        yield self.get_game_object_from_entity(entity, object_manager, name_resolver)

PlayerRepository

PlayerRepository(driver: GameDriver)

Repository for player entities.

This class provides methods to access player entities in the game.

Source code in icecap/infrastructure/repository/player_repository.py
def __init__(self, driver: GameDriver):
    self.driver = driver

get_local_player

get_local_player() -> Player

Retrieve the local player entity (the user's character).

It uses the object manager to get the local player's GUID and then searches for the corresponding entity.

Source code in icecap/infrastructure/repository/player_repository.py
def get_local_player(self) -> Player:
    """Retrieve the local player entity (the user's character).

    It uses the object manager to get the local player's
    GUID and then searches for the corresponding entity.
    """
    object_manager = self.driver.object_manager
    name_resolver = self.driver.name_resolver

    local_player_guid = object_manager.get_local_player_guid()

    for entity in object_manager.yield_objects():
        if entity.guid == local_player_guid:
            return self.get_player_from_entity(entity, object_manager, name_resolver)

    raise ValueError("Local player not found in the object manager.")

get_player_from_entity

get_player_from_entity(
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> Player

Extend an Entity object to a Player object.

This method takes an Entity object and extracts all the necessary information to create a Player object.

Source code in icecap/infrastructure/repository/player_repository.py
def get_player_from_entity(
    self,
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> Player:
    """Extend an Entity object to a Player object.

    This method takes an Entity object and extracts all the necessary information
    to create a Player object.
    """
    object_manager = object_manager or self.driver.object_manager
    name_resolver = name_resolver or self.driver.name_resolver

    position = object_manager.get_entity_position(entity)
    name = name_resolver.resolve_name(entity)

    unit_fields = object_manager.get_unit_fields(entity)
    race = Race(unit_fields.bytes_0_race)

    player = Player(
        guid=entity.guid,
        object_address=entity.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=name,
        entity_type=EntityType.PLAYER,
        unit_fields=UnitFields(
            level=unit_fields.level,
            hit_points=unit_fields.health,
            max_hit_points=unit_fields.max_health,
            faction=Faction.from_race(race),
            race=race,
            player_class=PlayerClass(unit_fields.bytes_0_class),
            gender=Gender(unit_fields.bytes_0_gender),
        ),
    )
    return player

refresh_player

refresh_player(player: Player) -> Player

Refresh the player data with the latest information from the game.

This method retrieves the latest data for a player from the game and returns a new Player instance with the updated data. The original Player instance is not modified.

Source code in icecap/infrastructure/repository/player_repository.py
def refresh_player(self, player: Player) -> Player:
    """Refresh the player data with the latest information from the game.

    This method retrieves the latest data for a player from the game and
    returns a new Player instance with the updated data. The original Player
    instance is not modified.
    """
    object_manager = self.driver.object_manager

    position = object_manager.get_entity_position(player)
    unit_fields = object_manager.get_unit_fields(player)

    return Player(
        guid=player.guid,
        object_address=player.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=player.name,
        entity_type=EntityType.PLAYER,
        unit_fields=UnitFields(
            level=unit_fields.level,
            hit_points=unit_fields.health,
            max_hit_points=unit_fields.max_health,
            faction=player.unit_fields.faction,
            race=player.unit_fields.race,
            player_class=player.unit_fields.player_class,
            gender=player.unit_fields.gender,
        ),
    )

yield_players

yield_players() -> Generator[Player, None, None]

Yield all player entities around the local player.

This method iterates through all objects around the local player and yields only those that are of type PLAYER. Each entity is extended to a Player object before being yielded.

Source code in icecap/infrastructure/repository/player_repository.py
def yield_players(self) -> Generator[Player, None, None]:
    """Yield all player entities around the local player.

    This method iterates through all objects around the local player and yields
    only those that are of type PLAYER. Each entity is extended to a Player object
    before being yielded.
    """
    object_manager = self.driver.object_manager
    name_resolver = self.driver.name_resolver

    for entity in object_manager.yield_objects():
        if entity.entity_type != EntityType.PLAYER:
            continue

        yield self.get_player_from_entity(entity, object_manager, name_resolver)

UnitRepository

UnitRepository(driver: GameDriver)

Repository for unit entities.

This class provides methods to access unit entities in the game.

Source code in icecap/infrastructure/repository/unit_repository.py
def __init__(self, driver: GameDriver):
    self.driver = driver

get_unit_from_entity

get_unit_from_entity(
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> Unit

Extend an Entity object to a Unit object.

This method takes an Entity object and extracts all the necessary information and creates a Unit object from it.

Source code in icecap/infrastructure/repository/unit_repository.py
def get_unit_from_entity(
    self,
    entity: Entity,
    object_manager: ObjectManager | None = None,
    name_resolver: NameResolver | None = None,
) -> Unit:
    """Extend an Entity object to a Unit object.

    This method takes an Entity object and extracts all the necessary information
    and creates a Unit object from it.
    """
    object_manager = object_manager or self.driver.object_manager
    name_resolver = name_resolver or self.driver.name_resolver

    position = object_manager.get_entity_position(entity)
    name = name_resolver.resolve_name(entity)

    unit_fields = object_manager.get_unit_fields(entity)
    race = Race(unit_fields.bytes_0_race)

    unit = Unit(
        guid=entity.guid,
        object_address=entity.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=name,
        entity_type=EntityType.UNIT,
        unit_fields=UnitFields(
            level=unit_fields.level,
            hit_points=unit_fields.health,
            max_hit_points=unit_fields.max_health,
            faction=Faction.from_race(race),
            race=race,
            player_class=PlayerClass(unit_fields.bytes_0_class),
            gender=Gender(unit_fields.bytes_0_gender),
        ),
    )
    return unit

refresh_unit

refresh_unit(unit: Unit) -> Unit

Refresh the unit data with the latest information from the game.

This method retrieves the latest data for a unit from the game and returns a new Unit instance with the updated data. The original Unit instance is not modified.

Source code in icecap/infrastructure/repository/unit_repository.py
def refresh_unit(self, unit: Unit) -> Unit:
    """Refresh the unit data with the latest information from the game.

    This method retrieves the latest data for a unit from the game and
    returns a new Unit instance with the updated data. The original Unit
    instance is not modified.
    """
    object_manager = self.driver.object_manager

    position = object_manager.get_entity_position(unit)
    unit_fields = object_manager.get_unit_fields(unit)

    return Unit(
        guid=unit.guid,
        object_address=unit.object_address,
        position=Position(x=position.x, y=position.y, z=position.z, rotation=position.rotation),
        name=unit.name,
        entity_type=EntityType.UNIT,
        unit_fields=UnitFields(
            level=unit_fields.level,
            hit_points=unit_fields.health,
            max_hit_points=unit_fields.max_health,
            faction=unit.unit_fields.faction,
            race=unit.unit_fields.race,
            player_class=unit.unit_fields.player_class,
            gender=unit.unit_fields.gender,
        ),
    )

yield_units

yield_units() -> Generator[Unit, None, None]

Yield all unit entities around the player.

This method iterates through all objects around the player and yields only those that are of type UNIT. Each entity is extended to a Unit object before being yielded.

Source code in icecap/infrastructure/repository/unit_repository.py
def yield_units(self) -> Generator[Unit, None, None]:
    """Yield all unit entities around the player.

    This method iterates through all objects around the player and yields
    only those that are of type UNIT. Each entity is extended to a Unit object
    before being yielded.
    """
    object_manager = self.driver.object_manager
    name_resolver = self.driver.name_resolver

    for entity in object_manager.yield_objects():
        if entity.entity_type != EntityType.UNIT:
            continue

        yield self.get_unit_from_entity(entity, object_manager, name_resolver)