#!/usr/bin/python import scanf, re, math class StageBonus: def __init__(self,first,second,third): self.first = first self.second = second self.third = third class Riders: def __init__(self,name,place,team,time,bonus,proj_bonus): self.name = name self.place = place self.team = team self.time = time self.bonus = bonus # will be the adjusted time for bonuses, in seconds self.proj_bonus = proj_bonus class Teams: def __init__(self,name,list,totalTime,projtotalTime,index): self.name = name self.list = list self.totalTime = totalTime self.projtotalTime = projtotalTime self.projRank = 0 self.index = index class JerseyBonus: def __init__(self,green1,green2,green3,polka1,polka2,polka3): self.green1 = green1 self.green2 = green2 self.green3 = green3 self.polka1 = polka1 self.polka2 = polka2 self.polka3 = polka3 # pick the top (smallest time) six for each team, we'll just reorder their positions in place def pickTeams(teamsTemp,ridersTemp): for team in teamsTemp: # we'll sort by the real time team.list.sort(lambda x, y: cmp(ridersTemp[x].time,ridersTemp[y].time)) return teamsTemp # rank the teams by the top six riders in each team, moving them inside teams def rankTeams(teamsTemp,ridersTemp): tempList = [] tempTeams = [] for j,team in enumerate(teamsTemp): totalTime = 0 projtotalTime = 0 bonus = 0 proj_bonus = 0 for i in range(6): totalTime += ridersTemp[team.list[i]].time projtotalTime += ridersTemp[team.list[i]].time #print 'index ' + str(team.index) + ' rider bib ' + str(team.list[i]) + ' real time ' + str(convertTime(ridersTemp[team.list[i]].time)) # calculate the bonus time for this team, it will be a negative number for bib in team.list: bonus += ridersTemp[bib].bonus proj_bonus += ridersTemp[bib].proj_bonus #print 'bonus ' + str(bonus) # apply it to totalTime totalTime = totalTime + bonus projtotalTime = projtotalTime + proj_bonus teamsTemp[j].totalTime = totalTime teamsTemp[j].projtotalTime = projtotalTime tempList.append([j,totalTime]) #print 'rankTeams ' + str(team.index) + ' ' + str(convertTime(totalTime)) # now sort the tempList by second index tempList.sort(lambda x,y: cmp(x[1],y[1])) # reorder teams for item in tempList: tempTeams.append(teamsTemp[item[0]]) # copy it back teamsTemp = tempTeams # now sort by projected time, and store it in .projRank tempList = [] for j,team in enumerate(teamsTemp): tempList.append([j,team.projtotalTime]) tempList.sort(lambda x,y: cmp(x[1],y[1])) for i,item in enumerate(tempList): teamsTemp[item[0]].projRank = i return teamsTemp def quickPrint(teams,quickfp): line = str('Rank').ljust(5) + str('Draft Order').ljust(12) + \ str('Team').ljust(20) + str('Time (w/bonus)').ljust(15) + \ str('Gap to 1st').ljust(15) + str('Gap to next').ljust(15) + \ str('Proj Rank').ljust(13) + str('Proj Gap to 1st') + '\n' quickfp.write(line) min_projtime = 100000000000 for team in teams: if team.projtotalTime < min_projtime: min_projtime = team.projtotalTime min_index = team.index for i, team in enumerate(teams): line = str(i+1).ljust(5) + str(team.index+1).ljust(12) + str(team.name).ljust(20) timeString = convertTime(team.totalTime) if (i==0): firstTime = team.totalTime lastTime = team.totalTime if (i>0): firstDiff = team.totalTime - firstTime if (i>0): timeDiff = team.totalTime - lastTime lastTime = team.totalTime #print str(team.index) + ' ' + str(team.totalTime) if (i==0): line = line + str(timeString).ljust(45) if (i>0): line = line + str(timeString).ljust(15) #quickfp.write(line) if (i>0): timeString = convertTime(firstDiff) line = line + str(timeString).ljust(15) #quickfp.write(line) if (i>0): timeString = convertTime(timeDiff) #line = ' The time to next is: %s.\n' % timeString line = line + str(timeString).ljust(15) # projected rank line = line + str(team.projRank+1).ljust(13) if team.index != min_index: timeString = convertTime(team.projtotalTime-min_projtime) else: timeString = '' line = line + str(timeString).ljust(22) line = line + '\n' quickfp.write(line) def writeBonuses(stageBonuses,riders,bonusfp,teams): for j,stage in enumerate(stageBonuses): line = 'Stage ' +str(j) +'\n' bonusfp.write(line) # This is really ghetto but I don't care name1 = 'No Team' name2 = 'No Team' name3 = 'No Team' if stage.first != 888: for team in teams: if stage.first in team.list: name1 = team.name if stage.second in team.list: name2 = team.name if stage.third in team.list: name3 = team.name line = 'First: %s (%s)\n' % (riders[stage.first].name, name1) bonusfp.write(line) line = 'Second: %s (%s)\n' % (riders[stage.second].name, name2) bonusfp.write(line) line = 'Third: %s (%s)\n\n' % (riders[stage.third].name, name3) bonusfp.write(line) else: line = 'No bonuses.\n\n' bonusfp.write(line) def printTeams(teams,riders,finalfp): # find the lowest time minTime = 10e10 for rider in riders: if (riders[rider].place == 1): minTime = riders[rider].time for i,team in enumerate(teams): line = '%s is ranked %d\n' % (team.name, i+1) # name, rank number finalfp.write(line) timeString = convertTime(team.totalTime) finalfp.write('The top six riders time total (w/bonus): %s\n' % timeString) line = str('pos').ljust(4) + str('bib').ljust(4) + str('Name').ljust(30) + str('Team').ljust(35) \ + str('Real Time').ljust(13) + str('Behind Leader').ljust(15) + str('Bonus Time') + '\n' finalfp.write(line) for i,rider in enumerate(team.list): if (i==6): line = '-------------------------------------------------------------------------------------------------------------' finalfp.write(line + '\n') line = str(riders[rider].place).ljust(4) + str(rider).ljust(4) + str(riders[rider].name).ljust(30) + str(riders[rider].team).ljust(35) \ + str(convertTime(riders[rider].time)).ljust(13) + str(convertTime(riders[rider].time - minTime)).ljust(15) + str(convertTime(riders[rider].bonus)) finalfp.write(line + '\n') finalfp.write('\n\n') def printToGraph(teamsTemp,graphfp,stage): line = str(stage).ljust(3) diffs = {} for i, team in enumerate(teamsTemp): timeString = convertTime(team.totalTime) #print timeString if (i==0): firstTime = team.totalTime diffs[team.index] = 0 if (i>0): diffs[team.index] = team.totalTime - firstTime for i in range(len(teamsTemp)): for j in range(len(teamsTemp)): if (i == j): line = line + str(float(diffs[j])/60.).ljust(15) line = line + '\n' graphfp.write(line) def convertTime(time): timeFlag = 0 if (time < 0): timeFlag = 1 time = -time hours = int(time/3600) minutes = int((time - hours * 3600)/60) seconds = int((time - hours * 3600 - minutes * 60)) if (timeFlag == 1): if (hours > 0): hours = -hours else: minutes = -minutes string = '%dh %dm %ds' % (hours,minutes,seconds) return string def readRiders(HTMLname,ridersTemp): HTMLfp = open(HTMLname, 'r') HTMLline = HTMLfp.readline() tbodyflag = 0 #ridersTemp ={} maxtime = 0 while HTMLline: # skip past the table header while ((tbodyflag == 0) and HTMLline.count('tbody') == 0): HTMLline = HTMLfp.readline() HTMLline = tabs.subn('',HTMLline)[0] HTMLline = apos.subn('\'',HTMLline)[0] if (HTMLline.count('tbody')): tbodyflag = 1 if (HTMLline.count('col_1')): placetemp = re.split('>|<',HTMLline)[2] place = int(float(placetemp)) if (HTMLline.count('col_2')): #print HTMLline nametemp = re.split('>|<',HTMLline) name = nametemp[4] if (HTMLline.count('col_3')): bibtemp = re.split('>|<',HTMLline) bib = int(bibtemp[4]) if (HTMLline.count('col_4')): teamtemp = re.split('>|<',HTMLline) team = teamtemp[2] if (HTMLline.count('col_5')): timetemp = re.split('>|<',HTMLline)[2] try: timetemp = scanf.sscanf(timetemp,'%dh %d\' %d"') time = 3600 * timetemp[0] + 60 * timetemp[1] + timetemp[2] # get the time in total seconds except: timetemp = scanf.sscanf(timetemp,'%d\' %d"') time = 60 * timetemp[0] + timetemp[1] if (time > maxtime): maxtime = time if (HTMLline.count('