1
+ from __future__ import annotations
2
+ from datetime import timedelta
3
+ from dataclasses import dataclass
4
+ from typing import ClassVar
5
+ from pydantic import field_serializer
6
+
7
+ from tortoise import fields
8
+ from tortoise .contrib .pydantic .creator import pydantic_model_creator
9
+ from tortoise .models import Model
10
+
11
+ from pwncore .models import (
12
+ AttackDefTeam
13
+ )
14
+
15
+ __all__ = ("PowerupType" , "UsedPowerup" , "Powerup" , "Powerup_Pydantic" )
16
+
17
+ @dataclass
18
+ class PowerupType :
19
+ name : str
20
+ cost : int
21
+ max_uses_default : int = 1
22
+ duration : timedelta | None = None
23
+ all_powerup_types : ClassVar [dict [str , PowerupType ]] = {}
24
+
25
+ def __post_init__ (self ):
26
+ self .all_powerup_types [self .name ] = self
27
+
28
+ class PowerupTypeField (fields .CharField ):
29
+ def __init__ (self , ** kwargs ):
30
+ super ().__init__ (128 , ** kwargs )
31
+ # if not issubclass(enum_type, Enum):
32
+ # raise ConfigurationError("{} is not a subclass of Enum!".format(enum_type))
33
+ # self._powerup_type = powerup_type
34
+
35
+ def to_db_value (self , powerup_type : PowerupType , instance ) -> str :
36
+ return powerup_type .name
37
+
38
+ def to_python_value (self , value : PowerupType ) -> PowerupType :
39
+ return value
40
+
41
+ Sabotage = PowerupType (name = "Sabotage" , cost = 500 , duration = timedelta (seconds = 5 ))
42
+ Shield = PowerupType (name = "Shield" , cost = 200 , max_uses_default = 3 , duration = timedelta (seconds = 10 ))
43
+ PointSiphon = PowerupType (name = "PointSiphon" , cost = 150 , duration = timedelta (seconds = 15 ))
44
+ Upgrade = PowerupType (name = "Upgrade" , cost = 100 )
45
+
46
+ class Powerup (Model ):
47
+ id = fields .IntField (pk = True )
48
+ powerup_type = PowerupTypeField ()
49
+ attack_def_team : fields .ForeignKeyRelation [AttackDefTeam ] = fields .ForeignKeyField (
50
+ "models.AttackDefTeam" , related_name = "powerups"
51
+ )
52
+ created_at = fields .DatetimeField (auto_now_add = True )
53
+ used_at_least_once = fields .BooleanField (default = False )
54
+ max_uses = fields .IntField (default = 1 )
55
+ used_count = fields .IntField (default = 0 )
56
+ used_instance = fields .ReverseRelation ["UsedPowerup" ]
57
+
58
+ async def save (self , * args , ** kwargs ):
59
+ if not self .max_uses :
60
+ self .max_uses = all_powerup_types [self .powerup_type ].max_uses_default
61
+ await super ().save (* args , ** kwargs )
62
+
63
+ class UsedPowerup (Model ):
64
+ id = fields .IntField (pk = True )
65
+ powerup : fields .ForeignKeyRelation [Powerup ] = fields .ForeignKeyField (
66
+ "models.Powerup" , related_name = "used_instances" , null = False
67
+ )
68
+ used_at = fields .DatetimeField (auto_now_add = True )
69
+
70
+ Powerup_Pydantic = pydantic_model_creator (Powerup )
0 commit comments