Example #1
0
def draw(request, key=None):
    success = True
    card_count = int(_get_request_var(request, 'count'))
    if not key:
        deck = Deck()
        deck.deck_count = int(_get_request_var(request, 'deck_count'))
        deck.open_new()
        random.shuffle(deck.stack)
        deck.shuffled = True
        deck.save()
    else:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            response = HttpResponse(json.dumps({'success':False,'error':'Deck ID does not exist.'}), content_type="application/json", status=404)
            response['Access-Control-Allow-Origin'] = '*'
            return response
    if card_count > len(deck.stack):
        success = False
    cards = deck.stack[0:card_count]
    deck.stack = deck.stack[card_count:]
    deck.save()

    a = []
    for card in cards:
        a.append(card_to_dict(card))
    if not success:
        resp = {'success':success, 'deck_id':deck.key, 'cards':a, 'remaining':len(deck.stack), 'error':'Not enough cards remaining to draw '+str(card_count)+' additional'}
    else:
        resp = {'success':success, 'deck_id':deck.key, 'cards':a, 'remaining':len(deck.stack)}

    response = HttpResponse(json.dumps(resp), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Example #2
0
def new_deck(request, key='', shuffle=False):
    #print(request.META['HTTP_ACCEPT'])
    deck_count = int(_get_request_var(request, 'deck_count'))
    deck_cards = _get_request_var(request, 'cards', None)
    if deck_count > 20:
        response = HttpResponse(json.dumps({'success':False,'error':'The max number of Decks is 20.'}), content_type="application/json")
        response['Access-Control-Allow-Origin'] = '*'
        return response
    if key:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            response = HttpResponse(json.dumps({'success':False,'error':'Deck ID does not exist.'}), content_type="application/json", status=404)
            response['Access-Control-Allow-Origin'] = '*'
            return response
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.open_new(deck_cards)
    shuffled = False
    if shuffle:
        random.shuffle(deck.stack)
        shuffled = True
    deck.save() #save the deck_count.

    resp = {'success':True, 'deck_id':deck.key, 'remaining':len(deck.stack), 'shuffled':shuffled}

    response = HttpResponse(json.dumps(resp), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Example #3
0
def draw(request, key=None):
    success = True
    card_count = int(_get_request_var(request, 'count'))
    if not key:
        deck = Deck()
        deck.deck_count = int(_get_request_var(request, 'deck_count'))
        deck.open_new()
        random.shuffle(deck.stack)
        deck.save()
    else:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            response = HttpResponse(json.dumps({'success':False,'error':'Deck ID does not exist.'}), content_type="application/json", status=404)
            response['Access-Control-Allow-Origin'] = '*'
            return response
    if card_count > len(deck.stack):
        success = False
    cards = deck.stack[0:card_count]
    deck.stack = deck.stack[card_count:]
    deck.save()

    a = []
    for card in cards:
        a.append(card_to_dict(card))
    if not success:
        resp = {'success':success, 'deck_id':deck.key, 'cards':a, 'remaining':len(deck.stack), 'error':'Not enough cards remaining to draw '+str(card_count)+' additional'}
    else:
        resp = {'success':success, 'deck_id':deck.key, 'cards':a, 'remaining':len(deck.stack)}

    response = HttpResponse(json.dumps(resp), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Example #4
0
def new_deck(request, key='', shuffle=False):
    #print(request.META['HTTP_ACCEPT'])
    deck_count = int(_get_request_var(request, 'deck_count'))
    deck_cards = _get_request_var(request, 'cards', None)
    if deck_count > 20:
        response = HttpResponse(json.dumps({'success':False,'error':'The max number of Decks is 20.'}), content_type="application/json")
        response['Access-Control-Allow-Origin'] = '*'
        return response
    if key:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            response = HttpResponse(json.dumps({'success':False,'error':'Deck ID does not exist.'}), content_type="application/json", status=404)
            response['Access-Control-Allow-Origin'] = '*'
            return response
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.open_new(deck_cards)
    shuffled = False
    if shuffle:
        random.shuffle(deck.stack)
        shuffled = True
    deck.save() #save the deck_count.

    resp = {'success':True, 'deck_id':deck.key, 'remaining':len(deck.stack), 'shuffled':shuffled}

    response = HttpResponse(json.dumps(resp), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Example #5
0
def new_deck(request, key='', shuffle=False):
    deck_count = int(_get_request_var(request, 'deck_count'))
    if deck_count > 20:
        return HttpResponse(json.dumps({'success':False,
                            'error':'The max number of Decks is 20.'}),
                            content_type="application/json")
    if key:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            return HttpResponse(json.dumps({'success': False,
                                'error':'Deck ID does not exist.'}),
                                content_type="application/json", status=404)
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.open_new()
    shuffled = False
    if shuffle:
        random.shuffle(deck.stack)
        shuffled = True
    deck.save()     # save the deck_count.
    if shuffled:
        resp = {'success': True,
                'deck_id': deck.key,
                'remaining': len(deck.stack),
                'shuffled': False}
    else:
        resp = {'success':True,
                'deck_id':deck.key,
                'remaining':len(deck.stack)}
    return HttpResponse(json.dumps(resp), content_type="application/json")
Example #6
0
def new_deck(request, key='', shuffle=False):
    deck_count = int(_get_request_var(request, 'deck_count'))
    deck_cards = _get_request_var(request, 'cards', None)
    jokers_enabled = get_jokers_enabled(request)
    print("new deck", jokers_enabled)
    if deck_count > 20:
        response = HttpResponse(
            json.dumps({'success': False, 'error': 'The max number of Decks is 20.'}),
            content_type="application/json"
        )
        response['Access-Control-Allow-Origin'] = '*'
        return response
    if key:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            deck_id_does_not_exist()
            
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.open_new(deck_cards, jokers_enabled)
    deck.shuffled = False
    if shuffle:
        random.shuffle(deck.stack)
        deck.shuffled = True
    deck.save()  # save the deck_count.

    resp = {'success': True, 'deck_id': deck.key, 'remaining': len(deck.stack), 'shuffled': deck.shuffled}

    response = HttpResponse(json.dumps(resp), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    return response
Example #7
0
def deck(request, 
          template_name="deck/index.html"
          ):
    if request.method == 'POST':
        form = DeckForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            slug = slugify(name)
            user = request.user
            format = form.cleaned_data['format']
            type = form.cleaned_data['type']
            packs = form.cleaned_data['packs']
            deck_list = form.cleaned_data['deck_list']
            deck = Deck(name = name,
                        slug=slug,
                        user=user,
                        format = format,
                        type = type,
                        packs = packs,
                        deck_list = deck_list
                        )
            deck.save()
            return HttpResponseRedirect('/deck/')
    else:
        form = DeckForm()
    
    decks = Deck.objects.filter(is_active=True).order_by('-tourney_wins')
    inactive_decks = Deck.objects.filter(is_active=False).order_by('-tourney_wins')
    users = User.objects.all()
    user = request.user
    if user in users:
        username = request.user.username
        user_decks = Deck.objects.filter(user=user)
    
    return render(request,
                  template_name, 
                  locals(), 
                  context_instance=RequestContext(request))
Example #8
0
def new_deck(request, key='', shuffle=False):
    deck_count = int(_get_request_var(request, 'deck_count'))
    if deck_count > 20:
        return HttpResponse(json.dumps({'success':False,'error':'The max number of Decks is 20.'}), content_type="application/json")
    if key:
        try:
            deck = Deck.objects.get(key=key)
        except Deck.DoesNotExist:
            return HttpResponse(json.dumps({'success':False,'error':'Deck ID does not exist.'}), content_type="application/json", status=404)
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.open_new()
    shuffled = False
    if shuffle:
        random.shuffle(deck.stack)
        shuffled = True
    deck.save() #save the deck_count.
    if shuffled:
        resp = {'success':True, 'deck_id':deck.key, 'remaining':len(deck.stack), 'shuffled':False}        
    else:
        resp = {'success':True, 'deck_id':deck.key, 'remaining':len(deck.stack)}
    return HttpResponse(json.dumps(resp), content_type="application/json")