Ejemplo n.º 1
0
def leaderboard(request, game_id):
    game = get_game(game_id)
    entry = get_entry(game, request.user)
    if not entry:
        return HttpResponseRedirect('/ncaa/')

    leaders = get_leaders(game)
    context = get_base_context(request, game_id, leaders=leaders)
    return render_with_request_context(request, 'leaderboard.html', context)
Ejemplo n.º 2
0
def join_game(request):
    game_id = request.POST.get('game_id', '')
    entry_name = request.POST.get('entry_name', '')
    password = request.POST.get('password', '')
    otp = None

    game = get_game(game_id)
    if not game or request.method != 'POST':
        return HttpResponseRedirect('/ncaa/')

    error = ''

    if not entry_name:
        error = 'You must provide an entry name'
    elif len(entry_name) > 50:
        error = 'Entry names cannot be longer than 50 characters'
    elif entry_forbidden_regex.search(entry_name):
        error = 'Entry names can only contain letters, numbers, spaces, and underscores'
    else:
        try:
            entry = UserEntry.objects.get(game=game, entry_name=entry_name)
        except UserEntry.DoesNotExist:
            pass
        else:
            error = 'There is already an entry with the name %s in this game' % entry_name

    if game.password and game.password != password:
        try:
            otp = GameOtp.objects.get(game=game, pw=password, active=True)
        except GameOtp.DoesNotExist:
            error = 'Incorrect password'

    self_entry = get_entry(game, request.user)
    if self_entry:
        error = 'You already have an entry in this game'

    if error:
        context = get_base_context(request,
                                   game_id,
                                   error=error,
                                   leaders=get_leaders(game))
        return render_with_request_context(request, 'game_home.html', context)

    with transaction.atomic():
        entry = UserEntry.objects.create(user=request.user,
                                         game=game,
                                         entry_name=entry_name)
        entry.update_score()

        if otp is not None:
            otp.active = False
            otp.entry = entry
            otp.save()

    return HttpResponseRedirect('/ncaa/game/%s/entry/%s/' %
                                (game_id, entry.id))
Ejemplo n.º 3
0
def entry_view(request, game_id, entry_id):
    game = get_game(game_id)
    self_entry = get_entry(game, request.user)
    if not self_entry:
        return HttpResponseRedirect('/ncaa/')

    try:
        entry = UserEntry.objects.get(id=entry_id, game__id=game_id)
    except UserEntry.DoesNotExist:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)

    teams = []

    teams_query = Q(entry=entry) & (~Q(net_cost=0) | ~Q(count=0))
    user_teams = UserTeam.objects.filter(teams_query).order_by(
        'team__team__abbrev_name')
    for user_team in user_teams:
        team_score = user_team.team.score * user_team.count
        estimated_team_score = user_team.team.estimated_score * user_team.count
        teams.append(
            (user_team.team, user_team.count, team_score, estimated_team_score,
             user_team.net_cost, estimated_team_score - user_team.net_cost))

    card_offers, stock_orders, card_executions, stock_executions = None, None, None, None
    if game.supports_cards:
        card_offers = TradeOffer.objects.filter(
            entry=self_entry, is_active=True,
            accepting_user__isnull=True).order_by('-offer_time')
        query = (Q(entry=self_entry) | Q(accepting_user=self_entry)) & Q(
            accepting_user__isnull=False)
        card_executions = TradeOffer.objects.filter(query).order_by(
            '-offer_time')[:10]
    if game.supports_stocks:
        if entry == self_entry:
            stock_orders = Order.open_orders.filter(
                entry=entry).order_by('-placed_time').select_related(
                    'security', 'entry')
        else:
            stock_orders = []
        query = (Q(buy_order__placer=entry.entry_name)
                 | Q(sell_order__placer=entry.entry_name)) & Q(
                     security__market__name=game.name)
        stock_executions = Execution.objects.filter(query).order_by(
            '-time')[:50]

    context = get_base_context(request,
                               game_id,
                               entry=entry,
                               teams=teams,
                               card_offers=card_offers,
                               stock_orders=stock_orders,
                               card_executions=card_executions,
                               stock_executions=stock_executions)
    return render_with_request_context(request, 'entry.html', context)
Ejemplo n.º 4
0
def offer_view(request, game_id, offer_id):
    game = get_game(game_id)
    self_entry = get_entry(game, request.user)
    if not self_entry:
        return HttpResponseRedirect('/ncaa/')
    try:
        offer = TradeOffer.objects.get(id=offer_id)
    except TradeOffer.DoesNotExist:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)

    context = get_base_context(request, game_id, offer=offer)

    return render_with_request_context(request, 'offer_page.html', context)
Ejemplo n.º 5
0
def game_team_view(request, game_id, team_id):
    game = get_game(game_id)
    entry = get_entry(game, request.user)
    if not entry:
        return HttpResponseRedirect('/ncaa/')

    team = get_team_from_identifier(team_id, game.game_type)
    if not team:
        return HttpResponseRedirect('/ncaa/')

    context = create_team_context(request, team=team, game=game)
    context['self_entry'] = entry
    context['security'] = Security.objects.get(team=context['game_team'])
    return render_with_request_context(request, 'team_view.html', context)
Ejemplo n.º 6
0
def create_offer(request, game_id, **kwargs):
    game = get_game(game_id)
    if not game.supports_cards:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)
    entry = get_entry(game, request.user)
    if not entry:
        return HttpResponseRedirect('/ncaa/')

    all_teams = GameTeam.objects.filter(
        game=game).order_by('team__abbrev_name')

    error = kwargs.get('error', '')

    context = get_base_context(request,
                               game_id,
                               all_teams=all_teams,
                               max_offer_size=MAX_OFFER_SIZE,
                               error=error)
    return render_with_request_context(request, 'create_offer.html', context)
Ejemplo n.º 7
0
def get_base_context(request, game_id, **kwargs):
    context = {}
    for key in kwargs:
        context[key] = kwargs[key]
    game, self_entry = None, None
    if game_id:
        game = get_game(game_id)
    context['game'] = game
    if request.user.is_authenticated():
        context['user_games'] = NcaaGame.objects.filter(
            entries__user=request.user)
        #context['user_entries'] = UserEntry.objects.filter(user=request.user)
        if game:
            context['self_entry'] = get_entry(game, request.user)
    if request.method == 'GET':
        start_tab = request.GET.get('start_tab', '')
        if start_tab:
            context['start_tab'] = start_tab
        context['show_login'] = request.GET.get('show_login', False)
    return context
Ejemplo n.º 8
0
def accept_offer(request, game_id, offer_id):
    game = get_game(game_id)
    self_entry = get_entry(game, request.user)
    if not self_entry or request.method != 'POST':
        return HttpResponseRedirect('/ncaa/')

    try:
        offer = TradeOffer.objects.get(id=offer_id)
    except TradeOffer.DoesNotExist:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)

    error = ''
    try:
        accept_trade(offer, self_entry)
    except Exception as e:
        error = str(e)

    if error:
        context = get_base_context(request, game_id, offer=offer, error=error)
        return render_with_request_context(request, 'offer_page.html', context)

    return HttpResponseRedirect('/ncaa/game/%s/entry/%s/' %
                                (game_id, self_entry.id))
Ejemplo n.º 9
0
def marketplace(request, game_id):
    game = get_game(game_id)
    if not game.supports_cards:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)
    entry = get_entry(game, request.user)
    if not entry:
        return HttpResponseRedirect('/ncaa/')

    offers_query = Q(entry__game=game, accepting_user=None, is_active=True)

    ask_filter = request.GET.get('ask_filter', '')
    bid_filter = request.GET.get('bid_filter', '')

    if ask_filter:
        offers_query = offers_query & Q(
            ask_side__components__team__abbrev_name__iexact=ask_filter)
    if bid_filter:
        offers_query = offers_query & Q(
            bid_side__components__team__abbrev_name__iexact=bid_filter)

    offers = TradeOffer.objects.filter(offers_query).order_by(
        '-offer_time')[:25]
    context = get_base_context(request, game_id, offers=offers)
    return render_with_request_context(request, 'marketplace.html', context)
Ejemplo n.º 10
0
def make_offer(request, game_id):
    game = get_game(game_id)
    if request.method != 'POST' or not game.supports_cards:
        return HttpResponseRedirect('/ncaa/game/%s/' % game_id)

    self_entry = get_entry(game, request.user)
    if not self_entry:
        return HttpResponseRedirect('/ncaa/')

    bids, asks = [], []
    teams_in_offer = set()

    try:
        for i in range(MAX_OFFER_SIZE):
            bid_team_name, bid_count_str = request.POST.get(
                'bid_%s_team' % i,
                ''), request.POST.get('bid_%s_count' % i, '')
            bid = create_offer_component(bid_team_name, bid_count_str, game)
            if bid:
                bid_team, bid_count = bid
                position = UserTeam.objects.get(entry=self_entry,
                                                team=bid_team)
                if bid_team in teams_in_offer:
                    raise Exception(
                        'Team %s cannot exist multiple times in the same offer'
                        % bid_team.team.abbrev_name)
                if game.position_limit and position.count - bid_count < -1 * game.position_limit:
                    raise Exception('You tried to offer %s shares of %s but your current position is %s and the position limit is %s'\
                        % (bid_count, bid_team_name, position.count, game.position_limit))
                teams_in_offer.add(bid_team)
                bids.append(bid)
            ask_team_name, ask_count_str = request.POST.get(
                'ask_%s_team' % i,
                ''), request.POST.get('ask_%s_count' % i, '')
            ask = create_offer_component(ask_team_name, ask_count_str, game)
            if ask:
                ask_team, ask_count = ask
                position = UserTeam.objects.get(entry=self_entry,
                                                team=ask_team)
                if ask_team in teams_in_offer:
                    raise Exception(
                        'Team %s cannot exist multiple times in the same offer'
                        % ask_team.team.abbrev_name)
                if game.position_limit and position.count + ask_count > game.position_limit:
                    raise Exception('You tried to acquire %s shares of %s but your current position is %s and the position limit is %s'\
                        % (ask_count, ask_team_name, position.count, game.position_limit))
                teams_in_offer.add(ask_team)
                asks.append(ask)

        bid_point_str, ask_point_str = request.POST.get('bid_points',
                                                        ''), request.POST.get(
                                                            'ask_points', '')
        bid_points, ask_points = 0, 0
        try:
            if bid_point_str:
                bid_points = int(bid_point_str)
                if bid_points < 0:
                    raise Exception(
                        'You cannot offer a negative number of points')
                if game.points_limit and self_entry.extra_points - bid_points < -1 * game.points_limit:
                    raise Exception('You tried to offer %s points but you have %s points and the point short limit is %s' %\
                        (bid_point_str, self_entry.extra_points, game.points_limit))
            if ask_point_str:
                ask_points = int(ask_point_str)
                if ask_points < 0:
                    raise Exception(
                        'You cannot ask for a negative number of points')
        except ValueError:
            raise Exception('You must enter integer values for points')

        if bid_points and ask_points:
            raise Exception(
                'You can\'t include points on both sides of an offer')

    except Exception as e:
        return create_offer(request, game_id, error=str(e))

    bid_side, ask_side = TradeSide(), TradeSide()
    if bid_points:
        bid_side.points = bid_points
    if ask_points:
        ask_side.points = ask_points
    bid_side.save()
    ask_side.save()

    cancel_on_game = request.POST.get('cancel_on_game', False)
    offer = TradeOffer.objects.create(entry=self_entry,
                                      ask_side=ask_side,
                                      bid_side=bid_side,
                                      cancel_on_game=cancel_on_game)

    for bid_team, bid_count in bids:
        bid_component = TradeComponent.objects.create(team=bid_team,
                                                      count=bid_count,
                                                      offer=bid_side)
    for ask_team, ask_count in asks:
        ask_component = TradeComponent.objects.create(team=ask_team,
                                                      count=ask_count,
                                                      offer=ask_side)

    return HttpResponseRedirect('/ncaa/game/%s/offer/%s/' %
                                (game_id, offer.id))