Generalize getting building ID (#656)

This commit is contained in:
Ross Scroggs
2017-12-20 10:15:10 -08:00
committed by Jay Lee
parent 156fd4ee6f
commit c6d0f3da92

View File

@ -8001,25 +8001,35 @@ def _getBuildingByNameOrId(cd, which_building):
return which_building[3:]
if GM_Globals[GM_MAP_BUILDING_NAME_TO_ID] is None:
_makeBuildingIdNameMap(cd)
# name is case sensitive. If case matches return immediately
# Exact name match, return ID
if which_building in GM_Globals[GM_MAP_BUILDING_NAME_TO_ID]:
return GM_Globals[GM_MAP_BUILDING_NAME_TO_ID][which_building]
# No exact name match, check for case insensitive name matches
which_building_lower = which_building.lower()
ci_matches = []
for buildingName, buildingId in GM_Globals[GM_MAP_BUILDING_NAME_TO_ID].iteritems():
# otherwise return a case mismatched name if only one exists
# if 2+ case mismatches exist, fail with descriptive error.
if buildingName.lower() == which_building_lower:
ci_matches.append({u'buildingName': buildingName, u'buildingId': buildingId})
# One match, return ID
if len(ci_matches) == 1:
return ci_matches[0][u'buildingId']
# No or multiple name matches, try ID
# Exact ID match, return ID
if which_building in GM_Globals[GM_MAP_BUILDING_ID_TO_NAME]:
return which_building
# No exact ID match, check for case insensitive id match
for buildingId in GM_Globals[GM_MAP_BUILDING_ID_TO_NAME]:
# Match, return ID
if buildingId.lower() == which_building_lower:
return buildingId
# Multiple name matches
if len(ci_matches) > 1:
print u'ERROR: multiple buildings with same name:'
for building in ci_matches:
print u' Name:%s id:%s' % (building[u'buildingName'], building[u'buildingId'])
print
print u'Please specify building name by exact case or by id.'
sys.exit(3)
# No matches
else:
print u'ERROR: No such building %s' % which_building
sys.exit(3)