-
Notifications
You must be signed in to change notification settings - Fork 21
/
app.js
116 lines (105 loc) · 3.76 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var moment = require('moment')
var gaussian = require('gaussian');
var dist = gaussian(0, 0.3);
var configFile = require('./config');
// Set up Postgres connection
var pg = require('pg');
var config = {
user: process.env.DB_USER || configFile.DB_USER,
database: process.env.DB_NAME || configFile.DB_NAME,
password: process.env.DB_PASS || configFile.DB_PASS,
port: process.env.DB_PORT || configFile.DB_PORT,
max: 20,
idleTimeoutMillis: 30000
}
var pool = new pg.Pool(config);
var encounter_encoded = process.env.ENC_ENC || configFile.ENC_ENC;
var app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
var server = require('http').Server(app);
var port = process.env.WS_PORT || configFile.WS_PORT;
var era = process.env.ERA || configFile.ERA;
server.listen(port, function (err) {
console.info('Running server on port ' + port);
});
app.post('/', function(req, res) {
var body = req.body;
if (body.type == "pokemon") {
var m = body.message;
m.pokemon_go_era = era;
m.hidden_time_unix_s = m.disappear_time;
m.hidden_time_utc = moment
.unix(m.hidden_time_unix_s).utc()
.format("YYYY-MM-DD HH:mm:ss");
m.latitude_jittered = parseFloat(m.latitude) + dist.ppf(Math.random()) * 0.0005;
m.longitude_jittered = parseFloat(m.longitude) + dist.ppf(Math.random()) * 0.0005;
if (m.is_lured == true) {
m.time_until_hidden_ms = null;
m.is_lured = "Lured";
} else {
m.is_lured = "Not Lured";
}
if (encounter_encoded == "t") {
buff = new Buffer(m.encounter_id, 'base64');
m.encounter_id = buff.toString();
}
// console.log(m);
pool.connect(function(err, client, done) {
if(err) {
return console.log('Error fetching client from pool', err);
}
query_string = "INSERT INTO spotted_pokemon ( \
encounter_id, \
last_modified_time, \
time_until_hidden_ms, \
hidden_time_unix_s, \
hidden_time_utc, \
spawnpoint_id, \
longitude, \
latitude, \
pokemon_id, \
longitude_jittered, \
latitude_jittered, \
pokemon_go_era, \
lured) \
VALUES ( \
'" + m.encounter_id + "'::text, \
" + m.last_modified_time + "::bigint, \
" + m.time_until_hidden_ms + "::bigint, \
" + m.hidden_time_unix_s + "::bigint, \
'" + m.hidden_time_utc + "'::timestamp, \
'" + m.spawnpoint_id + "'::text, \
" + m.longitude + "::double precision, \
" + m.latitude + "::double precision, \
" + m.pokemon_id + "::smallint, \
" + m.longitude_jittered + "::double precision, \
" + m.latitude_jittered + "::double precision, \
" + m.pokemon_go_era + "::integer, \
'" + m.is_lured + "'::text \
) \
ON CONFLICT (encounter_id, spawnpoint_id) \
DO UPDATE \
SET last_modified_time = EXCLUDED.last_modified_time, \
time_until_hidden_ms = EXCLUDED.time_until_hidden_ms, \
hidden_time_unix_s = EXCLUDED.hidden_time_unix_s, \
hidden_time_utc = EXCLUDED.hidden_time_utc;";
// console.log(query_string);
console.log("[" + moment().format() + "] Pokemon with ID " + m.pokemon_id + " found.")
client.query(query_string,
function(err, result) {
// console.log(result);
done();
if(err) {
return console.log("Error running query", err);
}
});
});
}
res.sendStatus(200);
});
pool.on('error', function(err, client) {
console.log('Idle client error', err.message, err.stack);
});