#!/usr/bin/env python
from bulletproof_vault import mystery_item
from random import getrandbits

rnd_bits = 0
rnd_data = 0
def roll_a_dice ():
	global rnd_bits
	global rnd_data
	if rnd_bits < 32:
		rnd_data |= getrandbits(32) << rnd_bits
		rnd_bits += 32
	x = rnd_data & ((1 << 32) - 1)
	rnd_data >>= 1
	rnd_bits -= 1
	return (x % 6) + 1

def winnings(stakes, score):
	if score >= 25:
		return int (score * 0.25) * stakes
	elif score >= 16:
		return 2 * stakes
	else:
		return int (score * stakes / 16)

balance = 10000

print ('        KEBAB CASINO')
print ('     Let\'s go gambling!')
print ('')
input ('  Start slot machine')

try:
	while True:
		print (f'Balance: {balance} KebabCoin')
		print ('  1. Gamble.')
		print ('  2. Cash out.')
		x = int (input ('Select action... '))
		if x == 2:
			break
		assert (x == 1)

		x = int (input ('Insert coins: '))
		assert x > 0
		if x > balance:
			input ('Insufficient funds!')
			continue

		balance -= x
		score = 0
		cdice = 3
		while cdice > 0:
			ndice = 0
			print (f'Number of dice: {cdice}')
			print (f'Current winnings: {winnings(x, score)}')
			input ('Roll...? ')
			for _ in range (cdice):
				value = roll_a_dice ()
				if value == 6:
					print ('Rolled a 6!  New dice!')
					ndice += 2
				else:
					print (f'Rolled a {value}{"!" if value >= 4 else "."}')
					score += value
			cdice = ndice
			print (f'Score: {score}')
			input ('Continue?')
		w = winnings (x, score)
		input (f'You are out of dice!  You achieved a score of {score}, earning you {w} KebabCoin! ')
		balance += w
	if balance < 1000:
		print ('99% of gamblers quit before they earn back their losses.')
	else:
		print (f'You have {balance} KBB (KeBaBCoin).')
		print ('  1. Deposit into KebabBankeN.')
		print ('  2. Convert it to Swedish legal tender.')
		print ('  3. Buy a mystery item from the bulletproof vault (13379876543210 KBB).')
		x = int (input (f'What do you want to do with your {"winnings" if balance > 1000 else "money"}? '))

		if x == 1:
			print ('Clearing transaction...  OK')
			print ('Proceeding with transfer...  OK')
			print ('Thank you for using KebabBankeN!  We pride ourselves in military-grade security with advanced blockchain technology to protect your assets.  Integrity, privacy, confidentiality!')
		elif x == 2:
			print ('Our current exchange rate for KBB/SEK is  0.00 SEK per KBB.')
			print ('Thank you for choosing KebabBankeN\'s monetary exchange service.')
		else:
			assert (x == 3)
			if balance < 13379876543210:
				print ('Insufficient funds!')
			else:
				print (f'Here is your mystery item: {mystery_item}')
				print ('')
				print ('        KEBAB CASINO')
				print ('  Welcome back, any time!')
				print ('')
except:
	print ('Cheating is against the rules!')

