-
Notifications
You must be signed in to change notification settings - Fork 0
/
haggle.js
183 lines (143 loc) · 3.95 KB
/
haggle.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
var haggle = exports; exports.constructor = function haggle(){};
var mysql = require('mysql');
var async = require('async');
var path = require('path');
var Hash = require('hashish');
var client = mysql.createClient({
'host': 'localhost',
'database': 'haggle'
});
var DBG = (process.env.DEBUG) ? console.log : false;
var HAGGLE_RUNNING = 1;
haggle.products = {};
haggle.post = function(req, res) {
console.log('got a post');
var pid = req.params.pid;
var uid = req.params.uid;
var price = req.params.price;
var body = "";
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
DBG&&DBG("POST: (" + pid + ", " + uid + "," + price + ")");
DBG&&DBG(" body: " + body);
negotiateHaggle(pid, uid, price, function(err, neg) {
res.send(neg);
});
});
};
haggle.get = function(req, res) {
var pid = req.params.pid;
var uid = req.params.uid;
var body = "";
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
try {
DBG&&DBG("GET: (" + pid + ", " + uid + ")");
processGet(pid, uid, function(err, haggle) {
var retBody ={
'err' : err,
'haggle' : haggle
};
return res.json({ 'response': retBody, 'err': err }, 200);
});
} catch(err) {
return res.json({ 'err': err }, 500);
}
});
};
haggle.poll = function() {
var sql = 'SELECT id, start_time, status ' +
'FROM haggle ';
client.query(sql, function(err, res) {
if (err) {
DBG&&DBG('ERROR READING FROM DB');
}
Hash.forEach(res, function(entry) {
var running = entry.status === HAGGLE_RUNNING;
var startTime = Date.now(entry.startTime);
var currTime = Date.now();
if (startTime <= currTime && running) {
// haggle.run(entry);
}
});
});
};
function processPost(pid, uid, body, cb) {
cb(null, { 'data': "Something Interesting" } );
}
function processGet(pid, uid, cb) {
cb(null, { 'data': "Something Interesting" } );
}
function negotiateHaggle(pid, uid, price, cb) {
if (!haggle.products[pid + ':' + uid]) {
newBuyer(pid, uid, function(err, res) {
// quit here maybe
if (err) {
throw new Error ('error getting things');
}
haggle.products[pid + ':' + uid] = {
'onTry': 0,
'minPrice': res.item[0][0].min_price,
'maxPrice': res.item[0][0].max_price,
'haggles': res.user[0][0].haggles
};
return cb(null, next(pid, uid, price));
});
} else {
return cb(null, next(pid, uid, price));
}
}
function next(pid, uid, price) {
var prod = haggle.products[pid + ':' + uid];
// this is a new try
++prod.onTry;
var max = parseInt(prod.maxPrice, 10);
var min = parseInt(prod.minPrice, 10);
var haggles = parseInt(prod.haggles, 10);
var diff = max - price;
var half = max + min / 2;
if (prod.lastRes && price < prod.lastRes) {
return "I want " + prod.lastRes;
}
if (parseInt(price, 10) >= prod.lastRes) {
return accept();
}
if (price >= (max * 0.85)) {
return accept();
}
if (prod.onTry >= 3 && price >= min) {
return accept();
}
if (prod.onTry === 2 && price >= half) {
return accept();
}
if (prod.onTry === 1 && price >= half + ((price + half) / 2)) {
return accept();
}
else {
/* console.log('hdjsahdjkasj', x1 + parseInt(price, 10)); */
var add = (max - parseInt(price, 10)) * 0.5;
var newPrice = parseInt(price, 10);
var res = add + newPrice;
prod.lastRes = res;
return "I want " + (newPrice + add);
}
}
function accept() {
return "deal brooo";
}
function newBuyer(pid, uid, cb) {
var itemQuery = 'SELECT min_price, max_price FROM item where id = ?';
var userQuery = 'SELECT haggles FROM user where id = ?';
var tasks = {
item: client.query.bind(client, itemQuery, [pid]),
user: client.query.bind(client, userQuery, [uid])
};
async.parallel(tasks, function(err, res) {
cb(err, res);
});
}