Ejemplo n.º 1
0
def manabaseanalysis(request):
    context = BASE_CONTEXT.copy()

    context['current_formats'] = Format.objects.filter(start_date__lte=timezone.now(),
                                                       end_date__gt=timezone.now()).order_by('format')
    # REVISIT - unsafe index
    context['format'] = context['current_formats'][0]

    card_list = '''1 Pithing Needle
4 Chromatic Star
1 Ancient Grudge
1 Boil
3 Nature's Claim
3 Pyroclasm
4 Expedition Map
3 Oblivion Stone
4 Sylvan Scrying
4 Ancient Stirrings
1 Relic of Progenitus
4 Wurmcoil Engine
3 Thragtusk
4 Chromatic Sphere
4 Karn Liberated
1 Grafdigger's Cage
2 Ugin, the Spirit Dragon
2 Ulamog, the Ceaseless Hunger
2 Warping Wail
1 Kozilek's Return
1 World Breaker'''

    if 'format' in request.POST:
        for ff in context['current_formats']:
            if request.POST['format'] == ff.format:
                context['format'] = ff
                break
    if 'cardlist' in request.POST:
        card_list = request.POST['cardlist']
    deck_cards = Deck.read_cards_from_text(card_list, throw_exception=False)

    if 'errors' in deck_cards:
        context['errors'] = deck_cards['errors']
    context['card_list'] = list()
    for key in deck_cards:
        if key != 'errors' and isinstance(deck_cards[key], dict) and 'physicalcard' in deck_cards[key]:
            context['card_list'].append('{} {}'.format(deck_cards[key]['card_count'], deck_cards[key]['physicalcard'].get_card_name()))

    analyzer = Analyzer(format=context['format'])
    deck_score_tuples, query = analyzer.analyze(deck_cards)
    context['analysis'] = query
    context['analysis_json'] = json.dumps(query, sort_keys=True, indent=2, cls=DjangoJSONEncoder)
    context['deckcards'] = list()
    if len(deck_score_tuples):
        all_deckcards = DeckCard.objects.filter(deck=deck_score_tuples[0][0])
        context['recommendation_score'] = 1000.0 - deck_score_tuples[0][1]
        for dc in all_deckcards:
            if dc.physicalcard.get_face_basecard().is_land():
                context['deckcards'].append(dc)
    return render(request, 'decks/manabase.html', context)
Ejemplo n.º 2
0
def recommendations(request):
    context = BASE_CONTEXT.copy()
    context['current_formats'] = Format.objects.filter(start_date__lte=timezone.now(),
                                                       end_date__gt=timezone.now()).order_by('format')
    # REVISIT - unsafe index
    context['format'] = context['current_formats'][0]

    include_lands = 'exclude_lands' not in request.POST
    context['exclude_lands'] = not include_lands

    pcs = list()
    if 'format' in request.POST:
        for ff in context['current_formats']:
            if request.POST['format'] == ff.format:
                context['format'] = ff
                break
    if 'cardlist' in request.POST:
        try:
            pcs_dict = Deck.read_cards_from_text(request.POST['cardlist'], throw_exception=False)
            for key in pcs_dict:
                if key != 'errors' and 'physicalcard' in pcs_dict[key]:
                    pcs.append(pcs_dict[key]['physicalcard'])
        except Deck.CardsNotFoundException as cnfe:
            pass
        #sys.stderr.write("cardlist is '{}'".format(pcs))
    context['card_list'] = (a.get_card_name() for a in pcs)
    context['seeds'] = (a.get_latest_card() for a in pcs)
    context['recommendations'] = ()
    context['spicy'] = list()
    if len(pcs):
        dcr = Recommender()
        context['recommendations'] = dcr.get_recommended_cards(pcs, context['format'], include_lands=include_lands)

        # get spicy...
        basic_supertype = Supertype.objects.filter(supertype='Basic').first()
        #sys.stderr.write("Basic type is {}\n".format(basic_supertype))
        for tcard in context['recommendations']:
            if basic_supertype in tcard.basecard.types.all():
                # Let's not look up similar cards to Basic lands
                continue

            sims = tcard.basecard.physicalcard.find_similar_cards(max_results=4)
            for sim in sims:
                #sys.stderr.write("%%%% SIM IS {} for {}\n".format(sim, tcard))
                if sim.basecard.physicalcard in pcs:
                    #sys.stderr.write("-- skipping {} for spicy because the user already wants it!\n".format(sim))
                    continue
                # Let's not consider Basic lands as spicy
                if basic_supertype in sim.basecard.supertypes.all():
                    #sys.stderr.write("-- skipping basic land for spicy.\n")
                    continue
                # Let's only recommend cards in the current format
                if FormatBasecard.objects.filter(basecard=sim.basecard, format=context['format']).count() == 0:
                    #sys.stderr.write("-- skipping {} for spicy because it isn't in format.\n".format(sim))
                    continue
                if sim in context['recommendations']:
                    #sys.stderr.write("-- skipping {} for spicy because it is already recommended.\n".format(sim))
                    continue
                if sim in context['spicy']:
                    #sys.stderr.write("-- skipping {} for spicy because it is already spicy!\n".format(sim))
                    continue
                #sys.stderr.write("starting with score of {}\n".format(tcard.annotations['match_confidence']))
                sim.annotations['spicy_score'] = tcard.annotations['match_confidence'] + (sim.annotations['similarity_score'] * 50.0)
                context['spicy'].append(sim)
                break
            if len(context['spicy']) >= 8:
                break
    return render(request, 'decks/recommendations.html', context)