Skip to content

Commit

Permalink
Update for 2024 edition, add support for multiple products
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurocon committed Jan 22, 2025
1 parent 8e0e54d commit d825d11
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 58 deletions.
16 changes: 11 additions & 5 deletions alexia/api/v1/methods/juliana.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ def juliana_order_save(request, event_id, user_id, purchases, rfid_data):

price = amount * product.get_price(event)

# Grolsch? Trust Juliana (variable price)
if product.pk == 1:
# One of the Wallstreet drink drinks? Trust Juliana (variable price)
WALLSTREET_DRINKS = [
1, # Grolsch
2, # Cola/Fanta
485, # Fuze Tea (big)
296, # Wine glass
]
if product.pk in WALLSTREET_DRINKS:
price = p['price'] / Decimal(100)

if price != p['price'] / Decimal(100):
Expand Down Expand Up @@ -196,7 +202,7 @@ def juliana_user_check(request, event_id, user_id):
def juliana_writeoff_save(request, event_id, writeoff_id, purchases):
"""Saves a writeoff order in the Database"""
event = _get_validate_event(request, event_id)

try:
writeoff_cat = WriteoffCategory.objects.get(id=writeoff_id)
except WriteoffCategory.DoesNotExist:
Expand Down Expand Up @@ -232,9 +238,9 @@ def juliana_writeoff_save(request, event_id, writeoff_id, purchases):

if price != p['price'] / Decimal(100):
raise InvalidParamsError('Price for product %s is incorrect' % p['product'])

purchase = WriteOffPurchase(order=order, product=product.name, amount=amount, price=price)
purchase.save()

order.save(force_update=True) # ensure order.amount is correct
return True
return True
25 changes: 18 additions & 7 deletions assets/js/juliana.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ State = {
// (to know how much we're writing off)
$("#keypad").hide();
$("#products").hide();

// HTML Magic and rendering
// argument is the Receipt object
$('#writeoff-screen').show();
Expand Down Expand Up @@ -320,22 +320,33 @@ Receipt = {
payNow: function () {
console.log('Processing payment now.');

var numBeers = 0;
// Start Wallstreet drink code

var WALLSTREET_DRINKS = [
1, // Grolsch
2, // Cola/Fanta
485, // Fuze Tea (big)
296, // Wine glass
]

var countsPerProduct = {};
for (var i in this.receipt) {
if (this.receipt[i]===undefined) continue;

var product = this.receipt[i].product;
var quantity = this.receipt[i].amount;

if(product === 1) {
numBeers = quantity;
if(WALLSTREET_DRINKS.includes(product)) {
countsPerProduct[product] = quantity;
}
}

if (numBeers) {
increasePrice(numBeers);
if (!countsPerProduct.isEmpty()) {
increasePrice(countsPerProduct);
}

// End Wallstreet drink code

var rpcRequest = {
jsonrpc: '2.0',
method: 'juliana.order.save',
Expand Down Expand Up @@ -394,7 +405,7 @@ Receipt = {
params: Receipt.payData,
id: 2 // id used for?
}

// writing off
IAjax.request(rpcRequest, function (result) {
if (result.error) {
Expand Down
152 changes: 109 additions & 43 deletions assets/js/wallstreet.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,123 @@
var socket = io('beta.ia.utwente.nl:21382');
const socket = io('beta.ia.utwente.nl:21382');
socket.emit('new client', "Juliana");
console.log(`Connected to Wallstreet websocket`)

var pStart = Settings.products[1].price / 100;
var pMax = 0.80;
var pMin = 0.20;
var pDelta = 0.05;
var pMulti = 0.5;

var p = pStart;

var lastAdvertisedPrice = parseInt(pStart * 100);

function newPrice() {
var cents = Math.round(p * 100);
Settings.products[1].price = cents;

if(cents != lastAdvertisedPrice) {
var diff;
var string = (cents / 100).toFixed(2).replace('.', ',');
string += ' ';
if (cents >= lastAdvertisedPrice) {
string += '+';
diff = (cents - lastAdvertisedPrice) / lastAdvertisedPrice;
} else {
string += '-';
diff = (lastAdvertisedPrice - cents) / lastAdvertisedPrice;
const linkToBeerPrice = [2, 485]; // Make sure sodas can't get more expensive than beer
const beerProductId = 1;
const products = [
{id: 1, pMax: 1.00, pMin: 0.25}, // Grolsch
{id: 2, pMax: 1.00, pMin: 0.25}, // Cola/Fanta
{id: 485, pMax: 1.00, pMin: 0.25}, // Fuze Tea (big)
{id: 296, pMax: 2.60, pMin: 0.65}, // Wine glass
];

// Build prices object from products listing above.
let prices = {};
for (const product of products) {
console.log(`Loading product ${product.id}...`)
prices[product.id] = {
pStart: Settings.products[product.id].price / 100,
pCurrent: Settings.products[product.id].price / 100,
pLastAdvertised: Settings.products[product.id].price,
pMax: product.pMax,
pMin: product.pMin,
}
}

const pDelta = 0.05;
const pMulti = 0.5;

function advertisePrices() {
let pricesChanged = false;
let priceChangeData = {};

for (const productId in prices) {
let lastAdvertisedPrice = prices[productId].pLastAdvertised;
let cents = Settings.products[productId].price;
let diff;
let string = (cents / 100).toFixed(2).replace('.', ',');
string += ' ';
if (cents >= lastAdvertisedPrice) {
string += '+';
diff = (cents - lastAdvertisedPrice) / lastAdvertisedPrice;
} else {
string += '-';
diff = (lastAdvertisedPrice - cents) / lastAdvertisedPrice;
}
string += (diff * 100).toFixed(1).replace('.', ',');
string += '%';

priceChangeData[productId] = string;

if (prices[productId].pLastAdvertised !== cents) {
pricesChanged = true;
}
}
string += (diff * 100).toFixed(1).replace('.', ',');
string += '%';

socket.emit('new price', string);
lastAdvertisedPrice = cents;
}
if (pricesChanged) {
console.log(`Sending new price changes to screen`)
socket.emit('new prices', priceChangeData);
for (const productId in prices) {
prices[productId].pLastAdvertised = Settings.products[productId].price;
}
}
}

function reducePrice() {
p = p - (pDelta * ((p / pMin) - 1));
function reducePrices() {
for (const productId in prices) {
let pMin = prices[productId].pMin;
const pOriginal = prices[productId].pCurrent;
let pCurrent = prices[productId].pCurrent;
pCurrent = pCurrent - (pDelta * ((pCurrent / pMin) - 1));

if(pCurrent < pMin) {
pCurrent = pMin;
}

if(p < pMin) {
p = pMin;
}
prices[productId].pCurrent = pCurrent;
Settings.products[productId].price = Math.round(prices[productId].pCurrent * 100);
console.log(`Reduced price of ${productId} from ${pOriginal} to ${pCurrent}`)

newPrice();
// Make sure soda prices also go down if beer price goes down below soda price
if (productId === beerProductId) {
for (const sodaId of linkToBeerPrice) {
if (prices[beerProductId].pCurrent < prices[sodaId].pCurrent) {
prices[sodaId].pCurrent = prices[beerProductId].pCurrent;
Settings.products[sodaId].price = Settings.products[beerProductId].price;
console.log(`Changed price of ${sodaId} to match lower beer price ${prices[beerProductId].pCurrent}`)
}
}
}
}
advertisePrices();
}

function increasePrice(numBeers) {
p = p + (pDelta * (numBeers ** pMulti));
function increasePrice(countsPerProduct) {
for (const productId in countsPerProduct) {
let pMax = prices[productId].pMax;
const pOriginal = prices[productId].pCurrent;
let pCurrent = prices[productId].pCurrent;
pCurrent = pCurrent + (pDelta * (countsPerProduct[productId] ** pMulti));

if(pCurrent > pMax) {
pCurrent = pMax;
}

if(p > pMax) {
p = pMax;
}
prices[productId].pCurrent = pCurrent;
Settings.products[productId].price = Math.round(prices[productId].pCurrent * 100);
console.log(`Increased price of ${productId} from ${pOriginal} to ${pCurrent}`)
}

// If beer got cheaper than the sodas after increase, make the soda price the same as the beer price
for (const sodaId of linkToBeerPrice) {
if (prices[beerProductId].pCurrent < prices[sodaId].pCurrent) {
prices[sodaId].pCurrent = prices[beerProductId].pCurrent;
Settings.products[sodaId].price = Settings.products[beerProductId].price;
console.log(`Changed price of ${sodaId} to match lower beer price ${prices[beerProductId].pCurrent}`)
}
}

newPrice();
advertisePrices();
}

setInterval(reducePrice, 60 * 1000);
setInterval(reducePrices, 60 * 1000);
6 changes: 3 additions & 3 deletions templates/billing/juliana.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ <h2>Error</h2>
{# Juliana #}
<script src="{% static 'js/juliana.js' %}"></script>
{% endcompress %}
{% if event.pk == 9814 %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script src="{% static 'js/wallstreet.js' %}"></script>
{% if event.pk == 13503 %}{# Wallstreet drink #}
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.8.1/socket.io.js"></script>
<script src="{% static 'js/wallstreet.js' %}"></script>
{% endif %}
</body>
</html>

0 comments on commit d825d11

Please sign in to comment.