Exemplo n.º 1
0
def userSongs():
    token = request.headers.get('Authorization')
    req = requests.get('https://dev-c-4o8wnx.auth0.com/userinfo',
                       headers={'Authorization': token}).content
    userInfo = json.loads(req)
    userId = User.query.filter_by(email=userInfo['email']).first().id
    userInfo = User.query.options(db.joinedload(
        'songs'), db.joinedload('favorites')).get(userId)
    return userInfo.to_dict(), 200
Exemplo n.º 2
0
def get_sets():
    # gets decodes userinfo out of token using auth0 api
    token = request.headers.get('Authorization')
    req = requests.get('https://codelet-app.auth0.com/userinfo',
                       headers={
                           'Authorization': token
                       }).content
    userInfo = json.loads(req)
    userId = User.query.filter_by(email=userInfo['email']).first().id

    userInfo = User.query.options(
        db.joinedload('sets').joinedload('votes'),
        db.joinedload('favorites')).get(userId)
    return userInfo.to_dict(), 200
Exemplo n.º 3
0
def view_turn(game_id, round_id, turn_id):
    game = Game.query.get(game_id)
    game_round = Round.query.get(round_id)

    turn = Turn.query.options(db.joinedload(Turn.player)).get(turn_id)
    if turn.completed_at:
        return redirect(
            url_for('salad_bowl.view_round',
                    game_id=game_id,
                    round_id=round_id))

    seconds_remaining = int(
        (turn.expected_complete_at - datetime.utcnow()).total_seconds())

    word = None
    if turn.player_id == g.current_player.id:
        if request.args.get('currentWord'):
            word = SaladBowlWord.query.get(int(request.args['currentWord']))
        else:
            unguessed_words_q = SaladBowlWord.query
            unguessed_words_q = unguessed_words_q.join(
                GuessedWord,
                db.and_(GuessedWord.round_id == round_id,
                        GuessedWord.word_id == SaladBowlWord.id),
                isouter=True)
            unguessed_words_q = unguessed_words_q.filter(
                SaladBowlWord.game_id == game_id)
            unguessed_words_q = unguessed_words_q.filter(
                GuessedWord.round_id.is_(None))
            word = random.choice(unguessed_words_q.all())

    from app.actions.salad_bowl.turn import EndTurnForm, WordGuessedForm

    return render_template(
        'salad_bowl/view_turn.html',
        game=game,
        game_round=game_round,
        turn=turn,
        seconds_remaining=seconds_remaining,
        word=word,
        end_turn_form=EndTurnForm() if word else None,
        word_guessed_form=WordGuessedForm(word_id=word.id) if word else None)
Exemplo n.º 4
0
def start_game(game_id):
    form = StartGameForm()

    if form.validate_on_submit(
    ):  # make sure game is open, stuff like that, user is logged in, user isnt already in game
        game = Game.query.options(db.joinedload(Game.teams)).get(game_id)
        game.started_at = datetime.utcnow()
        turn_order = list(range(len(game.teams)))
        shuffle(turn_order)
        for i, team in enumerate(game.teams):
            team.turn_order = turn_order[i]

        db.session.commit()

        return True, redirect(url_for('.view_game', game_id=game_id))

    return False, render_template('salad_bowl/actions/start_game.html',
                                  form=form,
                                  action_url=url_for('salad_bowl.start_game',
                                                     game_id=game_id))
Exemplo n.º 5
0
def home():

    sort = request.args.get('sort', 'total')

    total_value_calc = db.func.coalesce(
        db.func.sum(BankerCurrency.amount * Currency.value), 0)

    sorted_standings_q = db.session.query(
        Banker, total_value_calc.label('total_value')).outerjoin(
            BankerCurrency, BankerCurrency.banker_id == Banker.id).outerjoin(
                Currency, Currency.id == BankerCurrency.currency_id).options(
                    db.joinedload(Banker.currencies)).group_by(Banker.id)

    if sort == 'total':
        sorted_standings_q = sorted_standings_q.order_by(
            db.desc(total_value_calc), Banker.id)
    else:
        sort_by_currency_id = int(sort)

        sorting_subq = db.session.query(
            Banker.id.label('banker_id'),
            db.func.coalesce(BankerCurrency.amount,
                             0).label('sorted_column_amount')).outerjoin(
                                 BankerCurrency,
                                 db.and_(
                                     BankerCurrency.banker_id == Banker.id,
                                     BankerCurrency.currency_id ==
                                     sort_by_currency_id)).subquery()

        sorted_standings_q = sorted_standings_q.join(
            sorting_subq, sorting_subq.c.banker_id == Banker.id).order_by(
                db.desc(db.func.max(sorting_subq.c.sorted_column_amount)),
                Banker.id)

    sorted_standings = sorted_standings_q.limit(20).all()

    return render_template('ct_conomy/home.html',
                           sorted_standings=sorted_standings)
Exemplo n.º 6
0
def word_guessed(game_id, round_id, turn_id):
    form = WordGuessedForm()

    if form.validate_on_submit():
        current_players_team_id_q = db.session.query(PlayerTeam.team_id)
        current_players_team_id_q = current_players_team_id_q.join(Team)
        current_players_team_id = current_players_team_id_q.filter(Team.game_id == game_id,
            PlayerTeam.player_id == g.current_player.id).scalar()

        guessed_word = GuessedWord(
            word_id=form.word_id.data,
            round_id=round_id,
            team_id=current_players_team_id,
            player_id=g.current_player.id)
        db.session.add(guessed_word)
        db.session.flush()

        unguessed_words_q = SaladBowlWord.query
        unguessed_words_q = unguessed_words_q.join(GuessedWord, db.and_(
            GuessedWord.round_id == round_id, GuessedWord.word_id == SaladBowlWord.id), isouter=True)
        unguessed_words_q = unguessed_words_q.filter(SaladBowlWord.game_id == game_id)
        unguessed_words_q = unguessed_words_q.filter(GuessedWord.round_id.is_(None))
        if unguessed_words_q.count() == 0:
            turn = Turn.query.get(turn_id)
            turn.completed_at = datetime.utcnow()
            game_round = Round.query.get(round_id)
            game_round.completed_at = datetime.utcnow()
            db.session.flush()

            game = Game.query.options(db.joinedload(Game.rounds)).get(game_id)
            if all(round.completed_at for round in game.rounds):
                game.completed_at = datetime.utcnow()

        db.session.commit()

        return False, redirect(url_for('salad_bowl.view_turn', game_id=game_id, round_id=round_id, turn_id=turn_id))
Exemplo n.º 7
0
def view_game(game_id):

    if not g.current_player:  # BUild this into a decorator
        abort(404)

    player_game = PlayerGame.query.filter(
        PlayerGame.player_id == g.current_player.id,
        PlayerGame.game_id == game_id).one_or_none()
    if not player_game:
        abort(404)

    game_q = Game.query
    game_q = game_q.options(db.joinedload(Game.teams).joinedload(Team.players))
    game_q = game_q.options(db.joinedload(Game.rounds))
    game = game_q.get(game_id)

    game_rounds = sorted(game.rounds, key=lambda x: x.round_number)
    for game_round in game_rounds:
        if game_round.started_at and not game_round.completed_at:
            return redirect(
                url_for('salad_bowl.view_round',
                        game_id=game_id,
                        round_id=game_round.id))

    next_round = None
    for game_round in game_rounds:
        if not game_round.started_at:
            next_round = game_round
            break

    player_id_to_team = None
    submitted_words_by_player_id = None
    team_id_to_round_number_to_word_count = None
    num_completed_rounds = 0
    if next_round and next_round.round_number == 1:
        words = SaladBowlWord.query.filter(
            SaladBowlWord.game_id == game_id).all()
        submitted_words_by_player_id = {}
        for word in words:
            submitted_words_by_player_id[word.writer_id] = True

        if game.started_at:
            if not submitted_words_by_player_id.get(g.current_player.id):
                return redirect(
                    url_for('salad_bowl.add_words', game_id=game_id))

        player_id_to_team = {}
        for team in game.teams:
            for player in team.players:
                player_id_to_team[player.id] = team
    else:
        word_count_q = db.session.query(GuessedWord.team_id,
                                        Round.round_number,
                                        db.func.count(GuessedWord.round_id))
        word_count_q = word_count_q.join(Round)
        word_count_q = word_count_q.join(SaladBowlWord)
        word_count_q = word_count_q.group_by(Round.round_number,
                                             GuessedWord.team_id)
        word_count_q = word_count_q.filter(SaladBowlWord.game_id == game_id)

        team_id_to_round_number_to_word_count = defaultdict(
            lambda: defaultdict(int))
        for team_id, round_number, word_count in word_count_q.all():
            num_completed_rounds = max(num_completed_rounds, round_number)
            team_id_to_round_number_to_word_count[team_id][
                round_number] = word_count

    can_start_next_round = False
    if next_round:
        if next_round.round_number == 1:
            if all(
                    submitted_words_by_player_id.get(player.id)
                    for player in game.players):
                can_start_next_round = True
        else:
            can_start_next_round = True
    can_start_next_round = can_start_next_round and game.owner_player_id == g.current_player.id

    return render_template(
        'salad_bowl/view_game.html',
        game=game,
        teams=game.teams,
        player_id_to_team=player_id_to_team,
        submitted_words_by_player_id=submitted_words_by_player_id,
        team_id_to_round_number_to_word_count=
        team_id_to_round_number_to_word_count,
        num_completed_rounds=num_completed_rounds,
        next_round=next_round,
        can_start_next_round=can_start_next_round)
Exemplo n.º 8
0
def view_round(game_id, round_id):

    game = Game.query.options(db.joinedload(Game.teams)).get(game_id)
    game_round = Round.query.options(db.joinedload(Round.turns)).get(round_id)

    if game_round.completed_at:
        return redirect(url_for('salad_bowl.view_game', game_id=game_id))

    for turn in game_round.turns:
        if turn.started_at and not turn.completed_at:
            return redirect(
                url_for('salad_bowl.view_turn',
                        game_id=game_id,
                        round_id=round_id,
                        turn_id=turn.id))

    turn_length = game_round.seconds_per_turn
    seconds_remaining = turn_length

    if not game_round.turns:
        # we are starting a new round
        if game_round.round_number == 1:
            # start of game, pick first team
            next_team = next(
                (team for team in game.teams if team.turn_order == 0))
        else:
            # see who was going at the end of the previous round
            previous_turn_q = Turn.query
            previous_turn_q = previous_turn_q.join(Round)
            previous_turn_q = previous_turn_q.filter(
                Round.game_id == game_id,
                Round.round_number == game_round.round_number - 1)
            previous_turn_q = previous_turn_q.order_by(db.desc(
                Turn.started_at))
            previous_turn = previous_turn_q.first()

            next_team = next((team for team in game.teams
                              if team.id == previous_turn.team_id))
            seconds_remaining = turn_length - int(
                (previous_turn.expected_complete_at -
                 previous_turn.completed_at).total_seconds())
    else:
        # weve already had teams go this round
        previous_turn = None
        turn_order_index = None
        for turn in sorted(game_round.turns, key=lambda x: x.started_at):
            previous_turn = turn
        for team in game.teams:
            if team.id == previous_turn.team_id:
                turn_order_index = (team.turn_order + 1) % len(game.teams)
        next_team = next((team for team in game.teams
                          if team.turn_order == turn_order_index))

    current_players_team_id_q = db.session.query(PlayerTeam.team_id)
    current_players_team_id_q = current_players_team_id_q.join(Team)
    current_players_team_id = current_players_team_id_q.filter(
        Team.game_id == game_id,
        PlayerTeam.player_id == g.current_player.id).scalar()

    can_start_next_turn = next_team.id == current_players_team_id

    unguessed_words_q = SaladBowlWord.query
    unguessed_words_q = unguessed_words_q.join(
        GuessedWord,
        db.and_(GuessedWord.round_id == round_id,
                GuessedWord.word_id == SaladBowlWord.id),
        isouter=True)
    unguessed_words_q = unguessed_words_q.filter(
        SaladBowlWord.game_id == game_id)
    unguessed_words_q = unguessed_words_q.filter(
        GuessedWord.round_id.is_(None))
    unguessed_word_count = unguessed_words_q.count()

    return render_template('salad_bowl/view_round.html',
                           game=game,
                           game_round=game_round,
                           next_team=next_team,
                           seconds_remaining=seconds_remaining,
                           unguessed_word_count=unguessed_word_count,
                           can_start_next_turn=can_start_next_turn)
def category_sets(category_id):
    category_info = Category.query.options(
        db.joinedload('sets')).get(category_id)
    return category_info.to_dict_sets(), 200
Exemplo n.º 10
0
def get_and_normalize_all_data_for_user(current_user):
    """
    Get all a user's data to set-up their frontend store, including chronicles,
    tales, threads, choices, characters, etc., then normalize into dictionaries.
    """
    # Query all a user's chronicles
    user = User.query.options(
        db.joinedload(User.pcs),
        db.joinedload(User.assets),
        db.joinedload(User.meters),
        db.joinedload(User.statuses),

        db.joinedload(User.entities), \
        #   .joinedload(Entity.assets), \
        # db.joinedload(User.entities) \
        #   .joinedload(Entity.meters), \
        # db.joinedload(User.entities) \
        #   .joinedload(Entity.statuses), \
        # db.joinedload(User.entities) \
        #   .joinedload(Entity.slots), \

        db.joinedload(User.chronicles) \
          .joinedload(Chronicle.tales) \
          .joinedload(Tale.threads) \
          .joinedload(Thread.asset_effects), \
        db.joinedload(User.chronicles) \
          .joinedload(Chronicle.tales) \
          .joinedload(Tale.threads) \
          .joinedload(Thread.choices) \
          .joinedload(Choice.asset_locks) \
    ).get(current_user.id)

    # Normalize data before returning
    pcs = {pc.id: pc.to_dict() for pc in user.pcs}
    assets = {a.id: a.to_dict() for a in user.assets}
    statuses = {s.id: s.to_dict() for s in user.statuses}
    meters = {m.id: m.to_dict() for m in user.meters}
    entities = {e.id: e.to_dict() for e in user.entities}
    chronicles = {c.id: c.to_dict() for c in user.chronicles}

    tales = {}
    threads = {}
    choices = {}
    for chronicle in user.chronicles:
        for tale in chronicle.tales:
            tales[tale.id] = tale.to_dict()
            for thread in tale.threads:
                threads[thread.id] = thread.to_dict()
                for choice in thread.choices:
                    choices[choice.id] = choice.to_dict()

    user = normalize_user_data(
        user=current_user,
        pcs=pcs,
        chronicles=chronicles,
        tales=tales,
        threads=threads,
        choices=choices,
        entities=entities,
        assets=assets,
        statuses=statuses,
        meters=meters,
    )

    print("\n\nUSER, CHRONICLES, TALES, THREADS, CHOICES")
    # pprint(user)
    # pprint(chronicles)
    # pprint(tales)
    # pprint(threads)
    # pprint(choices)
    return user, pcs, chronicles, tales, threads, choices, entities, assets, statuses, meters