Ejemplo n.º 1
0
def team_list(request, game_id):
    context = get_base_context(request, game_id)
    game = context.get('game', None)
    if not game:
        return HttpResponseRedirect('/ncaa/')
    rows = []
    bid_total, ask_total, last_total, volume_total, points_total = Decimal('0.0'), Decimal('0.0'), Decimal('0.0'), 0, 0
    game_teams = GameTeam.objects.filter(game=game, team__is_eliminated=False).order_by('team__abbrev_name').select_related('team')
    if game.supports_stocks:
        securities = Security.objects.filter(market__game=context['game'])
        security_map = { }
        for security in securities:
            security_map[security.name] = security
        for team in game_teams:
            security = security_map[team.team.abbrev_name]
            bid, ask = security.get_bid(), security.get_ask()
            if bid:
                bid_total += bid
            if ask:
                ask_total += ask
            last_total += security.get_last()
            volume_total += team.volume
            points_total += team.score
            rows.append((team, security))
    else:
        for team in game_teams:
            rows.append((team, None))
    context['rows'] = rows
    context['bid_total'] = bid_total
    context['ask_total'] = ask_total
    context['last_total'] = last_total
    context['volume_total'] = volume_total
    context['points_total'] = points_total
    
    return render_with_request_context(request, 'team_list.html', context)
Ejemplo n.º 2
0
def save_settings(request, game_id):
    context = get_base_context(request, game_id)
    game = context['game']
    if not game or request.method != 'POST':
        return HttpResponseRedirect('/ncaa/')

    add_scoring_context(game, context)
    if not context['can_edit']:
        return HttpResponseRedirect('/ncaa/game/%s/scoring_settings/' % game_id)

    scoring_settings = ScoringSetting.objects.filter(game=game)
    errors = []
    for setting in scoring_settings:
        setting_str = request.POST.get(setting.scoreType.name.replace(' ', '_'), '')
        if not setting_str:
            continue
        try:
            setting_points = int(setting_str)
        except ValueError:
            errors.append('%s is not a valid score setting' % setting_str)
        else:
            if setting.points != setting_points:
                setting.points = setting_points
                setting.save()

    context['errors'] = errors
    return render_with_request_context(request, 'scoring_settings.html', context)
Ejemplo n.º 3
0
def do_create_game(request):
    if request.method != 'POST':
        return HttpResponseRedirect('/ncaa/')
    errors = []

    form = CreateGameForm(request.POST)
    if form.is_valid():
        data = form.cleaned_data

        game = NcaaGame.objects.create(name=data['game_name'], game_type=data['game_type'],\
            supports_cards=data['support_cards'], supports_stocks=data['support_stocks'])
        game_password = data.get('game_password', '')
        if game_password:
            game.password = game_password
        position_limit = data.get('position_limit', 0)
        points_limit = data.get('points_limit', 0)
        if position_limit:
            game.position_limit = position_limit
        if points_limit:
            game.points_limit = points_limit
        game.save()

        entry = UserEntry.objects.create(game=game, user=request.user, entry_name=data['entry_name'])

        return HttpResponseRedirect('/ncaa/game/%s/scoring_settings/' % game.id)
    else:
        for field in form:
            for e in field.errors:
                errors.append(e)
        context = get_base_context(request, None, game_types=GameType.objects.all(), errors=errors)
        return render_with_request_context(request, 'create_game.html', context)
Ejemplo n.º 4
0
def scoring_settings(request, game_id):
    context = get_base_context(request, game_id)
    game = context['game']
    if not game:
        return HttpResponseRedirect('/ncaa/')

    add_scoring_context(game, context)
    return render_with_request_context(request, 'scoring_settings.html', context)
Ejemplo n.º 5
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.º 6
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.º 7
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.º 8
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.º 9
0
def game_home(request, game_id):
    context = get_base_context(request, game_id)

    game = context['game']
    if not game:
        return HttpResponseRedirect('/ncaa/')
    context['leaders'] = get_leaders(game)

    card_executions, stock_executions = [], []
    if game.supports_cards:
        card_executions = TradeOffer.objects.filter(entry__game=game, accepting_user__isnull=False).order_by('-accept_time')[:10]
    if game.supports_stocks:
        stock_executions = Execution.objects.filter(security__market__name=game.name).order_by('-time')[:50]

    context['card_executions'] = card_executions
    context['stock_executions'] = stock_executions

    return render_with_request_context(request, 'game_home.html', context)
Ejemplo n.º 10
0
def cancel_offer(request, game_id, offer_id):
    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)
    self_entry = context.get('self_entry', None)
    if not self_entry:
        return HttpResponseRedirect('/ncaa/')

    error = ''
    if offer.is_accepted():
        error = 'This offer has already been accepted'
    else:
        offer.is_active = False
        offer.save()

    context['error'] = error
    return render_with_request_context(request, 'offer_page.html', context)
Ejemplo n.º 11
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.º 12
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.º 13
0
def market_maker(request, game_id):
    context = get_base_context(request, game_id)
    game = context.get('game', None)
    self_entry = context.get('self_entry', None)
    if not game or not self_entry or not game.supports_stocks:
        return HttpResponseRedirect('/ncaa/')

    rows = []
    game_teams = GameTeam.objects.filter(game=game, team__is_eliminated=False).order_by('team__abbrev_name').select_related('team')
    securities = Security.objects.filter(market__game=context['game'])
    security_map = {}
    for security in securities:
        security_map[security.name] = security
    for team in game_teams:
        security = security_map[team.team.abbrev_name]
        position = UserTeam.objects.filter(entry__id=self_entry.id, team__id=team.id)[0].count
        user_bid = security.get_bid_order(self_entry)
        user_ask = security.get_ask_order(self_entry)
        rows.append((team, security, position, user_bid, user_ask))

    context['rows'] = rows
    
    return render_with_request_context(request, 'market_maker.html', context)
Ejemplo n.º 14
0
def join_game(request):
    game_id = request.POST.get('game_id', '')
    entry_name = request.POST.get('entry_name', '')
    password = request.POST.get('password', '')

    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 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:
        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)
    entry = UserEntry.objects.create(user=request.user, game=game, entry_name=entry_name)
    entry.update_score()

    return HttpResponseRedirect('/ncaa/game/%s/entry/%s/' % (game_id, entry.id))
Ejemplo n.º 15
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(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
        teams.append((user_team.team, user_team.count, team_score))
    
    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.º 16
0
def home(request):
    entries = []
    if request.user.is_authenticated():
        entries = request.user.entries.all()
    return render_with_request_context(request, 'ncaa_home.html', get_base_context(request, None))
Ejemplo n.º 17
0
def create_game(request):
    context = get_base_context(request, None, game_types=GameType.objects.all())
    return render_with_request_context(request, 'create_game.html', context)
Ejemplo n.º 18
0
def leaderboard(request, game_id):
    context = get_base_context(request, game_id)
    if 'self_entry' not in context:
        return HttpResponseRedirect('/ncaa/')
    context['leaders'] = get_leaders(context['game'])
    return render_with_request_context(request, 'leaderboard.html', context)
Ejemplo n.º 19
0
def game_list(request):
    entries = request.user.entries.all()
    query = ~Q(entries__in=entries)
    other_games = NcaaGame.objects.filter(query)
    context = get_base_context(request, None, entries=entries, other_games=other_games)
    return render_with_request_context(request, 'game_list.html', context)