-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdamage.py
45 lines (34 loc) · 1.03 KB
/
damage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from dataclasses import dataclass
from enum import Enum, auto
from .dice import Roll, rollFactory
class damageType(Enum):
Acid = auto()
Bludgeoning = auto()
Cold = auto()
Fire = auto()
Force = auto()
Lightning = auto()
Necrotic = auto()
Piercing = auto()
Poison = auto()
Psychic = auto()
Radiant = auto()
Slashing = auto()
Thunder = auto()
class DamageTypeUndefined(KeyError):
def __init__(self, damageType: str, message: str) -> None:
super().__init__(message)
self.damageType = damageType
@dataclass
class Damage:
dmgType: damageType
dice: Roll
class DamageFactory:
def __call__(self, data: dict) -> Damage:
dmgTypeStr = data["damage_type"]["name"]
try:
dmgType = damageType[dmgTypeStr]
except KeyError as e:
raise DamageTypeUndefined(dmgTypeStr, f"damagetype enum does not have a definition for {dmgTypeStr}")
return Damage(dmgType, rollFactory(data["damage_dice"]))
damageFactory = DamageFactory()