def main(cursor): team_id = int(common.get_val("team", 0)) turn = int(common.get_val("turn", -1)) if team_id < 1: return "<div style='padding: 5px;'>%s</div>" % common.select_team_form(cursor, "view_orders") if turn < 1: turn = common.current_turn() the_world = world.World(cursor) player_dict = the_world.players() the_team = team_q.get_one_team(cursor, team_id) the_orders = the_team.get_orders(cursor, the_world, "international", turn) # output = ["<div style='padding: 5px; font-size: 14px; line-height: 17px;'>"] output = ["<div style='padding: 5px;'>"] player_updates = {} for o in the_orders: player_updates[o.player] = o.team if o.content == "Orders placeholder": continue output.append( '<br /><hr />Post: <a href="http://woarl.com/board/viewtopic.php?p=%s#p%s">%s</a><br />' % (o.post_id, o.post_id, o.post_id) ) output.append( 'Poster: <a href="http://woarl.com/board/memberlist.php?mode=viewprofile&u=%d">%s</a> - ' % (o.player, player_dict[o.player].name) ) output.append('<a href="web.py?mode=edit_player&player=%d">Local edit</a><br />' % (o.player)) output.append(common.bbcode_to_html(o.content.replace('<span class="stitle">', '<hr /><span class="stitle">'))) output.append("</div>") # Update player activity if turn == common.current_turn(): database.query(cursor, player_f.update_player_activity(player_updates)) # print "<br />".join(output) page_data["Title"] = "%s International orders" % the_team.name return "".join(output)
def main(cursor): team_id = int(common.get_val("team", 0)) turn = int(common.get_val("turn", -1)) the_world = world.World(cursor) the_world.prep_for_orders() if team_id < 1: return "<div style='padding: 5px;'>%s</div>" % common.select_team_form(cursor, 'view_orders') if turn < 1: turn = common.current_turn() # the_team = team_q.get_one_team(cursor, team_id) # the_team.get_resources(cursor) # the_team.get_spells(cursor) # the_team.get_techs(cursor) the_team = the_world.teams()[team_id] the_orders = the_team.get_orders(cursor, the_world, "normal", turn) output = ["<div style='padding:1px;'>"] player_updates = {} blocks = [] for o in the_orders: o.split() blocks.extend(o.blocks) # Setup for b in blocks: b.setup() # Execution for b in blocks: b.execute() for b in blocks: # Player activity needs to get updated if turn == common.current_turn(): player_updates[b.post.player] = b.post.team output.append(""" <div class="orders" style="border-color:{border};background-color:{background};"> <strong>{title}</strong>: {cost}<br /> {response} <br /> <div style="float: left; width: 50%;"> <textarea rows="6" style="float:left;width:99%;">{results}</textarea> </div> <div style="float: left; width: 50%;"> <textarea rows="6" style="float:right;width:99%;">{queries}</textarea> </div> <div style="clear: left;"> </div> </div> """.format( title = b.title_name, cost = str(b.cost), response = common.bbcode_to_html("<br />".join(b.input_response)), results = "\n".join(b.results), queries = "\n".join(b.queries), border = b.border_colour, background = b.background_colour, )) # output.append(""" # Time taken: %(total)s<br /><br /> # %(joined)s # """ % { # "total": total_time, # "joined": "<br />".join(time_list), # }) # # output.append(""" # <a href="#" id="all_q_link" style="float: right; padding-right: 10px;" onclick="$('#all_q_div').load('web.py', {'ajax':'True','mode':'run_queries','subject':'Normal orders','queries':$('#all_q_text').val()}); $('#all_q_link').hide(); return false;">Run queries</a> # <br /> # <textarea name="Name" id="Name" rows="8" style="width: 49%%;">%s</textarea> # <div style="float: right; width: 49%%; padding-right: 5px;" id="all_q_div"> # <textarea id="all_q_text" name="Name" id="Name" rows="8" style="width: 100%%;">%s</textarea> # </div> # """ % ("\n".join(total_results), "\n".join(total_queries))) output.append("</div>") # Update player activity if turn == common.current_turn(): database.query(cursor, player_f.update_player_activity(player_updates)) page_data['Title'] = "%s normal orders" % the_team.name return "".join(output)
def run_orders(options): start_time = time.time() cursor = database.get_cursor() cursor.track_queries = True the_world = spy_world.Spy_world(cursor) team_dict = the_world.active_teams() # Run orders #------------------------ print(database.shell_text("Running orders"), end="") queries, orders, debug = [], ["Team orders\n"], [] the_world.prep_for_orders() try: player_updates = {} the_orders = order_post_f.get_turn_orders(cursor, the_world) blocks = [] # for o in the_orders: for o in cli_f.progressbar(the_orders, "Splitting: ", 60, True): o.split() blocks.extend(o.blocks) # Setup # for b in blocks: for b in cli_f.progressbar(blocks, "Setting up: ", 60, True): b.setup() b.always_debug = True # Execution, in order of priority for priority in order_block.priorities: c = 0 for b in blocks: if b.priority != priority: continue try: b.execute() except Exception as e: print("") print(b.title_name) print(the_world.teams()[b.team].name) print("\n".join(cursor.queries[-5:-1])) print("") raise # Ensure we've handled all blocks for b in blocks: if not b.handled: raise Exception("Block with priority '%s' not handled" % b.priority) team_output = {} manual_output = {} team_debug = {} team_failures = {} for b in cli_f.progressbar(blocks, "Running Blocks: ", 60, True): # for b in blocks: if b.team not in team_output: team_output[b.team] = [] if b.team not in team_debug: team_debug[b.team] = [] if b.team not in team_failures: team_failures[b.team] = [] if b.team not in manual_output: manual_output[b.team] = [] # Player activity needs to get updated player_updates[b.post.player] = b.post.team # team_output[b.team].append("[o]%s[/o]" % b.title_name) team_output[b.team].append("\n".join(b.results)) team_output[b.team].append("") if b.manual_handle: manual_output[b.team].append("\n".join(b.results)) manual_output[b.team].append("") # Failures if len(b.failures) > 0: team_failures[b.team].append("\n".join(b.failures)) team_failures[b.team].append("") # Debug team_debug[b.team].append(b.debug[0]) team_debug[b.team].append("\n---\n".join(b.debug[1:len(b.debug)])) team_debug[b.team].append("") # Foreign results for team_id, res in b.foreign_results.items(): if team_id not in team_output: team_output[team_id] = [] team_output[team_id].insert(0, "") team_output[team_id].insert(0, "\n".join(res)) # Queries for team_id, fqueries in b.foreign_queries.items(): queries.extend(fqueries) queries.extend(b.queries) for team_id, the_team in team_dict.items(): if the_team.ir: continue orders.append("""# %s ###################################################################### [fullbox=#EEF,#AAF][h4]Turn %d Results[/h4][/fullbox] [url=http://woarl.com/stats/%s.html]Rob results[/url] """ % (the_team.name, common.current_turn(), team_f.team_hash(the_team.name))) if team_id in team_output: if team_id in manual_output: # orders.extend(team_output[team_id]) orders.extend(manual_output[team_id]) if team_id in team_debug: debug.extend(team_debug[team_id]) # Update player activity database.query(cursor, player_f.update_player_activity(player_updates)) # Save results results_f.save_results(cursor, team_output) results_f.save_failures(cursor, team_failures) # Now we work out the costs team_costs = {} for t, the_team in team_dict.items(): r = res_dict.Res_dict(the_team.resources) queries.extend(r.make_set_queries(t)) # Write queries to file f = open('%s/script_output/queries.sql' % common.data['server_fpath'], 'w') f.write("\n".join(queries)) f.close() # Write orders to file f = open('%s/script_output/orders.txt' % common.data['server_fpath'], 'w') f.write("\n".join(orders)) f.close() # Write debug to file f = open('%s/script_output/debug.txt' % common.data['server_fpath'], 'w') f.write("Team orders\n\n") f.write("\n".join(debug)) f.close() except Exception as e: print(database.shell_text("[r]Failure[/r]")) print(database.shell_text("[r]Re run as 'rob3 start -l True[/r]'")) raise # print(database.shell_text(" - [g]Done[/g]")) # os.system('mate %s' % '%s/queries.sql' % common.data['server_fpath']) # os.system('mate %s' % '%s/orders.txt' % common.data['server_fpath']) # Failable orders failable = ( re.compile(r'DELETE FROM squads WHERE id = [0-9]*;?'), ) failed_queries = [] # Execute order queries #------------------------ # print(database.shell_text("Executing order queries"), end="") for q in cli_f.progressbar(queries, "Running queries: ", 60, True): # for q in queries: if q == '': continue if q[0:2] == '--': continue try: cursor.execute(q) except Exception as e: ignorable = False for f in failable: if f.search(q) != None: ignorable = True failed_queries.append(q) if not ignorable: for f in failable: print("") print(f.search(q)) print("") cursor.execute('ROLLBACK') print(database.shell_text(" - [r]Failure[/r]")) print("Query: %s\n" % q) print(database.shell_text("[r]Re run as 'rob3 start -l True'[/r]")) raise # print(database.shell_text(" - [g]Done[/g]")) if len(failed_queries) > 0: print("Failed queries") print("\n".join(failed_queries)) # Build up a dict of the queries query_dict = {} for q in cursor.queries: if q in ("BEGIN", "COMMIT", "ROLLBACK"): continue if q not in query_dict: query_dict[q] = 0 query_dict[q] += 1 # What's our most popular query? pop_count, pop_query = 0, "" for q, c in query_dict.items(): if c > pop_count: pop_count = c pop_query = q print("\n\n--- Info ---") print("Time taken: %s" % str(round(time.time() - start_time, 3))[0:5]) print("Queries: %d" % len(cursor.queries)) print("Uniques: %d" % len(set(cursor.queries))) print("Most queried: %s (%d)" % (pop_query, pop_count)) # Verbose mode #------------------------ if options.verbose: cursor.execute("ROLLBACK") print("Rolled back") else: cursor.execute("COMMIT") print("Startup scripts executed") os.system("open %s/script_output/orders.txt" % common.data['server_fpath'])