def list_item(request): if request.method != 'POST': return JsonResponse({'listError': "Request error"}, status=400) authentication = authenticate(request, 'listError', "Authentication error") if not authentication[0]: return authentication[1] decoded = authentication[1] try: numPrice = int(request.POST.get('price')) except: return JsonResponse({'listError': "Invalid price"}, status=400) if numPrice < 1 or numPrice > 10000000: return JsonResponse({'listError': "Invalid price"}, status=400) user = User.objects.get(username=decoded['user']) if user.sp - math.floor(int(request.POST.get('price')) / 20) < 0: return JsonResponse({'listError': "Not enough SP"}, status=400) try: inventoryItem = InventoryItem.objects.get( id=request.POST.get('inventoryID'), user=user) except: return JsonResponse({'listError': "You don't have that item"}, status=400) user.sp -= math.floor(int(request.POST.get('price')) / 20) user.save() if inventoryItem.quantity > 1: inventoryItem.quantity -= 1 inventoryItem.save() else: inventoryItem.delete() marketItem = MarketItem.objects.create(user=user, item=inventoryItem.item, price=int( request.POST.get('price')), listTime=time.time()) body = { marketItem.id: { 'itemName': marketItem.item.name, 'seller': marketItem.user.username, 'rarity': marketItem.item.rarity, 'price': marketItem.price, 'listTime': int(marketItem.listTime * 1000) } } Pool(1).apply_async(requests.post, [WEB_SOCKET_BASE_DIR + 'item-listed'], {'json': body}) return JsonResponse({})
def auto_log_in(request): if request.method != 'POST': return JsonResponse({'error': "Request error"}, status=400) authentication = authenticate(request, '', '') if not authentication[0]: return authentication[1] decoded = authentication[1] user = User.objects.get(username=decoded['user']) return JsonResponse({ 'expirationDate': int(decoded['exp'] * 1000), 'sp': user.sp })
def buy_item(request): if request.method != 'POST': return JsonResponse({'buyError': "Request error"}, status=400) authentication = authenticate(request, 'buyError', "Authentication error") if not authentication[0]: return authentication[1] decoded = authentication[1] buyer = User.objects.get(username=decoded['user']) try: marketItem = MarketItem.objects.get(id=request.POST.get('marketID')) except: return JsonResponse({'buyError': "Item already bought"}, status=400) if buyer.sp < marketItem.price: return JsonResponse({'buyError': "Not enough SP"}, status=400) if buyer == marketItem.user: return JsonResponse({'buyError': "Cannot buy own item"}, status=400) clone = marketItem marketItem.delete() body = { 'marketID': request.POST.get('marketID'), 'seller': clone.user.username, 'price': clone.price } requests.post(WEB_SOCKET_BASE_DIR + 'item-bought', data=body) buyer.sp -= clone.price buyer.save() inventoryItem, created = InventoryItem.objects.get_or_create( user=buyer, item=clone.item, defaults={'quantity': 1}) if not created: inventoryItem.quantity += 1 inventoryItem.save() seller = clone.user seller.sp += clone.price seller.net_sp += clone.price seller.save() return JsonResponse({})
def fetch_inventory(request): if request.method != 'GET': return JsonResponse({'fetchError': "Request error"}, status=400) authentication = authenticate(request, 'fetchError', "Must be logged in...") if not authentication[0]: return authentication[1] decoded = authentication[1] user = User.objects.get(username=decoded['user']) response = {} filtered = InventoryItem.objects.filter(user=user) for x in range(filtered.count()): response['{}'.format(filtered[x].item.name)] = { 'quantity': filtered[x].quantity, 'rarity': filtered[x].item.rarity, 'inventoryID': filtered[x].id } return JsonResponse(response)
def free_sp(request): if request.method != 'GET': return JsonResponse({'freeSPError': "Request error"}, status=400) authentication = authenticate(request, 'freeSPError', "Authentication error") if not authentication[0]: return authentication[1] decoded = authentication[1] user = User.objects.get(username=decoded['user']) if time.time() - user.last_free_sp_time >= 7200: freeSPAmount = random.randint(1500, 3000) user.sp = user.sp + freeSPAmount user.net_sp = user.net_sp + freeSPAmount user.last_free_sp_time = time.time() user.save() return JsonResponse({'freeSP': freeSPAmount}) timeLeft = int((7200 - (time.time() - user.last_free_sp_time)) * 1000) return JsonResponse({'freeSPError': timeLeft}, status=400)
def buy_spin(request): if request.method != 'POST': return JsonResponse({'buyError': "Request error"}, status=400) authentication = authenticate(request, 'buyError', "Authentication error") if not authentication[0]: return authentication[1] decoded = authentication[1] # Subtract SP user = User.objects.get(username=decoded['user']) if user.sp - 500 < 0: return JsonResponse({'buyError': "Not enough SP"}, status=400) user.sp -= 500 user.save() # Determine InventoryItem, add InventoryItem to inventory, and update # InventoryItem's Item's in_circulation degree = random.random() * 360 items = Item.objects.filter(rarity=map_degree_to_rarity(degree)) index = random.randrange(items.count()) obj, created = InventoryItem.objects.get_or_create( user=user, item=items[index], defaults={'quantity': 1}) if not created: obj.quantity += 1 obj.save() item = Item.objects.get(name=items[index].name) item.in_circulation += 1 item.save() body = { 'itemName': obj.item.name, 'rarity': obj.item.rarity, 'unboxer': user.username } Pool(1).apply_async(requests.post, [WEB_SOCKET_BASE_DIR + 'item-unboxed'], {'data': body}) # Update stats user.total_spins += 1 if item.rarity == '???': user.tq_unboxed += 1 else: user.__dict__['{}_unboxed'.format(item.rarity).lower()] += 1 # if created: # user.items_found += 1 user.save() # Create response response = {'degree': degree} response['item'] = { 'name': "{}".format(item), 'rarity': item.rarity, 'description': item.description, 'circulationNum': item.in_circulation, 'quantity': obj.quantity } return JsonResponse(response)