Skip to content

Commit

Permalink
Merge pull request #237 from PyPlanet/feature/188
Browse files Browse the repository at this point in the history
[WIP] Admin read map list command
  • Loading branch information
tomvlk committed Jun 18, 2017
2 parents 3e2b453 + ba428ae commit a15d116
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
46 changes: 45 additions & 1 deletion pyplanet/apps/contrib/admin/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ async def on_start(self):
await self.instance.permission_manager.register('add_local_map', 'Add map from server disk', app=self.app, min_level=2)
await self.instance.permission_manager.register('remove_map', 'Remove map from server', app=self.app, min_level=2)
await self.instance.permission_manager.register('write_map_list', 'Write Matchsettings to file', app=self.app, min_level=2)
await self.instance.permission_manager.register('read_map_list', 'Read and load specific Matchsettings file', app=self.app, min_level=2)
await self.instance.permission_manager.register('shuffle', 'Shuffle map list order', app=self.app, min_level=2)

await self.instance.command_manager.register(
Command(command='next', target=self.next_map, perms='admin:next', admin=True),
Expand All @@ -45,7 +47,10 @@ async def on_start(self):
Command(command='erase', target=self.erase_map, perms='admin:remove_map', admin=True, description='Remove and delete map from maplist and disk.')
.add_param('nr', required=False, type=int, help='The number from a list window or the unique identifier.'),
Command(command='writemaplist', aliases=['wml'], target=self.write_map_list, perms='admin:write_map_list', admin=True)
.add_param('file', required=False, type=str, help='Give custom match settings file to save to.')
.add_param('file', required=False, type=str, help='Give custom match settings file to save to.'),
Command(command='readmaplist', aliases=['rml'], target=self.read_map_list, perms='admin:read_map_list', admin=True)
.add_param('file', required=True, type=str, help='Give custom match settings file to load from.'),
Command(command='shuffle', target=self.shuffle, perms='admin:shuffle', admin=True),
)

# If jukebox app is loaded, register the map actions.
Expand Down Expand Up @@ -143,6 +148,45 @@ async def write_map_list(self, player, data, **kwargs):
self.instance.map_manager.update_list(full_update=True)
)

async def read_map_list(self, player, data, **kwargs):
file_name = data.file
file_path = 'MatchSettings/{}'.format(file_name)

try:
await self.instance.map_manager.load_matchsettings(file_path)
message = '$ff0Match Settings has been loaded from: {}'.format(file_path)
except:
message = '$ff0Could not load match settings! Does the file exists? Check log for details.'

# Send message + reload all maps in memory.
await asyncio.gather(
self.instance.chat(message, player),
self.instance.map_manager.update_list(full_update=True)
)

async def shuffle(self, player, data, **kwargs):
setting = settings.MAP_MATCHSETTINGS
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:
message = '$ff0Default match settings file not configured in your settings!'
return await self.instance.chat(message, player)

try:
await self.instance.map_manager.load_matchsettings('MatchSettings/{}'.format(setting))
message = '$ff0Map list has been shuffled and reloaded from disk!'
except:
message = '$ff0Could not shuffle and reload map list.'

# Send message + reload all maps in memory.
await asyncio.gather(
self.instance.chat(message, player),
self.instance.map_manager.update_list(full_update=True)
)

async def add_local_map(self, player, data, **kwargs):
map_file = data.map

Expand Down
23 changes: 22 additions & 1 deletion pyplanet/contrib/map/manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os
import logging

from xmlrpc.client import Fault
Expand Down Expand Up @@ -235,7 +236,7 @@ async def add_map(self, filename, insert=True, save_matchsettings=True):
gbx_method = 'InsertMap' if insert else 'AddMap'

try:
return await self._instance.gbx(gbx_method, filename)
result = await self._instance.gbx(gbx_method, filename)
except Fault as e:
if 'unknown' in e.faultString:
raise MapNotFound('Map is not found on the server.')
Expand All @@ -250,6 +251,8 @@ async def add_map(self, filename, insert=True, save_matchsettings=True):
except Exception as e:
handle_exception(e, __name__, 'add_map', extra_data={'EXTRAHOOK': 'Map Insert bug, see #306'})

return result

async def upload_map(self, fh, filename, insert=True, overwrite=False):
"""
Upload and add/insert the map to the current online playlist.
Expand Down Expand Up @@ -348,3 +351,21 @@ async def save_matchsettings(self, filename=None):
except Exception as e:
logging.exception(e)
raise MapException('Can\'t save matchsettings to \'{}\'!'.format(filename)) from e

async def load_matchsettings(self, filename):
"""
Load Match Settings file and insert it into the current map playlist.
:param filename: File to load, relative to Maps folder.
:return: Boolean if loaded.
"""
try:
if not await self._instance.storage.driver.exists(
os.path.join(self._instance.storage.MAP_FOLDER, filename)
):
raise MapException('Can\'t find match settings file. Does it exist?')
else:
self._instance.gbx('LoadMatchSettings', filename)
except Exception as e:
logging.warning('Can\'t load match settings!')
raise MapException('Can\'t load matchsettings according the dedicated server, tried loading from \'{}\'!'.format(filename)) from e

0 comments on commit a15d116

Please sign in to comment.