Example #1
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 #2
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 #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.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 #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 shuffle(request, key=''):
    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:
        deck = Deck.objects.get(key=key)
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.shuffle()
    resp = {'success':True, 'deck_id':deck.key, 'remaining':len(deck.stack)}
    return HttpResponse(json.dumps(resp), content_type="application/json")
Example #8
0
def shuffle(request, key=''):
    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:
        deck = Deck.objects.get(key=key)
    else:
        deck = Deck()
        deck.deck_count = deck_count
    deck.shuffle()
    resp = {'success': True, 'deck_id': deck.key, 'remaining': len(deck.stack)}
    return HttpResponse(json.dumps(resp), content_type="application/json")
Example #9
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")