forked from Juerd/shalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
returns.p6
57 lines (51 loc) · 1.67 KB
/
returns.p6
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
#!perl6
use v6;
use lib ~$*PROGRAM.resolve.sibling: 'lib';
use Entity;
use Command;
use Color;
use Prompt;
my $port = 9101;
my $my-location = Entity.load('lhq-returns');
$my-location ~~ Place or die;
sub display ($x is copy) {
my Buf $buf .= new(0x1b, 0x42, 0x30, 0x1b, 0x25);
$buf.push: $x.subst(:global, "\n", "\x0d").encode;
$buf.push: Buf.new: 0x03;
return $buf;
}
react {
whenever IO::Socket::Async.listen('0.0.0.0', $port) -> $conn {
say "New connection";
whenever $conn.Supply -> $input is copy {
$input.=trim;
say "Received: $input";
if $input ~~ /<-[\x20..\x7F]>/ {
$conn.write: display "malformed barcode.";
next;
}
if Entity.load($input) -> $entity {
$entity.update;
if $entity ~~ Lendable {
my $l = $entity.location;
my $reply;
if $l.id eq $my-location.id {
$conn.write: display "Duplicate scan.";
next;
} elsif $l ~~ Person {
my $name = $l.id.subst(/^'angel-'/, '');
$reply = "Hi $name,\nthanks for returning\n$entity.id()!\n";
} else {
$reply = "Next time don't forget\nto check out!\n";
}
$entity.update;
$entity.is-at: $my-location;
$entity.store;
$conn.write: display $reply;
}
} else {
$conn.write: display "Unknown barcode\n$input";
}
}
}
}