diff --git a/alexia/api/v1/methods/juliana.py b/alexia/api/v1/methods/juliana.py index 8fd47de..5e622b2 100644 --- a/alexia/api/v1/methods/juliana.py +++ b/alexia/api/v1/methods/juliana.py @@ -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): @@ -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: @@ -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 \ No newline at end of file + return True diff --git a/assets/js/juliana.js b/assets/js/juliana.js index da85145..bbb263e 100644 --- a/assets/js/juliana.js +++ b/assets/js/juliana.js @@ -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(); @@ -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', @@ -394,7 +405,7 @@ Receipt = { params: Receipt.payData, id: 2 // id used for? } - + // writing off IAjax.request(rpcRequest, function (result) { if (result.error) { diff --git a/assets/js/wallstreet.js b/assets/js/wallstreet.js index e667434..1a1eaae 100644 --- a/assets/js/wallstreet.js +++ b/assets/js/wallstreet.js @@ -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); diff --git a/templates/billing/juliana.html b/templates/billing/juliana.html index 6df0080..35ea900 100644 --- a/templates/billing/juliana.html +++ b/templates/billing/juliana.html @@ -278,9 +278,9 @@

Error

{# Juliana #} {% endcompress %} - {% if event.pk == 9814 %} - - + {% if event.pk == 13503 %}{# Wallstreet drink #} + + {% endif %}