@task(ignore_result=True)
def retrieve_map_details(map_id):
	"""
	WARNING!
	This task is CPU/(and highly) RAM expensive.
	
	Checks for presence of specified tiles, counts some of them
	and at the end takes a beautiful photo of the map.
	
	Thanks for your help, erdbeere!
	"""
	logger = retrieve_map_details.get_logger()
	try:
		map_obj = Map.objects.get(pk=map_id)
	except Map.DoesNotExist:
		logger.error("[M- ] Map not found (pk={0}).".format(map_id))
		return False
	try:
		# I actually can use that! Thanks, erd!
		teemap = Teemap(map_obj.map_file.path)
		logger.info("[M-{0}] Loaded \"{1}\" map.".format(map_id, map_obj.name))
	except __HOLE__:
		logger.error("[M-{0}] Couldn't load \"{1}\" map" \
			.format(map_id, map_obj.name))
		has_unhookables = has_deathtiles = None
		shield_count = heart_count = grenade_count = None
	else:
		has_unhookables = has_deathtiles = is_fastcap = has_teleporters = \
			has_speedups = False
		shield_count = heart_count = grenade_count = 0
		logger.info("Counting map items...")
		for tile in teemap.gamelayer.tiles:
			if tile.index == indexes.DEATHTILE:
				has_deathtiles = True
			elif tile.index == indexes.UNHOOKABLE:
				has_unhookables = True
			elif tile.index == indexes.SHIELD:
				shield_count += 1
			elif tile.index == indexes.HEART:
				heart_count += 1
			elif tile.index == indexes.GRENADE:
				grenade_count += 1
		if teemap.telelayer:
			has_teleporters = True
		if teemap.speeduplayer:
			has_speedups = True

		# DISABLED due to huge (counted in GiBs) memory usage
		# logger.info("Rendering map screenshot.")
		# map_image = teemap.render(gamelayer_on_top=True)
		# map_image.save('{0}images/maps/full/{1}.png'.format(settings.MEDIA_ROOT,
		# 	map_obj.name))
		# logger.info("Finished rendering map screenshot.")
		# map_obj.has_image = True

	map_obj.has_unhookables = has_unhookables
	map_obj.has_deathtiles = has_deathtiles
	map_obj.has_teleporters = has_teleporters
	map_obj.has_speedups = has_speedups

	map_map_types = map_obj.map_types.filter(slug='fastcap-no-weapons')
	if not map_map_types:
		map_obj.shield_count = shield_count
		map_obj.heart_count = heart_count
		map_obj.grenade_count = grenade_count
	logger.info("Finished counting map items.")

	logger.info("Generating map CRC...")
	map_obj.map_file.open()
	map_obj.crc = '{0:x}'.format(crc32(map_obj.map_file.read()) & 0xffffffff)
	map_obj.map_file.close()
	map_obj.save()

	map_map_types = map_obj.map_types.filter(slug='fastcap')
	if map_map_types:
		logger.info("Creating a non-weapon twin...")
		new_map, created = Map.objects.get_or_create(
			name="{0}-no-weapons".format(map_obj.name),
			defaults={
				'author': map_obj.author,
				'added_by': map_obj.added_by,
				'map_file' :map_obj.map_file,
				'crc': map_obj.crc,
				'has_unhookables': has_unhookables,
				'has_deathtiles': has_deathtiles,
			})
		if not created:
			logger.info("Oh, it already exists! Updating...")
			new_map.author = map_obj.author
			new_map.added_by = map_obj.added_by
			new_map.map_file = map_obj.map_file
			new_map.crc = map_obj.crc
			new_map.has_unhookables = has_unhookables
			new_map.has_deathtiles = has_deathtiles
			new_map.save()
		else:
			new_map.map_types.add(MapType.objects.get(slug='fastcap-no-weapons'))
			new_map.map_types.add(*map_obj.map_types.exclude(slug='fastcap'))
			new_map.save()
	logger.info("[M-{0}] Finished processing \"{1}\" map." \
		.format(map_id, map_obj.name))
dataset/ETHPy150Open chaosk/teerace/teerace/race/tasks.py/retrieve_map_details