Exemple #1
0
def offers_handler():
    items = Item.objects.all()
    offers = Offer.objects.filter(is_active=True)
    for item in items:
        for buy_offer in offers.filter(item=item,
                                       order_type=OrderType.BUY.value):
            buy_offer_unit_price = ItemTransfer.get_unit_price(buy_offer)
            sale_offers = offers.filter(item=item,
                                        order_type=OrderType.SALE.value,
                                        price__lte=buy_offer.price)
            valid_sale_offers = filter(
                lambda offer: buy_offer_unit_price >= ItemTransfer.
                get_unit_price(offer), sale_offers)
            for sale_offer in sorted(
                    valid_sale_offers,
                    key=lambda offer: ItemTransfer.get_unit_price(offer)):
                diff = min(buy_offer.quantity, sale_offer.quantity)
                Trade(item=item,
                      seller=sale_offer.user,
                      buyer=buy_offer.user,
                      quantity=diff,
                      unit_price=sale_offer.price / sale_offer.quantity,
                      buyer_offer=buy_offer,
                      seller_offer=sale_offer).save()
                [
                    ItemTransfer.handling_offer(offer, diff)
                    for offer in [buy_offer, sale_offer]
                ]
                ItemTransfer.items_transfer(sale_offer, diff)
                ItemTransfer.items_transfer(buy_offer, diff, True)
                if not buy_offer.quantity:
                    break
Exemple #2
0
def propose_trade(request, team_id):
    players = []
    picks = []

    for key in request.POST:
        if 'player' in key:
            players.append(key.split('_')[1])
        elif 'pick' in key:
            picks.append(key.split('_')[1])

    t2 = Team.objects.get(pk=team_id)

    if len(players) == 0 and len(picks) == 0:
        msg = Message.objects.get(text=Message.EMPTY_TRADE)
        return HttpResponseRedirect(
            reverse('trades:league', args=(t2.league.id, )) + '?msg=' +
            str(msg.id))

    t1 = request.user.manager.team_set.get(league=t2.league)

    trade = Trade(team1=t1, team2=t2)

    trade.save()

    t1_picks = t2_picks = 0
    for pick_id in picks:
        pick = Pick.objects.get(pk=pick_id)
        if pick.team == t1:
            t1_picks += 1
        else:
            t2_picks += 1
        pp = PickPiece(trade=trade, pick=pick)
        pp.save()

    if t1_picks != t2_picks:
        trade.delete()
        msg = Message.objects.get(text=Message.UNEQUAL_PICKS)
        return HttpResponseRedirect(
            reverse('trades:league', args=(t1.league.id, )) + '?msg=' +
            str(msg.id))

    for player_id in players:
        player = Player.objects.get(pk=player_id)
        pp = PlayerPiece(trade=trade, player=player)
        pp.save()

    return HttpResponseRedirect(reverse('trades:trade', args=(trade.pk, )))
Exemple #3
0
def index(request, address):
    if request.method == 'GET':
        trades = Trade.objects.filter(
            Q(first_party=address) | Q(second_party=address))
        trades = list(map(lambda trade: tradeToJson(trade), trades))
        trades = '[' + ','.join(trades) + ']'
        return HttpResponse("{\"trades\": " + str(trades) + "}")
    elif request.method == 'POST':
        body = json.loads(request.body.decode("utf-8"))
        trade = Trade(first_party=address,
                      second_party=body['secondParty'],
                      address=body['address'])
        trade.save()
        card = Card.objects.filter(id=body['card']).first()
        tradeItem = TradeItem(card=card, party=address, trade=trade)
        tradeItem.save()
        return HttpResponse(tradeToJson(trade))
    elif request.method == 'DELETE':
        body = json.loads(request.body.decode("utf-8"))
        Trade.objects.filter(address=body['address']).delete()
        return HttpResponse("{ \"address\": \"" + body['address'] + "\"}")
Exemple #4
0
 def buy_offer(request, pk):
     sale_offer = get_object_or_404(Offer, id=pk)
     buy_offer = Offer(user=request.user,
                       quantity=sale_offer.quantity,
                       entry_quantity=sale_offer.quantity,
                       price=sale_offer.price,
                       order_type=OrderType.BUY.value,
                       item=sale_offer.item)
     buy_offer.save()
     diff = min(buy_offer.quantity, sale_offer.quantity)
     Trade(item=sale_offer.item,
           seller=sale_offer.user,
           buyer=buy_offer.user,
           quantity=diff,
           unit_price=sale_offer.price / sale_offer.quantity,
           buyer_offer=buy_offer,
           seller_offer=sale_offer).save()
     [
         ItemTransfer.handling_offer(offer, diff)
         for offer in [buy_offer, sale_offer]
     ]
     ItemTransfer.items_transfer(sale_offer, diff)
     ItemTransfer.items_transfer(buy_offer, diff, True)
 def get_trade():
     return trade or Trade()