Skip to content

Commit

Permalink
Merge pull request #240 from mathsman5133/3.6.1
Browse files Browse the repository at this point in the history
3.7.0
  • Loading branch information
lukasthaler authored Jun 17, 2024
2 parents e72a313 + 5775dc3 commit 6dbb043
Show file tree
Hide file tree
Showing 21 changed files with 1,084 additions and 1,459 deletions.
2 changes: 1 addition & 1 deletion coc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

__version__ = "3.6.0"
__version__ = "3.7.0"

from .abc import BasePlayer, BaseClan
from .clans import RankedClan, Clan
Expand Down
106 changes: 90 additions & 16 deletions coc/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,33 @@
from enum import Enum


class PlayerHouseElementType(Enum):
class ExtendedEnum(Enum):
"""An Enum class that allows for the `__str__` method to be implemented."""
def __str__(self):
return self.in_game_name

def __eq__(self, other):
"""Check if the enum is equal to another enum or a string."""
if isinstance(other, Enum):
return self.value == other.value
elif isinstance(other, str):
return str(self.name) == other or str(self.value) == other
return False

@property
def in_game_name(self) -> str:
raise NotImplementedError

@classmethod
def values(cls):
return list(map(lambda c: c.value, cls))

@classmethod
def names(cls):
return list(map(lambda c: c.name, cls))


class PlayerHouseElementType(ExtendedEnum):
"""Enum to map the type of element of the player house."""

ground = "ground"
Expand All @@ -33,46 +59,83 @@ class PlayerHouseElementType(Enum):
deco = "decoration"
walls = "walls"

def __str__(self):
return self.in_game_name

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the element type."""
lookup = {PlayerHouseElementType.ground: "Ground", PlayerHouseElementType.roof: "Roof",
PlayerHouseElementType.foot: "Foot", PlayerHouseElementType.deco: "Decoration",
PlayerHouseElementType.walls: "Walls"}
return lookup[self]
lookup = {"ground": "Ground", "roof": "Roof", "foot": "Foot", "decoration": "Decoration", "walls": "Walls"}
return lookup[self.value]


class Role(Enum):
class Role(ExtendedEnum):
"""Enum to map a player's role in the clan."""

member = "member"
elder = "admin"
co_leader = "coLeader"
leader = "leader"

def __str__(self):
return self.in_game_name

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the role."""
lookup = {Role.member: "Member", Role.elder: "Elder", Role.co_leader: "Co-Leader", Role.leader: "Leader"}
return lookup[self]
lookup = {"member": "Member", "admin": "Elder", "coLeader": "Co-Leader", "leader": "Leader"}
return lookup[self.value]


class WarRound(Enum):
class WarRound(ExtendedEnum):
previous_war = 0
current_war = 1
current_preparation = 2

def __str__(self):
return self.name

@property
def in_game_name(self) -> str:
lookup = ["Previous War", "Current War", "Current Preparation"]
return lookup[self.value]


class Resource(Enum):
class BattleModifier(ExtendedEnum):
"""Enum to map the type of battle modifiers."""
none = "none"
hard_mode = "hardMode"

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the battle modifier."""
lookup = {"none": "None", "hardMode": "Hard Mode"}
return lookup[self.value]


class WarState(ExtendedEnum):
"""Enum to map the state of the war.
Compared to the api docs a few states are missing, but those were never observed in the wild."""
not_in_war = "notInWar"
preparation = "preparation"
in_war = "inWar"
war_ended = "warEnded"

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the war state."""
lookup = {"notInWar": "Not in War", "preparation": "Preparation", "inWar": "In War", "warEnded": "War Ended"}
return lookup[self.value]


class WarResult(ExtendedEnum):
"""Enum to map the result of the war"""
win = "win"
lose = "lose"
tie = "tie"

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the war state."""
lookup = {"win": "Win", "lose": "Lose", "tie": "Tie"}
return lookup[self.value]


class Resource(ExtendedEnum):
elixir = "Elixir"
builder_elixir = "Elixir2"
dark_elixir = "DarkElixir"
Expand All @@ -82,6 +145,14 @@ class Resource(Enum):
glowy_ore = "RareOre"
starry_ore = "EpicOre"

@property
def in_game_name(self) -> str:
"""Get a neat client-facing string value for the resource."""
lookup = {"Elixir": "Elixir", "Elixir2": "Builder Elixir",
"DarkElixir": "Dark Elixir", "Gold": "Gold", "Gold2": "Builder Gold",
"CommonOre": "Shiny Ore", "RareOre": "Glowy Ore", "EpicOre": "Starry Ore"}
return lookup[self.value]


ELIXIR_TROOP_ORDER = [
"Barbarian",
Expand Down Expand Up @@ -115,6 +186,7 @@ class Resource(Enum):
"Ice Golem",
"Headhunter",
"Apprentice Warden",
"Druid",
]

SIEGE_MACHINE_ORDER = [
Expand Down Expand Up @@ -227,6 +299,8 @@ class Resource(Enum):
"Hog Rider Puppet",
"Haste Vial",
"Fireball",
"Spiky Ball",
"Rocket Spear"
]

ACHIEVEMENT_ORDER = [
Expand Down
4 changes: 3 additions & 1 deletion coc/miscmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
from typing import Any, Type, TypeVar, Optional

import coc
from .enums import PlayerHouseElementType
from .enums import ExtendedEnum, PlayerHouseElementType
from .utils import from_timestamp

T = TypeVar("T")


def try_enum(_class: Type[T], data: Any, **kwargs) -> Optional[T]:
"""Helper function to create a class from the given data."""
if issubclass(_class, ExtendedEnum):
return data and _class(data)
return data and _class(data=data, **kwargs)


Expand Down
Loading

0 comments on commit 6dbb043

Please sign in to comment.