Skip to content

Commit

Permalink
Retrieve and display author nicknames from map manager. (#1176)
Browse files Browse the repository at this point in the history
* Retrieve author nicknames in map manager.

* Update existing maps with MX ID and author nickname if they've changed.

* Small code tabs revert.

* Only retrieve author nickname for existing maps if it is not yet saved.

Co-authored-by: Tom Valk <tomvalk@lt-box.info>
  • Loading branch information
TheMaximum and tomvlk committed Sep 15, 2022
1 parent a127bb9 commit 78bd9d9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
8 changes: 3 additions & 5 deletions pyplanet/apps/contrib/jukebox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ async def get_fields(self):
'searching': True,
'search_strip_styles': True,
'renderer': lambda row, field:
row['author_login'],
# TODO: Activate after resolving #83.
# row['author_nickname']
# if 'author_nickname' in row and row['author_nickname'] and len(row['author_nickname'])
# else row['author_login'],
row['author_nickname']
if 'author_nickname' in row and row['author_nickname'] and len(row['author_nickname'])
else row['author_login'],
'width': 45,
},
]
Expand Down
4 changes: 4 additions & 0 deletions pyplanet/apps/contrib/rankings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ async def get_fields(self):
'sorting': True,
'searching': True,
'search_strip_styles': True,
'renderer': lambda row, field:
row['author_nickname']
if 'author_nickname' in row and row['author_nickname'] and len(row['author_nickname'])
else row['author_login'],
'width': 60,
},
]
Expand Down
53 changes: 47 additions & 6 deletions pyplanet/contrib/map/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ async def handle_map_change(self, info):
# Try to retrieve the MX-id from the filename.
mx_id = self._extract_mx_id(info['FileName'])

author_nickname = await self.get_map_author_nickname(info)

# Get or create.
map_info = await Map.get_or_create_from_info(
uid=info['UId'], name=info['Name'], author_login=info['Author'], file=info['FileName'],
environment=info['Environnement'], map_type=info['MapType'], map_style=info['MapStyle'],
uid=info['UId'], name=info['Name'], author_login=info['Author'], author_nickname=author_nickname,
file=info['FileName'], environment=info['Environnement'], map_type=info['MapType'], map_style=info['MapStyle'],
num_laps=info['NbLaps'], num_checkpoints=info['NbCheckpoints'], time_author=info['AuthorTime'],
time_bronze=info['BronzeTime'], time_silver=info['SilverTime'], time_gold=info['GoldTime'],
price=info['CopperPrice'], mx_id=mx_id,
Expand Down Expand Up @@ -143,6 +145,22 @@ async def update_list(self, full_update=False, detach_fks=True):
db_uids = [m.uid for m in maps]
diff = [x for x in raw_list if x['UId'] not in db_uids]

# Update existing maps with author nicknames.
for existing_map in [m for m in maps if m.author_nickname is None or len(m.author_nickname) == 0]:
details = [m for m in raw_list if m['UId'] == existing_map.uid][0]

author_nickname = await self.get_map_author_nickname(details)

map_instance = await Map.get_or_create_from_info(
details['UId'], details['FileName'], details['Name'], details['Author'],
author_nickname=author_nickname, environment=details['Environnement'],
time_gold=details['GoldTime'],
price=details['CopperPrice'], map_type=details['MapType'], map_style=details['MapStyle']
)

maps = [m for m in maps if m.uid != details['UId']]
maps.append(map_instance)

# Insert all missing maps into the DB.
rows = list()
for details in diff:
Expand All @@ -155,10 +173,12 @@ async def update_list(self, full_update=False, detach_fks=True):
name = name[:150]
logging.getLogger(__name__).warning('Map name is very long, truncating to 150 chars.')

author_nickname = await self.get_map_author_nickname(details)

rows.append(dict(
uid=details['UId'], file=details['FileName'], name=name, author_login=details['Author'],
environment=details['Environnement'], time_gold=details['GoldTime'], price=details['CopperPrice'],
map_type=details['MapType'], map_style=details['MapStyle'], mx_id=mx_id
author_nickname=author_nickname, environment=details['Environnement'], time_gold=details['GoldTime'],
price=details['CopperPrice'], map_type=details['MapType'], map_style=details['MapStyle'], mx_id=mx_id
))

if len(rows) > 0:
Expand Down Expand Up @@ -196,17 +216,38 @@ async def update_list(self, full_update=False, detach_fks=True):
# Detect any MX-id from the filename.
mx_id = self._extract_mx_id(details['FileName'])

author_nickname = await self.get_map_author_nickname(details)

# Map not yet in self._maps. Add it.
map_instance = await Map.get_or_create_from_info(
details['UId'], details['FileName'], details['Name'], details['Author'],
environment=details['Environnement'], time_gold=details['GoldTime'],
author_nickname=author_nickname, environment=details['Environnement'], time_gold=details['GoldTime'],
price=details['CopperPrice'], map_type=details['MapType'], map_style=details['MapStyle'],
mx_id=mx_id,
)
self._maps.add(map_instance)
updated.append(map_instance)
return updated

async def get_map_author_nickname(self, map_details):
"""
Get the map author nickname by map details.
If the map details contains an empty nickname, a GetMapInfo call to the server will be made.
This is because TM2020 does not yet return the author nickname via GetMapList, but does via GetMapInfo.
:param map_details: struct containing map details
:return: author nickname (None if not found)
"""
author_nickname = None
if 'AuthorNickname' in map_details:
author_nickname = map_details['AuthorNickname']

if author_nickname is None or author_nickname == '':
map_info = await self._instance.gbx('GetMapInfo', map_details['FileName'])
author_nickname = map_info['AuthorNickname']

return author_nickname

async def get_map(self, uid=None):
"""
Get map instance by uid.
Expand Down Expand Up @@ -419,7 +460,7 @@ async def remove_map(self, map, delete_file=False):
async def _override_timelimit(self, filename):
"""
Called to overwrite S_TimeLimit in MatchSettings file if the current map is extended
:param filename: Give the filename of the matchsettings.
"""
if self._is_extended and self._original_ta:
Expand Down

0 comments on commit 78bd9d9

Please sign in to comment.