<commit_before>"""Provides the SkillType and Skill classes, along with the SkillTypes
enumeration."""

from enum import Enum as _Enum
from random import choice

from sqlalchemy import Column, Enum, ForeignKey, Integer
from sqlalchemy.orm import relationship, backref

from .base import Base, ResourcesMixin
from .units import UnitType

from ..events import listen, on_exploit, on_kill


class SkillTypes(_Enum):
    """The possible skill types."""

    double_exploit = 'Improved materials gathering'
    tripple_exploit = 'Expert materials gathering'
    random_resurrect = 'Basic resurrection'
    specific_resurrect = 'Advanced resurrection'
    switch_sides = 'Turncoat spell'


class SkillType(Base, ResourcesMixin):
    """A skill which can be attached to a BuildingType instance for purchasing.
    Resources are used for purchasing."""

    __tablename__ = 'skill_types'
    skill_type = Column(Enum(SkillTypes), nullable=False)
    pop_time = Column(Integer, nullable=False, default=4)
    building_type_id = Column(
        Integer, ForeignKey('building_types.id'), nullable=False
    )
    building_type = relationship(
        'BuildingType', backref='skill_types', single_parent=True,
        cascade='all, delete-orphan'
    )


class Skill(Base):
    """Used to attach SkillTypes members to Building instances."""

    __tablename__ = 'skills'
    skill_type = Column(Enum(SkillTypes), nullable=False)
    building_id = Column(Integer, ForeignKey('buildings.id'), nullable=False)
    building = relationship(
        'Building', backref=backref('skills', cascade='all, delete-orphan'),
        single_parent=True
    )


@listen(on_exploit)
def check_exploit_skills(action):
    """Check to see if action.unit.home has either of te exploit skills
    assigned."""
    h = action.unit.home
    if h is None:
        return  # Nothing to do.
    if h.has_skill(SkillTypes.double_exploit):
        multiplier = 2
    elif h.has_skill(SkillTypes.tripple_exploit):
        multiplier = 3
    else:
        multiplier = 1
    action.amount *= multiplier


@listen(on_kill)
def check_resurrect_skills(unit, target):
    """Check to see if we should be resurrecting target."""
    h = target.home
    if h is None:
        return  # Nothing to do.
    if h.has_skill(SkillTypes.random_resurrect):
        ut = choice(UnitType.all())
    else:
        ut = target.type
    u = target.location.add_unit(ut, *target.coordinates)
    u.health = 0
    u.owner = target.owner
    u.save()
    if u.owner is not None:
        u.owner.message(f'{u.get_name()} is ready.')<commit_msg>If there's no resurrect skill, simply return.<commit_after>"""Provides the SkillType and Skill classes, along with the SkillTypes
enumeration."""

from enum import Enum as _Enum
from random import choice

from sqlalchemy import Column, Enum, ForeignKey, Integer
from sqlalchemy.orm import relationship, backref

from .base import Base, ResourcesMixin
from .units import UnitType

from ..events import listen, on_exploit, on_kill


class SkillTypes(_Enum):
    """The possible skill types."""

    double_exploit = 'Improved materials gathering'
    tripple_exploit = 'Expert materials gathering'
    random_resurrect = 'Basic resurrection'
    specific_resurrect = 'Advanced resurrection'
    switch_sides = 'Turncoat spell'


class SkillType(Base, ResourcesMixin):
    """A skill which can be attached to a BuildingType instance for purchasing.
    Resources are used for purchasing."""

    __tablename__ = 'skill_types'
    skill_type = Column(Enum(SkillTypes), nullable=False)
    pop_time = Column(Integer, nullable=False, default=4)
    building_type_id = Column(
        Integer, ForeignKey('building_types.id'), nullable=False
    )
    building_type = relationship(
        'BuildingType', backref='skill_types', single_parent=True,
        cascade='all, delete-orphan'
    )


class Skill(Base):
    """Used to attach SkillTypes members to Building instances."""

    __tablename__ = 'skills'
    skill_type = Column(Enum(SkillTypes), nullable=False)
    building_id = Column(Integer, ForeignKey('buildings.id'), nullable=False)
    building = relationship(
        'Building', backref=backref('skills', cascade='all, delete-orphan'),
        single_parent=True
    )


@listen(on_exploit)
def check_exploit_skills(action):
    """Check to see if action.unit.home has either of te exploit skills
    assigned."""
    h = action.unit.home
    if h is None:
        return  # Nothing to do.
    elif h.has_skill(SkillTypes.tripple_exploit):
        multiplier = 3
    elif h.has_skill(SkillTypes.double_exploit):
        multiplier = 2
    else:
        multiplier = 1
    action.amount *= multiplier


@listen(on_kill)
def check_resurrect_skills(unit, target):
    """Check to see if we should be resurrecting target."""
    h = target.home
    if h is None:
        return  # Nothing to do.
    elif h.has_skill(SkillTypes.specific_resurrect):
        ut = target.type
    elif h.has_skill(SkillTypes.random_resurrect):
        ut = choice(UnitType.all())
    else:
        return
    u = target.location.add_unit(ut, *target.coordinates)
    u.health = 0
    u.owner = target.owner
    u.save()
    if u.owner is not None:
        u.owner.message(f'{u.get_name()} is ready.')
{'fraction_non_alphanumeric': 0.09870918754745633, 'fraction_numerical': 0.0018982536066818527, 'mean_word_length': 3.4919011082693947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7107574', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1603, 'n_words': 452}