Skip to content

Commit

Permalink
Merge pull request #222 from czqoocavatsim/dev
Browse files Browse the repository at this point in the history
network work
  • Loading branch information
mufassilyasir authored Jun 18, 2023
2 parents a8e3840 + 5851e01 commit f564040
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 71 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Network/NetworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function createMonitoredPosition(Request $request)

$position = new MonitoredPosition();
$position->identifier = $request->get('identifier');
$position->staff_only = $request->get('staffOnly') == 'yes' ? true : false;
$position->staff_only = false; //$request->get('staffOnly') == 'yes' ? true : false;
$position->save();

return redirect()->route('network.monitoredpositions.view', strtolower($position->identifier));
Expand Down
94 changes: 39 additions & 55 deletions app/Jobs/ProcessSessionLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Vatsimphp\VatsimData;
use App\Services\VATSIMClient;

class ProcessSessionLogging implements ShouldQueue
{
Expand All @@ -37,68 +36,53 @@ public function __construct()
*/
public function handle()
{
//Get VATSIMData instance
$vatsimData = new VatsimData();
$dataLoaded = $vatsimData->loadData();

//If no data...
if (!$dataLoaded) {
Log::error('ProcessSessionLogs job: VATSIMPhp failed to load data');
return;
}
//BEGIN CONTROLLER SESSION CHECK

//Get monitored positions
$monitoredPositions = MonitoredPosition::all()->sortBy('staff_only');
$monitoredPositions = MonitoredPosition::all();

$vatsimData = new VATSIMClient();

$positionsFound = [];

//Go through each position and process sessions for each of them
foreach ($monitoredPositions as $position) {
//Get sessions with the positions callsign
$vatsimSessionInstances = $vatsimData->searchCallsign($position->identifier)->toArray();
$controllers = $vatsimData->searchCallsign($position->identifier, false);

foreach ($controllers as $controller) {

//If there is an active session
if ($activeSession = $position->activeSession()) {
if (empty($vatsimSessionInstances)) { //If the session isn't detected online anymore
//Update and end session
$activeSession->session_end = Carbon::now();
$activeSession->duration = $activeSession->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$activeSession->save();
SessionLog::firstOrCreate([
'cid' => $controller->cid,
'callsign' => $controller->callsign,
'session_end' => null,
], [
'session_start' => Carbon::now(),
'emails_sent' => 0,
'monitored_position_id' => $position->id,
'roster_member_id' => RosterMember::where('cid', $controller->cid)->value('id') ?? null,
]);

//If there is an associated roster member, give them the hours
if ($rosterMember = $activeSession->rosterMember) {
if ($rosterMember->certification == 'certified' && $rosterMember->active) {
$rosterMember->currency += $activeSession->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->monthly_hours += $activeSession->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->save();
}
array_push($positionsFound, $controller->callsign);
}
}

//Check existing sessions in db
$sessionLogs = SessionLog::whereNull('session_end')->get();
foreach ($sessionLogs as $log) {
if ((!in_array($log->callsign, $positionsFound)) || $vatsimData->searchCallsign($log->callsign, true)->cid != $log->cid) {
$log->session_end = Carbon::now();
$log->duration = $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$log->save();

//If there is an associated roster member, give them the hours
if ($rosterMember = $log->rosterMember) {
if (($rosterMember->certification == 'certified' || $rosterMember->certification == 'training') && $rosterMember->active) {
$rosterMember->currency += $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->monthly_hours += $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$rosterMember->save();
}
}
} else { //Looking for a new session
// if(empty($vatsimSessionInstances)){ // Should be empty if there's no sessions found, not with an index of 0
// Log::info('No sessions found for '.$position->identifier);
// continue;
// }
// Should only be executing if there's a session in progress
$instance = $vatsimSessionInstances[0];

//Create a new session
$session = SessionLog::create([
'roster_member_id' => RosterMember::whereCid($instance['cid'])->first()->id ?? null,
'cid' => $instance['cid'],
'session_start' => Carbon::parse($instance['time_logon']),
'monitored_position_id' => $position->id,
'emails_sent' => 0,
]);
/*
//Send notifications if staff only, not certified, etc
if ($position->staff_only && ($session->rosterMember == null || $session->rosterMember->certification == 'not_certified'))
{
Notification::route('mail', CoreSettings::find(1)->emailfirchief)->notify(new ControllerNotCertified($session));
$session->emails_sent++;
$session->save();
}
*/
$session->save();
}
}
}
}
}
6 changes: 3 additions & 3 deletions app/Models/Network/MonitoredPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function lastOnline()
return Carbon::create($session->session_end);
}

public function activeSession()
public function activeSession($callsign)
{
if ($session = $this->sessions->where('session_end', null)->first()) {
if ($session = SessionLog::where('callsign', $callsign)->whereNull('session_end')->first()) {
return $session;
} else {
return null;
return false;
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/Models/Network/SessionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class SessionLog extends Model

// session_start and session_end are in format 'Y-m-d H:i:s'
protected $fillable = [
'id', 'roster_member_id', 'cid', 'session_start', 'session_end', 'monitored_position_id', 'duration', 'emails_sent',
];
'id', 'roster_member_id', 'cid', 'session_start', 'session_end', 'monitored_position_id', 'duration', 'emails_sent', 'callsign'];

public function user()
{
Expand Down
51 changes: 51 additions & 0 deletions app/Services/VATSIMClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Services;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;

class VATSIMClient
{
public function getVATSIMData()
{
$client = new Client();
$responseStatus = $client->get('https://status.vatsim.net/status.json');
$dataUrl = json_decode($responseStatus->getBody())->data->v3[0];

$response = $client->get($dataUrl);

if ($response->getStatusCode() === 200) {
return json_decode($response->getBody());
}
}

public function searchCallsign($callsign, $precise)
{
$data = Cache::remember('vatsimdata', 55, function () {
return $this->getVATSIMData();
});

$controllers = [];

foreach ($data->controllers as $controller) {
if ($precise) {
if ($controller->callsign == $callsign) {
return $controller;
}
} else {
$controllerCallsignParts = explode('_', $controller->callsign);
$callsignParts = explode('_', $callsign);
if (($controllerCallsignParts[0] === $callsignParts[0]) && (end($controllerCallsignParts) === end($callsignParts))) {
array_push($controllers, $controller);
}
}
}

if ($precise) {
return false;
}

return $controllers;
}
}
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"type": "project",
"require": {
"php": "^8.0",
"asantibanez/livewire-calendar": "dev-master",
"atymic/twitter": "^3.2",
"barryvdh/laravel-dompdf": "^2.0",
"bugsnag/bugsnag-laravel": "^2.0",
Expand All @@ -20,6 +21,7 @@
"laravel/framework": "^9.0",
"laravel/tinker": "^2.0",
"laravel/ui": "^4.0",
"laravelcollective/html": "^6.4",
"lasserafn/php-initial-avatar-generator": "^4.0",
"lasserafn/php-initials": "^3.1",
"league/flysystem-aws-s3-v3": "^3.1",
Expand All @@ -32,8 +34,7 @@
"spatie/laravel-activitylog": "^4.7",
"spatie/laravel-cookie-consent": "^3.2",
"spatie/laravel-html": "^3.2",
"spatie/laravel-permission": "^5.10",
"laravelcollective/html": "^6.4"
"spatie/laravel-permission": "^5.10"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.8",
Expand Down Expand Up @@ -83,5 +84,6 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"repositories": [{ "type": "package", "canonical": false, "package": { "version": "master", "name": "asantibanez/livewire-calendar", "source": { "url": "https://github.com/Butochnikov/livewire-calendar.git", "type": "git", "reference": "master" } } }]
}
16 changes: 14 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('session_logs', function (Blueprint $table) {
$table->string('callsign');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('session_logs', function (Blueprint $table) {
$table->dropColumn('callsign');
});
}
};
10 changes: 5 additions & 5 deletions resources/views/admin/network/monitoredpositions/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@
</div>
{!! Form::open(['route' => 'network.monitoredpositions.create']) !!}
<div class="modal-body">
<p>ActivityBot will monitor positions for activity and will record sessions. You can specify a prefix or complete callsign.</p>
<p>ActivityBot will monitor positions for activity and will record sessions. Please DO NOT specify a prefix, the system will check all possible combinations.</p>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Identifier</label>
{!! Form::text('identifier', null, ['class' => 'form-control', 'placeholder' => 'CZQX_']) !!}
{!! Form::text('identifier', null, ['class' => 'form-control', 'placeholder' => 'NAT_FSS']) !!}
</div>
<div class="form-group">
{{-- <div class="form-group">
<label for="recipient-name" class="col-form-label">Staff Only</label>
{{ Form::checkbox('staffOnly', 'no', false) }}
</div>
</div> --}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
{!! Form::submit('Submit', ['class' => 'btn btn-primary']) !!}
{!! Form::submit('Create', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
Expand Down

0 comments on commit f564040

Please sign in to comment.