Skip to content

Commit c156ce6

Browse files
committed
Removed metaclass functionality of players.weapons.projectiles module.
1 parent c46df78 commit c156ce6

File tree

1 file changed

+79
-104
lines changed
  • addons/source-python/packages/source-python/players/weapons

1 file changed

+79
-104
lines changed

addons/source-python/packages/source-python/players/weapons/projectiles.py

Lines changed: 79 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,126 +19,101 @@
1919
# =============================================================================
2020
# >> CLASSES
2121
# =============================================================================
22-
class _ProjectileMeta(BaseEntity.__class__):
23-
"""Metaclass used to auto-create methods specific to the projectile."""
24-
25-
def __new__(cls, name, bases, odict):
26-
"""Create the class and create its methods dynamically."""
27-
# Store values to use later
28-
temp = {'_classname': None, '_filters': None}
29-
30-
# Loop through the base class attributes
31-
for attribute in ('_classname', '_filters'):
32-
33-
# Try to store the odict's value of
34-
# the attribute and delete it from odict
35-
with suppress(KeyError):
36-
temp[attribute] = odict[attribute]
37-
del odict[attribute]
38-
39-
# Create the object
40-
cls = super().__new__(cls, name, bases, odict)
41-
42-
# Is the the baseclass that uses the metaclass?
43-
if len(bases) != 1 or bases[0].__name__ != '_ProjectileBase':
44-
45-
# Do not add any methods
46-
return cls
47-
48-
# Create the iterator <weapon>_indexes method and set its docstring
49-
setattr(
50-
cls, '{0}_indexes'.format(cls._name),
51-
property(lambda self: cls._projectile_indexes(
52-
self, temp['_classname'], temp['_filters']), doc='Returns a ' +
53-
'generator of {0} indexes the player owns.'.format(cls._name)))
54-
55-
# Create the get_<weapon>_ammo method
56-
setattr(
57-
cls, 'get_{0}_ammo'.format(cls._name),
58-
lambda self: cls._get_projectile_ammo(
59-
self, temp['_classname'], temp['_filters']))
60-
61-
# Set the docstring for the method
62-
getattr(
63-
cls, 'get_{0}_ammo'.format(cls._name)).__doc__ = (
64-
"Returns the player's {0} ammo amount.".format(cls._name))
65-
66-
# Create the set_<weapon>_ammo method
67-
setattr(
68-
cls, 'set_{0}_ammo'.format(cls._name),
69-
lambda self, value: cls._set_projectile_ammo(
70-
self, value, temp['_classname'], temp['_filters']))
71-
72-
# Set the docstring for the method
73-
getattr(
74-
cls, 'set_{0}_ammo'.format(cls._name)).__doc__ = (
75-
"Sets the player's {0} ammo amount to the given value.".format(
76-
cls._name))
77-
78-
# Return the new class
79-
return cls
80-
81-
82-
class _ProjectileBase(metaclass=_ProjectileMeta):
83-
"""Base Projectile class used to interact with player projectiles."""
84-
85-
# Store the base attributes all as None
86-
_classname = None
87-
_filters = None
88-
89-
def _projectile_indexes(self, classname, filters):
90-
"""Iterate over all indexes the player owns for the projectile type."""
91-
return self.weapon_indexes(classname, filters)
92-
93-
def _get_projectile_ammo(self, classname, filters):
94-
"""Return the ammo amount the player has for the projectile type."""
95-
if classname is None:
96-
weapon = [weapon for weapon in WeaponClassIter(filters)][0]
97-
else:
98-
weapon = weapon_manager[classname]
99-
return self.get_property_int(
100-
weapon_manager.ammoprop + '%03d' % weapon.ammoprop)
101-
102-
def _set_projectile_ammo(self, value, classname, filters):
103-
"""Set the ammo amount of the player for the projectile type."""
104-
if classname is None:
105-
weapon = [weapon for weapon in WeaponClassIter(filters)][0]
106-
else:
107-
weapon = weapon_manager[classname]
108-
self.set_property_int(
109-
weapon_manager.ammoprop + '%03d' % weapon.ammoprop, value)
22+
class _HEGrenade(object):
23+
"""Class that interacts with a player based on the hegrenade weapon."""
11024

25+
def hegrenade_indexes(self):
26+
""""""
27+
yield from self.weapon_indexes('weapon_hegrenade')
11128

112-
class _HEGrenade(_ProjectileBase):
113-
"""Class that interacts with a player based on the hegrenade weapon."""
29+
def get_hegrenade_ammo(self):
30+
""""""
31+
return self.get_property_int(
32+
weapon_manager.ammoprop +
33+
'%03d' % weapon_manager['hegrenade'].ammoprop)
11434

115-
_name = 'hegrenade'
116-
_classname = 'weapon_hegrenade'
35+
def set_hegrenade_ammo(self, value):
36+
""""""
37+
self.set_property_int(
38+
weapon_manager.ammoprop +
39+
'%03d' % weapon_manager['hegrenade'].ammoprop, value)
11740

11841

119-
class _Flashbang(_ProjectileBase):
42+
class _Flashbang(object):
12043
"""Class that interacts with a player based on the flashbang weapon."""
12144

122-
_name = 'flashbang'
123-
_classname = 'weapon_flashbang'
45+
def flashbang_indexes(self):
46+
""""""
47+
yield from self.weapon_indexes('weapon_flashbang')
12448

49+
def get_flashbang_ammo(self):
50+
""""""
51+
return self.get_property_int(
52+
weapon_manager.ammoprop +
53+
'%03d' % weapon_manager['flashbang'].ammoprop)
54+
55+
def set_flashbang_ammo(self, value):
56+
""""""
57+
self.set_property_int(
58+
weapon_manager.ammoprop +
59+
'%03d' % weapon_manager['flashbang'].ammoprop, value)
12560

126-
class _SmokeGrenade(_ProjectileBase):
61+
62+
class _SmokeGrenade(object):
12763
"""Class that interacts with a player based on the smokegrenade weapon."""
12864

129-
_name = 'smokegrenade'
130-
_classname = 'weapon_smokegrenade'
65+
def smokegrenade_indexes(self):
66+
""""""
67+
yield from self.weapon_indexes('weapon_smokegrenade')
68+
69+
def get_smokegrenade_ammo(self):
70+
""""""
71+
return self.get_property_int(
72+
weapon_manager.ammoprop +
73+
'%03d' % weapon_manager['smokegrenade'].ammoprop)
74+
75+
def set_smokegrenade_ammo(self, value):
76+
""""""
77+
self.set_property_int(
78+
weapon_manager.ammoprop +
79+
'%03d' % weapon_manager['smokegrenade'].ammoprop, value)
13180

13281

133-
class _Decoy(_ProjectileBase):
82+
class _Decoy(object):
13483
"""Class that interacts with a player based on the decoy weapon."""
13584

136-
_name = 'decoy'
137-
_classname = 'weapon_decoy'
85+
def decoy_indexes(self):
86+
""""""
87+
yield from self.weapon_indexes('weapon_decoy')
13888

89+
def get_decoy_ammo(self):
90+
""""""
91+
return self.get_property_int(
92+
weapon_manager.ammoprop +
93+
'%03d' % weapon_manager['decoy'].ammoprop)
94+
95+
def set_decoy_ammo(self, value):
96+
""""""
97+
self.set_property_int(
98+
weapon_manager.ammoprop +
99+
'%03d' % weapon_manager['decoy'].ammoprop, value)
139100

140-
class _Incendiary(_ProjectileBase):
101+
102+
class _Incendiary(object):
141103
"""Class that interacts with a player based on incendiary weapons."""
142104

143-
_name = 'incendiary'
144-
_filters = 'incendiary'
105+
def incendiary_indexes(self):
106+
""""""
107+
yield from self.weapon_indexes(is_filters='incendiary')
108+
109+
def get_incendiary_ammo(self):
110+
""""""
111+
return self.get_property_int(
112+
weapon_manager.ammoprop +
113+
'%03d' % weapon_manager['molotov'].ammoprop)
114+
115+
def set_incendiary_ammo(self, value):
116+
""""""
117+
self.set_property_int(
118+
weapon_manager.ammoprop +
119+
'%03d' % weapon_manager['molotov'].ammoprop, value)

0 commit comments

Comments
 (0)