Skip to content

Commit

Permalink
[FEATURE] Add blacklist save and read commands. Blacklist is saved wh…
Browse files Browse the repository at this point in the history
…en player is blacklisted/unblacklisted, and loaded at pyplanet start.
  • Loading branch information
tomvlk committed Dec 17, 2017
1 parent 3a6d122 commit 06b7b3b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 12 deletions.
18 changes: 15 additions & 3 deletions docs/source/intro/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ This one is pretty important, and pretty simple too. Look at the examples bellow
}
Map settings (base)
~~~~~~~~~~~~~~~~~~~
Server files settings (base)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Some of these settings are required to be able to save match settings for example.
Some of these settings are required to be able to save match settings and to save the blacklisted players for example.

.. code-block:: python
:caption: base.py
Expand All @@ -281,18 +281,30 @@ Some of these settings are required to be able to save match settings for exampl
'default': '{server_login}.txt',
}
# Blacklist file is managed by the dedicated server and will be loaded and writen to by PyPlanet once a
# player gets blacklisted. The default will be the filename Maniaplanet always uses and is generic.
BLACKLIST_FILE = {
'default': 'blacklist.txt'
}
.. code-block:: yaml
:caption: base.yaml
MAP_MATCHSETTINGS:
default: 'maplist.txt'
BLACKLIST_FILE:
default: 'blacklist.txt'
.. code-block:: json
:caption: base.json
{
"MAP_MATCHSETTINGS": {
"default": "maplist.txt"
},
"BLACKLIST_FILE": {
"default": "blacklist.txt"
}
}
Expand Down
82 changes: 76 additions & 6 deletions pyplanet/apps/contrib/admin/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import asyncio

from pyplanet.conf import settings
from pyplanet.contrib.command import Command
from pyplanet.contrib.player.exceptions import PlayerNotFound
from pyplanet.views.generics.alert import show_alert
Expand Down Expand Up @@ -30,6 +31,8 @@ async def on_start(self):
await self.instance.permission_manager.register('force_team', 'Force player into a team', app=self.app, min_level=1)
await self.instance.permission_manager.register('switch_team', 'Force player into the other team', app=self.app, min_level=1)
await self.instance.permission_manager.register('warn', 'Warn a player', app=self.app, min_level=1)
await self.instance.permission_manager.register('write_blacklist', 'Write blacklist file', app=self.app, min_level=3)
await self.instance.permission_manager.register('read_blacklist', 'Read blacklist file', app=self.app, min_level=3)

await self.instance.command_manager.register(
Command(command='mute', aliases=['ignore'], target=self.ignore_player, perms='admin:ignore', admin=True).add_param(name='login', required=True),
Expand All @@ -50,7 +53,11 @@ async def on_start(self):
Command(command='switchteam', target=self.switch_team, perms='admin:switch_team', description='Force player into the other team', admin=True)
.add_param(name='login', required=True),
Command(command='warn', aliases=['warning'], target=self.warn_player, perms='admin:warn', description='Warn a player', admin=True)
.add_param(name='login', required=True)
.add_param(name='login', required=True),
Command(command='writeblacklist', aliases=['wbl'], target=self.write_blacklist, perms='admin:write_blacklist', description='Write blacklist file', admin=True)
.add_param('file', required=False, type=str, help='Give custom blacklist file to save to.'),
Command(command='readblacklist', aliases=['rbl'], target=self.read_blacklist, perms='admin:read_blacklist', description='Read blacklist file', admin=True)
.add_param('file', required=False, type=str, help='Give custom blacklist file to load from.'),
)

async def force_spec(self, player, data, **kwargs):
Expand Down Expand Up @@ -195,11 +202,22 @@ async def blacklist_player(self, player, data, **kwargs):
try:
blacklist_player = await self.instance.player_manager.get_player(data.login)
message = '$ff0Admin $fff{}$z$s$ff0 has blacklisted $fff{}$z$s$ff0.'.format(player.nickname, blacklist_player.nickname)
await self.instance.gbx.multicall(
self.instance.gbx('BlackList', data.login),
self.instance.gbx('Kick', data.login),
self.instance.chat(message)
)

try:
await self.instance.gbx.multicall(
self.instance.gbx('BlackList', data.login),
self.instance.gbx('Kick', data.login),
)
except:
return await self.instance.chat('$ff0Blacklisting failed!', player)

await self.instance.chat(message)

# Try to save to file.
try:
await self.instance.player_manager.save_blacklist()
except:
pass
except PlayerNotFound:
message = '$ff0Admin $fff{}$z$s$ff0 has blacklisted $fff{}$z$s$ff0.'.format(player.nickname, data.login)
await self.instance.gbx.multicall(
Expand All @@ -214,6 +232,12 @@ async def unblacklist_player(self, player, data, **kwargs):
self.instance.chat(message)
)

# Try to save to file.
try:
await self.instance.player_manager.save_blacklist()
except:
pass

async def change_level(self, player, data, **kwargs):
try:
target_player = await self.instance.player_manager.get_player(login=data.login)
Expand Down Expand Up @@ -258,3 +282,49 @@ async def warn_player(self, player, data, **kwargs):
message = '$i$f00Unknown login!'
await self.instance.chat(message, player.login)
return

async def write_blacklist(self, player, data, **kwargs):
setting = settings.BLACKLIST_FILE
if isinstance(setting, dict) and self.instance.process_name in setting:
setting = setting[self.instance.process_name]
if not isinstance(setting, str):
setting = None

if not setting and not data.file:
message = '$ff0Default blacklist file setting not configured in your settings file!'
return await self.instance.chat(message, player)

if data.file:
file_name = data.file
else:
file_name = setting.format(server_login=self.instance.game.server_player_login)

message = '$ff0Blacklist has been saved to the file: {}'.format(file_name)
try:
await self.instance.player_manager.save_blacklist(filename=file_name)
self.instance.chat(message, player)
except:
await self.instance.chat('$ff0Blacklist saving failed to {}'.format(file_name), player)

async def read_blacklist(self, player, data, **kwargs):
setting = settings.BLACKLIST_FILE
if isinstance(setting, dict) and self.instance.process_name in setting:
setting = setting[self.instance.process_name]
if not isinstance(setting, str):
setting = None

if not setting and not data.file:
message = '$ff0Default blacklist file setting not configured in your settings file!'
return await self.instance.chat(message, player)

if data.file:
file_name = data.file
else:
file_name = setting.format(server_login=self.instance.game.server_player_login)

message = '$ff0Blacklist has been loaded from the file: {}'.format(file_name)
try:
await self.instance.player_manager.save_blacklist(filename=file_name)
self.instance.chat(message, player)
except:
await self.instance.chat('$ff0Blacklist loading failed from {}'.format(file_name), player)
6 changes: 6 additions & 0 deletions pyplanet/conf/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
'default': 'maplist.txt'
}

# Blacklist file is managed by the dedicated server and will be loaded and writen to by PyPlanet once a
# player gets blacklisted. The default will be the filename Maniaplanet always uses and is generic.
BLACKLIST_FILE = {
'default': 'blacklist.txt'
}

# The storage configuration contains the same instance mapping of the dedicated servers and is used
# to access the filesystem on the dedicated server location.
# Please refer to the documentation for more information.
Expand Down
5 changes: 2 additions & 3 deletions pyplanet/conf/project_template/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Change the numbers according to the major release. First read the release notes and upgrade notes on our site:
# http://pypla.net/
pyplanet>=0.0.1,<1.0.0
# Core, require PyPlanet itself.
pyplanet

# Add your apps requirements here:

6 changes: 6 additions & 0 deletions pyplanet/conf/project_template/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
'default': 'maplist.txt',
}

# Blacklist file is managed by the dedicated server and will be loaded and writen to by PyPlanet once a
# player gets blacklisted. The default will be the filename Maniaplanet always uses and is generic.
BLACKLIST_FILE = {
'default': 'blacklist.txt'
}

# The storage configuration contains the same instance mapping of the dedicated servers and is used
# to access the filesystem on the dedicated server location.
# Please refer to the documentation for more information. http://pypla.net/
Expand Down
72 changes: 72 additions & 0 deletions pyplanet/contrib/player/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from pyplanet.contrib import CoreContrib
from pyplanet.contrib.player.exceptions import PlayerNotFound
from pyplanet.contrib.setting.core_settings import performance_mode
from pyplanet.core.exceptions import ImproperlyConfigured
from pyplanet.core.signals import pyplanet_performance_mode_begin, pyplanet_performance_mode_end
from pyplanet.core.storage.exceptions import StorageException

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,6 +83,12 @@ async def on_start(self):
player_list = await self._instance.gbx('GetPlayerList', -1, 0)
await asyncio.gather(*[self.handle_connect(player['Login']) for player in player_list])

# Load and activate blacklist.
try:
await self.load_blacklist()
except:
pass # Ignore any exception thrown

self._instance.signals.listen('maniaplanet:loading_map_end', self.map_loaded)

async def map_loaded(self, *args, **kwargs):
Expand Down Expand Up @@ -284,11 +292,75 @@ async def get_player(self, login=None, pk=None, lock=True):
raise PlayerNotFound('Player not found.')

async def get_player_by_id(self, identifier):
"""
Get player object by ID.
:param identifier: Identifier.
:return: Player object or None
"""
for player in self._online:
if player.flow.player_id == identifier:
return player
return None

async def save_blacklist(self, filename=None):
"""
Save the current blacklisted players to file given or fetch from config.
:param filename: Give the filename of the blacklist, Leave empty to use the current loaded and configured one.
:type filename: str
:raise: pyplanet.core.exceptions.ImproperlyConfigured
:raise: pyplanet.core.storage.exceptions.StorageException
"""
setting = settings.BLACKLIST_FILE
if isinstance(setting, dict) and self._instance.process_name in setting:
setting = setting[self._instance.process_name]
if not isinstance(setting, str):
setting = None

if not filename and not setting:
raise ImproperlyConfigured(
'The setting \'BLACKLIST_FILE\' is not configured for this server! We can\'t save the Blacklist!'
)
if not filename:
filename = setting.format(server_login=self._instance.game.server_player_login)

try:
await self._instance.gbx('SaveBlackList', filename)
except Exception as e:
logging.exception(e)
raise StorageException('Can\'t save blacklist file to \'{}\'!'.format(filename)) from e

async def load_blacklist(self, filename=None):
"""
Load blacklist file.
:param filename: File to load or will get from settings.
:raise: pyplanet.core.exceptions.ImproperlyConfigured
:raise: pyplanet.core.storage.exceptions.StorageException
:return: Boolean if loaded.
"""
setting = settings.BLACKLIST_FILE
if isinstance(setting, dict) and self._instance.process_name in setting:
setting = setting[self._instance.process_name]
if not isinstance(setting, str):
setting = None

if not filename and not setting:
raise ImproperlyConfigured(
'The setting \'BLACKLIST_FILE\' is not configured for this server! We can\'t load the Blacklist!'
)
if not filename:
filename = setting.format(server_login=self._instance.game.server_player_login)

try:
self._instance.gbx('LoadBlackList', filename)
except Exception as e:
logging.exception(e)
raise StorageException('Can\'t load blacklist according the dedicated server, tried loading from \'{}\'!'.format(
filename
)) from e

@property
def online(self):
"""
Expand Down

0 comments on commit 06b7b3b

Please sign in to comment.