mirror of
https://github.com/GAM-team/GAM.git
synced 2025-07-10 22:53:34 +00:00
Generalize getting building ID (#656)
This commit is contained in:
18
src/gam.py
18
src/gam.py
@ -8001,25 +8001,35 @@ def _getBuildingByNameOrId(cd, which_building):
|
|||||||
return which_building[3:]
|
return which_building[3:]
|
||||||
if GM_Globals[GM_MAP_BUILDING_NAME_TO_ID] is None:
|
if GM_Globals[GM_MAP_BUILDING_NAME_TO_ID] is None:
|
||||||
_makeBuildingIdNameMap(cd)
|
_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]:
|
if which_building in GM_Globals[GM_MAP_BUILDING_NAME_TO_ID]:
|
||||||
return GM_Globals[GM_MAP_BUILDING_NAME_TO_ID][which_building]
|
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()
|
which_building_lower = which_building.lower()
|
||||||
ci_matches = []
|
ci_matches = []
|
||||||
for buildingName, buildingId in GM_Globals[GM_MAP_BUILDING_NAME_TO_ID].iteritems():
|
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:
|
if buildingName.lower() == which_building_lower:
|
||||||
ci_matches.append({u'buildingName': buildingName, u'buildingId': buildingId})
|
ci_matches.append({u'buildingName': buildingName, u'buildingId': buildingId})
|
||||||
|
# One match, return ID
|
||||||
if len(ci_matches) == 1:
|
if len(ci_matches) == 1:
|
||||||
return ci_matches[0][u'buildingId']
|
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:
|
if len(ci_matches) > 1:
|
||||||
print u'ERROR: multiple buildings with same name:'
|
print u'ERROR: multiple buildings with same name:'
|
||||||
for building in ci_matches:
|
for building in ci_matches:
|
||||||
print u' Name:%s id:%s' % (building[u'buildingName'], building[u'buildingId'])
|
print u' Name:%s id:%s' % (building[u'buildingName'], building[u'buildingId'])
|
||||||
print
|
print
|
||||||
print u'Please specify building name by exact case or by id.'
|
print u'Please specify building name by exact case or by id.'
|
||||||
sys.exit(3)
|
# No matches
|
||||||
else:
|
else:
|
||||||
print u'ERROR: No such building %s' % which_building
|
print u'ERROR: No such building %s' % which_building
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
Reference in New Issue
Block a user