change award modal

pull/213/head
Aevann 2023-10-10 19:53:46 +03:00
parent 6474b8b468
commit 7759229178
5 changed files with 48 additions and 75 deletions

View File

@ -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
}
}
);
}

View File

@ -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')

View File

@ -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
}

View File

@ -52,31 +52,13 @@ def shop(v):
return render_template("shop.html", awards=list(AWARDS.values()), v=v, sales=sales)
@app.post("/buy/<award>")
@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/<kind>")
@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

View File

@ -77,13 +77,11 @@
</div>
</div>
<input autocomplete="off" id="giveaward" class="awardbtn btn btn-primary fl-r" type="submit" data-nonce="{{g.nonce}}" data-onclick="giveaward(this)" value="Give Award" data-bs-dismiss="modal" disabled>
<input autocomplete="off" id="giveaward" class="awardbtn btn btn-primary fl-r" type="submit" data-nonce="{{g.nonce}}" data-onclick="giveaward(this)" value="Give Award" data-bs-dismiss="modal">
<button type="button" id="buy" class="awardbtn btn btn-primary mr-3 fl-r" disabled data-areyousure="buy()" data-nonce="{{g.nonce}}" data-onclick="areyousure(this)">Buy</button>
<input autocomplete="off" id="buyandgiveaward" class="awardbtn btn btn-primary fl-r d-none" type="submit" data-nonce="{{g.nonce}}" data-areyousure="giveaward(this)" data-onclick="areyousure(this)" value="Give Award" data-dismiss="modal">
<div id="award_price_block" class="fl-r mt-2 mr-3 d-none text-small-sm">
Price: <span id="award_price"></span> coins/marseybux
</div>
<span id="award_price" class="fl-r mt-2 mr-3 text-small-sm"></span>
</div>
</div>
</div>