def list_cards_in_pile(request, key, pile): try: deck = Deck.objects.get(key=key) except Deck.DoesNotExist: return deck_id_does_not_exist() piles = {} for k in deck.piles: # iterate through all the piles and list cards for specified pile. r = len(deck.piles[k]) if k != pile: piles[k] = {"remaining": r} else: a = [] for card in deck.piles[k]: a.append(card_to_dict(card)) piles[k] = {"remaining": r, "cards": a} resp = { 'success': True, 'deck_id': deck.key, 'remaining': len(deck.stack), 'piles': piles } response = HttpResponse(json.dumps(resp), content_type="application/json") response['Access-Control-Allow-Origin'] = '*' return response
def draw(request, key): success = True card_count = int(_get_request_var(request, 'count')) 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
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
def draw_from_pile(request, key, pile): 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) cards = _get_request_var(request, 'cards', None) cards_in_response = [] p = deck.piles[ pile] #times like these that I question if I should have just made piles a model instead of a json field... if cards: # Ignore case cards = cards.upper() # Only allow real cards cards = [x for x in cards.split(',') if x in CARDS] for card in cards: try: p.remove(card) cards_in_response.append(card) except: response = HttpResponse(json.dumps({'success':False,'error':'The pile, '+pile+\ ' does not contain the requested cards.'}), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response else: card_count = int(_get_request_var(request, 'count')) if card_count > len(p): response = HttpResponse(json.dumps({'success':False,'error':'Not enough cards remaining to draw '+\ str(card_count)+' additional'}), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response cards_in_response = p[-card_count:] p = p[:-card_count] deck.piles[pile] = p deck.save() a = [] for card in cards_in_response: a.append(card_to_dict(card)) piles = {} for k in deck.piles: piles[k] = {"remaining": len(deck.piles[k])} resp = {'success': True, 'deck_id': deck.key, 'cards': a, 'piles': piles} response = HttpResponse(json.dumps(resp), content_type="application/json") response['Access-Control-Allow-Origin'] = '*' return response
def draw_from_pile(request, key, pile): 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) cards = _get_request_var(request, 'cards', None) cards_in_response = [] p = deck.piles[pile] #times like these that I question if I should have just made piles a model instead of a json field... if cards: # Ignore case cards = cards.upper() # Only allow real cards cards = [x for x in cards.split(',') if x in CARDS] for card in cards: try: p.remove(card) cards_in_response.append(card) except: response = HttpResponse(json.dumps({'success':False,'error':'The pile, '+pile+\ ' does not contain the requested cards.'}), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response else: card_count = int(_get_request_var(request, 'count')) if card_count > len(p): response = HttpResponse(json.dumps({'success':False,'error':'Not enough cards remaining to draw '+\ str(card_count)+' additional'}), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response cards_in_response = p[-card_count:] p = p[:-card_count] deck.piles[pile] = p deck.save() a = [] for card in cards_in_response: a.append(card_to_dict(card)) piles = {} for k in deck.piles: piles[k] = {"remaining":len(deck.piles[k])} resp = {'success':True, 'deck_id':deck.key, 'cards':a, 'piles':piles} response = HttpResponse(json.dumps(resp), content_type="application/json") response['Access-Control-Allow-Origin'] = '*' return response
def draw(request, key): card_count = int(_get_request_var(request, 'count')) deck = Deck.objects.get(key=key) 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)) resp = {'success':True, 'deck_id':deck.key, 'cards':a, 'remaining':len(deck.stack)} return HttpResponse(json.dumps(resp), content_type="application/json")
def draw(request, key): card_count = int(_get_request_var(request, 'count')) deck = Deck.objects.get(key=key) 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)) resp = { 'success': True, 'deck_id': deck.key, 'cards': a, 'remaining': len(deck.stack) } return HttpResponse(json.dumps(resp), content_type="application/json")
def draw_from_pile(request, key, pile, bottom=""): jokers_enabled = get_jokers_enabled(request) try: deck = Deck.objects.get(key=key) except Deck.DoesNotExist: return deck_id_does_not_exist() cards = _get_request_var(request, 'cards', None) cards_in_response = [] p = deck.piles[ pile] # times like these that I question if I should have just made piles a model instead of a json field... if cards: # Ignore case cards = cards.upper() # Only allow real cards cards = [x for x in cards.split(',') if x in CARDS + JOKERS] for card in cards: try: p.remove(card) cards_in_response.append(card) except: response = HttpResponse(json.dumps({ 'success': False, 'error': 'The pile, %s does not contain the requested cards.' % pile }), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response else: card_count = int(_get_request_var(request, 'count')) if card_count > len(p): response = HttpResponse(json.dumps({ 'success': False, 'error': 'Not enough cards remaining to draw %s additional' % str(card_count) }), content_type="application/json", status=404) response['Access-Control-Allow-Origin'] = '*' return response if bottom.lower() == "bottom": # draw from the bottom of the pile cards_in_response = p[0:card_count] p = p[card_count:len(p)] else: # draw cards from the top of the pile cards_in_response = p[-card_count:] p = p[:-card_count] deck.piles[pile] = p deck.save() a = [] for card in cards_in_response: a.append(card_to_dict(card)) piles = {} for k in deck.piles: piles[k] = {"remaining": len(deck.piles[k])} resp = {'success': True, 'deck_id': deck.key, 'cards': a, 'piles': piles} response = HttpResponse(json.dumps(resp), content_type="application/json") response['Access-Control-Allow-Origin'] = '*' return response