Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/SimpleClansStats.iml

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

2 changes: 1 addition & 1 deletion .idea/jsLibraryMappings.xml

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

90 changes: 20 additions & 70 deletions app/Clans.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ public function getTable()
return DB::table('sc_clans');
}

public function getClan(string $tag): ?array
{
$clan = $this->getTable()->where('tag', '=', $tag)->first();
if (!isset($clan)) {
return null;
}
$clan = ((array)$clan) + ["KDR" => 0.0, "leaders" => [], "members" => []];

foreach (self::$players->getMembers($tag) as $member) {
$clan["KDR"] += Utils::getKDR($member);
array_push($clan["members"], $member->name);
if ($member->leader) {
array_push($clan["leaders"], $member->name);
}
}
return $clan;
}

public function getClans(string $sortBy = "name"): Collection
{
return $this->getTable()->get()->sortBy($sortBy);
Expand Down Expand Up @@ -58,7 +76,7 @@ public function getTopClans(int $length = 10, string $sortBy = "KDR", bool $asc
continue;
}
$clan += (array) $clan_row;
$clan['color_tag'] = !array_key_exists('color_tag', $clan) ? '' : $this->addColors($clan['color_tag']);
$clan['color_tag'] = !array_key_exists('color_tag', $clan) ? '' : Utils::addColors($clan['color_tag']);
$clan['founded'] = !array_key_exists('founded', $clan) ? 0 : $clan['founded'];
$clan['last_used'] = !array_key_exists('last_used', $clan) ? 0 : $clan['last_used'];
if (isset($clan_row)) {
Expand Down Expand Up @@ -100,76 +118,8 @@ public function getHTMLColorTagByTag($tag = null): ?string
}
$row = $this->getTable()->where('tag', '=', $tag)->get('color_tag')->first();
if (isset($row)) {
return $this->addColors($row->color_tag);
return Utils::addColors($row->color_tag);
}
return null;
}

/**
* @param $string - without colors.
* @return string - with colors.
* @author lpostiglione
* @link https://github.com/lpostiglione/SimpleClansStats2/blob/master/includes/functions.inc.php
*/
private function addColors($string): string
{
$colors = array(
"g" => "<span class=\"c0-font\">",
"1" => "<span class=\"c1-font\">",
"2" => "<span class=\"c2-font\">",
"3" => "<span class=\"c3-font\">",
"4" => "<span class=\"c4-font\">",
"5" => "<span class=\"c5-font\">",
"6" => "<span class=\"c6-font\">",
"7" => "<span class=\"c7-font\">",
"8" => "<span class=\"c8-font\">",
"9" => "<span class=\"c9-font\">",
"a" => "<span class=\"ca-font\">",
"b" => "<span class=\"cb-font\">",
"c" => "<span class=\"cc-font\">",
"d" => "<span class=\"cd-font\">",
"e" => "<span class=\"ce-font\">",
"f" => "<span class=\"cf-font\">",
"l" => "<span class=\"cl-font\">",
"m" => "<span class=\"cm-font\">",
"n" => "<span class=\"cn-font\">",
"o" => "<span class=\"co-font\">",
"k" => "<span class=\"ck-font\">"
);

$string = str_replace("§0", "§g", $string);
$motdarr = explode("§", $string);
$spans = 0;
$colored = '<span class="colored">';

foreach ($motdarr as $row) {
if (!isset($isset)) {
$isset = "isset";
$colored .= $row;
} elseif (empty($row)) {
continue;
} elseif (strtolower(substr($row, 0, 1)) == "r") {
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
} else {
$color_code = strtolower(substr($row, 0, 1));
if (preg_match("/^[0-9a-g]$/i", $color_code)) {
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
}
$colored .= $colors[$color_code];
$colored .= substr($row, 1);
$spans++;
}
}
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
return $colored . "</span>";
}
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public function getClans(Request $request): Collection

return $this->clans->getTopClans($length, $sortBy, $order);
}

public function getClan(string $tag): array
{
return (array) $this->clans->getClan($tag);
}
}
30 changes: 27 additions & 3 deletions app/Http/Controllers/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@

class ViewController extends Controller
{
private $clans;
private $players;

public function __construct()
{
$this->clans = new Clans();
$this->players = new Players();
}

public function show($locale = 'en')
{
$view_name = Route::currentRouteName();
if (!isset($view_name)) {
$view_name = "index";
}
$clans = new Clans();
$players = new Players();
App::setLocale($locale);

return view($view_name, ['clans' => $clans, 'players' => $players, 'locale' => $locale]);
return view($view_name, ['clans' => $this->clans, 'players' => $this->players, 'locale' => $locale]);
}

//TODO locale
public function detail(string $id)
{
$detail_name = Route::currentRouteName();
$clan = null;
$player = null;
switch ($detail_name) {
case "clan":
$clan = $this->clans->getClan($id);
break;
case "player":
$player = $this->players->getPlayer($id);
}

return view('detail.' . $detail_name, ['clan' => $clan, 'player' => $player]);
}
}
30 changes: 28 additions & 2 deletions app/Players.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ public function getPlayers(): Collection
return $this->getTable()->get();
}

public function getPlayer(string $name): ?array
{
$player = $this->getPlayers()->where('name', '=', $name)->first();
if (!isset($player)) {
return null;
}
$player = (array) $player;
$last_deaths = DB::table("sc_kills")->where('victim', '=', $name)
->select('attacker', 'attacker_tag', 'kill_id')->get();
if (isset($last_deaths)) {
$last_deaths = $last_deaths->sortByDesc('kill_id')->splice(0, 5)->all();
} else {
$last_deaths = [];
}
$last_kills = DB::table("sc_kills")->where('attacker', '=', $name)
->select('victim', 'victim_tag', 'kill_id')->get();
if (isset($last_kills)) {
$last_kills = $last_kills->sortByDesc('kill_id')->splice(0, 5)->all();
} else {
$last_kills = [];
}
$player['last_kills'] = $last_kills;
$player['last_deaths'] = $last_deaths;
return (array)$player;
}

public function getMembers($clan_tag): Collection
{
return $this->getPlayers()->where('tag', '=', $clan_tag);
Expand Down Expand Up @@ -50,10 +76,10 @@ public function getLastPlayersKills(): Collection
$lastPlayers = collect([]);

DB::table('sc_kills')->select('attacker', 'attacker_tag', 'victim', 'victim_tag')->get()
->reverse()->splice(0, 10)->each(function ($player, $indx) use ($lastPlayers, $clans) {
->reverse()->splice(0, 10)->each(function ($player, $index) use ($lastPlayers, $clans) {
$player->attacker_colored_tag = $clans->getHTMLColorTagByTag($player->attacker_tag);
$player->victim_colored_tag = $clans->getHTMLColorTagByTag($player->victim_tag);
$lastPlayers->put($indx, $player);
$lastPlayers->put($index, $player);
});
return $lastPlayers;
}
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register()
*/
public function boot()
{
//
if (!$this->app->isLocal()) {
$this->app['request']->server->set('HTTPS', true);
}
}
}
120 changes: 116 additions & 4 deletions app/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,129 @@
class Utils
{

public static function getAllKills($player): int
{
$player = (array)$player;
return $player['neutral_kills'] + $player['rival_kills'] + $player['civilian_kills'];
}

public static function getKDR($player): float
{
$neutral = $player->neutral_kills * doubleval(Env::get('KILL_NEUTRAL'));
$rival = $player->rival_kills * doubleval(Env::get('KILL_RIVAL'));
$civilian = $player->civilian_kills * doubleval(Env::get('KILL_CIVILIAN'));
$player = (array)$player;
$neutral = $player['neutral_kills'] * doubleval(Env::get('KILL_NEUTRAL'));
$rival = $player['rival_kills'] * doubleval(Env::get('KILL_RIVAL'));
$civilian = $player['civilian_kills'] * doubleval(Env::get('KILL_CIVILIAN'));

$kills = ($civilian + $rival + $neutral);

$deaths = $player->deaths == 0 ? 1 : $player->deaths;
$deaths = $player['deaths'] == 0 ? 1 : $player['deaths'];

return $kills / $deaths;
}

public static function getWarringFromFlags($flags): string
{
$flags = json_decode($flags, true);
return implode(", ", $flags['warring']);
}

public static function getFormattedKDR($player): string
{
return number_format(self::getKDR($player), 2);
}

public static function unpackClans($clans): string
{
return implode(", ", explode("|", $clans));
}

public static function formatPastClans($packed_clans): string
{
$packed_clans = explode("|", $packed_clans);
foreach ($packed_clans as &$clan) {
$clan = self::addColors($clan);
}
return implode(", ", $packed_clans);
}

public static function formatDate($founded)
{
// TODO get format from env
return date("d/m/Y", $founded / 1000);
}

public static function formatDateTime($last_seen)
{
// TODO get format from env
return date("d/m/Y - H:i", $last_seen / 1000);
}

/**
* @param $string - without colors.
* @return string - with colors.
* @author lpostiglione
* @link https://github.com/lpostiglione/SimpleClansStats2/blob/master/includes/functions.inc.php
*/
public static function addColors($string): string
{
$colors = array(
"g" => "<span class=\"color-black\">",
"1" => "<span class=\"color-dark_blue\">",
"2" => "<span class=\"color-dark_green\">",
"3" => "<span class=\"color-dark_aqua\">",
"4" => "<span class=\"color-dark_red\">",
"5" => "<span class=\"color-dark_purple\">",
"6" => "<span class=\"color-gold\">",
"7" => "<span class=\"color-gray\">",
"8" => "<span class=\"color-dark_gray\">",
"9" => "<span class=\"color-blue\">",
"a" => "<span class=\"color-green\">",
"b" => "<span class=\"color-aqua\">",
"c" => "<span class=\"color-red\">",
"d" => "<span class=\"color-light_purple\">",
"e" => "<span class=\"color-yellow\">",
"f" => "<span class=\"color-white\">",
"l" => "<span class=\"bold\">",
"m" => "<span class=\"strikethrough\">",
"n" => "<span class=\"underline\">",
"o" => "<span class=\"italic\">",
"k" => "<span class=\"obfuscated\">"
);

$string = str_replace("§0", "§g", $string);
$motdarr = explode("§", $string);
$spans = 0;
$colored = '<span class="colored">';

foreach ($motdarr as $row) {
if (!isset($isset)) {
$isset = "isset";
$colored .= $row;
} elseif (empty($row)) {
continue;
} elseif (strtolower(substr($row, 0, 1)) == "r") {
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
} else {
$color_code = strtolower(substr($row, 0, 1));
if (preg_match("/^[0-9a-g]$/i", $color_code)) {
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
}
$colored .= $colors[$color_code];
$colored .= substr($row, 1);
$spans++;
}
}
while ($spans > 0) {
$colored .= "</span>";
$spans--;
}
return $colored . "</span>";
}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"php": "^7.2.5|^8.0",
"fideloper/proxy": "^4.4",
"laravel/framework": "^6.20",
"laravel/tinker": "^2.5"
"laravel/tinker": "^2.5",
"ext-json": "*"
},
"require-dev": {
"facade/ignition": "^1.16.4",
Expand Down
Loading