Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. def my_untie(url):
  2.     import challonge
  3.     from connect import my_connect
  4.  
  5.     err = my_connect(challonge, "LeFrenchMelee")
  6.  
  7.     from tools import url_parse
  8.     subdomain, name = url_parse(url)
  9.  
  10.     if subdomain:
  11.         name = subdomain+'-'+name
  12.  
  13.     tournament = challonge.tournaments.show(name)
  14.     tid = tournament["id"]
  15.    
  16.     from operator import itemgetter
  17.     participants = sorted(challonge.participants.index(tid), key=itemgetter("seed"))
  18.  
  19.     ranks = sorted(participants, key=itemgetter("final-rank"))
  20.  
  21.     from itertools import groupby
  22.     groups = groupby(ranks, itemgetter("final-rank"))
  23.     postoscore = {j[0]:i for i,j in enumerate(reversed(list(groups)))}
  24.  
  25.     tb = [0 for _ in participants]
  26.     buchholz = [[] for _ in participants]
  27.     diff = [0 for _ in participants]
  28.  
  29.     idtoseed = {p['id'] : p['seed'] for p in participants}
  30.     matches = challonge.matches.index(tid)
  31.     for m in matches:
  32.         if participants[idtoseed[m['loser-id']]-1]['final-rank'] == participants[idtoseed[m['winner-id']]-1]['final-rank']:
  33.             tb[idtoseed[m['winner-id']]-1] += 1
  34.  
  35.         buchholz[idtoseed[m['loser-id']]-1].append(postoscore[participants[idtoseed[m['winner-id']]-1]['final-rank']])
  36.         buchholz[idtoseed[m['winner-id']]-1].append(postoscore[participants[idtoseed[m['loser-id']]-1]['final-rank']])
  37.  
  38.         from tools import my_split, multi_split
  39.         score = multi_split(m['scores-csv']) if ',' in m['scores-csv'] else my_split(m['scores-csv'])
  40.         if score == ["", "{{win}}"]:
  41.             score = ["-1", "0"]
  42.         if score == ["{{win}}", ""]:
  43.             score = ["0", "-1"]
  44.  
  45.         diff[idtoseed[m['player1-id']]-1] += int(score[0])-int(score[1])
  46.         diff[idtoseed[m['player2-id']]-1] += int(score[1])-int(score[0])
  47.  
  48.     ranking = sorted(zip(participants, tb, buchholz, diff), key=lambda p:(p[0]['final-rank'], -p[1], -(sum(p[2])-min(p[2])-max(p[2])), -p[3]))
  49.     ranking_groups = groupby(ranking, lambda p:(p[0]['final-rank'], -p[1], -(sum(p[2])-min(p[2])-max(p[2])), -p[3]))
  50.  
  51.     r = 1
  52.     ranking_t = []
  53.     for _, l in ranking_groups:
  54.         l = list(l)
  55.         for p in l:
  56.             ranking_t.append((r, p[0]['name'], p[0]['final-rank'], p[1], sum(p[2])-min(p[2])-max(p[2]), p[3]))
  57.         r += len(l)
  58.    
  59.     from html import page, par, form, input, br, link, val, textarea, table, tr, td, th
  60.    
  61.     ranking_l = table(tr('\t'.join([th(i) for i in ['rank', 'name', 'official rank', 'TB', 'Buchholz', 'Pts Diff']]))+'\n'+
  62.                       '\n'.join([tr('\t'.join([td(str(p[0])), td(p[1])]+[td(str(i)) for i in p[2:]])) for p in ranking_t]))
  63.  
  64.     tournaments = challonge.tournaments.index()
  65.  
  66.     other_links = [t['full-challonge-url'] for t in tournaments]
  67.  
  68.     other_list = [link(l.split("/")[-1], "./untie?url="+l) for l in other_links]
  69.     other_l = br().join(other_list)
  70.  
  71.     my_form = form(input("url"), "Untie!")
  72.    
  73.     return page("Untie", par(val("name", name))+my_form+par(ranking_l)+par(other_l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement