-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.js
69 lines (55 loc) · 1.77 KB
/
analyzer.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
/**
* Module dependencies.
*/
var appConfig = require('./config'),
services = require('./lib/services.js'),
random = require('secure_random'),
connection = require('./db.js');
function setup(conn) {
var queue = conn.queue('analyze_queue', {durable: false}, function() {
queue.subscribe(function(msg) {
console.log(msg.body);
random.getRandomInt(1, 3, function(err, value) {
// The value will between 1-3
var winner = '';
if(value == 1) {
winner = 'quoteA';
} else if (value == 2) {
winner = 'quoteB';
} else if (value == 3) {
winner = 'quoteX';
}
var sql = 'SELECT * FROM bets WHERE id_game = ' +
connection.escape(msg.body.id);
connection.query(sql, function(err, result) {
if (err) throw err;
for(var bet in result){
if(result[bet].tip == winner) {
var win = result[bet].quote * result[bet].amount;
var sql_win = 'UPDATE users SET credits = credits + ' +
win + ' WHERE id = ' +
result[bet].id_user;
connection.query(sql_win, function(err, result) {
if (err) throw err;
});
}
var sql_analyzed = 'UPDATE bets SET analyzed = 1 WHERE id = ' +
result[bet].id;
connection.query(sql_analyzed, function(err, result) {
if (err) throw err;
});
}
});
});
});
queue.bind('bettingExchange', 'analyze_queue');
});
}
// Start server
services.getRabbitMqConnection(function(conn) {
if (conn) {
setup(conn);
} else {
console.log("failed to connect to rabbitmq");
}
});