Skip to content

Commit

Permalink
Added a new method, random url selection and cleaned up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hcphoon01 committed Oct 9, 2019
1 parent ce46ba2 commit 84ae60e
Show file tree
Hide file tree
Showing 8 changed files with 1,477 additions and 163 deletions.
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esversion": 8,
"eqeqeq": true
}
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ script:
- npm run cover

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"

# send notification to discord
after_success:
- wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh
- chmod +x send.sh
- ./send.sh success $WEBHOOK_URL
after_failure:
- wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh
- chmod +x send.sh
- ./send.sh failure $WEBHOOK_URL
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A data handler for the VATSIM status system coded in JavaScript
* `getControllerCount()` - Returns the number of controllers connected to the VATSIM network
* `getAirportInfo(airport)` - Returns the details of pilots into or out of a certain airport along with the controllers controlling that airport, the airport argument is a 4 letter ICAO code
* `getPopularAirports()` - Returns a list of the top 10 airports for arriving and departing aircraft

* `getClientDetails(cid)` - Returns the details of a specific pilots flight, where cid is a valid VATSIM cid as an integer

## Tests

Expand Down
267 changes: 143 additions & 124 deletions handler.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,148 @@
const fs = require('fs');
const request = require('request');

class DataHandler{

constructor(overwrite = null){
if(this.shouldUpdate() && !overwrite) {this.update()};
}

shouldUpdate(){
if(!fs.existsSync("vatsimData.json")) {return true};

const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file)
const oldDT = Date.parse(parsed['updated_date']);

const dateDifference = Date.now() - oldDT;
const minutes = Math.floor(dateDifference / 60000);

if (minutes > 2) {return true};
}

async update(){
const body = await this.downloadFile();
const parsedJSON = JSON.parse(body);
parsedJSON['updated_date'] = new Date();
const json = JSON.stringify(parsedJSON);
fs.writeFileSync("vatsimData.json", json);
}

downloadFile(){
return new Promise((resolve, reject) => {
request("http://us.data.vatsim.net/vatsim-data.json", (error, response, body) => {
if (error) reject(error);
if (response.statusCode != 200) {
reject('Invalid status code <' + response.statusCode + '>');
}
resolve(body);
});
});
}

getClientCount(){
const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file);

return(parsed['pilots'].length + parsed['controllers'].length)
}

getPilotCount(){
const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file);

return(parsed['pilots'].length)
}

getControllerCount(){
const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file);

return(parsed['controllers'].length)
}

getAirportInfo(airport = null){
const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file);
let airportInfo = [];

parsed['pilots'].forEach(pilot => {
if(pilot['plan']['departure'] == airport || pilot['plan']['arrival'] == airport){
airportInfo[airportInfo.length] = pilot;
}
});

parsed['controllers'].forEach(controller => {
if(controller['callsign'].includes(airport) && controller['frequency'] != 99998){
airportInfo[airportInfo.length] = controller;
}
})

return(airportInfo);
}

getPopularAirports(){
const file = fs.readFileSync("vatsimData.json");
const parsed = JSON.parse(file);
let airportList = [];
let newAirport;

parsed['pilots'].forEach(pilot => {
if (pilot.plan.departure != ''){
newAirport = true;
for(var i = 0; i < airportList.length; i++) {
if (airportList[i].id == pilot.plan.departure) {
airportList[i].count += 1
newAirport = false;
}
}
if (newAirport == true){
airportList.push({
id: pilot['plan']['departure'],
count: 1
})
}
}
if (pilot.plan.arrival != ''){
newAirport = true;
for(var i = 0; i < airportList.length; i++) {
if (airportList[i].id == pilot.plan.arrival) {
airportList[i].count += 1
newAirport = false;
}
}
if (newAirport == true){
airportList.push({
id: pilot['plan']['arrival'],
count: 1
})
}
}
})
airportList.sort(function(a, b){
return b.count-a.count
});
return(airportList.slice(0,10))
}
class DataHandler {
constructor(overwrite = null) {
if (this.shouldUpdate() && !overwrite) this.update();
}

shouldUpdate() {
if (!fs.existsSync('vatsimData.json')) return true;

const file = fs.readFileSync('vatsimData.json');
const parsed = JSON.parse(file);
const oldDT = Date.parse(parsed.updated_date);

const dateDifference = Date.now() - oldDT;
const minutes = Math.floor(dateDifference / 60000);

if (minutes > 2) return true;

return false;
}

async update() {
const body = await this.downloadFile();
const parsedJSON = JSON.parse(body);

parsedJSON.updated_date = new Date();
const json = JSON.stringify(parsedJSON);
fs.writeFileSync('vatsimData.json', json);
}

downloadFile() {
return new Promise((resolve, reject) => {
const urlList = [
'http://us.data.vatsim.net/vatsim-data.json',
'http://eu.data.vatsim.net/vatsim-data.json',
'http://apac.data.vatsim.net/vatsim-data.json'
];
const url = urlList[Math.floor(Math.random()*urlList.length)];
request(url, (error, response, body) => {
if (error) reject(error);

if (response.statusCode !== 200) {
reject('Invalid status code <' + response.statusCode + '>');
}

resolve(body);
});
});
}

loadFile(){
return(JSON.parse(fs.readFileSync('vatsimData.json')));
}

getClientCount() {
const parsed = this.loadFile();
return (parsed.pilots.length + parsed.controllers.length);
}

getPilotCount() {
const parsed = this.loadFile();
return (parsed.pilots.length);
}

getControllerCount() {
const parsed = this.loadFile();
return (parsed.controllers.length);
}

getAirportInfo(airport = null) {
const parsed = this.loadFile();
let airportInfo = [];

parsed.pilots.forEach(pilot => {
if (pilot.plan.departure === airport || pilot.plan.arrival === airport) {
airportInfo.push(pilot);
}
});

parsed.controllers.forEach(controller => {
if (controller.callsign.includes(airport) && controller.frequency !== 99998) {
airportInfo.push(controller);
}
});
return airportInfo;
}

getPopularAirports() {
const parsed = this.loadFile();
let airportList = [];
let newAirport;

parsed.pilots.forEach(pilot => {
if (pilot.plan.departure !== '') {
newAirport = true;
airportList.forEach(airport => {
if (airport.id === pilot.plan.departure) {
airport.count++;
newAirport = false;
}
});
if (newAirport) {
airportList.push({
id: pilot.plan.departure,
count: 1
});
}
}
if (pilot.plan.arrival !== '') {
newAirport = true;
airportList.forEach(airport => {
if (airport.id === pilot.plan.arrival) {
airport.count++;
newAirport = false;
}
});
if (newAirport) {
airportList.push({
id: pilot.plan.arrival,
count: 1
});
}
}
});

airportList.sort((a, b) => b.count - a.count);

return(airportList.slice(0,10));
}

getClientDetails(cid) {
const parsed = this.loadFile();
let pilotDetails = [];

parsed.pilots.forEach(pilot => {
if (pilot.member.cid === cid){
pilotDetails.push(pilot);
}
});
return pilotDetails[0];
}
}

module.exports = DataHandler;
module.exports = DataHandler;
const handler = new DataHandler();
console.log(handler.getClientDetails(1298096));
Loading

0 comments on commit 84ae60e

Please sign in to comment.