Skip to content

Commit

Permalink
Merge pull request #319 from czqoocavatsim/JoshuaBranch
Browse files Browse the repository at this point in the history
NetworkActivity Messages
  • Loading branch information
JoshuaMicallefYBSU authored Aug 20, 2024
2 parents 523f7c8 + bcd9bbe commit 8552575
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/DiscordTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Http\Request;
use App\Services\DiscordClient;
use App\Jobs\DiscordTrainingUpdates;
use App\Jobs\ProcessSessionLogging;

class DiscordTestController extends Controller
{
Expand All @@ -23,10 +23,10 @@ public function EditTagTest()
return $results;
}

public function Shanwick()
public function Job()
{
// Dispatch the job
$job = DiscordTrainingUpdates::dispatch();
$job = ProcessSessionLogging::dispatch();

// Call the handle method directly to get the result synchronously
$result = $job->handle();
Expand Down
33 changes: 32 additions & 1 deletion app/Jobs/ProcessSessionLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Services\VATSIMClient;
use App\Services\DiscordClient;

class ProcessSessionLogging implements ShouldQueue
{
Expand Down Expand Up @@ -51,7 +52,7 @@ public function handle()

foreach ($controllers as $controller) {

SessionLog::firstOrCreate([
$session = SessionLog::firstOrCreate([
'cid' => $controller->cid,
'callsign' => $controller->callsign,
'session_end' => null,
Expand All @@ -62,6 +63,20 @@ public function handle()
'roster_member_id' => RosterMember::where('cid', $controller->cid)->value('id') ?? null,
]);

if($session->user){
$name = $session->user->fullName('FL');
} else {
$name = $controller->cid;
}

if($session->discord_id == null){
$discord = new DiscordClient();
$discord_id = $discord->ControllerConnection($controller->callsign, $name);
}

$session->discord_id = $discord_id;
$session->save();

array_push($positionsFound, $controller->callsign);
}
}
Expand All @@ -74,6 +89,22 @@ public function handle()
$log->duration = $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
$log->save();

// dd($log);

if($log->user){
$name = $log->user->fullName('FLC');
} else {
$name = $log->cid;
}

if($log->discord_id !== null){
$discord = new DiscordClient();
$data = $discord->ControllerDisconnect($log->discord_id, $log->callsign, $name, $log->session_start, $log->duration);

$log->discord_id = null;
$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) {
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Network/SessionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Roster\RosterMember;
use Illuminate\Database\Eloquent\Model;
use App\Services\VATSIMClient;
use App\Models\Users\User;
use Spatie\Activitylog\Traits\LogsActivity;

// Log of all sessions
Expand Down Expand Up @@ -52,7 +53,7 @@ class SessionLog extends Model

public function user()
{
$this->belongsTo(User::class);
return $this->belongsTo(User::class);
}

public function position()
Expand Down
47 changes: 47 additions & 0 deletions app/Services/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,53 @@ public function sendMessageWithEmbed($channelId, $title, $description)
return $response->getStatusCode() == 200;
}

public function ControllerConnection($callsign, $name)
{
$response = $this->client->post("channels/1275443682992197692/messages", [
'json' => [
"tts" => false,
"embeds" => [
[
'title' => $callsign.' Just Connected!',
'description' => 'A new controller has just connected to the network!
Controller Name: '.$name.'
Online from: <t:'.Carbon::now()->timestamp.':t>',
'color' => hexdec('6EC40C'),
]
]
]
]);

$responseData = json_decode($response->getBody(), true);

return $responseData['id'];
}

public function ControllerDisconnect($id, $callsign, $name, $connect_time, $total_time)
{
$response = $this->client->patch("channels/1275443682992197692/messages/{$id}", [
'json' => [
"tts" => false,
"embeds" => [
[
'title' => $callsign.' is Offline',
'description' => $name.' was connected to '.$callsign.'.
Online from <t:'.Carbon::parse($connect_time)->timestamp.':t> to <t:'.Carbon::now()->timestamp.':t>.
Time: '.sprintf('%d hours %d minutes', intdiv($total_time, 1), ($total_time - intdiv($total_time, 1)) * 60),

'color' => hexdec('990000'),
]
]
]
]);

$responseData = json_decode($response->getBody(), true);

return $responseData;
}

public function assignRole($discordId, $roleId)
{
ProcessDiscordRoles::dispatch(true, $discordId, $roleId);
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

// Discord shortcut
Route::get('/discord', [DiscordController::class, 'joinShortcut']);
Route::get('/discord/function-test', [DiscordTestController::class, 'Shanwick']);
Route::get('/discord/function-test', [DiscordTestController::class, 'Job']);

// Public news articles
Route::get('/news/{id}', [NewsController::class, 'viewArticlePublic'])->name('news.articlepublic')->where('id', '[0-9]+');
Expand Down

0 comments on commit 8552575

Please sign in to comment.