Skip to content

Commit 8552575

Browse files
Merge pull request #319 from czqoocavatsim/JoshuaBranch
NetworkActivity Messages
2 parents 523f7c8 + bcd9bbe commit 8552575

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

app/Http/Controllers/DiscordTestController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use App\Services\DiscordClient;
7-
use App\Jobs\DiscordTrainingUpdates;
7+
use App\Jobs\ProcessSessionLogging;
88

99
class DiscordTestController extends Controller
1010
{
@@ -23,10 +23,10 @@ public function EditTagTest()
2323
return $results;
2424
}
2525

26-
public function Shanwick()
26+
public function Job()
2727
{
2828
// Dispatch the job
29-
$job = DiscordTrainingUpdates::dispatch();
29+
$job = ProcessSessionLogging::dispatch();
3030

3131
// Call the handle method directly to get the result synchronously
3232
$result = $job->handle();

app/Jobs/ProcessSessionLogging.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Queue\InteractsWithQueue;
1313
use Illuminate\Queue\SerializesModels;
1414
use App\Services\VATSIMClient;
15+
use App\Services\DiscordClient;
1516

1617
class ProcessSessionLogging implements ShouldQueue
1718
{
@@ -51,7 +52,7 @@ public function handle()
5152

5253
foreach ($controllers as $controller) {
5354

54-
SessionLog::firstOrCreate([
55+
$session = SessionLog::firstOrCreate([
5556
'cid' => $controller->cid,
5657
'callsign' => $controller->callsign,
5758
'session_end' => null,
@@ -62,6 +63,20 @@ public function handle()
6263
'roster_member_id' => RosterMember::where('cid', $controller->cid)->value('id') ?? null,
6364
]);
6465

66+
if($session->user){
67+
$name = $session->user->fullName('FL');
68+
} else {
69+
$name = $controller->cid;
70+
}
71+
72+
if($session->discord_id == null){
73+
$discord = new DiscordClient();
74+
$discord_id = $discord->ControllerConnection($controller->callsign, $name);
75+
}
76+
77+
$session->discord_id = $discord_id;
78+
$session->save();
79+
6580
array_push($positionsFound, $controller->callsign);
6681
}
6782
}
@@ -74,6 +89,22 @@ public function handle()
7489
$log->duration = $log->session_start->floatDiffInMinutes(Carbon::now()) / 60;
7590
$log->save();
7691

92+
// dd($log);
93+
94+
if($log->user){
95+
$name = $log->user->fullName('FLC');
96+
} else {
97+
$name = $log->cid;
98+
}
99+
100+
if($log->discord_id !== null){
101+
$discord = new DiscordClient();
102+
$data = $discord->ControllerDisconnect($log->discord_id, $log->callsign, $name, $log->session_start, $log->duration);
103+
104+
$log->discord_id = null;
105+
$log->save;
106+
}
107+
77108
//If there is an associated roster member, give them the hours
78109
if ($rosterMember = $log->rosterMember) {
79110
if (($rosterMember->certification == 'certified' || $rosterMember->certification == 'training') && $rosterMember->active) {

app/Models/Network/SessionLog.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\Roster\RosterMember;
77
use Illuminate\Database\Eloquent\Model;
88
use App\Services\VATSIMClient;
9+
use App\Models\Users\User;
910
use Spatie\Activitylog\Traits\LogsActivity;
1011

1112
// Log of all sessions
@@ -52,7 +53,7 @@ class SessionLog extends Model
5253

5354
public function user()
5455
{
55-
$this->belongsTo(User::class);
56+
return $this->belongsTo(User::class);
5657
}
5758

5859
public function position()

app/Services/DiscordClient.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,53 @@ public function sendMessageWithEmbed($channelId, $title, $description)
5656
return $response->getStatusCode() == 200;
5757
}
5858

59+
public function ControllerConnection($callsign, $name)
60+
{
61+
$response = $this->client->post("channels/1275443682992197692/messages", [
62+
'json' => [
63+
"tts" => false,
64+
"embeds" => [
65+
[
66+
'title' => $callsign.' Just Connected!',
67+
'description' => 'A new controller has just connected to the network!
68+
69+
Controller Name: '.$name.'
70+
Online from: <t:'.Carbon::now()->timestamp.':t>',
71+
'color' => hexdec('6EC40C'),
72+
]
73+
]
74+
]
75+
]);
76+
77+
$responseData = json_decode($response->getBody(), true);
78+
79+
return $responseData['id'];
80+
}
81+
82+
public function ControllerDisconnect($id, $callsign, $name, $connect_time, $total_time)
83+
{
84+
$response = $this->client->patch("channels/1275443682992197692/messages/{$id}", [
85+
'json' => [
86+
"tts" => false,
87+
"embeds" => [
88+
[
89+
'title' => $callsign.' is Offline',
90+
'description' => $name.' was connected to '.$callsign.'.
91+
92+
Online from <t:'.Carbon::parse($connect_time)->timestamp.':t> to <t:'.Carbon::now()->timestamp.':t>.
93+
Time: '.sprintf('%d hours %d minutes', intdiv($total_time, 1), ($total_time - intdiv($total_time, 1)) * 60),
94+
95+
'color' => hexdec('990000'),
96+
]
97+
]
98+
]
99+
]);
100+
101+
$responseData = json_decode($response->getBody(), true);
102+
103+
return $responseData;
104+
}
105+
59106
public function assignRole($discordId, $roleId)
60107
{
61108
ProcessDiscordRoles::dispatch(true, $discordId, $roleId);

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

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

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

0 commit comments

Comments
 (0)