-
Notifications
You must be signed in to change notification settings - Fork 2
/
drinkomatic.lua
executable file
·775 lines (648 loc) · 16.5 KB
/
drinkomatic.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#!/usr/bin/env lem
local utils = require 'lem.utils'
local io = require 'lem.io'
local sha1 = require 'sha1'
local sqlite = require 'lem.sqlite3'
local bqueue = require 'bqueue'
local assert, error = assert, error
local type, tostring = type, tostring
local write, format = io.write, string.format
local db = assert(sqlite.open(arg[1] or 'test.db', sqlite.READWRITE))
local timeout = 30
--- some helper functions ---
function string.utf8trim(str, len)
local chars, i, n = 0, 1, #str
while i <= n do
local c = str:byte(i)
if c >= 252 then i = i + 5
elseif c >= 248 then i = i + 4
elseif c >= 240 then i = i + 3
elseif c >= 224 then i = i + 2
elseif c >= 192 then i = i + 1
end
chars = chars + 1
if chars == len then
return str:sub(1, i)
end
i = i + 1
end
return str .. (' '):rep(len - chars)
end
local function print(...)
return write(format(...), '\n')
end
local function clearscreen()
return write "\x1B[1J\x1B[H"
end
local function main_menu()
print "-------------------------------------------"
print " Swipe card to log in."
print " Scan barcode to check price of product."
print ""
print " 1 | Create new account."
print " 2 | Update or create new product."
print " /<p>| Check price for the product <p>."
print " . | Print this menu."
print "-------------------------------------------"
local r = assert(db:fetchone(
"SELECT SUM(balance)/COUNT(1), MIN(balance) FROM users"))
local s = assert(db:fetchone(
"SELECT SUM(balance) FROM users WHERE balance < 0"))
print(" Average balance: %16.2f DKK", r[1])
print(" Largest single debt: %16.2f DKK", r[2])
print(" Total debt: %16.2f DKK", s[1])
end
local function user_menu()
print "-------------------------------------------"
print " Swipe card to switch user."
print " Scan barcode to buy product."
print " Press enter to log out."
print ""
print " / | Switch card."
print " * | Show my log."
print " + | Add money to account."
print " - | Transfer money."
print " <n> | Buy <n> items."
print " /<p>| Buy the product <p>."
print " . | Print this menu."
print "-------------------------------------------"
end
local function idle()
clearscreen()
main_menu()
return 'IDLE'
end
local function login(hash, id)
local r = assert(db:fetchone("\z
SELECT id, name, balance \z
FROM users \z
WHERE hash = ?", hash))
if r == true then
if id then
clearscreen()
print " Unknown card swiped, logged out."
main_menu()
else
print " Unknown card swiped.."
end
return 'MAIN'
end
clearscreen()
print("-------------------------------------------")
print(" Logged in as : %s", r[2])
print(" Balance : %.2f DKK", r[3])
print("")
print(" NB. If your name is just numbers,")
print(" please tell Esmil to change it.")
user_menu()
return 'USER', r[1]
end
local function product_dump(p)
print("-------------------------------------------")
print(" Product : %s", p[1])
print(" Price : %.2f DKK", p[2])
print("-------------------------------------------")
end
--- declare states ---
MAIN = {
wait = timeout,
timeout = idle,
card = login,
barcode = function(code)
print " Price check.."
local r = assert(db:fetchone(
"SELECT name, price FROM products WHERE barcode = ?", code))
if r == true then
print " Unknown product."
return 'MAIN'
end
product_dump(r)
return 'MAIN'
end,
keyboard = {
['1'] = function()
print " Please enter user name (or press enter to abort):"
return 'NEWUSER_NAME'
end,
['2'] = function()
print(" Scan barcode (or /<barcode>, or press enter to abort):")
return 'PROD_CODE'
end,
['.'] = function()
main_menu()
return 'MAIN'
end,
[''] = function()
print(" ENTAR!")
return 'MAIN'
end,
function(cmd) --default
if cmd:sub(1,1) == '/' then
return (MAIN['barcode'])(cmd:sub(2))
end
print(" Unknown command '%s'.", cmd)
main_menu()
return 'MAIN'
end,
},
}
IDLE = {
card = MAIN.card,
barcode = MAIN.barcode,
keyboard = MAIN.keyboard,
}
NEWUSER_NAME = {
wait = 120, -- allow 2 minutes for typing account name
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = 'NEWUSER_NAME',
keyboard = {
[''] = function()
print " Aborted."
return 'MAIN'
end,
function(name) --default
print(" Hello %s! Please swipe your card..", name)
return 'NEWUSER_HASH', name
end,
},
}
NEWUSER_HASH = {
wait = timeout,
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = function(hash, name)
print " Card swiped, thank you! Creating account.."
local ok, err = db:fetchone("\z
INSERT INTO users (name, hash, balance) \z
VALUES (?, ?, 0.0)", name, hash)
if not ok then
print(" Error creating account: %s", err)
return 'MAIN'
end
return login(hash)
end,
barcode = 'NEWUSER_HASH',
keyboard = function()
print " Aborted."
return 'MAIN'
end,
}
PROD_CODE = {
wait = timeout,
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = function(code)
print(" Scanned: %s", code)
local r = assert(db:fetchone("\z
SELECT id, name, price \z
FROM products \z
WHERE barcode = ?", code))
if r == true then
print " Not found in database, creating new product."
print " Type name of product (or press enter to abort):"
return 'PROD_NEW_NAME', code
end
print(" Already in database, updating info.")
print(" Type name of product (or press enter to keep '%s'):", r[2])
return 'PROD_EDIT_NAME', { id = r[1], name = r[2], price = r[3] }
end,
keyboard = function(entry)
if entry:sub(1,1) == '/' then
return (PROD_CODE['barcode'])(entry:sub(2))
end
print " Aborted."
return 'MAIN'
end,
}
PROD_NEW_NAME = {
wait = 120, -- allow 2 minutes for typing product name
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = 'PROD_NEW_NAME',
keyboard = {
[''] = function()
print " Aborted."
return 'MAIN'
end,
function(name, code) --default
print " Enter price (or press enter to abort):"
return 'PROD_NEW_PRICE', name, code
end,
},
}
PROD_NEW_PRICE = {
wait = timeout,
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = 'PROD_NEW_PRICE',
keyboard = {
[''] = function()
print " Aborted."
return 'MAIN'
end,
function(price, name, code) --default
local n = tonumber(price)
if not n then
print(" Unable to parse '%s', try again (or press enter to abort):", price)
return 'PROD_NEW_PRICE', name, code
end
print " Creating new product.."
local ok, err = db:fetchone("\z
INSERT INTO products (barcode, price, name) \z
VALUES (?, ?, ?)", code, n, name)
if not ok then
print(" Error creating product: %s", err)
return 'MAIN'
end
product_dump(assert(db:fetchone(
"SELECT name, price FROM products WHERE barcode = ?", code)))
return 'MAIN'
end,
},
}
PROD_EDIT_NAME = {
wait = 120, -- allow 2 minutes for typing product name
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = 'PROD_EDIT_NAME',
keyboard = function(name, product)
if name ~= '' then
product.name = name
end
print(" Type new price (or press enter to keep %.2f DKK):", product.price)
return 'PROD_EDIT_PRICE', product
end,
}
PROD_EDIT_PRICE = {
wait = timeout,
timeout = function()
print " Aborted due to inactivity."
return 'MAIN'
end,
card = login,
barcode = 'PROD_EDIT_PRICE',
keyboard = function(price, product)
if price ~= '' then
local n = tonumber(price)
if not n then
print(" Unable to parse '%s', try again (or press enter to keep %.2f DKK):",
price, product.price)
return 'PROD_EDIT_PRICE', product
end
product.price = n
end
print " Updating product.."
local ok, err = db:fetchone("\z
UPDATE products \z
SET name = ?, price = ? \z
WHERE id = ?", product.name, product.price, product.id)
if not ok then
print(" Error updating product: %s", err)
return 'MAIN'
end
product_dump(assert(db:fetchone(
"SELECT name, price FROM products WHERE id = ?", product.id)))
return 'MAIN'
end,
}
USER = {
wait = timeout,
timeout = idle,
card = login,
barcode = function(code, id, count)
local r = assert(db:fetchone("\z
SELECT id, name, price \z
FROM products \z
WHERE barcode = ?", code))
if r == true then
print " Unknown product.."
return 'USER', id
end
local pid = r[1]
local price = r[3]
if count then
print(" Buying %s for %d * %.2f = %.2f DKK",
r[2], count, price, count * price)
else
print(" Buying %s for %.2f DKK", r[2], price)
count = 1
end
assert(db:exec("\z
BEGIN; \z
UPDATE users SET balance = balance - @count * @amount WHERE id = @id; \z
INSERT INTO log (dt, uid, oid, count, amount) \z
VALUES (datetime('now'), @id, @oid, @count, @amount); \z
COMMIT", { id = id, oid = pid, count = count, amount = price }))
r = assert(db:fetchone(
"SELECT balance FROM users WHERE id = ?", id))
print(" New balance: %.2f DKK", r[1])
return 'USER', id
end,
keyboard = {
['/'] = function(id)
print " Swipe new card (or press enter to abort):"
return 'SWITCH_CARD', id
end,
['*'] = function(id)
local r = assert(db:fetchall("\z
SELECT substr(dt,6,11), \z
CASE \z
WHEN count NOT NULL THEN oname \z
WHEN oid <> ?1 THEN 'Transfer to ' || oname \z
WHEN uid <> ?1 THEN 'Transfer from ' || uname \z
WHEN amount >= 0 THEN 'Deposit' \z
ELSE 'Withdrawal' END, \z
CASE WHEN count NOT NULL THEN count \z
WHEN oid <> ?1 THEN 1 ELSE -1 END, \z
amount \z
FROM full_log \z
WHERE uid = ?1 OR (count IS NULL AND oid = ?1) \z
ORDER BY dt DESC LIMIT 38", id))
for i = #r, 1, -1 do
local row = r[i]
if row[3] == 1 or row[3] == -1 then
print("%s %s %8.2f DKK",
row[1], row[2]:utf8trim(23), -row[3]*row[4])
else
print("%s %s %4d * %6.2f %8.2f DKK",
row[1], row[2]:utf8trim(22), row[3], row[4], -row[3]*row[4])
end
end
return 'USER', id
end,
['+'] = function(id)
print " Enter amount (or press enter to abort):"
return 'DEPOSIT', id
end,
['-'] = function(id)
print " Enter user id (or press enter for user list):"
return 'TRANSFER_LIST', id, 0
end,
['.'] = function(id)
user_menu()
return 'USER', id
end,
['n'] = function(id)
print " Sigh. A number. That is [1-9][0-9]*"
return 'USER', id
end,
[''] = function(id, count)
if count then
print " Aborted."
return 'USER', id
end
return idle()
end,
function(cmd, id, count) --default
if cmd:sub(1,1) == '/' then
return (USER['barcode'])(cmd:sub(2), id, count)
end
local count = tonumber(cmd)
if count then
print(" Buying %d of the next thing scanned. Press ENTER to abort.",
count)
return 'USER', id, count
end
print(" Unknown command '%s'.", cmd)
user_menu()
return 'USER', id
end,
},
}
SWITCH_CARD = {
wait = timeout,
timeout = function(_, id)
print " Aborted due to inactivity."
return 'USER', id
end,
card = function(hash, id)
print "Updating hash.."
local ok, err = db:fetchone(
"UPDATE users SET hash = ? WHERE id = ?", hash, id)
if not ok then
print("Error updating hash: %s", err)
else
print("Done.")
end
return 'USER', id
end,
barcode = 'SWITCH_CARD',
keyboard = function(_, id)
print " Aborted."
return 'USER', id
end,
}
DEPOSIT = {
wait = timeout,
timeout = function(_, id)
print " Aborted due to inactivity."
return 'USER', id
end,
card = login,
barcode = 'DEPOSIT',
keyboard = {
[''] = function(id)
print " Aborted."
return 'USER', id
end,
function(amount, id) --default
local n = tonumber(amount)
if not n then
print(" Unable to parse '%s', try again (or press enter to abort):", amount)
return 'DEPOSIT', id
end
if n >= 0 then
print(" Inserting %.2f DKK", n)
else
print(" Withdrawing %.2f DKK", -n)
end
assert(db:exec("\z
BEGIN; \z
UPDATE users SET balance = balance + @amount WHERE id = @id; \z
INSERT INTO log (dt, uid, oid, count, amount) \z
VALUES (datetime('now'), @id, @id, NULL, @amount); \z
COMMIT", { id = id, amount = n }))
local r = assert(db:fetchone(
"SELECT balance FROM users WHERE id = ?", id))
print(" New balance: %.2f DKK", r[1])
return 'USER', id
end,
},
}
TRANSFER_LIST = {
wait = timeout,
timeout = function(_, id)
print " Aborted due to inactivity."
return 'USER', id
end,
card = login,
barcode = 'TRANSFER_LIST',
keyboard = {
[''] = function(id, offset)
local r = assert(db:fetchall(
"SELECT id, name FROM users ORDER BY id LIMIT 29 OFFSET ?", offset))
local n = #r
if n == 0 then
print " Aborted."
return 'USER', id
end
for i = 1, n < 39 and n or 38 do
local row = r[i]
print(" %4d) %s", row[1], row[2])
end
if n < 39 then
print " Enter user id (or press enter to abort):"
else
print " Enter user id (or press enter to continue list):"
end
return 'TRANSFER_LIST', id, offset + 29
end,
function(cmd, id) --default
local n = tonumber(cmd)
if not n then
print(" Unable to parse '%s', aborted.", cmd)
return 'USER', id
end
local r = assert(db:fetchone(
"SELECT name FROM users WHERE id = ?", n))
if r == true then
print(" No such user. Aborted.")
return 'USER', id
end
print(" Enter amount to transfer to %s (or press enter to abort):", r[1])
return 'TRANSFER_AMOUNT', id, n
end,
},
}
TRANSFER_AMOUNT = {
wait = timeout,
timeout = function(_, id)
print " Aborted due to inactivity."
return 'USER', id
end,
card = login,
barcode = 'TRANSFER_AMOUNT',
keyboard = {
[''] = function(id)
print " Aborted."
return 'USER', id
end,
function(cmd, id, oid) --default
local n = tonumber(cmd)
if not n then
print(" Unable to parse '%s', aborted.", cmd)
return 'USER', id
end
if n <= 0 then
print " Fark you.. Aborted."
return 'USER', id
end
assert(db:exec("\z
BEGIN; \z
UPDATE users SET balance = balance - @amount WHERE id = @id; \z
UPDATE users SET balance = balance + @amount WHERE id = @oid; \z
INSERT INTO log (dt, uid, oid, count, amount) \z
VALUES (datetime('now'), @id, @oid, NULL, @amount); \z
COMMIT", { id = id, oid = oid, amount = n }))
r = assert(db:fetchone(
"SELECT balance FROM users WHERE id = ?", id))
print(" New balance: %.2f DKK", r[1])
return 'USER', id
end,
},
}
--- the "engine" ---
-- all input events goes through this queue
local input = bqueue.new()
-- spawn coroutines to read from
-- inputs and add to the input queue
utils.spawn(function()
local stdin = io.stdin
while true do
local line = assert(stdin:read('*l'))
input:put{ from = 'keyboard', data = line }
end
end)
utils.spawn(function()
local ins = assert(io.open(arg[2] or 'card', 'r'))
local ctx = sha1.new()
while true do
local line = assert(ins:read('*l', '\r'))
input:put{ from = 'card', data = ctx:add(line):add('\r'):hex() }
end
end)
utils.spawn(function()
local ins = assert(io.open(arg[3] or 'barcode', 'r'))
while true do
local line = assert(ins:read('*l', '\r'))
input:put{ from = 'barcode', data = line }
end
end)
-- this is function reads events from the
-- input queue and "runs" the state machine
local function run(...)
local valid_sender = {
timeout = true,
card = true,
barcode = true,
keyboard = true
}
local handle_state
local lookup = {
['string'] = function(s, data, ...) return handle_state(s, ...) end,
['function'] = function(f, data, ...) return handle_state(f(data, ...)) end,
['table'] = function(t, data, ...)
local f = t[data]
if f then return handle_state(f(...)) end
f = assert(t[1], 'no default handler found')
return handle_state(f(data, ...))
end,
}
function handle_state(str, ...)
local state = _ENV[str]
if not state then
error(format("%s: invalid state", tostring(str)))
end
local cmd, err = input:get(state.wait)
if not cmd then
if err == 'timeout' then
cmd = { from = 'timeout', data = 'timeout' }
else
error(err)
end
end
if not valid_sender[cmd.from] then
error(format("%s: spurirous command from '%s'", str, tostring(cmd.from)))
end
local edge = state[cmd.from]
if not edge then
error(format("%s: no edge defined for '%s'", str, cmd.from))
end
local handler = lookup[type(edge)]
if not handler then
error(format("%s: invalid edge '%s'", str, cmd.from))
end
return handler(edge, cmd.data, ...)
end
return handle_state(...)
end
return run(idle())
-- vim: set ts=2 sw=2 noet: