From 775922917861efd483bd94841d4612c07085c92b Mon Sep 17 00:00:00 2001 From: Aevann Date: Tue, 10 Oct 2023 19:53:46 +0300 Subject: [PATCH] change award modal --- files/assets/js/award_modal.js | 58 ++++++++----------------------- files/assets/js/bottom.js | 1 + files/assets/js/core.js | 2 +- files/routes/awards.py | 54 ++++++++++++++-------------- files/templates/modals/award.html | 8 ++--- 5 files changed, 48 insertions(+), 75 deletions(-) diff --git a/files/assets/js/award_modal.js b/files/assets/js/award_modal.js index 1eccdde06e..62d47bb6c9 100644 --- a/files/assets/js/award_modal.js +++ b/files/assets/js/award_modal.js @@ -102,19 +102,6 @@ function pick(kind, price, coins, marseybux) { coins = parseInt(coins) marseybux = parseInt(marseybux) - const buy = document.getElementById('buy') - - if (kind == "grass" && coins < price) - buy.disabled = true; - else if (kind == "benefactor" && marseybux < price) - buy.disabled = true; - else if (coins+marseybux < price) - buy.disabled = true; - else - buy.disabled = false; - - let ownednum = Number(document.getElementById(`${kind}-owned`).textContent); - document.getElementById('giveaward').disabled = (ownednum == 0); document.getElementById('kind').value=kind; if (document.getElementsByClassName('picked').length > 0) { document.getElementsByClassName('picked')[0].classList.toggle('picked'); @@ -162,33 +149,18 @@ function pick(kind, price, coins, marseybux) { document.getElementById('note').maxLength = 200; } - document.getElementById('award_price_block').classList.remove('d-none'); - document.getElementById('award_price').textContent = price; -} - -function buy() { - const kind = document.getElementById('kind').value; - url = `/buy/${kind}` - const xhr = createXhrWithFormKey(url); - xhr[0].onload = function() { - let data - try {data = JSON.parse(xhr[0].response)} - catch(e) {console.error(e)} - success = xhr[0].status >= 200 && xhr[0].status < 300; - showToast(success, getMessageFromJsonData(success, data)); - if (success) { - if (kind != "lootbox") - { - document.getElementById('giveaward').disabled=false; - let owned = document.getElementById(`${kind}-owned`) - let ownednum = Number(owned.textContent) + 1; - owned.textContent = ownednum - } - } - }; - - xhr[0].send(xhr[1]); + const ownednum = Number(document.getElementById(`${kind}-owned`).textContent); + if (ownednum) { + document.getElementById('award_price').textContent = `${ownednum} owned`; + document.getElementById('giveaward').classList.remove('d-none'); + document.getElementById('buyandgiveaward').classList.add('d-none'); + } + else { + document.getElementById('award_price').textContent = `Price: ${price} coins/marseybux`; + document.getElementById('giveaward').classList.add('d-none'); + document.getElementById('buyandgiveaward').classList.remove('d-none'); + } } function giveaward(t) { @@ -204,10 +176,10 @@ function giveaward(t) { }, () => { let owned = document.getElementById(`${kind}-owned`) - let ownednum = Number(owned.textContent) - 1; - owned.textContent = ownednum - if (ownednum == 0) - document.getElementById('giveaward').disabled=true; + let ownednum = Number(owned.textContent); + if (ownednum) { + owned.textContent = ownednum - 1 + } } ); } diff --git a/files/assets/js/bottom.js b/files/assets/js/bottom.js index c3bd8e6e31..f24870c3b8 100644 --- a/files/assets/js/bottom.js +++ b/files/assets/js/bottom.js @@ -189,6 +189,7 @@ document.addEventListener("click", function (e) { return } document.getElementById('giveaward').dataset.action = element.dataset.url + document.getElementById('buyandgiveaward').dataset.action = element.dataset.url const effect_author_tab = document.getElementById('effect-author-tab') const effect_content_tab = document.getElementById('effect-content-tab') diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 58ac3cc4ea..7cfda27dda 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -119,7 +119,7 @@ if (!location.pathname.endsWith('/submit') && !location.pathname.endsWith('/chat const formDOM = targetDOM.parentElement; if (formDOM.id == 'note_section') { - document.getElementById('giveaward').click(); + document.querySelector('.awardbtn:not(.d-none)').click(); return } diff --git a/files/routes/awards.py b/files/routes/awards.py index 2776317d36..a572819829 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -52,31 +52,13 @@ def shop(v): return render_template("shop.html", awards=list(AWARDS.values()), v=v, sales=sales) -@app.post("/buy/") -@limiter.limit('1/second', scope=rpath) -@limiter.limit('1/second', scope=rpath, key_func=get_ID) -@limiter.limit("100/minute;200/hour;1000/day", deduct_when=lambda response: response.status_code < 400) -@limiter.limit("100/minute;200/hour;1000/day", deduct_when=lambda response: response.status_code < 400, key_func=get_ID) -@auth_required -def buy(v, award): - if award == 'ghost': - abort(403, "You can't buy this award!") - - AWARDS = deepcopy(AWARDS_ENABLED) - - if v.house: - AWARDS[v.house] = HOUSE_AWARDS[v.house] - - if award not in AWARDS: abort(400) - og_price = AWARDS[award]["price"] - - award_title = AWARDS[award]['title'] +def buy_award(v, kind): + og_price = AWARDS[kind]["price"] price = int(og_price * v.award_discount) - - if award == "grass": + if kind == "grass": currency = 'coins' - elif award == "benefactor": + elif kind == "benefactor": currency = 'marseybux' else: currency = 'combined' @@ -98,8 +80,7 @@ def buy(v, award): badge_grant(badge_id=69, user=v) g.db.add(v) - - if award == "lootbox": + if kind == "lootbox": lootbox_items = [] for _ in range(LOOTBOX_ITEM_COUNT): # five items per lootbox if IS_FISTMAS(): @@ -124,7 +105,7 @@ def buy(v, award): return {"message": lootbox_msg} else: - award_object = AwardRelationship(user_id=v.id, kind=award, price_paid=price) + award_object = AwardRelationship(user_id=v.id, kind=kind, price_paid=price) g.db.add(award_object) g.db.add(v) @@ -132,6 +113,26 @@ def buy(v, award): if CARP_ID and v.id != CARP_ID and og_price >= 5000: send_repeatable_notification(CARP_ID, f"@{v.username} has bought a `{award_title}` award!") + return award_object + + +@app.post("/buy/") +@limiter.limit('1/second', scope=rpath) +@limiter.limit('1/second', scope=rpath, key_func=get_ID) +@limiter.limit("100/minute;200/hour;1000/day", deduct_when=lambda response: response.status_code < 400) +@limiter.limit("100/minute;200/hour;1000/day", deduct_when=lambda response: response.status_code < 400, key_func=get_ID) +@auth_required +def buy(v, kind): + AWARDS = deepcopy(AWARDS_ENABLED) + + if v.house: + AWARDS[v.house] = HOUSE_AWARDS[v.house] + + if kind not in AWARDS: abort(400) + + award_title = AWARDS[kind]['title'] + + buy_award(v, kind) return {"message": f"{award_title} award bought!"} @@ -167,7 +168,8 @@ def award_thing(v, thing_type, id): AwardRelationship.comment_id == None ).first() - if not award: abort(404, "You don't have that award") + if not award: + award = buy_award(v, kind) if thing_type == 'post': award.post_id = thing.id else: award.comment_id = thing.id diff --git a/files/templates/modals/award.html b/files/templates/modals/award.html index 553f4bf0c3..64a1953f97 100644 --- a/files/templates/modals/award.html +++ b/files/templates/modals/award.html @@ -77,13 +77,11 @@ - + - + -
- Price: coins/marseybux -
+