Merge branch 'master' of github.com:brmlab/brmbar

This commit is contained in:
Mrkva 2015-07-01 01:08:12 +02:00
commit 48c583a81c
2 changed files with 29 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
import sys
import subprocess
from PySide import QtCore, QtGui, QtDeclarative
@ -8,6 +9,15 @@ from brmbar import Database
import brmbar
# User credit balance limit; sale will fail when balance is below this limit.
LIMIT_BALANCE = -200
# When below this credit balance, an alert hook script (see below) is run.
ALERT_BALANCE = 0
# This script is executed when a user is buying things and their balance is
# below LIMIT_BALANCE (with argument "limit") or below ALERT_BALANCE
# (with argument "alert").
ALERT_SCRIPT = "./alert.sh"
class ShopAdapter(QtCore.QObject):
""" Interface between QML and the brmbar package """
@ -70,6 +80,18 @@ class ShopAdapter(QtCore.QObject):
db.commit()
return acct
@QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def canSellItem(self, itemid, userid):
user = brmbar.Account.load(db, id = userid)
if -user.balance() > ALERT_BALANCE:
return True
elif -user.balance() > LIMIT_BALANCE:
subprocess.call(["sh", ALERT_SCRIPT, "alert"])
return True
else:
subprocess.call(["sh", ALERT_SCRIPT, "limit"])
return False
@QtCore.Slot('QVariant', 'QVariant', result='QVariant')
def sellItem(self, itemid, userid):
user = brmbar.Account.load(db, id = userid)
@ -99,6 +121,11 @@ class ShopAdapter(QtCore.QObject):
db.commit()
return balance
@QtCore.Slot('QVariant', result='QVariant')
def balance_user(self, userid):
user = brmbar.Account.load(db, id=userid)
return user.negbalance_str()
@QtCore.Slot(result='QVariant')
def balance_cash(self):
balance = shop.cash.balance_str()

View file

@ -58,6 +58,8 @@ Item {
if (acct.acctype == "cash") { //Copied from BarButton.onButtonClick
shop.sellItemCash(dbid)
status_text.setStatus("Sold! Put " + price + " Kč in the money box.", "#ffff7c")
} else if (!shop.canSellItem(dbid, acct.id)) {
status_text.setStatus("NOT SOLD! "+acct.name+"'s credit is TOO LOW: "+shop.balance_user(acct.id), "#ff4444")
} else {
var balance = shop.sellItem(dbid, acct.id)
status_text.setStatus("Sold! "+acct.name+"'s credit is "+balance+".", "#ffff7c")