Skip to content

Commit 9cd433c

Browse files
committed
Added boolean player settings.
1 parent 042b2e2 commit 9cd433c

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

addons/source-python/packages/source-python/settings/player.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
# =============================================================================
66
# >> IMPORTS
77
# =============================================================================
8-
# Python Imports
9-
# Collections
8+
# Python
109
from collections import OrderedDict
1110

12-
# Source.Python Imports
13-
# Core
11+
# Source.Python
1412
from core import AutoUnload
15-
# Menus
16-
from menus import PagedMenu
17-
from menus import PagedOption
18-
# Settings
13+
from menus import PagedMenu, PagedOption
1914
from settings.menu import _player_settings
20-
from settings.types import _SettingsType
21-
from settings.types import _FloatSetting
22-
from settings.types import _IntegerSetting
23-
from settings.types import _StringSetting
15+
from settings.types import (
16+
_BoolSetting, _FloatSetting, _IntegerSetting, _SettingsType, _StringSetting
17+
)
2418

2519

2620
# =============================================================================
@@ -130,6 +124,9 @@ def add_int_setting(
130124
# Return the setting
131125
return self[name]
132126

127+
def add_bool_setting(self, name, default, text=None):
128+
self[name] = _BoolSetting(name, default, text)
129+
133130
def add_string_setting(self, name, default, text=None):
134131
"""Add a new string setting to the dictionary."""
135132
# Add the new string setting to the dictionary

addons/source-python/packages/source-python/settings/types.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@
55
# =============================================================================
66
# >> IMPORTS
77
# =============================================================================
8-
# Python Imports
9-
# Collections
8+
# Python
109
from collections import OrderedDict
11-
# Contextlib
1210
from contextlib import suppress
1311

14-
# Source.Python Imports
15-
# Engines
12+
# Source.Python
13+
from cvars import ConVar
1614
from engines.server import engine_server
17-
# Menus
1815
from menus import PagedMenu
1916
from menus import PagedOption
20-
# Messages
2117
from messages import SayText
22-
# Players
2318
from players.helpers import uniqueid_from_index
24-
# Settings
2519
from settings import _settings_strings
2620
from settings.storage import _player_settings_storage
2721

@@ -103,6 +97,16 @@ def menu(self):
10397
"""Return the setting's menu."""
10498
return self._menu
10599

100+
@property
101+
def _type(self):
102+
"""Inheriting class must define attribute."""
103+
raise NotImplementedError('No _type defined for class.')
104+
105+
@property
106+
def _is_valid_setting(self):
107+
"""Inheriting class must define method."""
108+
raise NotImplementedError('No _is_valid_setting defined for class.')
109+
106110
def get_setting(self, index):
107111
"""Return the setting value for the given player index."""
108112
# Get the client's convar value
@@ -112,7 +116,7 @@ def get_setting(self, index):
112116
with suppress(ValueError):
113117

114118
# Typecast the given value
115-
value = self._type(value)
119+
value = self._typecase_value(value)
116120

117121
# Is the given value a proper one for the convar?
118122
if self._is_valid_setting(value):
@@ -136,7 +140,7 @@ def get_setting(self, index):
136140
with suppress(ValueError):
137141

138142
# Typecast the given value
139-
value = self._type(value)
143+
value = self._typecast_value(value)
140144

141145
# Is the given value a proper one for the convar?
142146
if self._is_valid_setting(value):
@@ -145,8 +149,23 @@ def get_setting(self, index):
145149
return value
146150

147151
# Return the default value
152+
if isinstance(self.default, ConVar):
153+
return self._typecast_default_convar()
148154
return self.default
149155

156+
def _typecast_value(self, value):
157+
"""Cast the value to the proper type."""
158+
return self._type(value)
159+
160+
def _typecast_default_convar(self):
161+
"""Return the typecasted value of the default ConVar."""
162+
return getattr(
163+
self.default,
164+
'get_{value_type}'.format(
165+
value_type=str(self._type)
166+
)
167+
)()
168+
150169
def _menu_build(self, menu, index):
151170
"""Set the default value in the menu description."""
152171
self.menu.description.tokens = {'value': self.get_setting(index)}
@@ -212,6 +231,24 @@ class _IntegerSetting(_NumericalSetting):
212231
_type = int
213232

214233

234+
class _BoolSetting(_SettingsType):
235+
"""Class used to store boolean value settings."""
236+
237+
_type = bool
238+
239+
def _typecast_value(self, value):
240+
"""Cast the given value to a boolean."""
241+
try:
242+
value = int(value)
243+
return bool(value)
244+
except ValueError:
245+
return ''
246+
247+
def _is_valid_setting(self, value):
248+
"""Return whether the given value is a valid boolean value."""
249+
return isinstance(value, self._type)
250+
251+
215252
class _StringSetting(_SettingsType):
216253
"""Class used to store string value settings with available options."""
217254

0 commit comments

Comments
 (0)