Skip to content

Commit

Permalink
Refactor character classes to use BaseCharacter and integrate KingsDa…
Browse files Browse the repository at this point in the history
…ta for minigame functionality
  • Loading branch information
KiloOscarSix committed Nov 26, 2024
1 parent 556454f commit a3a01f1
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 47 deletions.
48 changes: 24 additions & 24 deletions CharacterService_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from renpy import store
import renpy.exports as renpy

from game.characters.character_ren import Character
from game.characters.base_character_ren import BaseCharacter
from game.characters.NonPlayableCharacter_ren import NonPlayableCharacter
from game.characters.main_character_ren import MainCharacter
from game.characters.Relationship_ren import Relationship
Expand All @@ -19,7 +19,7 @@

class CharacterService:
@staticmethod
def get_user_by_str(name: str) -> Character:
def get_user_by_str(name: str) -> BaseCharacter:
name = name.lower()
try:
return getattr(store, name.replace(" ", "_"))
Expand All @@ -32,7 +32,7 @@ def get_user_by_str(name: str) -> Character:
raise AttributeError(f"{name} is not a valid character.")

@staticmethod
def get_user(character: Union[Character, str]) -> Character:
def get_user(character: Union[BaseCharacter, str]) -> BaseCharacter:
if isinstance(character, str):
return CharacterService.get_user_by_str(character)

Expand All @@ -46,7 +46,7 @@ def get_user(character: Union[Character, str]) -> Character:

@staticmethod
def get_relationship(
character: Character, target: Optional[Character] = None
character: BaseCharacter, target: Optional[BaseCharacter] = None
) -> "Relationship":
if target is None:
target = mc
Expand Down Expand Up @@ -101,9 +101,9 @@ def compat_get_max_rel(

@staticmethod
def has_relationship(
character: Character,
character: BaseCharacter,
relationship: "Relationship",
target: Optional[Character] = None,
target: Optional[BaseCharacter] = None,
) -> bool:
if target is None:
target = mc
Expand All @@ -112,9 +112,9 @@ def has_relationship(

@staticmethod
def set_relationship(
character: Character,
character: BaseCharacter,
relationship: "Relationship",
target: Optional[Character] = None,
target: Optional[BaseCharacter] = None,
) -> None:
if target is None:
target = mc
Expand Down Expand Up @@ -186,7 +186,7 @@ def get_profile_pictures(character_name: str) -> tuple[str, ...]:

@staticmethod
def is_exclusive_girlfriend(
character: Character, target: Optional[Character] = None
character: BaseCharacter, target: Optional[BaseCharacter] = None
) -> bool:
if target is None:
target = mc
Expand All @@ -198,7 +198,7 @@ def is_exclusive_girlfriend(
)

@staticmethod
def is_girlfriend(character: Character, target: Optional[Character] = None) -> bool:
def is_girlfriend(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

Expand All @@ -208,8 +208,8 @@ def is_girlfriend(character: Character, target: Optional[Character] = None) -> b

@staticmethod
def is_girlfriends(
characters: Iterable[Character],
target: Optional[Character] = None,
characters: Iterable[BaseCharacter],
target: Optional[BaseCharacter] = None,
) -> bool:
if target is None:
target = mc
Expand All @@ -220,7 +220,7 @@ def is_girlfriends(
)

@staticmethod
def is_exclusive(character: Character, target: Optional[Character] = None) -> bool:
def is_exclusive(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

Expand All @@ -231,16 +231,16 @@ def is_exclusive(character: Character, target: Optional[Character] = None) -> bo
)

@staticmethod
def is_fwb(character: Character, target: Optional[Character] = None) -> bool:
def is_fwb(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

return CharacterService.has_relationship(character, Relationship.FWB, target)

@staticmethod
def is_fwbs(
characters: Iterable[Character],
target: Optional[Character] = None,
characters: Iterable[BaseCharacter],
target: Optional[BaseCharacter] = None,
) -> bool:
if target is None:
target = mc
Expand All @@ -250,30 +250,30 @@ def is_fwbs(
)

@staticmethod
def is_dating(character: Character, target: Optional[Character] = None) -> bool:
def is_dating(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

return CharacterService.has_relationship(character, Relationship.DATING, target)

@staticmethod
def is_kissed(character: Character, target: Optional[Character] = None) -> bool:
def is_kissed(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

return CharacterService.has_relationship(character, Relationship.KISSED, target)

@staticmethod
def is_friend(character: Character, target: Optional[Character] = None) -> bool:
def is_friend(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

return CharacterService.has_relationship(character, Relationship.FRIEND, target)

@staticmethod
def is_friends(
characters: Iterable[Character],
target: Optional[Character] = None,
characters: Iterable[BaseCharacter],
target: Optional[BaseCharacter] = None,
) -> bool:
if target is None:
target = mc
Expand All @@ -283,16 +283,16 @@ def is_friends(
)

@staticmethod
def is_ex(character: Character, target: Optional[Character] = None) -> bool:
def is_ex(character: BaseCharacter, target: Optional[BaseCharacter] = None) -> bool:
if target is None:
target = mc

return CharacterService.has_relationship(character, Relationship.EX, target)

@staticmethod
def is_exes(
characters: Iterable[Character],
target: Optional[Character] = None,
characters: Iterable[BaseCharacter],
target: Optional[BaseCharacter] = None,
):
if target is None:
target = mc
Expand Down
10 changes: 5 additions & 5 deletions NonPlayableCharacter_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from game.characters.Frat_ren import Frat
from game.characters.Moods_ren import Moods
from game.characters.Relationship_ren import Relationship
from game.characters.character_ren import Character
from game.characters.base_character_ren import BaseCharacter
from game.characters.character_traits_ren import CharacterTrait
from game.characters.CharacterService_ren import CharacterService
from game.phone.Message_ren import Message
Expand All @@ -21,14 +21,14 @@


@runtime_checkable
class NonPlayableCharacter(Character, Protocol):
class NonPlayableCharacter(BaseCharacter, Protocol):
_profile_pictures: tuple[str, ...]
_profile_picture: str
_profile_picture_65x65: "Displayable"

points: int

relationships: dict["Character", "Relationship"]
relationships: dict["BaseCharacter", "Relationship"]
mood: Moods

pending_text_messages: list["Message"]
Expand Down Expand Up @@ -101,8 +101,8 @@ def is_talkative(self) -> bool:
return CharacterTrait.TALKATIVE in self.traits

@property
def preferred_reputation(self) -> "Reputations":
...
def preferred_reputation(self) -> Optional["Reputations"]:
return None

@property
def frat_requirement(self) -> Optional[Frat]:
Expand Down
4 changes: 2 additions & 2 deletions PlayableCharacters_ren.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Protocol, runtime_checkable

from game.characters.character_ren import Character
from game.characters.base_character_ren import BaseCharacter
from game.characters.NonPlayableCharacter_ren import NonPlayableCharacter
from game.items.Item_ren import Item
from game.characters.Frat_ren import Frat
Expand All @@ -18,7 +18,7 @@


@runtime_checkable
class PlayableCharacter(Character, Protocol):
class PlayableCharacter(BaseCharacter, Protocol):
_username: str
_profile_picture: str
money: int
Expand Down
45 changes: 32 additions & 13 deletions character_ren.py → base_character_ren.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from abc import abstractmethod
from typing import Optional, Protocol, runtime_checkable
from typing import Any, Optional, Protocol, runtime_checkable

from game.characters.npcs.chloe_ren import Chloe
from game.characters.Moods_ren import Moods
from game.characters.Relationship_ren import Relationship
from game.minigames.kings.kings_data_ren import KingsData

chloe: Chloe
name: str
Expand All @@ -14,12 +15,17 @@


@runtime_checkable
class Character(Protocol):
class BaseCharacter(Protocol):
_profile_pictures: tuple[str, ...]

relationships: dict["Character", "Relationship"]
relationships: dict["BaseCharacter", "Relationship"]
mood: Moods

_kings_data: Optional["KingsData"] = None

def __call__(self, *args: Any, **kwds: Any) -> str:
return str(self)

def __hash__(self) -> int:
return hash(self.name)

Expand All @@ -37,7 +43,7 @@ def __str__(self) -> str:
return self.__class__.__name__

def __eq__(self, __value: object) -> bool:
if not isinstance(__value, Character):
if not isinstance(__value, BaseCharacter):
return NotImplemented

return self.name == __value.name
Expand Down Expand Up @@ -98,45 +104,58 @@ def is_threatened(self) -> bool:

# endregion Moods
# region Relationships
def get_relationship(self, target: "Character") -> "Relationship":
def get_relationship(self, target: "BaseCharacter") -> "Relationship":
return self.relationships.setdefault(target, Relationship.FRIEND)

def has_relationship(
self, relationship: "Relationship", target: "Character"
self, relationship: "Relationship", target: "BaseCharacter"
) -> bool:
return self.get_relationship(target) == relationship

def is_exclusive_girlfriend(self, target: "Character") -> bool:
def is_exclusive_girlfriend(self, target: "BaseCharacter") -> bool:
return any(
character.is_girlfriend(target)
for character in target.relationships
if character != self
)

def is_girlfriend(self, target: "Character") -> bool:
def is_girlfriend(self, target: "BaseCharacter") -> bool:
return self.has_relationship(Relationship.GIRLFRIEND, target)

def is_exclusive(self, target: "Character") -> bool:
def is_exclusive(self, target: "BaseCharacter") -> bool:
return any(
character.is_girlfriend(target) or character.is_fwb(target)
for character in target.relationships
if character != self
)

def is_fwb(self, target: "Character") -> bool:
def is_fwb(self, target: "BaseCharacter") -> bool:
return self.has_relationship(Relationship.FWB, target)

def is_friend(self, target: "Character") -> bool:
def is_friend(self, target: "BaseCharacter") -> bool:
return self.has_relationship(Relationship.FRIEND, target)

def is_ex(self, target: "Character") -> bool:
def is_ex(self, target: "BaseCharacter") -> bool:
return self.has_relationship(Relationship.EX, target)

def any_fwb(self, *characters: "Character") -> bool:
def any_fwb(self, *characters: "BaseCharacter") -> bool:
return any(self.is_fwb(character) for character in characters)

# endregion Relationships

# region Kings Minigame
@property
def kings(self) -> "KingsData":
try:
self._kings_data
except AttributeError:
self._kings_data = KingsData()

if self._kings_data is None:
self._kings_data = KingsData()

return self._kings_data
# endregion Kings Minigame

# _profile_pictures: list[str] = field(default_factory=list)
# _profile_picture: str = ""
Expand Down
12 changes: 11 additions & 1 deletion main_character_ren.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from PlayableCharacters_ren import PlayableCharacter
from Frat_ren import Frat
from game.minigames.kings.kings_data_ren import KingsData

name: str
name = "Alex"

"""renpy
init python:
Expand All @@ -19,5 +20,14 @@ def __init__(self) -> None:
self.relationships = {}
self.inventory = []

self._kings_data = KingsData()

def __repr__(self) -> str:
return super().__repr__()

@property
def kings(self) -> "KingsData":
if self._kings_data is None:
self._kings_data = KingsData()

return self._kings_data
Loading

0 comments on commit a3a01f1

Please sign in to comment.