Esempio n. 1
0
def buydash(ticker, shares, league_id, player_id):
	league = League.objects.get(pk=league_id)
	player = Player.objects.get(pk=player_id)
	buyingPrice = getPriceFromAPI(ticker,False)
	tmpPrice = buyingPrice*shares
	if tmpPrice > player.buyingPower:
		storage = messages.get_messages(request)
		messages.add_message(request, messages.ERROR, 'You do not have sufficient balance.')
		storage.used = False
		return render(request, 'dashboard.html')
	pAssets = Asset.objects.filter(leagueID = league,playerID = player.id)
	assetExists = False
	for i in pAssets:
		if i.ticker == ticker:
			assetExists = True
			new_asset = i
	if assetExists == False:
		new_asset = Asset(ticker = ticker, playerID = player.id, leagueID = league, shares = shares, buyingPrice = buyingPrice)
	else:
		new_asset.shares += shares
		new_asset.buyingPrice = buyingPrice
	new_asset.save()
	player.totalWorth += tmpPrice
	new_transaction = Transaction(leagueID = league, playerID = player.id, price = tmpPrice, ticker = ticker, shares = shares, isBuy = True)
	player.buyingPower -= tmpPrice
	player.cumWorth = player.totalWorth + player.buyingPower
	player.save()
	new_transaction.save()
Esempio n. 2
0
def sms(request):
	league = League.objects.get(name="SoftwareEngineering")
	player = Player.objects.get(id=18)
	purchase = request.POST.get('Body', '')
	processed = purchase.split()
	print(processed)
	operation = processed[0]
	shares = decimal.Decimal(processed[1])
	ticker = processed[2]
	if operation == "BUY":
		buyingPrice = getPriceFromAPI(ticker,False) #allow crypto in future
		tmpPrice = buyingPrice*decimal.Decimal(shares)
		message = '<Response><Message>You bought %s shares of %s for $%s</Message></Response>' % (shares,ticker, tmpPrice)
		if tmpPrice > player.buyingPower:
			return redirect('/home')
		new_asset = Asset(ticker = ticker, playerID = player.id, leagueID = league, shares = shares, buyingPrice = buyingPrice)
		new_asset.save()
		new_transaction = Transaction(leagueID = league, playerID = player.id, price = tmpPrice, ticker = ticker, shares = shares, isBuy = True)
		player.buyingPower = player.buyingPower-tmpPrice
		player.totalWorth += tmpPrice
		player.cumWorth = player.buyingPower + player.totalWorth
		player.save()
		new_transaction.save()
		return HttpResponse(message, content_type='text/xml')

	if operation == "SELL":
		buyingPrice = getPriceFromAPI(ticker,False) #allow crypto in future
		tmpPrice = buyingPrice*decimal.Decimal(shares)
		asset = Asset.objects.get(ticker=ticker,playerID=18,leagueID=league.id)
		message = '<Response><Message>You sold %s shares of %s for $%s</Message></Response>' % (shares,ticker, tmpPrice)
		currShares = asset.shares
		asset.shares = currShares - shares
		if asset.shares == 0:
			asset.delete()
		else:
			asset.save()
		player.buyingPower = player.buyingPower+tmpPrice
		player.totalWorth -= tmpPrice
		player.cumWorth = player.buyingPower + player.totalWorth
		player.save()

		return HttpResponse(message, content_type='text/xml')
Esempio n. 3
0
def selldash(shares,player_id, league_id, asset_id):
	player = Player.objects.get(pk=player_id)
	league = League.objects.get(pk=league_id)
	asset = Asset.objects.get(pk=asset_id)
	currShares = asset.shares
	if shares > currShares:
		storage = messages.get_messages(request)
		messages.add_message(request, messages.ERROR, 'You do not own this many shares.')
		storage.used = False
		#raise forms.ValidationError("You do not own this many shares.")
		return render(request, 'sellform.html', {'form': form, 'league':league,'player':player,'asset':asset})
	marketPrice = getPriceFromAPI(asset.ticker, False)
	sellTotal = marketPrice*shares
	player.buyingPower += sellTotal
	player.totalWorth -= sellTotal
	player.cumWorth = player.buyingPower + player.totalWorth
	player.save()
	asset.shares = currShares - shares
	if asset.shares == 0:
		asset.delete()
	else:
		asset.save()
Esempio n. 4
0
def leagues(request,league_id):
	current_user = request.user
	league = League.objects.get(pk=league_id)
	endDate = league.endDate
	presentDate = timezone.now()
	players = Player.objects.filter(leagueID = league).order_by('-cumWorth')
	for i in players:
		worth = 0
		assets = Asset.objects.filter(playerID=i.id)
		for a in assets:
				if league.isCrypto:
					marketPrice = getCryptoPriceFromAPI2(a.ticker, league.isCrypto)
					print(marketPrice)
				else:
					marketPrice = getPriceFromAPI(a.ticker, league.isCrypto)
				worth+= (marketPrice*a.shares)
		i.totalWorth = worth
		i.cumWorth = i.totalWorth + i.buyingPower
		i.save()
	players = Player.objects.filter(leagueID = league).order_by('-cumWorth')
	count = 0

	rank = 0
	numAIbeat = 0
	for p in players:
		count+=1
		if p.userID.id == league.adminID:
			admin = p.userID # admin is auth_user object
		if p.userID.id == request.user.id:
			currPlayer = p
			rank = count
		if p.isAi and count > rank: # AI placing worse than user
			numAIbeat += 1
	if (endDate < presentDate): # league has ended, redirect to leaderboard.html
		if league.hasEnded == False: # need to handle trophies
			league.hasEnded = True
			current_user.profile.trophies[3] += 1 # increment for game played
			current_user.profile.TitanCoins += 50
			if rank < 4: # top 3 = win
				current_user.profile.trophies[2] += 1 # increment for win
				current_user.profile.TitanCoins += 100
			current_user.profile.trophies[4] = current_user.profile.trophies[4] + numAIbeat
			if numAIbeat>0:
				current_user.profile.TitanCoins += 25*numAIbeat
			if admin.id == request.user.id: # this user is admin
				if current_user.profile.trophies[5] < count: # new record for # ppl managed
					current_user.profile.trophies[5] = count
					current_user.profile.TitanCoins += 100
			current_user.save()
			#current_user.profile.TitanCoins +=
		return render(request, 'leaderboard.html', {'league': league, 'admin': admin, 'players':players,'currPlayer':currPlayer, 'rank':rank})

	pAssets = Asset.objects.filter(playerID = currPlayer.id)
	assetWorth = list()
	for a in pAssets:
		if league.isCrypto:
			marketPrice = getCryptoPriceFromAPI2(a.ticker, league.isCrypto)
		else:
			marketPrice = getPriceFromAPI(a.ticker, league.isCrypto)
		assetWorth.append(marketPrice*a.shares)
	#print(pAssets)

	return render(request, 'individualleague.html', {'league': league, 'admin': admin, 'players':players,'currPlayer':currPlayer,'pAssets':pAssets,'rank':rank,'assetWorth':assetWorth})
Esempio n. 5
0
def submitSell(request,league_id,player_id,asset_id):
	current_user = request.user
	if request.method == 'POST':
		form = SellForm(request.POST)
		if (form.is_valid() and current_user.is_authenticated):
			shares = form.cleaned_data.get('shares')
			player = Player.objects.get(pk=player_id)
			league = League.objects.get(pk=league_id)
			asset = Asset.objects.get(pk=asset_id)
			currShares = asset.shares
			if shares > currShares:
				storage = messages.get_messages(request)
				messages.add_message(request, messages.ERROR, 'You do not own this many shares.')
				storage.used = False
				return render(request, 'sellform.html', {'form': form, 'league':league,'player':player,'asset':asset})
			#marketPrice = getPriceFromAPI(asset.ticker, False)
			if league.isCrypto == True:
				marketPrice = getCryptoPriceFromAPI2(asset.ticker, True)
			else:
				marketPrice = getPriceFromAPI(asset.ticker,False) #allow crypto in future
			if marketPrice == -1:
				storage = messages.get_messages(request)
				messages.add_message(request, messages.ERROR, 'Please enter a valid ticker. Cryptocurrency leagues only accept cryptocurrency.')
				storage.used = False
				return render(request, 'sellform.html', {'form': form, 'league':league,'player':player,'asset':asset})
			elif marketPrice == -22:
				storage = messages.get_messages(request)
				messages.add_message(request, messages.ERROR, 'Too many requests at this time.')
				storage.used = False
				return render(request, 'sellform.html', {'form': form, 'league':league,'player':player,'asset':asset})
			elif marketPrice <0:
				storage = messages.get_messages(request)
				messages.add_message(request, messages.ERROR, 'Invalid Input.')
				storage.used = False
				return render(request, 'sellform.html', {'form': form, 'league':league,'player':player,'asset':asset})
			sellTotal = marketPrice*shares
			player.buyingPower += sellTotal
			player.totalWorth -= sellTotal
			player.cumWorth = player.buyingPower + player.totalWorth
			player.save()
			# update trophies array to count sells
			current_user.profile.trophies[1] +=1
			current_user.save()
			asset.shares = currShares - shares
			if asset.shares == 0:
				asset.delete()
			else:
				asset.save()
			try:
				message = client.messages.create(
					to="+17329985271",
					from_="+17325079667",
					body="You sold %d shares of %s at $%d!" % (shares,ticker,tmpPrice))
			except:
				pass
			url = '/leagues/'+str(league.id)+'/'
			return redirect(url)

		else:


			return render(request, 'sellform.html', {'form': form})
	else:
		return render(request, 'buypage.html')
Esempio n. 6
0
def submitBuy(request,league_id,player_id):
	league = League.objects.get(pk=league_id)
	player = Player.objects.get(pk=player_id)
	form = BuyForm(request.POST)


	if True:
		form.is_valid()

		current_user = request.user
		ticker = form.cleaned_data.get('ticker')
	# if(ticker == 'GOOG'):
		# return redirect('/processInvalid')
	# else:
		shares = form.cleaned_data.get('shares')
		#isCrypto = form.cleaned_data.get('isCrypto')
		isCrypto = league.isCrypto
		print(isCrypto)
		#buyingPrice = form.cleaned_data.get('buyingPrice')
		if isCrypto == True:
			buyingPrice = getCryptoPriceFromAPI2(ticker, True)
		else:
			buyingPrice = getPriceFromAPI(ticker,False) #allow crypto in future
		if buyingPrice == -1:
			storage = messages.get_messages(request)
			messages.add_message(request, messages.ERROR, 'Please enter a valid ticker. Cryptocurrency leagues only accept cryptocurrency.')
			storage.used = False
			return render(request, 'buypage.html', {'form': form,'league':league,'player':player})
		elif buyingPrice == -22:
			storage = messages.get_messages(request)
			messages.add_message(request, messages.ERROR, 'Too many requests at this time.')
			storage.used = False
			return render(request, 'buypage.html', {'form': form,'league':league,'player':player})
		elif buyingPrice <0:
			storage = messages.get_messages(request)
			messages.add_message(request, messages.ERROR, 'Invalid Input.')
			storage.used = False
			return render(request, 'buypage.html', {'form': form,'league':league,'player':player})
		tmpPrice = buyingPrice*shares
		if tmpPrice > player.buyingPower:
			storage = messages.get_messages(request)
			messages.add_message(request, messages.ERROR, 'You do not have sufficient balance.')
			storage.used = False
			return render(request, 'buypage.html', {'form': form,'league':league,'player':player})
		pAssets = Asset.objects.filter(leagueID = league,playerID = player.id)
		assetExists = False
		for i in pAssets:
			if i.ticker == ticker:
				assetExists = True
				new_asset = i
		if assetExists == False:
			new_asset = Asset(ticker = ticker, playerID = player.id, leagueID = league, shares = shares, buyingPrice = buyingPrice)
		else:
			new_asset.shares += shares
			new_asset.buyingPrice = buyingPrice
		new_asset.save()
		player.totalWorth += tmpPrice
		new_transaction = Transaction(leagueID = league, playerID = player.id, price = tmpPrice, ticker = ticker, shares = shares, isBuy = True)
		player.buyingPower -= tmpPrice
		player.cumWorth = player.totalWorth + player.buyingPower
		player.save()
		new_transaction.save()
		try:
			message = client.messages.create(
				to="+17329985271",
				from_="+17325079667",
				body="You bought %d shares of %s at $%d!" % (shares,ticker,tmpPrice))
		except:
			pass
		# update trophies array to count buys
		current_user.profile.trophies[0] += 1
		current_user.save()

		url = '/receipt/'+str(new_transaction.id)+'/'
		return redirect(url)
	else:
		return render(request, 'buypage.html', {'form': form,'league':league,'player':player})