def select_combatant(combatantId): #Makes sure combatantId is a digit if combatantId.isdigit(): pass else: #CGI Script checks to see if the function returned none, if it does it # prints out an appropriate error message in html return None #Making sure there isn't any negative numbers if int(combatantId) <= 0: return None connection = get_connection() cursor = connection.cursor() sql = """select combatant.id, combatant.name, species.name, type, (base_atk + plus_atk), (base_dfn + plus_dfn), (base_hp + plus_hp) from combatant, species where combatant.id = %s and species.id = species_id""" #We're only grabbing one row cursor.execute(sql, [combatantId]) row = cursor.fetchone() cursor.close() connection.close() return row
def name_lookup(lookup_id): connection = get_connection() cursor = connection.cursor() sql = """select name from combatant where id = %s""" cursor.execute(sql, [lookup_id]) name = cursor.fetchone() name = name[0] cursor.close() connection.close() return name
def print_combatants(): connection = get_connection() sql = "SELECT id, name FROM combatant" cursor = connection.cursor() cursor.execute(sql) #Format string stores the id as cid for cgi to pull it later name_link_fmt = """<a href='combatantid.py?cid={0}'>{1}</a>""" for combatantId, name in cursor.fetchall(): print("<br/>") print(name_link_fmt.format(combatantId, name)) cursor.close() connection.close()
def get_record(combatantId): connection = get_connection() cursor = connection.cursor() sql = """ select COUNT(combatant_one) as 'wins' from fight where winner_id = %s""" cursor.execute(sql, [combatantId]) wins = cursor.fetchone() wins = wins[0] sql = """select COUNT(combatant_one) as 'total' from fight where (combatant_one = %s or combatant_two = %s);""" variables = (combatantId, combatantId) cursor.execute(sql, variables) total = cursor.fetchone() total = total[0] cursor.close() connection.close() return (wins, total)
def print_ranks(): connection = get_connection() #Gets a count of how many times an Id has appeared in the winner_id column sql = """select winner_id, COUNT(winner_id) from fight group by winner_id order by count(winner_id) DESC;""" cursor = connection.cursor() cursor.execute(sql) print("<table style='width:40%'>") print("<tr>") #What standing the combatant is print("<th>#</th>") #WThe combatant's name print("<th>Combatant</th>") #How many wins the combatant has print("<th>Wins</th>") print("</tr>") #Displays the index with a dot index_fmt = """<td>{0}.</td>""" #format string used to generate a link to the combatant's detail page name_link_fmt = """<td><a href='combatantid.py?cid={0}'>{1}</a></td>""" #Simple format for displaying the wins nuber in <td> tags wins_fmt = """<td>{0}</td>""" #Iterates through each row and index keeps count of which row it's on for index, row in enumerate(cursor.fetchall()): combatant_name = name_lookup(row[0]) print("<tr>") #Plus on since there's no #0 spot print(index_fmt.format(index+1)) print(name_link_fmt.format(row[0], combatant_name)) print(wins_fmt.format(row[1])) print("</tr>") print("</table>") cursor.close() connection.close()
def print_battles(): connection = get_connection() #Pulling all the useful information from the fight table. TIMESTAMPDIFF is used to find # out how much time as elasped between the start and the finish time sql = """select combatant_one, combatant_two, winner_id, DATE_FORMAT(start, '%Y-%m-%d %T'), TIMESTAMPDIFF(SECOND, start, finish) from fight order by start;""" cursor = connection.cursor() cursor.execute(sql) print("<table style='width:80%'>") print("<tr>") #Setting up headers for the link to the match, the combatant's names, the victor, and date # information print("<th>Match Link</th>") print("<th>First Combatant</th>") print("<th>Second Combatant</th>") print("<th>Victor</th>") print("<th>Start Time</th>") print("<th>Match Length </th>") print("</tr>") #battle_link_fmt is used to generate a link which gives all the relevant information # battledetails.py needs to generate a battle detail pages battle_link_fmt = """<td style='text-align:center'><a href='battledetails.py?cmbt1={0}&cmbt2={1}&victor={2}&str_time={3}&length={4}'>{5}.</a></td>""" #a format string used to generate a link to the combatant's detail page name_link_fmt = """<td><a href='combatantid.py?cid={0}'>{1}</a></td>""" #A for loop that iterates through the rows and generates the correct table row info for index, row in enumerate(cursor.fetchall()): #Grabs the names based of the ids combatant_1 = name_lookup(row[0]) combatant_2 = name_lookup(row[1]) #Quick check to see which combatant won and assign the string to victor to avoid # another lookup if row[2] == row[0]: victor = combatant_1 elif row[2] == row[1]: victor = combatant_2 else: victor = "Error! Victor data corruption!" #Returns a formatted version of how many seconds difference there was between start and ed # of the match time. time_elapsed = time_string(row[4]) print("<tr>") print( battle_link_fmt.format(combatant_1, combatant_2, victor, row[3], time_elapsed, index + 1)) print(name_link_fmt.format(row[0], combatant_1)) print(name_link_fmt.format(row[1], combatant_2)) print(name_link_fmt.format(row[2], victor)) print("<td>" + row[3] + "</td>") print("<td>" + time_elapsed + "</td>") print("</tr>") print("</table>") cursor.close() connection.close()