Exemple #1
0
def run_draft(match_id, team_id):
    c = Cursor("emporium_draft")
    items = c.execute(ITEMS_FOR_DRAFT_QUERY, (match_id, team_id)).fetchall()
    for i, item in enumerate(items):
        if not i % 100:
            print "  processing item #%s" % i
        draft_item(c, match_id, team_id, item)
Exemple #2
0
def match_to_json(m, user=None):
    """
    Right now this function is a performance clusterfuck. Almost all of the data in
    here can be gathered with a single query and windowed multi-join, but lets wait
    until shit breaks, eh?
    """
    c = Cursor()

    if not isinstance(m, tuple):
        c.execute(MATCH_SELECT_SQL, (m, ))
        m = c.fetchone()

    if not m:
        raise InvalidRequestError("Failed to match_to_json with arg %s" % m)

    match = {}

    match['id'] = m.id
    match['game'] = m.game
    match['when'] = int(m.match_date.strftime("%s"))
    match['teams'] = []
    match['extra'] = {}
    match['stats'] = {}

    # This will most definitily require some f*****g caching at some point
    bet_stats = c.execute("""SELECT
            sum(array_length(items, 1)) as skins_count,
            count(*) as count,
            sum(value) as value,
            team
        FROM bets WHERE match=%s AND state >= 'confirmed' GROUP BY team""", (m.id, )).fetchall(as_list=True)

    match['stats']['players'] = sum(map(lambda i: i.count, bet_stats))
    match['stats']['skins'] = sum(map(lambda i: i.skins_count, bet_stats))
    match['stats']['value'] = sum(map(lambda i: i.value, bet_stats))
    bet_stats = { i.team: i for i in bet_stats}

    # Grab team information, including bets (this should be a join)
    c.execute("SELECT * FROM teams WHERE id IN %s", (tuple(m.teams), ))
    teams = c.fetchall()

    total_bets = sum(map(lambda i: i.count, bet_stats.values())) * 1.0
    total_value = sum(map(lambda i: i.value, bet_stats.values()))

    for index, team in enumerate(teams):
        team_data = {
            "id": team.id,
            "name": team.name,
            "tag": team.tag,
            "logo": team.logo,
            "stats": {
                "players": 0,
                "skins": 0,
                "value": 0,
            },
            "odds": 0
        }

        if index in bet_stats:
            team_data['stats']['players'] = bet_stats[index].count
            team_data['stats']['skins'] = bet_stats[index].skins_count
            team_data['stats']['value'] = bet_stats[index].value
            team_data['odds'] = float("{0:.2f}".format(bet_stats[index].count / total_bets))

        match['teams'].append(team_data)

    if user:
        match['me'] = {}

        mybet = c.execute("""
            SELECT id, team, items::steam_item[], state, value
            FROM bets WHERE match=%s AND better=%s
        """, (m.id, user)).fetchone()

        if mybet:
            match['me']['id'] = mybet.id
            match['me']['team'] = mybet.team
            match['me']['state'] = mybet.state
            match['me']['value'] = mybet.value
            match['me']['state'] = mybet.state

            if total_value > mybet.value:
                my_return = ((total_value * 1.0) / match['teams'][mybet.team]['stats']['value']) * mybet.value
            else:
                my_return = mybet.value

            match['me']['return'] = float("{0:.2f}".format(my_return))

            # Load items in sub query :(
            match['me']['items'] = []

            items = c.execute("SELECT id, name, price, meta FROM items WHERE id IN %s", (tuple(
                map(lambda i: i.item_id, mybet.items)), ))

            for item in items.fetchall():
                match['me']['items'].append({
                    "id": item.id,
                    "name": item.name,
                    "price": float(item.price),
                    "image": item.meta['image']
                })


    for key in ['league', 'type', 'event', 'streams', 'maps', 'note']:
        if key in m.meta:
            match['extra'][key] = m.meta[key]

    match['results'] = m.results
    return match