Skip to content

Commit

Permalink
server visiblity settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hobsRKM committed Jun 9, 2024
1 parent c83b2fd commit 0698faa
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 13 deletions.
56 changes: 56 additions & 0 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\SaAdmin;
use App\Models\SaAdminsFlags;
use App\Models\SaServer;
use App\Models\ServerVisibilitySetting;
use App\Services\RconService;
use Carbon\Carbon;
use Illuminate\Http\Request;
Expand All @@ -21,12 +22,46 @@ class ServerController extends Controller
* @param RconService $rcon
* @return \Illuminate\Http\JsonResponse
*/

public function showServerSettings()
{
$servers = SaServer::all();
$serverVisibilitySettings = ServerVisibilitySetting::pluck('is_visible', 'server_id')->toArray();

return view('settings.servers', compact('servers', 'serverVisibilitySettings'));
}

public function updateServerSettings(Request $request)
{
$serverSettings = $request->input('servers', []);

try {
// Insert new settings
foreach ($serverSettings as $serverId => $isVisible) {
ServerVisibilitySetting::updateOrCreate(
['server_id' => $serverId],
['is_visible' => $isVisible]
);
}

return redirect()->back()->with('success', 'Server visibility settings updated successfully.');
} catch (\Exception $e) {
return redirect()->back()->withErrors(['error' => 'Failed to update server visibility settings: ' . $e->getMessage()]);
}
}

public function getAllServerInfo(RconService $rcon)
{
$servers = SaServer::all();
$serverVisibilitySettings = ServerVisibilitySetting::pluck('is_visible', 'server_id')->toArray();

$formattedServers = [];

foreach ($servers as $server) {
if (isset($serverVisibilitySettings[$server->id]) && !$serverVisibilitySettings[$server->id]) {
continue; // Skip hidden servers
}

list($serverIp, $serverPort) = explode(":", $server->address);
// Fetch server information using the SteamService
try {
Expand Down Expand Up @@ -70,7 +105,28 @@ public function getAllServerInfo(RconService $rcon)

return response()->json($formattedServers);
}
public function syncNewServers(Request $request)
{
$servers = SaServer::all();
$synced = false;

foreach ($servers as $server) {
$existingSetting = ServerVisibilitySetting::where('server_id', $server->id)->first();
if (!$existingSetting) {
ServerVisibilitySetting::create([
'server_id' => $server->id,
'is_visible' => 1 // Default to visible, change as needed
]);
$synced = true;
}
}

if ($synced) {
return response()->json(['success' => 'New servers synced successfully.']);
} else {
return response()->json(['error' => 'No new servers to sync.']);
}
}
private function isPortOpen($ip, $port, $timeout = 1) {
$fp = @fsockopen($ip, $port, $errno, $errstr, $timeout);
if (!in_array($errno, [SOCKET_ETIMEDOUT,SOCKET_EHOSTUNREACH,SOCKET_ENETUNREACH]) && stripos(strtolower($errstr), 'failed') === false) {
Expand Down
19 changes: 12 additions & 7 deletions app/Http/Controllers/WeaponSkinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function load($type)
{
$skins = json_decode(File::get(resource_path('json/skins.json')), true);
$appliedSkins = DB::connection('mysqlskins')->table('wp_player_skins')->where('steamid', Auth::user()?->steam_id)->get();
$appliedKnife = DB::connection('mysqlskins')->table('wp_player_knife')->where('steamid', Auth::user()?->steam_id)->first()->knife;

$filteredSkins = array_filter($skins, function($skin) use ($type) {
if($type == 'knife' && in_array($skin['weapon_defindex'], [
Expand Down Expand Up @@ -104,8 +105,11 @@ public function load($type)

// Mark skin as applied if it exists in appliedSkins
foreach ($filteredSkins as &$skin) {
$skin['is_applied'] = $appliedSkins->contains(function ($value) use ($skin) {
return $value->weapon_defindex == $skin['weapon_defindex'] && $value->weapon_paint_id == $skin['paint'];
$skin['is_applied'] = $appliedSkins->contains(function ($value) use ($skin, $type, $appliedKnife){
if($type == 'knife'){
return $value->weapon_defindex == $skin['weapon_defindex'] && $value->weapon_paint_id == $skin['paint'] && $appliedKnife == $skin['weapon_name'] ;
}
return $value->weapon_defindex == $skin['weapon_defindex'] && $value->weapon_paint_id == $skin['paint'] ;
});
}

Expand Down Expand Up @@ -203,13 +207,13 @@ public function agents()
public function gloves()
{
$gloves = json_decode(File::get(resource_path('json/gloves.json')), true);
$appliedGloveIndex = DB::connection('mysqlskins')->table('wp_player_gloves')->where('steamid', Auth::user()?->steam_id)->first()->weapon_defindex;

// Fetch applied gloves from the database
$appliedGloves = DB::connection('mysqlskins')->table('wp_player_skins')
->where('steamid', Auth::user()?->steam_id)
->pluck('weapon_paint_id')
->toArray();

// Group gloves by paint name prefix
$gloveTypes = [];
foreach ($gloves as $glove) {
Expand All @@ -218,9 +222,10 @@ public function gloves()
$gloveTypes[$paintPrefix] = [];
}

// Mark glove as applied if it exists in appliedGloves
$glove['is_applied'] = in_array($glove['paint'], $appliedGloves);

// Mark glove as applied if it exists in appliedGloves
$glove['is_applied'] = in_array($glove['paint'], $appliedGloves) && $glove['weapon_defindex'] == $appliedGloveIndex;
// dump($glove);
$gloveTypes[$paintPrefix][] = $glove;
}

Expand All @@ -231,7 +236,6 @@ public function gloves()
});
$gloveTypes[$type] = $gloves;
}

return view('weapons.gloves', compact('gloveTypes'));
}

Expand All @@ -242,14 +246,15 @@ public function loadGloves($type)
->where('steamid', Auth::user()?->steam_id)
->pluck('weapon_paint_id')
->toArray();
$appliedGloveIndex = DB::connection('mysqlskins')->table('wp_player_gloves')->where('steamid', Auth::user()?->steam_id)->first()->weapon_defindex;

$filteredGloves = array_filter($gloves, function($glove) use ($type) {
return str_contains(strtolower($glove['paint_name']), strtolower($type));
});

// Mark glove as applied if it exists in appliedGloves
foreach ($filteredGloves as &$glove) {
$glove['is_applied'] = in_array($glove['paint'], $appliedGloves);
$glove['is_applied'] = in_array($glove['paint'], $appliedGloves) && $glove['weapon_defindex'] == $appliedGloveIndex;;
}

// Sort applied gloves to be first
Expand Down
21 changes: 21 additions & 0 deletions app/Models/ServerVisibilitySetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ServerVisibilitySetting extends Model
{
use HasFactory;

protected $fillable = [
'server_id',
'is_visible',
];

public function server()
{
return $this->belongsTo(SaServer::class, 'server_id');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

class CreateServerVisibilitySettingsTable extends Migration
{
public function up()
{
Schema::create('server_visibility_settings', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('server_id');
$table->boolean('is_visible')->default(true);
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('server_visibility_settings');
}
}
2 changes: 1 addition & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@
"admins.updateVIP": "Update VIP",
"admins.vipAddedSuccessfully": "VIP Added Successfully",
"admins.playerNick": "Player Nick",
"admins.settings": "Settings"
"admins.settings": "Panel"
}
9 changes: 7 additions & 2 deletions resources/views/components/menu/vertical-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,18 @@
@endif
@if(PermissionsHelper::isSuperAdmin())
<li class="menu menu-heading">
<div class="heading"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-minus"><line x1="5" y1="12" x2="19" y2="12"></line></svg><span>{{ __('admins.settings') }}</span></div>
<div class="heading"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-minus"><line x1="5" y1="12" x2="19" y2="12"></line></svg><span>Settings</span></div>
</li>
<li class="menu {{ Request::is('*settings*') ? 'active' : '' }}">
<li class="menu {{ Request::is('*settings') ? 'active' : '' }}">
<a href="{{getAppSubDirectoryPath();}}/settings" aria-expanded="false" class="dropdown-toggle">
<div class=""><i class="fa fa-cog fa-fw me-3"></i><span>{{ __('admins.settings') }}</span></div>
</a>
</li>
<li class="menu {{ Request::is('*settings/servers*') ? 'active' : '' }}">
<a href="{{getAppSubDirectoryPath();}}/settings/servers" aria-expanded="false" class="dropdown-toggle">
<div class=""><i class="fa fa-cog fa-fw me-3"></i><span>Servers</span></div>
</a>
</li>
@endif
<li class="menu menu-heading">
<div class="heading"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-minus"><line x1="5" y1="12" x2="19" y2="12"></line></svg><span>{{ __('dashboard.steam') }}</span></div>
Expand Down
135 changes: 135 additions & 0 deletions resources/views/settings/servers.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<x-base-layout :scrollspy="false">
<x-slot:pageTitle>
{{ __('Server Visibility Settings') }}
</x-slot>
<x-slot:headerFiles>
<style>
.custom-switch {
display: inline-block;
width: 40px;
height: 20px;
position: relative;
}
.custom-switch input {
display: none;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 20px;
}
.slider:before {
position: absolute;
content: "";
height: 14px;
width: 14px;
left: 3px;
bottom: 3px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #28a745;
}
input:checked + .slider:before {
transform: translateX(20px);
}
</style>
</x-slot:headerFiles>
<div class="container mt-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>{{ __('Server Visibility Settings') }}</h1>
<button id="syncServersButton" class="btn btn-secondary">Sync New Servers</button>
</div>
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('settings.servers.update') }}" method="POST">
@csrf
<div class="card mb-4">
<div class="card-header">
<h2>{{ __('Servers') }}</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">{{ __('Hostname') }}</th>
<th scope="col">{{ __('Address') }}</th>
<th scope="col">{{ __('Visibility') }}</th>
</tr>
</thead>
<tbody>
@foreach($servers as $server)
<tr>
<td>{{ $server->hostname }}</td>
<td>{{ $server->address }}</td>
<td>
<input type="hidden" name="servers[{{ $server->id }}]" value="0"> <!-- Hidden input for unchecked boxes -->
<label class="custom-switch">
<input type="checkbox" id="server_{{ $server->id }}" name="servers[{{ $server->id }}]" value="1" {{ isset($serverVisibilitySettings[$server->id]) && $serverVisibilitySettings[$server->id] ? 'checked' : '' }}>
<span class="slider"></span>
</label>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<div class="card-footer text-center">
<button type="submit" class="btn btn-primary">Update Settings</button>
</div>
</div>
</form>
</div>
<x-slot:footerFiles>
<script>
document.getElementById('syncServersButton').addEventListener('click', function() {
fetch('{{ route('settings.servers.sync') }}', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).then(response => response.json()).then(data => {
if (data.success) {
Snackbar.show({
text: 'New Servers Synced for visibility settings.',
actionTextColor: '#fff',
backgroundColor: '#00ab55',
pos: 'top-center'
});
} else {
Snackbar.show({
text: 'No New Servers Found to sync.',
actionTextColor: '#fff',
backgroundColor: '#ff0000',
pos: 'top-center'
});
}
}).catch(error => {
alert('An error occurred while syncing new servers.');
});
});
</script>
</x-slot:footerFiles>
</x-base-layout>
2 changes: 1 addition & 1 deletion resources/views/weapons/partials/gloves-types.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@foreach($gloves as $glove)
<div class="col-md-3 mb-4">
<a class="card style-6 glove-card" href="javascript:void(0);" data-skin-image="{{ $glove['image'] }}" data-skin-name="{{ $glove['paint_name'] }}">
<span id="glove_{{ $glove['paint'] }}" class="glove_active agent_active badge badge-danger">{{ $glove['is_applied'] ? 'Active' : '' }}</span>
<span id="glove_{{ $glove['paint'] }}" class="glove_active badge badge-danger">{{ $glove['is_applied'] ? 'Active' : '' }}</span>
<div class="loader-skins"></div> <!-- Add loader -->
<img src="{{ $glove['image'] }}" class="card-img-top lazy" alt="{{ $glove['paint_name'] }}" crossorigin="anonymous">
<div class="card-footer">
Expand Down
10 changes: 8 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,11 @@

use App\Http\Controllers\SettingsController;

Route::get('/settings', [SettingsController::class, 'showSettings'])->name('settings.show')->middleware('superadmin');
Route::post('/settings', [SettingsController::class, 'updateSettings'])->name('settings.update')->middleware('superadmin');
Route::middleware(['superadmin'])->group(function () {
Route::get('/settings/servers', [ServerController::class, 'showServerSettings'])->name('settings.servers');
Route::post('/settings/servers/update', [ServerController::class, 'updateServerSettings'])->name('settings.servers.update');
Route::post('/settings/servers/sync', [ServerController::class, 'syncNewServers'])->name('settings.servers.sync');
Route::get('/settings', [SettingsController::class, 'showSettings'])->name('settings.show');
Route::post('/settings', [SettingsController::class, 'updateSettings'])->name('settings.update');
});

0 comments on commit 0698faa

Please sign in to comment.