This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.js
186 lines (154 loc) · 4.86 KB
/
agent.js
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
const express = require('express');
const {setLogLevel, logExpression} = require('@cisl/zepto-logger');
const argv = require('minimist')(process.argv.slice(2));
const appSettings = require('./appSettings.json')
const {postToService} = require('./utils');
const packageJson = require('./package.json');
let myPort = argv.port || appSettings.defaultPort || 14007;
let logLevel = 1;
if (argv.level) {
logLevel = argv.level;
logExpression(`Setting log level to ${logLevel}`, 1);
}
setLogLevel(logLevel);
const ingredient_list = [
'egg',
'flour',
'milk',
'sugar',
'chocolate',
'vanilla',
'blueberry'
];
let currentUtility = null;
let currentName = null;
let currentRound = null;
let currentBid = null;
let roundActive = false;
let roundStart = 0;
let roundDuration = 0;
const ingredientPattern = new RegExp(`(\\d+ (?:${ingredient_list.join('|')})s?)`, 'g');
const pricePattern = new RegExp(/(?:\${1}([\d\.]+)|([\d\.]+) USD)/);
const app = express();
app.set('port', myPort);
app.set('json spaces', 2);
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.get('/', (req, res) => {
res.json({
agent: 'kleene',
version: packageJson.version
});
});
app.post('/receiveMessage', (req, res) => {
logExpression('inside /receiveMessage', 2);
logExpression(req.body, 2);
if (!roundActive) {
return res.json({
status: 'round_not_active',
});
}
logExpression(`time in round remaining: ${roundDuration - ((Date.now() - roundStart) / 1000)}`, 1);
if (req.body.addressee.toLowerCase() !== currentName.toLowerCase()) {
return res.json({
status: 'not_my_name'
});
}
const acceptPhrases = [`${currentName} I accept`.toLowerCase(), `${currentName} I agree`.toLowerCase()].includes(req.body.text.toLowerCase());
if (currentBid && acceptPhrases) {
const payload = {
text: "You got it! Enjoy your purchase!",
speaker: currentName,
role: 'seller',
addressee: 'Human',
timeStamp: new Date(),
bid: currentBid
};
payload.bid.type = 'Accept';
postToService('environment-orchestrator', '/relayMessage', payload);
return res.json({status: 'acknowledged'});
}
const current = {};
logExpression(`Matching items with: ${ingredientPattern}`);
const items = req.body.text.match(ingredientPattern);
if (items) {
for (let item of items) {
item = item.split(/[ ]+/);
if (item[1].substr(-1) === 's') {
item[1] = item[1].substring(0, item[1].length - 1);
}
current[item[1]] = parseInt(item[0]);
}
}
logExpression('Parsed items from input:', 2);
logExpression(current, 2);
logExpression(`Matching price with: ${pricePattern}`);
const bidReg = req.body.text.match(pricePattern);
let bidPrice;
if (bidReg) {
bidPrice = parseFloat((bidReg[1] === null) ? bidReg[2] : bidReg[1])
}
else {
bidPrice = Math.floor(Math.random() * 10);
}
logExpression(`Parsed bid price: ${bidPrice}`, 2);
if (Math.random() > 0.4) {
bidPrice += Math.random() * 4;
}
logExpression(`Adjusted bid price: ${bidPrice}`, 2);
bidPrice = parseFloat(bidPrice.toFixed(2));
logExpression(`Rounded bid price: ${bidPrice}`, 2);
currentBid = {
quantity: current,
price: {
value: bidPrice,
unit: 'USD'
}
};
logExpression('Current bid:', 2);
logExpression(currentBid);
const payload = {
text: `How about I sell you ${Object.keys(currentBid.quantity).map((k) => `${currentBid.quantity[k]} ${k}`).join(', ')} for ${currentBid.price.value.toFixed(2)} ${currentBid.price.unit}?`,
speaker: currentName,
role: 'seller',
addressee: 'Human',
timeStamp: new Date(),
bid: currentBid
};
payload.bid.type = 'SellOffer';
postToService('environment-orchestrator', '/relayMessage', payload);
return res.json({status: 'acknowledged'});
});
app.post('/receiveRejection', (req, res) => {
logExpression('inside /receiveRejection', 2);
logExpression(req.body, 2);
res.json({status: 'acknowledged'});
});
app.post('/setUtility', (req, res) => {
logExpression('inside /setUtility', 2);
logExpression(req.body, 2);
currentName = req.body.name;
currentUtility = req.body;
delete currentUtility.name;
res.json({status: 'acknowledged'});
});
app.post('/startRound', (req, res) => {
logExpression('inside /startRound', 2);
logExpression(req.body, 2);
currentRound = req.body.roundNumber;
roundDuration = parseFloat(req.body.roundDuration);
roundStart = Date.now();
roundActive = true;
logExpression(`Round ${currentRound} started`, 1);
res.json({status: 'acknowledged'});
});
app.post('/endRound', (req, res) => {
logExpression('inside /endRound', 2);
logExpression(req.body, 2);
roundActive = false;
logExpression(`Round ${currentRound} ended`, 1);
res.json({status: 'acknowledged'});
});
app.listen(app.get('port'), () => {
logExpression(`Express server listening on ${app.get('port')}`, 1);
});