Esempio n. 1
0
 def confirmation(self):
     return "%s units %s for $%s." % (priceformat.quantity(self.quantity),
             "bought" if self.order_type == 'B' else "sold",
             priceformat.currency(self.cash_exchanged))
Esempio n. 2
0
def show_word(request, word_id):

    word = get_object_or_404(Word, id=word_id)
    price = word.latest_price()
    latest_round = Round.objects.latest()

    buy_price = max(price, consts.MIN_BUY_PRICE)

    # See if the user is authenticated or not
    if request.user.is_authenticated():
        tweets_ok = request.user.get_profile().tweets_ok
        user_round = request.user.get_active_user_round()
    else:
        user_round = None
        tweets_ok = True

    # generate starting cash if it's existent.
    if user_round:
        cash = user_round.cash
        slots_held = user_round.slots_held()
        try:
            holding = (user_round.holding_set.filter(word=word).exclude(quantity=0))[0]
        except IndexError:
            holding = None
        try:
            last_order = user_round.order_set.order_by("-created_on")[0]
        except IndexError:
            last_order = None
    else:
        holding = None
        last_order = None
        cash = latest_round.starting_cash
        slots_held = 0

    qty_held = holding.quantity if holding else 0
    forbidden = None

    slots_available = consts.MAX_SLOTS - slots_held

    qty_available = slots_available // word.slots

    if slots_available <= 0:
        forbidden = (
            "You can't buy any more words because you've already filled \
%d slots. You'll have to sell some of your portfolio before you buy \
new words."
            % consts.MAX_SLOTS
        )
    elif slots_available < word.slots:
        forbidden = (
            "You can't buy this word because you only have %d slots \
available, and this word requires %d. You'll have to sell some of your \
portfolio before you buy new words."
            % (slots_available, word.slots)
        )

    sell_units = []
    sell_price = price * (1 - consts.COMMISSION)
    if qty_held >= 1:
        sell_units.append(("all", qty_held, qty_held * sell_price))
    if qty_held >= 2:
        sell_units.append(("half", int(qty_held / 2), int(qty_held / 2) * sell_price))
    if qty_held >= 8:
        sell_units.append(("a quarter", int(qty_held / 4), int(qty_held / 4) * sell_price))

    buy_message = None
    if buy_price > price:
        buy_message = "You can buy this word at the minimum buy price of $%s." % (
            priceformat.currency(consts.MIN_BUY_PRICE)
        )

    if buy_price == 0:
        forbidden = "You can't buy this word right now because we have no \
data on its current frequency of usage."
        qty_afford = 0
    else:
        c = word.latest_count()
        if word.latest_count() < consts.MIN_NUMBER_TO_BUY:
            forbidden = "You can't buy this word right now because the price \
is too low for us to provide accurate data."
            qty_afford = 0
        else:
            qty_afford = int(cash / buy_price)
            if qty_afford == 0:
                forbidden = "You can't afford to buy any shares of this word."

    if forbidden:
        buy_units = []
    else:

        def units(total):
            yield total
            if total > 1:
                yield total / 2
                for unit in units(total / 10):
                    yield unit

        buy_units = units(consts.MAX_SLOTS)
        qty_possible = min(qty_afford, qty_available)
        buy_units = filter(lambda u: u <= qty_possible, buy_units)
        if not buy_units or buy_units[0] != qty_possible:
            buy_units.insert(0, qty_possible)
        buy_units = [(unit, buy_price * unit, word.slots * unit) for unit in buy_units[:4]]

    return render_to_response(
        "game/words/show.html",
        {
            "word": word,
            "price": price,
            "buy_message": buy_message,
            "holding": holding,
            "forbidden": forbidden,
            "buy_units": buy_units,
            "sell_units": sell_units,
            "commission_percent": int(100 * consts.COMMISSION),
            "qty_held": qty_held,
            "qty_afford": qty_afford,
            "cash": cash,
            "tweets_ok": tweets_ok,
            "slots_available": slots_available,
            "slots_maximum": consts.MAX_SLOTS,
            "limiter": "cash" if qty_afford < qty_available else "slots",
        },
        context_instance=RequestContext(request),
    )