Skip to content

Commit 1924663

Browse files
authored
Player settings update (#188)
* Added boolean player settings. * Added boolean settings menu options. * No longer tries to get value for bots and just returns the default. * Added menu for IntegerSetting. * Removed FloatSetting for player settings. * StringSetting changed to show the displayed value from the menu in the chosen message.
1 parent 18744f7 commit 1924663

File tree

5 files changed

+234
-173
lines changed

5 files changed

+234
-173
lines changed

addons/source-python/packages/source-python/menus/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ def __init__(self, data=None, select_callback=None, build_callback=None):
4848
:param callable|None select_callback: A function that gets called
4949
whenever a selection was made.
5050
51-
The callback will recieve 3 parameters:
51+
The callback will receive 3 parameters:
5252
1. The instance of this menu.
5353
2. The player's index who made the selection.
5454
3. The player's choice.
5555
5656
:param callable|None build_callback: A function that gets called
5757
before a menu is displayed.
5858
59-
The callback will recieve 2 parameters:
59+
The callback will receive 2 parameters:
6060
1. The instance of this menu.
61-
2. The index of the player who will recieve this menu.
61+
2. The index of the player who will receive this menu.
6262
"""
6363
super().__init__(list() if data is None else data)
6464

@@ -119,7 +119,7 @@ def send(self, *ply_indexes, **tokens):
119119
If no indexes were given, the menu will be sent to every player.
120120
121121
:param ply_indexes:
122-
Player indexes that should recieve the menu.
122+
Player indexes that should receive the menu.
123123
:param tokens:
124124
Translation tokens for menu options, title and description.
125125
"""
@@ -237,7 +237,7 @@ def register_select_callback(self, callback):
237237
:param callable callback: A function that gets called
238238
whenever a selection was made.
239239
240-
The callback will recieve 3 parameters:
240+
The callback will receive 3 parameters:
241241
1. The instance of this menu.
242242
2. The player's index who made the selection.
243243
3. The player's choice.
@@ -253,9 +253,9 @@ def register_build_callback(self, callback):
253253
:param callable callback: A function that gets called
254254
before a menu is displayed.
255255
256-
The callback will recieve 2 parameters:
256+
The callback will receive 2 parameters:
257257
1. The instance of this menu.
258-
2. The index of the player who will recieve this menu.
258+
2. The index of the player who will receive this menu.
259259
"""
260260
self.build_callback = callback
261261
return callback

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ class _AvailableSettings(dict):
2121
def __init__(self):
2222
"""Create the main settings menu on instantiation."""
2323
super().__init__()
24-
self._menu = PagedMenu(
24+
self.menu = PagedMenu(
2525
select_callback=self._chosen_item,
2626
title=_settings_strings['Main Title'])
2727

28-
@property
29-
def menu(self):
30-
"""Return the main settings menu instance."""
31-
return self._menu
32-
3328
def _private_send_menu(self, *args):
3429
"""Called when a private say command is used for sending the menu."""
3530
# Send the menu

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

Lines changed: 24 additions & 60 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, IntegerSetting, SettingsType, StringSetting
17+
)
2418

2519

2620
# =============================================================================
@@ -46,11 +40,11 @@ def __init__(self, name, text=None):
4640
'Given name "{0}" is not valid'.format(name))
4741

4842
# Set the base attributes
49-
self._name = name
50-
self._text = text
43+
self.name = name
44+
self.text = text
5145

5246
# Create the instance's menu
53-
self._menu = PagedMenu(
47+
self.menu = PagedMenu(
5448
select_callback=self._chosen_item,
5549
title=name if text is None else text)
5650

@@ -60,7 +54,7 @@ def __init__(self, name, text=None):
6054
def __setitem__(self, item, value):
6155
"""Validate the given value and its type before setting the item."""
6256
# Is the given value a proper type?
63-
if not isinstance(value, (_SettingsDictionary, _SettingsType)):
57+
if not isinstance(value, (_SettingsDictionary, SettingsType)):
6458

6559
# Raise an error
6660
raise ValueError(
@@ -80,70 +74,39 @@ def __setitem__(self, item, value):
8074
value = self[item]
8175

8276
# Set the item's prefix
83-
value._prefix = self.prefix + '_'
77+
value.prefix = self.prefix
78+
if not value.prefix.endswith('_'):
79+
value.prefix += '_'
8480

8581
# Does the section's name need added to the prefix?
8682
if not isinstance(self, PlayerSettings):
8783

8884
# Add the section's name to the prefix
89-
value._prefix += self.name.lower().replace(' ', '_') + '_'
85+
value.prefix += self.name.lower().replace(' ', '_') + '_'
9086

9187
# Add the option to the menu
9288
self.menu.append(PagedOption(
9389
value.name if value.text is None else value.text, value))
9490

95-
@property
96-
def name(self):
97-
"""Return the name of the _SettingsDictionary instance."""
98-
return self._name
99-
100-
@property
101-
def text(self):
102-
"""Return the text of the _SettingsDictionary instance."""
103-
return self._text
104-
105-
@property
106-
def prefix(self):
107-
"""Return the prefix of the _SettingsDictionary instance."""
108-
return self._prefix
109-
110-
@property
111-
def menu(self):
112-
"""Return the instance's menu object."""
113-
return self._menu
114-
115-
def add_float_setting(
116-
self, name, default, text=None, min_value=None, max_value=None):
117-
"""Add a new float setting to the dictionary."""
118-
# Add the new float setting to the dictionary
119-
self[name] = _FloatSetting(name, default, text, min_value, max_value)
120-
121-
# Return the setting
122-
return self[name]
123-
12491
def add_int_setting(
12592
self, name, default, text=None, min_value=None, max_value=None):
12693
"""Add a new integer setting to the dictionary."""
127-
# Add the new integer setting to the dictionary
128-
self[name] = _IntegerSetting(name, default, text, min_value, max_value)
94+
self[name] = IntegerSetting(name, default, text, min_value, max_value)
95+
return self[name]
12996

130-
# Return the setting
97+
def add_bool_setting(self, name, default, text=None):
98+
"""Add a new boolean setting to the dictionary."""
99+
self[name] = BoolSetting(name, default, text)
131100
return self[name]
132101

133102
def add_string_setting(self, name, default, text=None):
134103
"""Add a new string setting to the dictionary."""
135-
# Add the new string setting to the dictionary
136-
self[name] = _StringSetting(name, default, text)
137-
138-
# Return the setting
104+
self[name] = StringSetting(name, default, text)
139105
return self[name]
140106

141107
def add_section(self, name, text=None):
142108
"""Add a new section to the dictionary."""
143-
# Add the new section to the dictionary
144109
self[name] = _SettingsDictionary(name, text)
145-
146-
# Return the section
147110
return self[name]
148111

149112
@staticmethod
@@ -186,17 +149,18 @@ def __init__(self, name, prefix, text=None):
186149
super().__init__(name, text)
187150

188151
# Set the prefix for the settings
189-
self._prefix = prefix.lower()
152+
self.prefix = prefix.lower()
190153

191154
# Add the instance to the main dictionary
192155
_player_settings[name] = self
193156

194157
# Add the settings instance to the main settings menu
195-
_player_settings.menu.append(
196-
PagedOption(name if text is None else text, self))
158+
self.option = PagedOption(name if text is None else text, self)
159+
_player_settings.menu.append(self.option)
197160

198161
def unregister_settings(self):
199162
"""Unregister the given settings from the dictionary."""
163+
_player_settings.menu.remove(self.option)
200164
del _player_settings[self.name]
201165

202166
def _unload_instance(self):

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, uniqueid):
4141
super().__init__()
4242

4343
# Store the given uniqueid
44-
self._uniqueid = uniqueid
44+
self.uniqueid = uniqueid
4545

4646
# If _player_settings_storage is initializing, don't try to call it
4747
if _IN_INITIALIZATION:
@@ -73,11 +73,6 @@ def __setitem__(self, variable, value):
7373
"""WHERE variables.name=? AND players.uniqueid=?""",
7474
(value, variable, self.uniqueid))
7575

76-
@property
77-
def uniqueid(self):
78-
"""Return the instance's uniqueid."""
79-
return self._uniqueid
80-
8176

8277
class _PlayerSettingsDictionary(dict):
8378
"""Dictionary class used to store user specific settings values."""
@@ -88,13 +83,13 @@ def __init__(self):
8883
super().__init__()
8984

9085
# Connect to the database
91-
self._connection = connect(_STORAGE_PATH)
86+
self.connection = connect(_STORAGE_PATH)
9287

9388
# Set the text factory
9489
self.connection.text_factory = str
9590

9691
# Get the cursor instance
97-
self._cursor = self.connection.cursor()
92+
self.cursor = self.connection.cursor()
9893

9994
# Create the variables table if it does not exist
10095
self.cursor.execute(
@@ -140,16 +135,6 @@ def __missing__(self, uniqueid):
140135
# Return the _UniqueSettings instance
141136
return value
142137

143-
@property
144-
def connection(self):
145-
"""Return the connection to the database."""
146-
return self._connection
147-
148-
@property
149-
def cursor(self):
150-
"""Return the cursor instance."""
151-
return self._cursor
152-
153138
def on_level_shutdown(self):
154139
"""Store the dictionary to the database on map change."""
155140
self.connection.commit()

0 commit comments

Comments
 (0)