forked from antixenoinitiative/sentry.api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (110 loc) · 4.26 KB
/
index.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
/**
* AXI Sentry is a application which manages Thargoid Incursions via a database and discord bot which listens and interfaces with EDDN.
* @author CMDR Mgram, CMDR Airom
*/
//------------------ DEV SWITCHES ------------------
// To enable or disble components for testing purposes
const enableListener = 1; // Set to 0 to disable listener from running
const enableAPI = 1; // Set to 0 to disable API from running
//--------------------------------------------------
require("dotenv").config();
const zlib = require("zlib");
const zmq = require("zeromq");
const api = require('express')();
const path = require('path');
const db = require('./db/index');
const endpoint = require('./api/index');
// Global Variables
const SOURCE_URL = 'tcp://eddn.edcd.io:9500'; //EDDN Data Stream URL
const targetAllegiance = "Thargoid";
const targetGovernment = "$government_Dictatorship;";
let msg;
let watchlist;
// Star System processing logic
async function processSystem(msg) {
const { StarSystem, timestamp, SystemAllegiance, SystemGovernment } = msg.message; // Destructuring msg
let date = new Date();
let time = date.getTime(timestamp); // Converting msg timestamp to Unix Epoch
if (SystemAllegiance != undefined && time >= Date.now() - 86400000) { // Checking if report is recent
id = await db.getSysID(StarSystem);
if (watchlist.includes(StarSystem)) { // Check in watchlist
if (SystemAllegiance == targetAllegiance && SystemGovernment == targetGovernment) { // Check if the system is under Incursion
db.addIncursions(id,time);
console.log(`Incursion Logged: ${StarSystem}`);
watchlist = await db.getWatchlist(); // Refresh the watchlist with the new systems to monitor
} else {
db.setStatus(StarSystem,0);
console.log(`${StarSystem} removed from Watchlist because alli = [${SystemAllegiance}], gov = [${SystemGovernment}]`)
watchlist = await db.getWatchlist();
}
} else { // Not in watchlist
if (SystemAllegiance == targetAllegiance && SystemGovernment == targetGovernment) { // Check if the system is under Incursion
if (id == 0) { // Check if system is NOT in DB
db.addSystem(StarSystem).then((res) => {
db.addIncursions(res,time);
});
} else {
db.setStatus(StarSystem, 1);
db.addIncursions(id,time);
}
console.log(`System Logged: ${StarSystem}`);
watchlist = await db.getWatchlist();
}
}
}
}
// Sentry Listener
async function run() {
const sock = new zmq.Subscriber;
watchlist = await db.getWatchlist();
sock.connect(SOURCE_URL);
sock.subscribe('');
console.log("[✔] EDDN Listener Connected: ", SOURCE_URL);
// Data Stream Loop
for await (const [src] of sock) { // For each data packet
msg = JSON.parse(zlib.inflateSync(src));
processSystem(msg);
}
}
// API Code
if (enableAPI == 1) {
api.listen(process.env.PORT,() => {
console.log('[✔] Sentry API Operational'); // Upon a successful connection will log to console
});
} else { console.error(`WARN: API Disabled`)}
api.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/dist/index.html'));
});
api.get('/styles.css', function(req, res) {
res.sendFile(path.join(__dirname, '/dist/styles.css'));
});
api.get('/incursionshistory', async function(req, res) {
const { rows } = await db.query(
`SELECT incursions.inc_id,systems.system_id,systems.name,incursions.time
FROM incursions
INNER JOIN systems
ON incursions.system_id=systems.system_id;`
);
res.json(endpoint.Response(rows))
},
);
api.get('/incursions', async function(req, res) {
const { rows } = await db.query(`SELECT * FROM systems WHERE status = '1'`);
res.json(endpoint.Response(rows))
},
);
api.get('/systems', async function(req, res) {
const { rows } = await db.query(`SELECT * FROM systems`);
res.json(endpoint.Response(rows))
},
);
api.get('/presence', async function(req, res) {
const { rows } = await db.query(`SELECT systems.name,presence.presence_lvl,presence.time
FROM presence
INNER JOIN systems
ON presence.system_id=systems.system_id;`);
res.json(endpoint.Response(rows));
},
);
// Switch Statements
if (enableListener == 1) { run(); } else { console.error(`WARN: Sentry Disabled`)}