Ejemplo n.º 1
0
def display_team_losses(cursor, the_world, team_id):
    from queries import battle_q

    empty_div = '<div class="ti_section" id="war_losses_div"></div>'
    the_team = the_world.teams()[team_id]

    output = ['<div class="ti_section" id="war_losses_div">']

    # team_units		= the_team.get_units()
    squad_dict = the_world.squads()
    team_dict = the_world.teams()
    unit_dict = the_world.units()
    campaign_dict = the_world.campaigns()

    # Get a list of all the campaigns this team was part of this turn
    campaign_list = []
    query = """SELECT c.id
		FROM campaign_teams ct, campaigns c
			WHERE ct.team = %d
			AND ct.campaign = c.id
			AND c.turn = %d""" % (
        the_team.id,
        common.current_turn(),
    )
    try:
        the_world.cursor.execute(query)
    except Exception as e:
        raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n", ""), query))
    for row in the_world.cursor:
        campaign_list.append(str(row["id"]))

        # No campaigns, no losses
    if campaign_list == []:
        return empty_div

        # Now we need a list of all the battles in this campain
    battle_list = []
    battle_dict = {}
    query = "SELECT * FROM battles WHERE campaign in (%s) ORDER BY campaign, start" % ",".join(campaign_list)
    try:
        the_world.cursor.execute(query)
    except Exception as e:
        raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n", ""), query))
    for row in the_world.cursor:
        battle_dict[row["id"]] = row
        battle_list.append(str(row["id"]))

        # No battles, no section of results here!
    if battle_list == []:
        return empty_div

        # 	Losses for other teams
        # ------------------------
    output.append(
        "<div style='float:right;width:50%;'><span class='stitle'>Losses across the campaign</span><br /><br />"
    )
    for c in campaign_list:
        losses = campaign_q.get_all_losses(cursor, c)

        output.append("<strong>%s</strong><br />" % campaign_dict[int(c)].name)

        for t, l in losses.items():
            if l == 0:
                continue
            output.append("{team}: {losses}<br />".format(team=team_dict[t].name, losses=common.approx(l)))

        output.append("<br />")

        # print(c, campaign_q.get_all_losses(cursor, c))
    output.append("</div>")

    # 	Our losses
    # ------------------------
    # Now for a list of all squad losses, sort by battle
    query = """SELECT squad, battle, losses
		FROM squad_battle_history
			WHERE battle IN (%s)
				ORDER BY battle""" % ",".join(
        battle_list
    )
    try:
        the_world.cursor.execute(query)
    except Exception as e:
        raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n", ""), query))

    losses = {"Total": {}}
    for row in the_world.cursor:
        if row["losses"] == 0:
            continue

        if row["battle"] not in losses:
            losses[row["battle"]] = {}

        the_squad = squad_dict[row["squad"]]

        if the_squad.team != the_team.id:
            continue
        if the_squad.unit not in losses[row["battle"]]:
            losses[row["battle"]][the_squad.unit] = 0

        if the_squad.unit not in losses["Total"]:
            losses["Total"][the_squad.unit] = 0

        losses[row["battle"]][the_squad.unit] += row["losses"]
        losses["Total"][the_squad.unit] += row["losses"]

    last_campaign = -1
    for b, the_battle in battle_dict.items():
        if the_battle["campaign"] != last_campaign:
            last_campaign = the_battle["campaign"]
            output.append("<span class='stitle'>%s</span><br />" % campaign_dict[last_campaign].name)

        if b == "Total" or b not in losses:
            continue
        if len(losses[b]) == 0:
            continue
        output.append("<strong>%s</strong><br />" % the_battle["name"])
        for u, a in losses[b].items():
            output.append("%s: %s<br />" % (unit_dict[u].name, format(a, ",")))

        output.append("<br />")

    if len(battle_dict) > 1:
        output.append("<br /><strong>Total losses</strong><br />")
        for u, a in losses["Total"].items():
            output.append("%s: %s<br />" % (unit_dict[u].name, format(a, ",")))

    output.append("</div>")
    return "".join(output)
Ejemplo n.º 2
0
	def draw_city(self, cursor, city_id):
		city_is_special = False
		
		# Handles
		the_city = self.city_dict[city_id]
		the_team = self.team_dict[the_city.team]
		
		team_clean_name = the_team.clean_name()
		
		# Test for 1 icon	
		image_size = map_data.map_image_size(the_city.size)
		if image_size == 0:
			return ""
		
		my_left		= (the_city.x - self.left) * 2.5 - (image_size/2.0)
		my_top		= (the_city.y - self.top) * 2.5 - (image_size/2.0)
		
		# Check it's within the picture size of the map
		if (the_city.x - image_size/2) < self.left:		return ""
		if (the_city.x + image_size/2) > self.right:	return ""
		
		if (the_city.y - image_size/2) < self.top:		return ""
		if (the_city.y + image_size/2) > self.bottom:	return ""
		
		#	Mouseover
		#------------------------
		# Name
		# clean_name = the_city.name.lower().replace(' ', '').replace("'", "")
		# clean_name = common.js_name(the_city.name.lower().replace(' ', '').replace("'", ''))
		clean_name = safe_re.sub("", the_city.name)
		# safe_re
		
		# Location
		city_x = the_city.x
		city_y = the_city.y
		
		if the_team.ir:
			city_title = ["<strong>Team</strong>: %s <span style='font-size:0.9em;'>(IR)</span><br />" % the_team.name]
		else:
			city_title = ["<strong>Team</strong>: %s<br />" % the_team.name]
		
		# Port
		if the_city.port == True:
			city_title.append('Is a port')
		else:
			city_title.append('Not a port')
		
		# Nomadic
		if the_city.nomadic == True:
			city_title.append('<br />Nomadic')
		
		# Walls
		team_logo = '%s_unwalled.png' % team_clean_name
		has_harbour_walls = False
		
		# Harbour walls id = 18
		if 18 in the_city.buildings_amount and the_city.buildings_amount[18] > 0:
			has_harbour_walls = True
			city_title.append('<br />Harbour walls')
		
		if the_city.walls != []:
			if len(the_city.walls) > 1:
				if has_harbour_walls:
					city_title.append('<br />%d walls' % (len(the_city.walls)-1))
				else:
					city_title.append('<br />%d walls' % len(the_city.walls))
			else:
				if not has_harbour_walls:
					city_title.append('<br />1 wall')
			team_logo = '%s_walled.png' % team_clean_name
		
		# Supply
		if the_city.supplies != []:
			supply_string = ", ".join([resource_list.data_list[s].name for s in the_city.supplies])
			city_title.append('<br />%s' % supply_string)
		
		# Terrain
		city_title.append('<br />Terrain type: %s' % map_data.terrain[mapper_q.get_terrain(cursor, city_x, city_y)].title())
		
		# Area - Used for debugging overlap
		# area = image_size/2.5
		# area = area * area
		# city_title.append("<br />Area: %s" % area)
		
		# Overlap
		if the_city.overlap > 0:
			city_title.append('<br />Overlap: %s%%' % the_city.overlap)
		
		# Population
		if the_city.team not in self.personalise:
			city_title.append('<br />Size: %s' % common.approx(the_city.size))
			if the_city.slaves > 0:
				city_title.append('<br />Slaves: %s' % common.approx(the_city.slaves))
		else:
			city_title.append('<br />Population: %s' % common.number_format(the_city.population))
			if the_city.slaves > 0:
				city_title.append('<br />Slaves: %s' % common.number_format(the_city.slaves))
		
		# Artefact
		artefact_count = 0
		if city_id in self.cities_with_artefact:
			for a, the_artefact in self.artefact_dict.items():
				if the_artefact.city == city_id:
					artefact_count += 1
					
		if artefact_count > 0:
			city_is_special = True
			if artefact_count > 1:
				city_title.append('<br /><strong>City contains %d artefacts</strong>' % artefact_count)
			else:
				city_title.append('<br /><strong>City contains an artefact</strong>')
		
		# Wonder
		if city_id in self.cities_with_wonder:
			for w, the_wonder in self.wonder_dict.items():
				if the_wonder.city == city_id:
					if the_wonder.completed:
						city_title.append('<br /><strong>City contains a wonder</strong>')
						city_is_special = True
					else:
						city_title.append('<br /><strong>City contains an uncompleted wonder</strong>')
		
		# Description
		if the_city.description != '':
			city_title.append('<br />%s' % the_city.description.replace("\\", ""))
		
		# GM stuff
		if self.gm:
			city_title.append('<br />Image size: %s' % image_size)
		
		# Info only for the team map
		if the_city.team in self.personalise:
			city_title.append("<br />")
			
			# Buildings?
			# in_progress_dict, city_buildings = the_city.get_buildings()
			
			walls		= []
			buildings	= []
			in_progress = []
			
			for b, a in the_city.buildings_amount.items():
				if self.building_dict[b].wall == True: walls.append(self.building_dict[b].name)
				if self.building_dict[b].wall != True: buildings.append(self.building_dict[b].name)
			
			for b, p in the_city.buildings.items():
				if p > 0:
					in_progress.append(self.building_dict[b].name)
			
			if len(buildings) > 0:
				city_title.append("<br /><strong>Buildings</strong>: %s" % ", ".join(buildings))
			
			if len(walls) > 0:
				city_title.append("<br /><strong>Walls</strong>: %s" % ", ".join(walls))
			
			if len(in_progress) > 0:
				city_title.append("<br /><strong>In progress</strong>: %s" % ", ".join(in_progress))
		
		if the_city.founded == common.current_turn():
			new_city_style = "border: 1px solid #FFA;"
			city_title.append("<br />Founded: <strong>Turn %d</strong>" % the_city.founded)
		elif city_is_special:
			# new_city_style = "border: 2px dotted #FFF;"
			new_city_style = ""
			team_logo = '%s_special.png' % team_clean_name
		else:
			new_city_style = ""
			city_title.append("<br />Founded: Turn %d" % the_city.founded)
		
		# Land controlled?
		# control = ""
		# if the_city.team in self.personalise:
		# 	control_image_size = map_control_size(the_city.size)
		# 	if control_image_size == 0:
		# 		return ""
		# 	
		# 	c_left		= (the_city.x - self.left) * 2.5 - (control_image_size/2.0)
		# 	c_top		= (the_city.y - self.top) * 2.5 - (control_image_size/2.0)
		# 	
		# 	control = """<img class="city_icon" src="%(icon_path)scontrol.png" style="top:%(top)spx; left: %(left)spx;" width="%(image_size)s" height="%(image_size)s" />""" % {
		# 		"icon_path":	self.icon_path,
		# 		"top":			c_top,
		# 		"left":			c_left,
		# 		"image_size":	control_image_size,
		# 	}
		
		# # Output - Function float
		# output = """<img class="city_icon" id="{0[clean_name]}" src="{0[icon_path]}{0[team_logo]}" style="top:{0[top]}px; left:{0[left]}px;" width="{0[image_size]}" height="{0[image_size]}" onmouseover="$('#{0[clean_name]}_hover').fadeIn(250);" onmouseout="$('#{0[clean_name]}_hover').hide(250);"/>""".format({
		# 	"icon_path":	self.icon_path,
		# 	"clean_name":	clean_name,
		# 	"top": my_top,
		# 	"left": my_left,
		# 	"image_size": image_size,
		# 	"team_logo": team_logo,
		# })
		
		# Output - Fixed float
		output = """<img class="city_icon" id="{clean_name}" src="{icon_path}{team_logo}" style="top:{top}px; left:{left}px;{new_city}" width="{image_size}" height="{image_size}" onmouseover="$('#{clean_name}_hover').fadeIn(250);" onmouseout="$('#{clean_name}_hover').hide(250);"/>""".format(
			icon_path =		self.icon_path,
			clean_name =	clean_name,
			top =			my_top,
			left =			my_left,
			image_size =	image_size,
			team_logo =		team_logo,
			new_city =		new_city_style,
		)
		
		if self.mouseover == 1:
			hover_top = my_top - 5
			hover_top = min(self.height*2.5 - 125, hover_top)
			
			hover_left = my_left + image_size + 20
			hover_left = max(hover_left, 5)
		
			if hover_left > ((self.width * 2.5) - 350):
				hover_left = my_left - 350
		
			# Floater
			self.append_last.append("""<div id="%(clean_name)s_hover" class="city_hover" style="top:%(hover_top)spx; left:%(hover_left)spx;"><div class="city_title">%(name)s&nbsp;&nbsp;&nbsp;%(city_x)s,%(city_y)s</div>%(city_title)s</div>""" % {
				"name": the_city.name,
				"clean_name": clean_name,
				"hover_top": hover_top,
				"hover_left": hover_left,
				"city_x": the_city.x,
				"city_y": the_city.y,
				"city_title": "".join(city_title),
			})
		
		# Now return it
		return "".join(output)