def main(cursor, campaign_id = -1): output = ["""<div style="padding: 5px;">"""] campaign_id = int(common.get_val('campaign', campaign_id)) battle_id = int(common.get_val('battle', 0)) ajax = common.get_val('ajax', 0) if campaign_id < 1: if battle_id < 1: return "No campaign selected" else: the_battle = battle_q.get_one_battle(cursor, battle_id) campaign_id = the_battle.campaign the_campaign = campaign_q.get_one_campaign(cursor, campaign_id) battle_dict = battle_q.get_battles_from_campaign(cursor, campaign_id) team_dict = team_q.get_all_teams(cursor) city_dict = city_q.get_all_cities(cursor) if not ajax: output.append(""" <div style="float:right;border:0px solid #000; width: 50%;"> <form id="delete_form" action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="campaign" id="campaign" value="{id}" /> <input type="hidden" name="mode" id="mode" value="remove_campaign" /> <input style="float:right; margin-right:100px;" type="button" value="Delete campaign" onclick="var answer = confirm('Delete {esc_name}?') if (answer) $('#delete_form').submit();" /> </form> <div id="campaign_info"> </div> </div> <a href="web.py?mode=list_campaigns&turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a> <a href="web.py?mode=setup_campaign&campaign={camp}" class="block_link" style="display:inline;">Setup campaign</a> <br /><br /> <span class="stitle">{name}</span> <br /><br /> <table border="0" cellspacing="0" cellpadding="5"> """.format( turn = common.current_turn(), camp = the_campaign.id, name = the_campaign.name, id = campaign_id, esc_name = common.js_name(the_campaign.name), )) else: output.append("""<table border="0" cellspacing="0" cellpadding="5" width="100%">""") output.append(""" <tr class="row2"> <th>Battle</th> <th>Time</th> <th>Location</th> <th>Type</th> <th>Result</th> <th> </th> <th colspan="2">Perform</th> </tr>""") # Display battles i = -1 for battle_id, the_battle in battle_dict.items(): i += 1 location = "%d, %d" % (the_battle.x, the_battle.y) city_link = "" if the_battle.city > 0: city_link = '<!-- PY --> <a href="web.py?mode=edit_city&city=%d">Edit city</a><!-- PYEND -->' % the_battle.city output.append(""" <tr class="row{i}"> <td>{name} ({battle_id}){city_link}</td> <td>{start} : {duration}</td> <td>{location} {city}</td> <td>{type}</td> <td>{result}</td> <td style="padding:0px;"><a href="web.py?mode=setup_battle&battle={battle_id}" class="block_link">Setup</a></td> <td style="padding:0px;"><a href="web.py?mode=perform_battle&battle={battle_id}" class="block_link">By unit</a></td> <td style="padding:0px;"><a href="web.py?mode=perform_by_army&battle={battle_id}" class="block_link">By army</a></td> </tr> """.format( i=i%2, city_link = city_link, name=common.doubleclick_text("battles", "name", battle_id, the_battle.name, label_style="font-weight:bold;"), start=common.doubleclick_text("battles", "start", battle_id, the_battle.start, size=2), duration=common.doubleclick_text("battles", "duration", battle_id, the_battle.duration, size=2), location=location, battle_id=battle_id, city = city_dict[the_battle.city].name, type = battle.battle_types[the_battle.type], result = battle.result_types[the_battle.result], )) if ajax: onload = "" else: onload = common.onload("$('#form_city').focus();") # Add new battle i += 1 output.append(""" <tr class="row{i}"> <form action="exec.py" id="add_battle_form" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" value="add_battle" /> <input type="hidden" name="campaign" value="{campaign_id}" /> <td style="padding: 1px;"><input type="text" name="name" id="new_name" value="" /></td> <td style="padding: 1px;"> <input type="text" name="start" value="-1" size="2"/> <input type="text" name="duration" value="" size="2"/> </td> <td style="padding: 1px;"> <input type="text" name="location" value="" size="8"/> </td> <td style="padding: 1px;"> {type} </td> <td> </td> <td colspan="3" style="padding: 0px;"> <!--<a class="block_link" href="#" onclick="$('#add_battle_form').submit();">Add</a>--> <input type="submit" value="Add" /> </td> </form> </tr>""".format( i=i%2, campaign_id=campaign_id, type = common.option_box("type", elements = battle.battle_types, element_order = [], custom_id = ""), )) # Add by city city_dict = city_q.get_cities_for_dropdown(cursor) keys, names = [], {} for c, the_city in city_dict.items(): if the_city.dead > 0: continue if not team_dict[the_city.team].active: continue keys.append(c) names[c] = the_city.name i += 1 output.append(""" <tr class="row{i}"> <form action="exec.py" id="add_battle_form" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" value="add_battle" /> <input type="hidden" name="campaign" value="{campaign_id}" /> <td style="padding: 1px;">{city_list}</td> <td style="padding: 1px;"> <input type="text" name="start" value="-1" size="2"/> <input type="text" name="duration" value="" size="2"/> </td> <td> </td> <td style="padding: 1px;"> {type} </td> <td> </td> <td colspan="3" style="padding: 0px;"> <!--<a class="block_link" href="#" onclick="$('#add_battle_form').submit();">Add</a>--> <input type="submit" value="Add" /> </td> </form> {onload} </tr> </table> <br /><br /> """.format( i=i%2, campaign_id=campaign_id, onload=onload, city_list = common.option_box( name='city', elements=names, element_order=keys, custom_id="form_city", ), type = common.option_box("type", elements = battle.battle_types, element_order = [], custom_id = ""), )) # Chosen kills output.append(""" <div style="float:right;border:0px solid #000; width: 60%;"> <div id="player_kills"> </div> </div> """) # Now for moving all the armies output.append(""" <!-- PY --> <table border="0" cellspacing="0" cellpadding="5"> <!-- <tr class="row2"> <th>Team</th> <th colspan="3">Move</th> <th> </th> </tr> -->""") the_campaign.get_sides_full(cursor) for s in range(1, the_campaign.sides+1): teams_on_side = the_campaign.sides_full[s] if len(teams_on_side) > 0: i = 0 output.append("<tr class='row2'><td colspan='5' style='text-align:center;'>Side %d</td></tr>" % s) for t in teams_on_side: i += 1 output.append(""" <tr class="row{i}"> <td>{team}</td> <td style='padding:0px;'> <!-- <a class="block_link" href="#" onclick="$.get('exec.py', {{mode: 'move_armies', team:{team_id}, campaign:{campaign}, location: ''}}, function () {{$('#ajax_result_{team_id}').html('Moved to location of final battle').val());}}); return false;">Final battle</a> --> <a class="block_link" href="exec.py?mode=move_armies&team={team_id}&campaign={campaign}">Final battle</a> </td> <td style='padding:0px;'> <a class="block_link" href="#" onclick="$.get('exec.py', {{mode: 'move_armies', team:{team_id}, campaign:{campaign}, location: $('#location_{team_id}').value}}, function () {{$('#ajax_result_{team_id}').html('Moved to ' + $('#location_{team_id}').val());}}); return false;">Location:</a> </td> <td style="padding:2px;"> <form action="exec.py" method="post" accept-charset="utf-8" id="team_form_{team_id}"> <input type="hidden" name="mode" value="move_armies" /> <input type="hidden" name="team" value="{team_id}" /> <input type="hidden" name="campaign" value="{campaign}" /> <input type="text" name="location" id="location_{team_id}" value="" size="6"/> </form> </td> <td id="ajax_result_{team_id}"> </td> </tr> """.format( i = i % 2, team_id = t['team'], team = team_dict[t['team']].name, campaign = campaign_id, )) output.append("</table><!-- PYEND -->") output.append("</div><!-- PY -->%s<!-- PYEND -->" % common.onload(""" $('#campaign_info').load('web.py',{'mode':'campaign_info','ajax':'True','campaign':'%d'}); $('#player_kills').load('web.py',{'mode':'player_kills','ajax':'True','campaign':'%d'}); """ % (campaign_id, campaign_id))) if ajax: output.append("<br />") return "".join(output)
def main(cursor, campaign_id=-1): output = ['<div style="padding: 5px;">'] grab_coords = common.get_val('coords', '') radius = int(common.get_val('radius', 10)) if grab_coords != '' and radius > 0: # 138, 1224 grabbed_armies = grab_armies(cursor, grab_coords, radius) else: grabbed_armies = "" campaign_id = int(common.get_val('campaign', campaign_id)) if campaign_id < 1: return "No city selected" # Get stuff from DB the_campaign = campaign_q.get_one_campaign(cursor, campaign_id) team_dict = team_q.get_all_teams(cursor) city_dict = city_q.get_cities_for_dropdown(cursor) battle_dict = battle_q.get_battles_from_campaign(cursor, campaign_id) if len(battle_dict) < 1: return common.redirect("web.py?mode=list_battles&campaign=%d" % campaign_id) # last_battle = battle_dict[list(battle_dict.keys())[-1]] last_battle = battle_q.get_last_battle_from_campaign(cursor, campaign_id) if not last_battle: return common.redirect("web.py?mode=list_battles&campaign=%d" % campaign_id) # coords_cities coords_cities = ['<select name="coords">'] for city_id, the_city in city_dict.items(): if city_id == last_battle.city: coords_cities.append("<option value='%s,%s' selected='selected'>%s</option>" % (the_city.x, the_city.y, the_city.name)) else: coords_cities.append("<option value='%s,%s'>%s</option>" % (the_city.x, the_city.y, the_city.name)) coords_cities.append("</select>") coords_cities = "".join(coords_cities) output.append(""" <a href="web.py?mode=list_campaigns&turn={turn}" class="block_link" style="display:inline; text-align:left;">Campaigns of this turn</a> <a href="web.py?mode=list_battles&campaign={id}" class="block_link" style="display:inline;">List battles</a> <a href="web.py?mode=perform_battle&battle={last_battle}" class="block_link" style="display:inline;">Last battle</a> <br /><br /> <form action="exec.py" id="mass_add_army_form" method="post" accept-charset="utf-8" style="display:block; float:right; border: 0px solid #000; margin-right:20px;"> <strong>Mass add units</strong><br /> <input type="hidden" name="mode" value="mass_add_armies" /> <input type="hidden" name="campaign" value="{id}" /> {team_list} <br /> <textarea name="army_names" rows="10" cols="40">{grabbed_armies}</textarea> <br /> <input type="submit" value="Add armies" /> <br /><br /> </form> <form action="web.py" id="grab_armies_form_cities" method="get" accept-charset="utf-8" style="display:block; float:right; border: 0px solid #000; margin-right:20px;"> <strong>Grab armies</strong><br /> <input type="hidden" name="mode" value="setup_campaign" /> <input type="hidden" name="campaign" value="{id}" /> <label for="coords">Coords: </label>{coords_cities}<br /> <label for="radius">Radius: </label><input type="text" name="radius" id="radius" value="10" /><br /> <input type="submit" value="Grab armies" /> <br /><br /> </form> <form action="web.py" id="grab_armies_form" method="get" accept-charset="utf-8" style="display:block; float:right; border: 0px solid #000; margin-right:20px;"> <strong>Grab armies</strong><br /> <input type="hidden" name="mode" value="setup_campaign" /> <input type="hidden" name="campaign" value="{id}" /> <label for="coords">Coords: </label><input type="text" name="coords" id="coords" value="{default_coods}" /><br /> <label for="radius">Radius: </label><input type="text" name="radius" id="radius" value="10" /><br /> <input type="submit" value="Grab armies" /> <br /><br /> </form> <form action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" id="mode" value="setup_campaign_commit" /> <input type="hidden" name="id" id="id" value="{id}" /> Editing: {name_text} <br /><br /> <table border="0" cellspacing="5" cellpadding="5"> <tr> <td><label for="turn">Turn:</label></td> <td style="padding: 1px;">{turn_text}</td> <td width="5"> </td> <td><label for="turn">Sides:</label></td> <td style="padding: 1px;">{sides_text}</td> </tr> </table> <br /> <input type="submit" value="Perform edit" /> </form> <br /><br />""".format( id = campaign_id, name = the_campaign.name, turn = the_campaign.turn, name_text = common.text_box("name", the_campaign.name, size=20), turn_text = common.text_box("turn", the_campaign.turn, size=5), sides_text = common.text_box("sides", the_campaign.sides, size=5), team_list = team_f.structured_list(cursor, include_irs=True), grabbed_armies = grabbed_armies, last_battle = last_battle.id, coords_cities = coords_cities, default_coods = "%s, %s" % (last_battle.x, last_battle.y), )) # Sides and teams side_form = ['<select id="side_menu">'] for s in range(1, the_campaign.sides+1): side_form.append('<option value="{s}">{s}</option>'.format(s=s)) side_form.append("</select>") side_form = "".join(side_form) # Form JS js = """ var side = $("#side_menu").attr("value"); var team_id = $("#new_team").attr("value"); var team_name = $("#new_team :selected").text(); team_list_content = $("#team_list_" + side).html(); if (team_list_content != "") { $("#team_list_" + side).html(team_list_content + ", " + team_name); } else { $("#team_list_" + side).html(team_name); } $("#ajax_target").load("exec.py", {mode: "add_team_to_campaign", campaign: %s, side: side, team: team_id}); $("#new_team").focus(); return false; """ % campaign_id js = js.replace("\t", "").replace("\n", "") output.append(''' <form action="" onsubmit='{js}' method="post" accept-charset="utf-8"> {team_list} {side} <input type="submit" value="Add" /> </form> <!-- PY --> <form action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="mode" value="edit_campaign_armies" /> <input type="hidden" name="campaign" value="{c}" /> <input type="submit" value="Apply armies" /> <!-- PYEND --> <table border="0" cellspacing="0" cellpadding="5" style="width:99%"> <tr class="row2"> <th width="15">#</th> <th>Teams</th> </tr> '''.format( c=campaign_id, team_list=team_f.structured_list(cursor, include_irs=True, field_id="new_team"), side=side_form, js=js, )) army_ids = [] the_campaign.get_sides_full(cursor) the_campaign.get_armies_full(cursor) campaign_armies = campaign_q.get_campaign_armies_from_turn(cursor, the_campaign.turn) teams_on_side_str = [] for s in range(1, the_campaign.sides+1): teams_on_side = the_campaign.sides_full[s] if len(teams_on_side) > 0: teams_on_side_str = ['<table border="0" cellspacing="0" cellpadding="5" width="100%">'] for t in teams_on_side: where = "campaign=%d and team=%d" % (campaign_id, t['team']) # Display "make secret" or "make public" public_display, secret_display = "", "" if t['secret']: secret_display = "display: none;" else: public_display = "display: none;" teams_on_side_str.append(""" <tr id="team_row_{t}" class="row1"> <td width="100">{name}</td> <td width="65"> {started} -> {finished} </td> <td>{remove}</td> <td> <a href="#" id="team_public_{t}" onclick="$('#ajax_target').load('web.py', {{mode:'campaign_team_public', team:{t}, campaign:{c}}}); {show_hide} return false;" class="mini_link" style="{public_display}">Make public</a> <a href="#" id="team_secret_{t}" onclick="$('#ajax_target').load('web.py', {{mode:'campaign_team_secret', team:{t}, campaign:{c}}}); {show_hide} return false;" class="mini_link" style="{secret_display}'">Make secret</a> </td> </tr> """.format( t = t['team'], c = campaign_id, name=team_dict[t['team']].name, started=common.doubleclick_text_full("campaign_teams", "started", where, t['started'], size=2), finished=common.doubleclick_text_full("campaign_teams", "finished", where, t['finished'], size=2), remove='''<a href="#" onclick='$("#ajax_target").load("exec.py", {mode: "remove_team_from_campaign", campaign: %d, team: %d, side: %d}); $("#team_row_%d").hide(); return false;'>Remove</a>''' % (campaign_id, t['team'], s, t['team']), show_hide = "$('#team_public_%d').toggle(); $('#team_secret_%d').toggle();" % (t['team'], t['team']), public_display = public_display, secret_display = secret_display, )) # Now to put in the armies form army_form = [] garrison_form = [] team_armies = army_q.get_armies_from_team(cursor, t['team'], include_garrisons=True) # the_campaign.get_armies_full for army_id, the_army in team_armies.items(): selected = False start_finish = "" if army_id in the_campaign.armies_full: selected = True where = "campaign=%d and army=%d" % (campaign_id, army_id) start_finish = "%s -> %s" % ( common.doubleclick_text_full("campaign_armies", "started", where, the_campaign.armies_full[army_id]['started'], size=2), common.doubleclick_text_full("campaign_armies", "finished", where, the_campaign.armies_full[army_id]['finished'], size=2)) # Is it selected? if selected: checked = "checked='checked'" rclass = "selected_army" else: checked = "" rclass = "" # Is it being used elsewhere this turn? elsewhere = "<td> </td>" if army_id in campaign_armies: if campaign_id in campaign_armies[army_id]: elsewhere_count = len(campaign_armies[army_id]) - 1 else: elsewhere_count = len(campaign_armies[army_id]) if elsewhere_count == 1: elsewhere = "<td style='background-color:#700;'> </td>" elif elsewhere_count == 2: elsewhere = "<td style='background-color:#B00;'> </td>" elif elsewhere_count > 2: elsewhere = "<td style='background-color:#F00;'> </td>" army_s = '''<tr id="row_{a}" class="{rclass}"> <td><label for="a_{a}" style="width:100%; display:block;">{name}</label></td> <td><input type="checkbox" name="a_{a}" id="a_{a}" value="True" onchange="if ($('#a_{a}').attr('checked')) {{$('#row_{a}').addClass('selected_army');}} else {{$('#row_{a}').removeClass('selected_army');}}" {checked}/></td> <td>{start_finish} </td> {elsewhere} </tr>'''.format( a = army_id, name = the_army.name, # cb = common.check_box("a_%d" % army_id, checked=selected), start_finish=start_finish, checked = checked, rclass = rclass, elsewhere = elsewhere, ) if the_army.garrison: garrison_form.append(army_s) else: army_form.append(army_s) army_ids.append(army_id) teams_on_side_str.append(""" <tr> <td colspan="4"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td width="300"> <table border="0" cellspacing="0" cellpadding="3"> {armies} </table> </td> <td> <table border="0" cellspacing="0" cellpadding="3"> {garrisons} </table> </td> <td> </td> </tr> </table> </td> </tr>""".format( armies = "".join(army_form), garrisons = "".join(garrison_form), c_id = campaign_id, team = t['team'], )) teams_on_side_str.append("</table>") output.append(""" <tr> <td colspan="2"><hr /></td> </tr> <tr> <td><strong>{s}</strong></td> <td> {teams} <br /> <span id="team_list_{s}"></span> </td> </tr> """.format( s=s, teams="".join(teams_on_side_str), # teams=", ".join([team_dict[t].name for t in teams_on_side]), )) # Field for the ids of the armies listed that we need to sort out output.append('<input type="hidden" name="army_ids" value="%s" />' % ",".join([str(a) for a in army_ids])) output.append('<!-- PY --><tr><td colspan="2"><input type="submit" value="Apply armies" /></td></tr><!-- PYEND -->') output.append('''</table> <!-- PY --></form><!-- PYEND --> <br />''') # Delete button output.append(""" <form id="delete_form" action="exec.py" method="post" accept-charset="utf-8"> <input type="hidden" name="campaign" id="campaign" value="{id}" /> <input type="hidden" name="mode" id="mode" value="remove_campaign" /> <input style="float:right; margin-right:100px;" type="button" value="Delete campaign" onclick="var answer = confirm('Delete {esc_name}?') if (answer) $('#delete_form').submit();" /> </form>""".format( id = campaign_id, esc_name = common.js_name(the_campaign.name), )) output.append(common.onload("$('#new_team').focus();")) output.append('<a href="web.py?mode=list_battles&campaign=%d" class="block_link">List battles</a>' % campaign_id) output.append("</div>") return "".join(output)