Skip to content

Commit

Permalink
Extremely basic general store.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffz committed Jul 6, 2017
1 parent f1349b5 commit 021bc3d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 41 deletions.
10 changes: 6 additions & 4 deletions saylua/modules/arcade/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from saylua.wrappers import api_login_required
from flask import g, request
from models.db import Game, GameLog
from saylua.modules.users.models.db import User

from saylua.utils import int_or_none

import json

Expand All @@ -18,7 +19,8 @@ def api_send_score(game_id):
# TODO sanity check the game log and other variables sent to catch
# low hanging fruit attempts at cheating.
data = request.get_json()
GameLog.record_score(g.user.id, game_id, data.get('score'))
cc, ss = User.update_currency(g.user.id, cc=data.get('score'))
return json.dumps(dict(cloud_coins=cc, star_shards=ss))
score = int_or_none(data.get('score')) or 0
GameLog.record_score(g.user.id, game_id, score)
g.user.cloud_coins += score
return json.dumps(dict(cloud_coins=g.user.cloud_coins, star_shards=g.user.star_shards))
return json.dumps(dict(error='Bad request.')), 400
35 changes: 30 additions & 5 deletions saylua/modules/commerce/templates/shops/npc.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
{% extends "layout.html" %}
{% set page_title = "NPC Shop" %}
{% set page_title = "The General Store" %}
{% block body %}
<h1>Vera Everly's Shop</h1>
<img src="/static/img/npc/vera-everly.png" style="width:40%; float: left;">
<img src="/static/img/npc/evana-devree.png" style="width:40%; float: left;">
<img src="/static/img/npc/rufus-scippio.png" style="width:40%; float: left;">
<h1>The General Store</h1>
<div class="npc-bust">
<img src="{{ random_character_image() }}" />
</div>
<p>
Heh, welcome to the General Store. The most general store in all of Saylua.
We sell literally everything.
</p>

<div class="grid-container">
{% for item in items %}
<div class="grid-element">
<a href="{{ item.url() }}">
<img src="{{ item.image_url() }}" class="item" />
{{ item.name }}
</a>
<p>
{{ (item.buyback_price)|format_number }} Cloud Coins
</p>
<form method="post">
<input type="number" name="amount" value="1" />
<button class="subtle-button" name="item_id" value="{{ item.id }}">
Buy
</button>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
</form>
</div>
{% endfor %}
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion saylua/modules/commerce/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
url('/market/', view_func=views.market.market_main, name='market', methods=['GET', 'POST']),

# Shop URLs
url('/shop/<name>/', view_func=views.shops.npc_shop_view, name='view_shop'),
url('/shop/<name>/', view_func=views.shops.npc_shop_view, name='view_shop', methods=['GET', 'POST']),
]
9 changes: 8 additions & 1 deletion saylua/modules/commerce/views/bank.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from saylua import db

from saylua.modules.users.models.db import User, InvalidCurrencyException
from saylua.utils import get_from_request
from saylua.wrappers import login_required
Expand All @@ -22,11 +24,16 @@ def bank_transfer():
cc = form.cloud_coins.data or 0

if not ss and not cc:
flash('You must enter at least one currency to send. ', 'error')
flash('You must enter at least one currency to send.', 'error')
elif ss < 0 or cc < 0:
flash('You cannot send negative currency.', 'error')
else:
recipient = recipient_check.user
try:
User.transfer_currency(g.user.id, recipient.id, cc, ss)
db.session.add(g.user)
db.session.add(recipient)
db.session.commit()
except InvalidCurrencyException:
flash('You do not have enough funds to send the amount entered.', 'error')
except:
Expand Down
25 changes: 23 additions & 2 deletions saylua/modules/commerce/views/shops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
from flask import render_template
from saylua import db
from flask import render_template, request, g, flash

from saylua.modules.items.models.db import Item
from saylua.modules.users.models.db import InvalidCurrencyException
from saylua.utils import int_or_none


def npc_shop_view(name):
return render_template("shops/npc.html")
items = db.session.query(Item).all()

purchase_id = int_or_none(request.form.get('item_id'))
amount = int_or_none(request.form.get('amount'))
if purchase_id and purchase_id > 0 and amount and amount > 0:
purchase = db.session.query(Item).get(purchase_id)
entry = purchase.give_items(g.user.id, amount)

try:
price = purchase.buyback_price
g.user.cloud_coins -= price * amount
db.session.add(entry)
db.session.commit()
flash("You've successfully purchased %d %s for %d cloud coins." % (amount, purchase.name, amount * price))
except InvalidCurrencyException:
flash('You do not have the funds to purchase this item.', 'error')
return render_template("shops/npc.html", items=items)
38 changes: 12 additions & 26 deletions saylua/modules/users/models/db/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ def make_password_reset_code(self):
def validate_email(self, key, address):
return address.lower()

@validates('cloud_coins')
def validate_cloud_coins(self, key, cc):
if cc < 0:
raise InvalidCurrencyException("Currency cannot be negative!")
return cc

@validates('star_shards')
def validate_star_shards(self, key, ss):
if ss < 0:
raise InvalidCurrencyException("Currency cannot be negative!")
return ss

@classmethod
def by_username(cls, username):
return (
Expand All @@ -186,18 +198,6 @@ def hash_password(cls, password, salt=None):
def check_password(cls, user, password):
return cls.hash_password(password, user.password_hash) == user.password_hash

@classmethod
def update_currency(cls, user_id, cc=0, ss=0):
user = db.session.query(User).get(user_id)
user.star_shards += ss
user.cloud_coins += cc

# Throw exceptions if the currency amount is invalid
cls.except_if_currency_invalid(user)
db.session.add(user)
db.session.commit()
return [user.cloud_coins, user.star_shards]

@classmethod
def transfer_currency(cls, from_id, to_id, cc=0, ss=0):
from_user = db.session.query(User).get(from_id)
Expand All @@ -209,20 +209,6 @@ def transfer_currency(cls, from_id, to_id, cc=0, ss=0):
to_user.star_shards += ss
to_user.cloud_coins += cc

# Throw exceptions if the currency amount is invalid
if cc < 0 or ss < 0:
raise InvalidCurrencyException('Transfers cannot be negative!')
cls.except_if_currency_invalid(from_user)
cls.except_if_currency_invalid(to_user)
db.session.add(from_user)
db.session.add(to_user)
db.session.commit()

@classmethod
def except_if_currency_invalid(cls, user):
if user.star_shards < 0 or user.cloud_coins < 0:
raise InvalidCurrencyException('Currency cannot be negative!')

def __init__(self, username, *args, **kwargs):
self.active_username = username
Username.create(username, self)
Expand Down
4 changes: 2 additions & 2 deletions saylua/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ <h3>Games and Fun</h3>
</a>
<div class="left-menu dropdown-menu menu" id="trade-menu">
<h3>Buy and Sell</h3>
<a href="/shops/">
<a href="/shop/general/">
<img src="/static/img/icons/tag_blue.png" alt="tag" title="Sales" aria-label="Sales" />
NPC Shops
General Store
</a>
<a href="/bank/">
<img src="/static/img/icons/coins.png" alt="coins" title="Bank" aria-label="Bank" />
Expand Down

0 comments on commit 021bc3d

Please sign in to comment.