Skip to content

Commit

Permalink
✨ Code restructurization
Browse files Browse the repository at this point in the history
  • Loading branch information
donatmarko committed Dec 30, 2019
1 parent 84d7316 commit 19da33f
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 10 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions api/get_online.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

ini_set('display_errors', 'on');
error_reporting(E_ALL);
header("Content-Type: application/json");
date_default_timezone_set('Etc/UTC');

include('config-inc.php');
require 'classes/whazzup.php';

// VID
$vid = isset($_GET['vid']) && !empty($_GET['vid']) ? $_GET['vid'] : null;

// Callsign
$callsign = isset($_GET['cs']) && !empty($_GET['cs'] && $_GET['cs'] !== '%') ? strtoupper($_GET['cs']) : null;


$wz = new Whazzup('https://api.donatus.hu/oavi/json/whazzup.json', true);
$result = [];

// if filtered to a specific member or session
if ($vid || $callsign)
{
foreach ($wz->GetAll() as $client)
{
if ($vid)
{
if ($client["vid"] == $vid)
$result[] = $client;
}
if ($callsign)
{
if ($client["callsign"] == $callsign)
$result[] = $client;
}
}
echo json_encode($result);
}
else // if we like to return every online member
echo json_encode($wz->GetAll());
170 changes: 170 additions & 0 deletions api/get_tracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

ini_set('display_errors', 'on');
error_reporting(E_ALL);
header("Content-Type: application/json");
date_default_timezone_set('Etc/UTC');

include('config-inc.php');
require 'classes/whazzup.php';

// VID
$vid = isset($_GET['vid']) && !empty($_GET['vid']) ? $_GET['vid'] : null;

// Callsign
$callsign = isset($_GET['cs']) && !empty($_GET['cs'] && $_GET['cs'] !== '%') ? strtoupper($_GET['cs']) : null;

// Session ID
$id = isset($_GET['id']) ? $_GET['id'] : null;
$client = isset($_GET['cl']) ? $_GET['cl'] : null;

$sql = new mysqli($sql_server, $sql_username, $sql_password, $sql_database);
if ($sql->connect_error)
die('Connection failed: ' . $sql->connect_error);
$sql->query("SET time_zone = 'Etc/UTC'");

$sqlnav = new mysqli($sql_server, $sql_username, $sql_password, $sql_database_nav);
if ($sqlnav->connect_error)
die('Connection failed: ' . $sqlnav->connect_error);

$sessions = [];
$airports = [];

$queryPilot = 'SELECT "PILOT" AS type, id, callsign, vid, rating, server, software, online, connected_at, disconnected_at, latitude, longitude, altitude, groundspeed, mode_a, fp_aircraft, fp_speed, fp_rfl, fp_departure, fp_destination, fp_alternate, fp_alternate2, fp_type, fp_pob, fp_route, fp_item18, fp_rule, fp_deptime, fp_eet, fp_endurance, sim_type, last_tracked_at FROM pilots';
$queryATC = 'SELECT "ATC" AS type, id, callsign, vid, rating, server, software, online, connected_at, disconnected_at, latitude, longitude, radar_range, frequency, atis, last_tracked_at FROM atcs';

if ($id && $client !== null)
{
$queryPilot .= ' WHERE id=' . $id;
$queryATC .= ' WHERE id=' . $id;
}
else if ($vid && $callsign)
{
$queryPilot .= ' WHERE vid=' . $vid . ' AND callsign="' . $callsign . '"';
$queryATC .= ' WHERE vid=' . $vid . ' AND callsign="' . $callsign . '"';
}
else if ($vid)
{
$queryPilot .= ' WHERE vid=' . $vid;
$queryATC .= ' WHERE vid=' . $vid;
}
else if ($callsign)
{
$queryPilot .= ' WHERE callsign LIKE "' . $callsign . '"';
$queryATC .= ' WHERE callsign LIKE "' . $callsign . '"';
}
else
die(json_encode(['error' => 'incorrect filter']));

$queryPilot .= ' ORDER BY connected_at DESC';
$queryATC .= ' ORDER BY connected_at DESC';

if ($client == 0 || $client == 1)
{
$queryATC = $sql->query($queryATC);
while ($row = $queryATC->fetch_assoc())
{
$row["online"] = $row["online"] == 1;
if ($row["online"])
{
$duration = time() - strtotime($row["connected_at"]);
}
else
{
$duration = strtotime($row["disconnected_at"]) - strtotime($row["connected_at"]);
}
$data["duration"] = date("H:i:s", $duration);

$sessions[] = array_merge($row, $data);
}
}

if ($client == 0 || $client == 2)
{
$queryPilot = $sql->query($queryPilot);
while ($row = $queryPilot->fetch_assoc())
{
if (!array_key_exists($row['fp_departure'], $airports))
{
$querynav = $sqlnav->query("SELECT * FROM airports WHERE icao='" . $row["fp_departure"] . "'");
if ($rownav = $querynav->fetch_assoc())
{
$airports[$row["fp_departure"]] = [
"name" => $rownav["name"],
"lat" => $rownav["latitude"],
"lon" => $rownav["longitude"],
"arrivals" => 0,
"departures" => 0,
];
}
}
if (!array_key_exists($row['fp_destination'], $airports))
{
$querynav = $sqlnav->query("SELECT * FROM airports WHERE icao='" . $row["fp_destination"] . "'");
if ($rownav = $querynav->fetch_assoc())
{
$airports[$row["fp_destination"]] = [
"name" => $rownav["name"],
"lat" => $rownav["latitude"],
"lon" => $rownav["longitude"],
"arrivals" => 0,
"departures" => 0,
];
}
}

if (array_key_exists($row["fp_departure"], $airports))
{
$data["departure"] = [
"lat" => $airports[$row["fp_departure"]]["lat"],
"lon" => $airports[$row["fp_departure"]]["lon"],
];
$airports[$row["fp_departure"]]["departures"]++;
}
if (array_key_exists($row["fp_destination"], $airports))
{
$data["destination"] = [
"lat" => $airports[$row["fp_destination"]]["lat"],
"lon" => $airports[$row["fp_destination"]]["lon"],
];
$airports[$row["fp_destination"]]["arrivals"]++;
}

$row["online"] = $row["online"] == 1;
if ($row["online"])
{
$duration = time() - strtotime($row["connected_at"]);
}
else
{
$duration = strtotime($row["disconnected_at"]) - strtotime($row["connected_at"]);
}
$data["duration"] = date("H:i:s", $duration);

$sessions[] = array_merge($row, $data);
}
}

$sql->close();
$sqlnav->close();

if ($id && count($sessions) == 1)
{
echo json_encode($sessions[0]);
}
else
{
$airportList = [];
foreach ($airports as $key => $value)
$airportList[] = array_merge(["icao" => $key], $value);

function date_compare($a, $b)
{
$t1 = strtotime($a['connected_at']);
$t2 = strtotime($b['connected_at']);
return $t1 < $t2;
}
usort($sessions, 'date_compare');

echo json_encode(["sessions" => $sessions, "airports" => $airportList]);
}
File renamed without changes.
14 changes: 4 additions & 10 deletions js/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getFlight(id)
{
$.ajax({
cache: false,
url: "get.php?t=0&cl=2",
url: "api/get_tracker.php?cl=2",
data: { id: id },
success: function(data) {
var c = '<h4>Member details:</h4>';
Expand Down Expand Up @@ -82,7 +82,7 @@ function getATC(id)
{
$.ajax({
cache: false,
url: "get.php?t=0&cl=1",
url: "api/get_tracker.php?cl=1",
data: { id: id },
success: function(data) {
var c = '<h4>Member details:</h4>';
Expand Down Expand Up @@ -135,15 +135,9 @@ function loadSessions(vid = 0, callsign = '', cl = 0)
$('[name="vid"]').val(vid);
$('[name="client"]').val(cl);

/*if (vid == 540147)
{
alert('You must not search for the BOSS!');
return;
}*/

$.ajax({
cache: false,
url: "get.php?t=0",
url: "api/get_tracker.php",
data: { vid: vid, cs: callsign, cl: cl },
success: function(data) {
var tbl = '<table class="table table-striped table-sm table-hover">';
Expand Down Expand Up @@ -226,5 +220,5 @@ $("#frmSearch").on('submit', function(e) {
});

$(document).ready(function() {
// loadSessions(540147);

});

0 comments on commit 19da33f

Please sign in to comment.