def newplayer(request): success = 0 playerid = 0 playername = '' if request.method == 'POST': form = NewPlayerForm(request.POST) if form.is_valid(): cd = form.cleaned_data player = Player(name=cd['name']) player.save() squares = Square.objects.all().order_by('pk') if len(squares)-1 >= 24: squarei = random.sample(range(1,len(squares)),24) elif len(squares)-1 > 0: squarei = [] for i in range(24): squarei.append(random.randint(1,len(squares)-1)) else: squarei = [] for i in range(24): squarei.append(0) for i in range(25): if i < 12: spot = Boardsquare(player=player,square=squares[squarei[i]],checked=False,order=i) elif i == 12: spot = Boardsquare(player=player,square=squares[0],checked=True,order=i) else: spot = Boardsquare(player=player,square=squares[squarei[i-1]],checked=False,order=i) spot.save() playerid = player.pk playername = player.name success = 1 else: form = NewPlayerForm() return render(request, 'bingo/newplayer.html', {'form': form, 'success': success, 'id': playerid, 'name': playername})
def cashinbingo(request,playerid): player = Player.objects.get(pk=int(playerid)) if not player.bingo: raise Http404 else: checked = [] squares = Square.objects.all().order_by('pk') newsquares = list(Square.objects.filter(pk__gt=1).order_by('pk')) boardsquares = Boardsquare.objects.filter(player=player).order_by('order') for boardsquare in boardsquares: while boardsquare.square in newsquares: newsquares.remove(boardsquare.square) if boardsquare.checked: checked.append(1) else: checked.append(0) #totalsquares = Square.objects.count() lines = [] for i in range(5): lines.append([5*i,5*i+1,5*i+2,5*i+3,5*i+4]) lines.append([i,5+i,10+i,15+i,20+i]) lines.append([0,6,12,18,24]) lines.append([4,8,12,16,20]) for line in lines: if all(checked[i] == 1 for i in line): for pos in line: order = boardsquares[pos].order if len(newsquares) > 0: i = random.randint(0,len(newsquares)-1) newsquare = Boardsquare(player=player,square=newsquares[i],order=order,checked=False) newsquares.remove(newsquares[i]) elif len(squares)-1 > 0: i = random.randint(1,len(squares)-1) newsquare = Boardsquare(player=player,square=squares[i],order=order,checked=False) else: newsquare = Boardsquare(player=player,square=squares[0],order=order,checked=False) boardsquares[pos].order = 100 boardsquares[pos].save() newsquare.save() break player.stashed_bingos += 1 player.save() return HttpResponseRedirect(reverse('bingo.views.playerview',args=(playerid,)))