From 247133229cb28f7b3c00dffbcbdff9117d1e7982 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 11 Sep 2017 09:00:42 +0000 Subject: [PATCH] Implement purchasing option for custom css --- webapp/titanembeds/blueprints/user/user.py | 28 +++++++++++++++++- webapp/titanembeds/static/js/donate.js | 32 ++++++++++++++++++++- webapp/titanembeds/templates/donate.html.j2 | 26 +++++++++++++++-- 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/webapp/titanembeds/blueprints/user/user.py b/webapp/titanembeds/blueprints/user/user.py index 16bc518..4884cb9 100644 --- a/webapp/titanembeds/blueprints/user/user.py +++ b/webapp/titanembeds/blueprints/user/user.py @@ -420,4 +420,30 @@ def donate_confirm(): def donate_thanks(): tokens = get_titan_token(session["user_id"]) transaction = request.args.get("transaction") - return render_template("donate_thanks.html.j2", tokens=tokens, transaction=transaction) \ No newline at end of file + return render_template("donate_thanks.html.j2", tokens=tokens, transaction=transaction) + +@user.route('/donate', methods=['PATCH']) +@discord_users_only() +def donate_patch(): + item = request.form.get('item') + amount = int(request.form.get('amount')) + if amount <= 0: + abort(400) + subtract_amt = 0 + if item == "custom_css_slots": + subtract_amt = 100 + amt_change = -1 * subtract_amt * amount + subtract = set_titan_token(session["user_id"], amt_change, "BUY " + item + " x" + str(amount)) + if not subtract: + return ('', 402) + session["tokens"] += amt_change + if item == "custom_css_slots": + entry = db.session.query(Cosmetics).filter(Cosmetics.user_id == session["user_id"]).first() + if not entry: + entry = Cosmetics(session["user_id"]) + entry.css = True + entry.css_limit = 0 + entry.css_limit += amount + db.session.add(entry) + db.session.commit() + return ('', 204) \ No newline at end of file diff --git a/webapp/titanembeds/static/js/donate.js b/webapp/titanembeds/static/js/donate.js index 4e170b7..49713d1 100644 --- a/webapp/titanembeds/static/js/donate.js +++ b/webapp/titanembeds/static/js/donate.js @@ -1,4 +1,4 @@ -/* global $ */ +/* global $, location, Materialize */ (function () { $('#token-slider').on('input', function(){ var slider_value = $("#token-slider").val(); @@ -16,4 +16,34 @@ $(document.body).append(form); form.submit(); }); + + function patchForm(item, amount) { + var funct = $.ajax({ + dataType: "json", + method: "PATCH", + data: {"item": item, "amount": amount}, + }); + return funct.promise(); + } + + $("#buy-custom-css-slots-btn").click(function () { + var amount = $.trim($("#custom-css-slots-amount").val()); + if (amount == "") { + return; + } + var formPatch = patchForm("custom_css_slots", amount); + formPatch.done(function (data) { + alert("Successfully bought " + amount + " custom css slots!"); + location.reload(); + }); + formPatch.fail(function (data) { + if (data.status == 400) { + Materialize.toast('Amount cannot be zero or under!', 10000); + } else if (data.status == 402) { + Materialize.toast('Insufficient token funds!', 10000); + } else { + Materialize.toast('Purchasing custom css slots failed!', 10000); + } + }); + }); })(); \ No newline at end of file diff --git a/webapp/titanembeds/templates/donate.html.j2 b/webapp/titanembeds/templates/donate.html.j2 index d3f2093..0a203c9 100644 --- a/webapp/titanembeds/templates/donate.html.j2 +++ b/webapp/titanembeds/templates/donate.html.j2 @@ -9,8 +9,7 @@

The Name-Your-Price Tool

-

Currently if you donate, we cannot give much back in return, yet. However, we do have some donatator features up our sleeves and will be implemented.

-

For now, you will receive Titan Tokens™ (to be spent on donator features) and a supporter role on our support server.

+

Donate to receive Titan Tokens™ (to be spent on donator features below) and a supporter role on our support server.

@@ -19,6 +18,29 @@
+
+
+
+
+

Current token amount: {{ session["tokens"] }}

+
+
+
+
+

Donators' Shoppe

+

Your personal pit-stop before your next conquest mission to embed all yer servers!

+
+
+
+

Custom CSS Slots [100 tokens]

+

Pick an amount of custom css slots you would like to stock up.

+
+ + Buy +
+
+
+
{% endblock %} {% block script %}