Exemplo n.º 1
0
def load_season(season=None, league_only=False):
    if season is None:
        where = 'start. start_date <= UNIX_TIMESTAMP() AND `end`.start_date > UNIX_TIMESTAMP()'
    else:
        try:
            number = int(season)
            code = "'{season}'".format(season=season)
        except ValueError:
            number = 0
            code = sqlescape(season)
        where = 'start.`number` = {number} OR start.`code` = {code}'.format(
            number=number, code=code)
    sql = """
        SELECT start.`number`, start.`code`, `start`.start_date, `end`.start_date AS end_date
        FROM season AS `start`
        LEFT JOIN season AS `end`
        ON `start`.`number` + 1 = `end`.`number`
        WHERE {where}
    """.format(where=where)
    season = Container(guarantee.exactly_one(db().execute(sql)))
    where = 'd.created_date >= {start_ts}'.format(start_ts=season.start_date)
    if season.end_date:
        where = '{where} AND d.created_date < {end_ts}'.format(
            where=where, end_ts=season.end_date)
    if league_only:
        where = "{where} AND d.competition_id IN ({competition_ids_by_type_select})".format(
            where=where,
            competition_ids_by_type_select=query.
            competition_ids_by_type_select('League'))
    season.decks = load_decks(where)
    season.start_date = dtutil.ts2dt(season.start_date)
    season.end_date = dtutil.ts2dt(
        season.end_date) if season.end_date else None
    return season
Exemplo n.º 2
0
def load_card(name):
    c = guarantee.exactly_one(oracle.load_cards([name]))
    c.decks = deck.load_decks(
        'd.id IN (SELECT deck_id FROM deck_card WHERE card = {name})'.format(
            name=sqlescape(name)))
    c.season = Container()
    c.all = Container()
    c.all.wins = sum(filter(None, [d.wins for d in c.decks]))
    c.all.losses = sum(filter(None, [d.losses for d in c.decks]))
    c.all.draws = sum(filter(None, [d.draws for d in c.decks]))
    if c.all.wins or c.all.losses or c.all.draws:
        c.all.win_percent = round(
            (c.all.wins / (c.all.wins + c.all.losses)) * 100, 1)
    else:
        c.all.win_percent = ''
    c.all.num_decks = len(c.decks)
    season_decks = [
        d for d in c.decks if d.created_date > rotation.last_rotation()
    ]
    c.season.wins = sum(filter(None, [d.wins for d in season_decks]))
    c.season.losses = sum(filter(None, [d.losses for d in season_decks]))
    c.season.draws = sum(filter(None, [d.draws for d in season_decks]))
    if c.season.wins or c.season.losses or c.season.draws:
        c.season.win_percent = round(
            (c.season.wins / (c.season.wins + c.season.losses)) * 100, 1)
    else:
        c.season.win_percent = ''
    c.season.num_decks = len(season_decks)
    c.played_competitively = c.all.wins or c.all.losses or c.all.draws
    return c
Exemplo n.º 3
0
def load_season(season=None):
    if season is None:
        where = 'start. start_date <= UNIX_TIMESTAMP() AND `end`.start_date > UNIX_TIMESTAMP()'
    else:
        try:
            number = int(season)
            code = "'{season}'".format(season=season)
        except ValueError:
            number = 0
            code = sqlescape(season)
        where = 'start.`number` = {number} OR start.`code` = {code}'.format(
            number=number, code=code)
    sql = """
        SELECT start.`number`, start.`code`, `start`.start_date, `end`.start_date AS end_date
        FROM season AS `start`
        LEFT JOIN season AS `end`
        ON `start`.`number` + 1 = `end`.`number`
        WHERE {where}
    """.format(where=where)
    season = Container(guarantee.exactly_one(db().execute(sql)))
    season.decks = load_decks(
        'd.created_date >= {start_ts} AND d.created_date < IFNULL({end_ts}, 999999999999)'
        .format(start_ts=season.start_date, end_ts=season.end_date))
    season.start_date = dtutil.ts2dt(season.start_date)
    season.end_date = dtutil.ts2dt(season.end_date)
    return season
Exemplo n.º 4
0
def adjust_elo(winning_deck_id: int, losing_deck_id: int) -> None:
    if not losing_deck_id:
        return  # Intentional draws do not affect Elo.
    winner = guarantee.exactly_one(
        person.load_people(
            'p.id IN (SELECT person_id FROM deck WHERE id = {winning_deck_id})'
            .format(winning_deck_id=sqlescape(winning_deck_id))))
    loser = guarantee.exactly_one(
        person.load_people(
            'p.id IN (SELECT person_id FROM deck WHERE id = {losing_deck_id})'.
            format(losing_deck_id=sqlescape(losing_deck_id))))
    adj = adjustment(winner.elo or STARTING_ELO, loser.elo or STARTING_ELO)
    sql = 'UPDATE person SET elo = IFNULL(elo, {starting_elo}) + %s WHERE id = %s'.format(
        starting_elo=sqlescape(STARTING_ELO))
    db().execute(sql, [adj, winner.id])
    db().execute(sql, [-adj, loser.id])
Exemplo n.º 5
0
def load_person(person):
    try:
        person_id = int(person)
        username = "******".format(person=person)
    except ValueError:
        person_id = 0
        username = sqlescape(person)
    return guarantee.exactly_one(load_people('p.id = {person_id} OR p.mtgo_username = {username}'.format(person_id=person_id, username=username)))
Exemplo n.º 6
0
def active_league():
    where = 'c.id = ({id_query})'.format(id_query=active_competition_id_query())
    leagues = competition.load_competitions(where)
    if len(leagues) == 0:
        start_date = dtutil.now(tz=dtutil.WOTC_TZ)
        end_date = determine_end_of_league(start_date)
        name = determine_league_name(end_date)
        comp_id = competition.get_or_insert_competition(start_date, end_date, name, 'League', None)
        leagues = [competition.load_competition(comp_id)]
    return guarantee.exactly_one(leagues)
Exemplo n.º 7
0
def load_person(person: Union[int, str],
                season_id: Optional[int] = None) -> Person:
    try:
        person_id = int(person)
        username = "******".format(person=person)
    except ValueError:
        person_id = 0
        username = sqlescape(person)
    return guarantee.exactly_one(
        load_people(
            'p.id = {person_id} OR p.mtgo_username = {username} OR p.discord_id = {person_id}'
            .format(person_id=person_id, username=username),
            season_id=season_id))
Exemplo n.º 8
0
def load_card(name: str, season_id: Optional[int] = None) -> Card:
    c = guarantee.exactly_one(oracle.load_cards([name]))
    c.decks = deck.load_decks('d.id IN (SELECT deck_id FROM deck_card WHERE card = {name})'.format(name=sqlescape(name)), season_id=season_id)
    c.all = Container()
    c.all_wins = sum(filter(None, [d.wins for d in c.decks]))
    c.all_losses = sum(filter(None, [d.losses for d in c.decks]))
    c.all_draws = sum(filter(None, [d.draws for d in c.decks]))
    if c.all_wins or c.all_losses:
        c.all_win_percent = round((c.all_wins / (c.all_wins + c.all_losses)) * 100, 1)
    else:
        c.all_win_percent = ''
    c.all_num_decks = len(c.decks)
    c.played_competitively = c.all_wins or c.all_losses or c.all_draws
    return c
Exemplo n.º 9
0
def active_league():
    where = 'c.id = ({id_query})'.format(
        id_query=active_competition_id_query())
    leagues = competition.load_competitions(where)
    if len(leagues) == 0:
        start_date = datetime.datetime.combine(
            dtutil.now().date(), datetime.time(tzinfo=dtutil.WOTC_TZ))
        end_date = determine_end_of_league(start_date)
        name = "League {MM} {YYYY}".format(
            MM=calendar.month_name[end_date.month], YYYY=end_date.year)
        comp_id = competition.get_or_insert_competition(
            start_date, end_date, name, 'League', None)
        leagues = [competition.load_competition(comp_id)]
    return guarantee.exactly_one(leagues)
Exemplo n.º 10
0
def league_run_api(person):
    decks = league.active_decks_by(person)
    if len(decks) == 0:
        return return_json(None)

    run = guarantee.exactly_one(decks)

    decks = league.active_decks()
    already_played = [m.opponent_deck_id for m in deck.get_matches(run)]
    run.can_play = [
        d.person for d in decks
        if d.person != person and d.id not in already_played
    ]

    return return_json(run)
Exemplo n.º 11
0
def drop(person):
    error = validate_api_key()
    if error:
        return error

    decks = league.active_decks_by(person)
    if len(decks) == 0:
        return return_json(
            generate_error('NO_ACTIVE_RUN',
                           'That person does not have an active run'))

    run = guarantee.exactly_one(decks)

    league.retire_deck(run)
    result = {'success': True}
    return return_json(result)
Exemplo n.º 12
0
def delete_match(match_id):
    m = guarantee.exactly_one(
        load_matches('m.id = {match_id}'.format(match_id=sqlescape(match_id))))
    db().begin()
    sql = 'DELETE FROM deck_match WHERE match_id = ?'
    db().execute(sql, [m.id])
    sql = 'DELETE FROM `match` WHERE id = ?'
    db().execute(sql, [m.id])
    if m.winner:
        sql = 'UPDATE deck SET wins = wins - 1 WHERE id = ?'
        db().execute(sql, [m.winner])
        sql = 'UPDATE deck SET losses = losses - 1 WHERE id = ?'
        db().execute(sql, [m.loser])
    else:
        sql = 'UPDATE deck SET draws = draws - 1 WHERE id IN (?, ?)'
        db().execute(sql, [m.left_id, m.right_id])
    db().commit()
Exemplo n.º 13
0
def load_deck(deck_id) -> Deck:
    return guarantee.exactly_one(
        load_decks('d.id = {deck_id}'.format(deck_id=sqlescape(deck_id))))
def load_competition(competition_id):
    return guarantee.exactly_one(load_competitions('c.id = {competition_id}'.format(competition_id=sqlescape(competition_id))))
Exemplo n.º 15
0
def associate(d, discord_id):
    person = guarantee.exactly_one(load_people('d.id = {deck_id}'.format(deck_id=sqlescape(d.id))))
    sql = 'UPDATE person SET discord_id = %s WHERE id = %s'
    return db().execute(sql, [discord_id, person.id])