Skip to content

Commit

Permalink
Ranks - Playtime, weapon and map stats and few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 11, 2024
1 parent 2f87e6c commit 7de05c9
Show file tree
Hide file tree
Showing 112 changed files with 5,664 additions and 13 deletions.
7 changes: 5 additions & 2 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class DashboardController extends Controller
{
Expand Down Expand Up @@ -126,6 +128,7 @@ public function getTop5Players()
$player->deaths = $player->k4stats->deaths;
$player->game_win = $player->k4stats->game_win;
$player->game_lose = $player->k4stats->game_lose;
$player->profile = '';
}

$totalPlayers = Ranks::count();
Expand All @@ -134,7 +137,7 @@ public function getTop5Players()
$topPlayers = ZenithPlayerStorage::orderByRaw('JSON_EXTRACT(`K4-Zenith-Ranks.storage`, "$.Points") DESC')
->take(5)
->get();

$serverId = Crypt::encrypt(Session::get('Ranks_server'));
foreach ($topPlayers as $player) {
$playerRank = $player['K4-Zenith-Ranks.storage'];
$playerStats = $player['K4-Zenith-Stats.storage'];
Expand All @@ -144,7 +147,7 @@ public function getTop5Players()
$player->avatar = !empty($response['response']['players'][0]['avatar']) ? $response['response']['players'][0]['avatar'] : 'https://mdbootstrap.com/img/Photos/Avatars/img(32).jpg';
$player->ratingImage = CommonHelper::getCSRatingImage($playerRank['Points']);
$player->rank = CommonHelper::getCSRankImage($playerRank['Rank'] ?? 'N/A');

$player->profile = env('VITE_SITE_DIR')."/ranks/profile/$player->player_steamid/$serverId";
// Assigning additional stats
$player->kills = $playerStats['Kills'] ?? 0;
$player->deaths = $playerStats['Deaths'] ?? 0;
Expand Down
90 changes: 90 additions & 0 deletions app/Http/Controllers/K4Ranks/RanksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
use App\Helpers\ModuleHelper;
use App\Http\Controllers\Controller;
use App\Models\K4Ranks\Ranks;
use App\Models\K4Ranks\ZenithMapStats;
use App\Models\K4Ranks\ZenithPlayerStorage;
use App\Models\K4Ranks\ZenithWeaponStats;
use App\Models\ModuleServerSetting;
use Carbon\Carbon;
use Carbon\CarbonInterval;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;

class RanksController extends Controller
Expand Down Expand Up @@ -98,7 +102,9 @@ public function getPlayersList(Request $request)
$playerRank = $player['K4-Zenith-Ranks.storage'];
$player->player_steamid = $player->steam_id;
$response = CommonHelper::steamProfile($player);
$serverId = Crypt::encrypt(Session::get('Ranks_server'));
$formattedData[] = [
"profile" => env('VITE_SITE_DIR')."/ranks/profile/$player->player_steamid/$serverId",
"position" => $player->position,
"name" => !empty($response['response']['players'][0]['personaname']) ? $response['response']['players'][0]['personaname'] : 'Profile',
"player_steamid" => $player->steam_id,
Expand Down Expand Up @@ -127,4 +133,88 @@ public function getPlayersList(Request $request)
]);

}

public function viewProfile(Request $request, $steam_id, $server_id) {
ModuleHelper::useConnection('Ranks',$server_id);
$player = ZenithPlayerStorage::where('steam_id', $steam_id)->firstOrFail();
$playerTimeStats = $player['K4-Zenith-TimeStats.storage'];
$playerStats = $player['K4-Zenith-Stats.storage'];
$playerRank = $player['K4-Zenith-Ranks.storage'];

// Fetch the total playtime, Terrorist playtime, and CT playtime
$totalPlaytime = $playerTimeStats['TotalPlaytime'] ?? 0;
$terroristPlaytime = $playerTimeStats['TerroristPlaytime'] ?? 0;
$ctPlaytime = $playerTimeStats['CounterTerroristPlaytime'] ?? 0;

// Format to 1 decimal point and ensure it's in hours
$formattedPlaytime = number_format(CarbonInterval::minutes($totalPlaytime)->totalHours, 2);
$formattedTPlaytime = number_format(CarbonInterval::minutes($terroristPlaytime)->totalHours, 2);
$formattedCTPlaytime = number_format(CarbonInterval::minutes($ctPlaytime)->totalHours, 2);

// Extract round statistics
$roundWin = $playerStats['RoundWin'] ?? 0;
$roundLose = $playerStats['RoundLose'] ?? 0;
$roundsOverall = $playerStats['RoundsOverall'] ?? 0;
$mvp = $playerStats['MVP'] ?? 0;
$headshots = $playerStats['Headshots'] ?? 0;
$totalKills = $playerStats['Kills'] ?? 0;
$gamesWon = $playerStats['GameWin'] ?? 0;

// Fetch top 5 maps based on the sum of kills
DB::statement("SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
$topMaps = ZenithMapStats::where('steam_id', $steam_id)
->select('map_name', 'game_win', 'game_lose')
->where('steam_id', $steam_id)
->groupBy('map_name')
->orderBy('game_win', 'desc')
->limit(8)
->get();
// Calculate win rate for each map
$topMaps = $topMaps->transform(function ($map) {
$totalRounds = $map->game_win + $map->game_lose;
$map->win_rate = $totalRounds > 0 ? ($map->game_win / $totalRounds) * 100 : 0;
return $map;
})->sortByDesc('win_rate');

$weaponStats = ZenithWeaponStats::where('steam_id', $steam_id)
->select('weapon', 'kills', 'headshots', 'hits', 'chest_hits', 'stomach_hits')
->orderBy('kills', 'desc')
->limit(5)
->get()
->transform(function ($weapon) {
// Define the image path based on weapon name
$imagePath = 'images/weapons/weapon_' . strtolower($weapon->weapon) . '.png';

// Check if the image exists, if not, use a default image
$weapon->image_url = File::exists(public_path($imagePath))
? asset($imagePath)
: asset('images/weapons/weapon_elite.png');

return $weapon;
});

// Fetch Steam profile info (avatar and name) from CommonHelper
$player->player_steamid = $player->steam_id;
$response = CommonHelper::steamProfile($player);

// Extract avatar and name from the response
$avatar = !empty($response['response']['players'][0]['avatar'])
? $response['response']['players'][0]['avatar']
: 'https://mdbootstrap.com/img/Photos/Avatars/img(32).jpg';
$name = !empty($response['response']['players'][0]['personaname'])
? $response['response']['players'][0]['personaname']
: 'Profile';

// Fetch the rating image based on points
$points = $playerRank['Points'] ?? 0;

$rank = $playerRank['Rank'] ?? 'N/A';
$rankImage = CommonHelper::getCSRankImage($rank);
$ratingImage = CommonHelper::getCSRatingImage($points);
$firstBlood = $playerStats['FirstBlood'];
$bombPlanted = $playerStats['BombPlanted'];
$seen = Carbon::parse($player->last_online ?? now())->diffForHumans();
return view('k4Ranks.profile', compact('firstBlood','bombPlanted','seen','avatar','name','rankImage','ratingImage','mvp','headshots','totalKills','gamesWon','player', 'formattedPlaytime', 'formattedTPlaytime', 'formattedCTPlaytime', 'roundWin', 'roundLose', 'roundsOverall', 'topMaps', 'weaponStats'));

}
}
27 changes: 27 additions & 0 deletions app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Http\Controllers;

use App\Helpers\CommonHelper;
use App\Helpers\ModuleHelper;
use App\Helpers\PermissionsHelper;
use App\Models\K4Ranks\ZenithPlayerStorage;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -43,6 +46,30 @@ public function handleSteamCallback()
$user->save();
}
Auth::login($user);
try {
if(env('RANKS') == 'Enabled') {
ModuleHelper::useConnection('Ranks');
$player = ZenithPlayerStorage::where('steam_id', $steamUser->getId())->first();
if ($player) {
$playerRank = $player['K4-Zenith-Ranks.storage'];
$points = $playerRank['Points'] ?? 0;
$rank = $playerRank['Rank'] ?? 'N/A';

// Fetch rank and rating images using the same logic
$rankImage = CommonHelper::getCSRankImage($rank);
$ratingImage = CommonHelper::getCSRatingImage($points);

// Store the rank and rank image in the session
session([
'rank' => $rank,
'rank_image' => $rankImage,
'rating_image' => $ratingImage,
]);
}
}
} catch(\Exception $e) {
Log::error('auth.rank.cache'. $e->getMessage());
}
return redirect()->route('home')->with('success', __('admins.steamAuthSuccess'));;
} else {
return redirect()->route('home')->with('error', __('admins.steamAuthError'));
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,19 @@ public function serverPlayerAction(Request $request) {
$requestType = $request->input('action');
$playerName = $request->input('name');
$serverId = $request->input('serverId');
$duration = '1440'; // 1 day
$reason = $request->input('reason');
switch ($requestType){
case "ban":
if(PermissionsHelper::hasUnBanPermission())
return $this->executeCommand('css_ban "' . $playerName . '" 1440', $serverId);
return $this->executeCommand('css_ban "' . $playerName . '" 1440 "' . $reason . '"', $serverId);
break;
case "kick":
if(PermissionsHelper::hasKickPermission())
return $this->executeCommand('css_kick "' . $playerName . '"', $serverId);
return $this->executeCommand('css_kick "' . $playerName . '" "' . $reason . '"', $serverId);
break;
case "mute":
if(PermissionsHelper::hasMutePermission())
return $this->executeCommand('css_mute "' . $playerName . '" 1440', $serverId);
return $this->executeCommand('css_mute "' . $playerName . '" 1440 "' . $reason . '"', $serverId);
break;
default: abort(403);
}
Expand Down
18 changes: 18 additions & 0 deletions app/Models/K4Ranks/ZenithMapStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models\K4Ranks;

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

class ZenithMapStats extends Model
{
use HasFactory;

public $table = 'zenith_map_stats';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setConnection('mysqlranks');
}
}
6 changes: 6 additions & 0 deletions app/Models/K4Ranks/ZenithPlayerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class ZenithPlayerStorage extends Model
];

public $timestamps = false;

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setConnection('mysqlranks');
}
}
17 changes: 17 additions & 0 deletions app/Models/K4Ranks/ZenithWeaponStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models\K4Ranks;

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

class ZenithWeaponStats extends Model
{
use HasFactory;

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setConnection('mysqlranks');
}
}
Binary file added public/images/maps/de_ancient.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_anubis.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_dust2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_inferno.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_mirage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_nuke.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/de_vertigo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/maps/default.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_ak47.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_aug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_awp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_bayonet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_bizon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_cz75a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_deagle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_elite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_famas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_fiveseven.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_g3sg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_galilar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_glock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_hkp2000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_knife_butterfly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_knife_canis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_knife_cord.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/weapons/weapon_knife_css.png
Binary file added public/images/weapons/weapon_knife_falchion.png
Binary file added public/images/weapons/weapon_knife_flip.png
Binary file added public/images/weapons/weapon_knife_gut.png
Binary file added public/images/weapons/weapon_knife_karambit.png
Binary file added public/images/weapons/weapon_knife_kukri.png
Binary file added public/images/weapons/weapon_knife_m9_bayonet.png
Binary file added public/images/weapons/weapon_knife_outdoor.png
Binary file added public/images/weapons/weapon_knife_push.png
Binary file added public/images/weapons/weapon_knife_skeleton.png
Binary file added public/images/weapons/weapon_knife_stiletto.png
Binary file added public/images/weapons/weapon_knife_tactical.png
Binary file added public/images/weapons/weapon_knife_ursus.png
Binary file added public/images/weapons/weapon_knife_widowmaker.png
Binary file added public/images/weapons/weapon_m249.png
Binary file added public/images/weapons/weapon_m4a1.png
Binary file added public/images/weapons/weapon_m4a1_silencer.png
Binary file added public/images/weapons/weapon_mac10.png
Binary file added public/images/weapons/weapon_mag7.png
Binary file added public/images/weapons/weapon_mp5sd.png
Binary file added public/images/weapons/weapon_mp7.png
Binary file added public/images/weapons/weapon_mp9.png
Binary file added public/images/weapons/weapon_negev.png
Binary file added public/images/weapons/weapon_nova.png
Binary file added public/images/weapons/weapon_p250.png
Binary file added public/images/weapons/weapon_p90.png
Binary file added public/images/weapons/weapon_revolver.png
Binary file added public/images/weapons/weapon_sawedoff.png
Binary file added public/images/weapons/weapon_scar20.png
Binary file added public/images/weapons/weapon_sg556.png
Binary file added public/images/weapons/weapon_ssg08.png
Binary file added public/images/weapons/weapon_taser.png
Binary file added public/images/weapons/weapon_tec9.png
Binary file added public/images/weapons/weapon_ump45.png
Binary file added public/images/weapons/weapon_usp_silencer.png
Binary file added public/images/weapons/weapon_xm1014.png
Loading

0 comments on commit 7de05c9

Please sign in to comment.