Beispiel #1
0
def view_player(id):
    session = Session()

    player = session.query(Player).filter(Player.id == id).one()
    players = session.query(Player).all()
    decks = session.query(Deck).all()

    pmap = {}
    for d in players:
        pmap[d.id] = d

    dmap = {}
    for d in decks:
        dmap[d.id] = d

    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking()
    rank_data = ranking.get_player_ranking(id)

    table = rank_manager.ranking_table([rank_data], True)

    games_data, decks_data = get_player_info(id)

    titles = rank_manager.get_titles(id)

    return render_template('players/view_player.html',
                           player=player,
                           players=pmap,
                           decks=dmap,
                           ranking_table=table,
                           games_data=games_data,
                           decks_data=decks_data,
                           titles=titles)
Beispiel #2
0
def send_player_ranking(ranking_data, block_name, titles):
    session = Session()
    players = session.query(Player).all()

    team = {}

    for player in players:
        name = player.name
        if player.nickname is not None:
            name = player.nickname

        team[player.id] = {'name': name}

    admin = request.args.get('admin', '') == 'True'

    rank_manager = RankingManager()
    table = rank_manager.ranking_table(ranking_data, True)

    for id in team:
        player = team[id]
        player['link'] = '/players/profile/%s' % id

    return render_template('players/index.html',
                           admin=admin,
                           rank_table=table,
                           teams=team,
                           block_name=block_name,
                           titles=titles)
Beispiel #3
0
def update_game(gameId):
    payload = request.json
    params = ['p1Wins', 'p2Wins']
    for param in params:
        if param not in payload:
            abort(400)

    p1_wins = payload['p1Wins']
    p2_wins = payload['p2Wins']

    if not 0 <= p1_wins <= 2:
        abort(400)

    if not 0 <= p2_wins <= 2:
        abort(400)

    session = Session()
    game = session.query(Game).filter(Game.id == gameId).one()
    game.p1_wins = p1_wins
    game.p2_wins = p2_wins
    session.add(game)
    session.commit()

    RankingManager().refresh()

    return make_response()
Beispiel #4
0
def new_tournament():
    payload = request.json
    params = ['name', 'type', 'players', 'tier']
    for param in params:
        if param not in payload:
            abort(400)

    name = payload['name']
    type = payload['type']
    players = payload['players']
    tier = payload['tier']

    if name.strip() == '':
        abort(400)

    if tier.strip() == '':
        abort(400)

    if tier not in ['T1', 'T2', 'T3']:
        abort(400)

    if len(players) < 4:
        abort(400)

    ttype = TournamentType(type)

    if ttype is None:
        abort(400)

    manager = TournamentManager()
    manager.new_tournament(ttype, name, players, tier)

    RankingManager().refresh()

    return make_response()
Beispiel #5
0
def process_single_tournament(session, tournament):
    tid = tournament.id

    participants = session.query(Participant).filter(
        Participant.tournament_id == tid).all()
    games = session.query(Game).filter(Game.tournament_id == tid).order_by(
        Game.id.asc()).all()

    players, decks = get_players_and_decks(session)

    args = {}
    args['admin'] = request.args.get('admin', '') == 'True'
    args['tournament'] = tournament
    args['rounds'] = helper.group_by_round(games)
    args['teams'] = get_teams_data(participants, players, decks)

    rankingManager = RankingManager()
    table = rankingManager.ranking_table(
        rankingManager.get_tournament_ranking(tid))

    args['rank_table'] = table

    return 'tournaments/single_tournament.html', args
Beispiel #6
0
def index_view():
    session = Session()
    decks = session.query(Deck).all()

    team = {}

    for deck in decks:
        _class = ''
        if deck.status == 'inactive':
            _class = 'blue-grey lighten-5'
        team[deck.id] = {'name': deck.name, '_class': _class}

    admin = request.args.get('admin', '') == 'True'

    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking()

    table = rank_manager.ranking_table(ranking.decks, True)

    return render_template('decks/index.html',
                           admin=admin,
                           rank_table=table,
                           teams=team)
Beispiel #7
0
def process_thg_tournament(session, tournament):
    tid = tournament.id

    participants = session.query(Participant).filter(
        Participant.tournament_id == tid).all()
    games = session.query(Game).filter(Game.tournament_id == tid).order_by(
        Game.id.asc()).all()

    players, decks = get_players_and_decks(session)

    players_data = {}
    for p in participants:
        players_data[p.player_id] = {
            'name': players[p.player_id].name,
            'deck': decks[p.deck_id].name
        }
        players_data[p.player2_id] = {
            'name': players[p.player2_id].name,
            'deck': decks[p.deck2_id].name
        }

    args = {}
    args['admin'] = request.args.get('admin', '') == 'True'
    args['tournament'] = tournament
    args['rounds'] = helper.group_by_round(games)
    args['teams'] = get_teams_data(participants, players, decks)

    rank_manager = RankingManager()
    tournament_ranking = rank_manager.get_tournament_ranking(tid)
    table = rank_manager.ranking_table(tournament_ranking['teams'])
    players_table = rank_manager.ranking_table(tournament_ranking['players'])

    args['rank_table'] = table
    args['players_rank_table'] = players_table
    args['players'] = players_data

    return 'tournaments/thg_tournament.html', args
Beispiel #8
0
def tier_view():
    session = Session()
    decks = session.query(Deck).all()

    team = {}

    for deck in decks:
        _class = ''
        if deck.status == 'inactive':
            _class = 'blue-grey lighten-5'
        team[deck.id] = {
            'name': deck.name,
            '_class': _class,
            'status': deck.status
        }

    admin = request.args.get('admin', '') == 'True'

    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking()

    tiers = tier_sort(ranking.decks[:])

    tiers = [x for x in tiers if team[x['id']]['status'] != 'inactive']

    for deck in ranking.decks:
        data = team[deck['id']]

        if data['_class'] == '':
            data['_class'] = get_tier_class(deck['tier'])

    table = rank_manager.ranking_table(tiers, True)

    return render_template('decks/index.html',
                           admin=admin,
                           rank_table=table,
                           teams=team)
Beispiel #9
0
def create_deck():
    payload = request.json
    if 'name' not in payload or len(payload['name'].strip()) == 0:
        return None, 400

    deck = Deck()
    deck.name = payload['name']
    deck.type = 1
    deck.status = 'active'

    session = Session()
    session.add(deck)
    session.commit()

    RankingManager().refresh()

    return '', 200
Beispiel #10
0
def change_status(tid):
    payload = request.json
    params = ['status']
    for param in params:
        if param not in payload:
            abort(400)

    status = payload['status']

    session = Session()

    tournament = session.query(Tournament).filter(Tournament.id == tid).one()
    if status == 'finished':
        tournament.status = 'finished'
        session.add(tournament)
        session.commit()

    RankingManager().refresh()

    return make_response()
Beispiel #11
0
def year_view(year):
    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking(year)

    return send_player_ranking(ranking.players, 'Players (%s)' % year,
                               ranking.titles)
Beispiel #12
0
def index_view():
    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking()

    return send_player_ranking(ranking.players, 'Players', ranking.titles)
Beispiel #13
0
def player_ranking_by_tournament_type(type, block_name, year=None):
    rank_manager = RankingManager()
    ranking = rank_manager.get_ranking(year)

    ranking_players = ranking.tournaments_types[type.value]
    return send_player_ranking(ranking_players, block_name, ranking.titles)
Beispiel #14
0
    def get_available_decks_for_next_tournament(self, tier):
        session = Session()

        ds = session.query(Deck).filter(Deck.status == 'active').all()
        parts = session.query(Participant).all()
        # tournaments = session.query(Tournament).filter(Tournament.status == 'finished').order_by(
        #     Tournament.id.asc()).all()

        decks = {}
        participants = {}

        for p in parts:
            participants[p.id] = p

        for deck in ds:
            decks[deck.id] = deck

        rank_manager = RankingManager()
        ranking = rank_manager.get_ranking()
        # for tournament in tournaments:
        #     if tournament.type == TournamentType.DRAFT.value:
        #         continue
        #     t = ranking.get_tournament_ranking(tournament.id)
        #     winner = self.get_winner_deck_id(tournament, participants, t)
        #
        #     deck = decks[winner]
        #
        #     result.remove(deck)

        # sort decks
        play_map = {}
        for deck_data in ranking.decks:
            mp = deck_data['tp']

            play_map[deck_data['id']] = mp

        def deck_sort(a, b):
            ga = play_map[a['id']]
            gb = play_map[b['id']]

            return (ga > gb) - (ga < gb)

        tiers = group_by_key(ranking.decks, 'tier')

        sort_grouped_lists(tiers, deck_sort)

        for t in tiers:
            lst = tiers[t]
            tiers[t] = randomize_grouped_by_matchs_played(lst, play_map)

        if tier == 'T1':
            order = ['T1', 'T2', 'T3']
        elif tier == 'T2':
            order = ['T2', 'T3', 'T1']
        else:
            order = ['T3', 'T2', 'T1']

        result = []
        for t in order:
            lst = tiers[t]
            for d in lst:
                did = d['id']
                if did not in decks:
                    continue
                result.append(decks[did])

        return result
Beispiel #15
0
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import scoped_session

app = Flask(__name__)

# Configurations
app.config.from_object('config.DevelopmentConfig')

# Define the database object which is imported
# by modules and controllers
# connect to the database
db = SQLAlchemy(app)
Session = scoped_session(db.sessionmaker(bind=db.engine))

# Import a module / component using its blueprint handler variable (mod_auth)
from app.views import init as init_views
from app.api import init as init_api

# Register blueprint(s)

init_views(app)
init_api(app)

# create database
db.create_all()

from app.core import RankingManager

# init ranking
RankingManager().refresh()
Session.remove()