fully implemented the AbstractTrack and DefaultTrack, removing the necessity of default_track in mod_config.json

This commit is contained in:
Faraphel 2022-08-11 18:55:57 +02:00
parent 99287bbd56
commit f13b18aa57
5 changed files with 27 additions and 18 deletions

View file

@ -9,7 +9,7 @@ from source.mkw import Tag
from source.mkw.Cup import Cup from source.mkw.Cup import Cup
from source.mkw.MKWColor import bmg_color_text, bmg_color_raw from source.mkw.MKWColor import bmg_color_text, bmg_color_raw
from source.mkw.ModSettings import AbstractModSettings from source.mkw.ModSettings import AbstractModSettings
from source.mkw.Track.CustomTrack import CustomTrack from source.mkw.Track import CustomTrack, DefaultTrack
import json import json
from source.mkw.OriginalTrack import OriginalTrack from source.mkw.OriginalTrack import OriginalTrack
@ -68,14 +68,14 @@ class ModConfig:
Representation of a mod Representation of a mod
""" """
__slots__ = ("name", "path", "nickname", "variant", "default_track", "_tracks", "version", __slots__ = ("name", "path", "nickname", "variant", "_tracks", "version",
"original_track_prefix", "swap_original_order", "keep_original_track", "original_track_prefix", "swap_original_order", "keep_original_track",
"enable_random_cup", "tags_cups", "track_file_template", "enable_random_cup", "tags_cups", "track_file_template",
"multiplayer_disable_if", "macros", "messages", "global_settings", "multiplayer_disable_if", "macros", "messages", "global_settings",
"specific_settings", "lpar_template", "tags_template") "specific_settings", "lpar_template", "tags_template")
def __init__(self, path: Path | str, name: str, nickname: str = None, version: str = None, variant: str = None, def __init__(self, path: Path | str, name: str, nickname: str = None, version: str = None, variant: str = None,
tags_cups: list[Tag] = None, default_track: "Track | TrackGroup" = None, tags_cups: list[Tag] = None,
tracks: list["Track | TrackGroup"] = None, original_track_prefix: bool = None, tracks: list["Track | TrackGroup"] = None, original_track_prefix: bool = None,
swap_original_order: bool = None, keep_original_track: bool = None, enable_random_cup: bool = None, swap_original_order: bool = None, keep_original_track: bool = None, enable_random_cup: bool = None,
track_file_template: str = None, multiplayer_disable_if: str = None, macros: dict[str, str] = None, track_file_template: str = None, multiplayer_disable_if: str = None, macros: dict[str, str] = None,
@ -269,7 +269,7 @@ class ModConfig:
# if there is still tracks in the buffer, create a cup with them and fill with default> # if there is still tracks in the buffer, create a cup with them and fill with default>
if len(track_buffer) > 0: if len(track_buffer) > 0:
track_buffer.extend([self.default_track] * (4 - len(track_buffer))) track_buffer.extend([DefaultTrack()] * (4 - len(track_buffer)))
yield Cup(tracks=track_buffer, cup_name=f"{current_tag_name}/{current_tag_count + 1}") yield Cup(tracks=track_buffer, cup_name=f"{current_tag_name}/{current_tag_count + 1}")
def get_unordered_cups(self) -> Generator["Cup", None, None]: def get_unordered_cups(self) -> Generator["Cup", None, None]:
@ -291,7 +291,7 @@ class ModConfig:
# if there is still tracks in the buffer, create a cup with them and fill with default # if there is still tracks in the buffer, create a cup with them and fill with default
if len(track_buffer) > 0: if len(track_buffer) > 0:
track_buffer.extend([self.default_track] * (4 - len(track_buffer))) track_buffer.extend([DefaultTrack()] * (4 - len(track_buffer)))
yield Cup(tracks=track_buffer) yield Cup(tracks=track_buffer)
def get_cups(self) -> Generator["Cup", None, None]: def get_cups(self) -> Generator["Cup", None, None]:

View file

@ -28,7 +28,7 @@ class AbstractTrack(ABC):
setattr(self, key, value) setattr(self, key, value)
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__} {id(self)}>" return f"<{self.__class__.__name__} {hex(id(self))}>"
def get_tracks(self) -> Generator["AbstractTrack", None, None]: def get_tracks(self) -> Generator["AbstractTrack", None, None]:
""" """
@ -46,10 +46,11 @@ class AbstractTrack(ABC):
:param default: default value if no tag template is found :param default: default value if no tag template is found
:return: formatted representation of the tag :return: formatted representation of the tag
""" """
for tag in filter(lambda tag: tag in mod_config.tags_template[template_name], self.tags): for tag in filter(lambda tag: tag in mod_config.tags_templates[template_name], self.tags):
return mod_config.multiple_safe_eval(mod_config.tags_template[template_name][tag], env={"tag": tag}) return mod_config.multiple_safe_eval(mod_config.tags_templates[template_name][tag], env={"tag": tag})
return default return default
@abstractmethod
def repr_format(self, mod_config: "ModConfig", template: str) -> str: def repr_format(self, mod_config: "ModConfig", template: str) -> str:
""" """
return the representation of the track from the format return the representation of the track from the format
@ -57,7 +58,7 @@ class AbstractTrack(ABC):
:param mod_config: configuration of the mod :param mod_config: configuration of the mod
:return: formatted representation of the track :return: formatted representation of the track
""" """
return mod_config.multiple_safe_eval(template, env={"track": self, "get_tag_template": self.get_tag_template}) ...
@abstractmethod @abstractmethod
def get_filename(self, mod_config: "ModConfig") -> str: def get_filename(self, mod_config: "ModConfig") -> str:

View file

@ -7,7 +7,6 @@ class CustomTrack(AbstractTrack):
""" """
Represent a custom track Represent a custom track
""" """
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__} name={getattr(self, 'name', '/')} tags={getattr(self, 'tags', '/')}>" return f"<{self.__class__.__name__} name={getattr(self, 'name', '/')} tags={getattr(self, 'tags', '/')}>"
@ -23,13 +22,17 @@ class CustomTrack(AbstractTrack):
return TrackGroup.from_dict(track_dict) return TrackGroup.from_dict(track_dict)
return cls(**track_dict) return cls(**track_dict)
def repr_format(self, mod_config: "ModConfig", template: str) -> str:
return mod_config.multiple_safe_eval(
template,
env={
"track": self,
"get_tag_template": lambda *args, **kwargs: self.get_tag_template(mod_config, *args, **kwargs)
}
)
def get_filename(self, mod_config: "ModConfig") -> str: def get_filename(self, mod_config: "ModConfig") -> str:
return self.repr_format(mod_config=mod_config, template=mod_config.track_file_template) return self.repr_format(mod_config=mod_config, template=mod_config.track_file_template)
def is_new(self, mod_config: "ModConfig") -> bool: def is_new(self, mod_config: "ModConfig") -> bool:
"""
Return if the track should be considered as new for random selection
:param mod_config: mod configuration
:return: is the track new
"""
return mod_config.safe_eval(mod_config.global_settings["replace_random_new"].value, env={"track": self}) is True return mod_config.safe_eval(mod_config.global_settings["replace_random_new"].value, env={"track": self}) is True

View file

@ -2,8 +2,11 @@ from source.mkw.Track.AbstractTrack import AbstractTrack
class DefaultTrack(AbstractTrack): class DefaultTrack(AbstractTrack):
def repr_format(self, mod_config: "ModConfig", template: str) -> str:
return " " # the name is always a blank space. Using nothing result in the filename being used instead
def get_filename(self, mod_config: "ModConfig") -> str: def get_filename(self, mod_config: "ModConfig") -> str:
return "beginner_course" return "beginner_course" # by default, use the T11 track, beginner_course
def is_new(self, mod_config: "ModConfig") -> bool: def is_new(self, mod_config: "ModConfig") -> bool:
return False return False # default track are never selected for random cup

View file

@ -1 +1,3 @@
from source.mkw.Track import CustomTrack, DefaultTrack, AbstractTrack from source.mkw.Track.CustomTrack import CustomTrack
from source.mkw.Track.DefaultTrack import DefaultTrack
from source.mkw.Track.AbstractTrack import AbstractTrack