Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

allow multiple purchases

  • Loading branch information...
commit 79b00b297aa84a9dda8a60fce13e809a10b34cec 1 parent 640f547
Emil Renner Berthing esmil authored

Showing 1 changed file with 47 additions and 22 deletions. Show diff stats Hide diff stats

  1. +47 22 drinkomatic.lua
69 drinkomatic.lua
@@ -28,9 +28,9 @@ local function main_menu()
28 28 print " Swipe card to log in."
29 29 print " Scan barcode to check price of product."
30 30 print ""
31   - print " * | Print this menu."
32   - print " 1 | Create new account."
33   - print " 2 | Update or create new product."
  31 + print " 1 | Create new account."
  32 + print " 2 | Update or create new product."
  33 + print " - | Print this menu."
34 34 print "-------------------------------------------"
35 35 end
36 36
@@ -40,9 +40,10 @@ local function user_menu()
40 40 print " Scan barcode to buy product."
41 41 print " Press enter to log out."
42 42 print ""
43   - print " * | Print this menu."
44   - print " 1 | Add money to account."
45   - print " 2 | Switch card."
  43 + print " / | Add money to account."
  44 + print " * | Switch card."
  45 + print " <n> | Buy <n> items."
  46 + print " - | Print this menu."
46 47 print "-------------------------------------------"
47 48 end
48 49
@@ -110,10 +111,6 @@ MAIN = {
110 111 end,
111 112
112 113 keyboard = {
113   - ['*'] = function()
114   - main_menu()
115   - return 'MAIN'
116   - end,
117 114 ['1'] = function()
118 115 print " Please enter user name (or press enter to abort):"
119 116 return 'NEWUSER_NAME'
@@ -122,6 +119,10 @@ MAIN = {
122 119 print(" Scan barcode (or press enter to abort):")
123 120 return 'PROD_CODE'
124 121 end,
  122 + ['-'] = function()
  123 + main_menu()
  124 + return 'MAIN'
  125 + end,
125 126 [''] = function()
126 127 print(" ENTAR!")
127 128 return 'MAIN'
@@ -358,7 +359,7 @@ USER = {
358 359
359 360 card = login,
360 361
361   - barcode = function(code, id)
  362 + barcode = function(code, id, count)
362 363 local r = assert(db:fetchone("\z
363 364 SELECT id, name, price \z
364 365 FROM products \z
@@ -372,14 +373,20 @@ USER = {
372 373 local pid = r[1]
373 374 local price = r[3]
374 375
375   - print(" Buying %s for %.2f DKK", r[2], price)
  376 + if count then
  377 + print(" Buying %s for %d * %.2f = %.2f DKK",
  378 + r[2], count, price, count * price)
  379 + else
  380 + print(" Buying %s for %.2f DKK", r[2], price)
  381 + count = 1
  382 + end
376 383
377 384 assert(db:exec("\z
378 385 BEGIN; \z
379   - UPDATE users SET balance = balance - @price WHERE id = @id; \z
  386 + UPDATE users SET balance = balance - @count * @price WHERE id = @id; \z
380 387 INSERT INTO log (dt, uid, pid, count, price) \z
381   - VALUES (datetime('now'), @id, @pid, 1, @price); \z
382   - COMMIT", { id = id, pid = pid, price = price }))
  388 + VALUES (datetime('now'), @id, @pid, @count, @price); \z
  389 + COMMIT", { id = id, pid = pid, count = count, price = price }))
383 390
384 391 r = assert(db:fetchone(
385 392 "SELECT balance FROM users WHERE id = ?", id))
@@ -389,20 +396,38 @@ USER = {
389 396 end,
390 397
391 398 keyboard = {
392   - ['*'] = function(id)
393   - user_menu()
394   - return 'USER', id
395   - end,
396   - ['1'] = function(id)
  399 + ['/'] = function(id)
397 400 print " Enter amount (or press enter to abort):"
398 401 return 'DEPOSIT', id
399 402 end,
400   - ['2'] = function(id)
  403 + ['*'] = function(id)
401 404 print " Swipe new card (or press enter to abort):"
402 405 return 'SWITCH_CARD', id
403 406 end,
404   - [''] = idle,
  407 + ['-'] = function(id)
  408 + user_menu()
  409 + return 'USER', id
  410 + end,
  411 + ['n'] = function(id)
  412 + print " Sigh. A number. That is [1-9][0-9]*"
  413 + return 'USER', id
  414 + end,
  415 + [''] = function(id, count)
  416 + if count then
  417 + print " Aborted."
  418 + return 'USER', id
  419 + end
  420 +
  421 + return idle()
  422 + end,
405 423 function(cmd, id) --default
  424 + local count = tonumber(cmd)
  425 + if count then
  426 + print(" Buying %d of the next thing scanned. Press ENTER to abort.",
  427 + count)
  428 + return 'USER', id, count
  429 + end
  430 +
406 431 print(" Unknown command '%s'.", cmd)
407 432 user_menu()
408 433 return 'USER', id

0 comments on commit 79b00b2

Please sign in to comment.
Something went wrong with that request. Please try again.