Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top Ten - Season Support #788

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 21 additions & 1 deletion app/Console/Commands/RefreshAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Console\Commands;

use App\Jobs\ProcessAnalytic;
use App\Models\Season;
use App\Support\Analytics\AnalyticInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
Expand All @@ -20,21 +21,40 @@ class RefreshAnalytics extends Command
public function handle(): int
{
$availableAnalytics = Storage::disk('stats')->files();
$seasons = Season::all();

// ALL
foreach ($availableAnalytics as $availableAnalytic) {
$analytic = 'App\Support\Analytics\Stats\\'.Str::before($availableAnalytic, '.php');

/** @var AnalyticInterface $analyticClass */
$analyticClass = new $analytic();

$startTime = time();
$this->output->writeln('Processing: '.$analyticClass->title());
$this->output->writeln('Processing (All Seasons): '.$analyticClass->title());

ProcessAnalytic::dispatchSync($analyticClass);

$this->output->writeln('Processed in '.(time() - $startTime).' seconds.');
}

// Seasons
$seasons->each(function (Season $season) use ($availableAnalytics) {
foreach ($availableAnalytics as $availableAnalytic) {
$analytic = 'App\Support\Analytics\Stats\\'.Str::before($availableAnalytic, '.php');

/** @var AnalyticInterface $analyticClass */
$analyticClass = new $analytic($season);

$startTime = time();
$this->output->writeln('Processing (' . $season->name . '): '.$analyticClass->title());

ProcessAnalytic::dispatchSync($analyticClass);

$this->output->writeln('Processed in '.(time() - $startTime).' seconds.');
}
});

return CommandAlias::SUCCESS;
}
}
6 changes: 5 additions & 1 deletion app/Jobs/ProcessAnalytic.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public function handle(): void
{
DB::transaction(function () {
Analytic::purgeKey($this->analytic->key());
Analytic::purgeKey($this->analytic->key(), $this->analytic->season);

$results = $this->analytic->results(1000);

Expand Down Expand Up @@ -76,6 +76,7 @@ private function handleGameResults(?Collection $games): void
$analytic->key = $this->analytic->key();
$analytic->value = (float) $game->{$this->analytic->property()};
$analytic->game()->associate($game);
$analytic->season()->associate($this->analytic->season);
$analytic->saveOrFail();
});
}
Expand All @@ -89,6 +90,7 @@ private function handleGamePlayerResults(?Collection $gamePlayers): void
$analytic->value = (float) $gamePlayer->{$this->analytic->property()};
$analytic->game()->associate($gamePlayer->game);
$analytic->player()->associate($gamePlayer->player);
$analytic->season()->associate($this->analytic->season);
$analytic->saveOrFail();
});
}
Expand All @@ -101,6 +103,7 @@ private function handleServiceRecordResults(?Collection $serviceRecords): void
$analytic->key = $this->analytic->key();
$analytic->value = (float) $serviceRecord->{$this->analytic->property()};
$analytic->player()->associate($serviceRecord->player);
$analytic->season()->associate($this->analytic->season);
$analytic->saveOrFail();
});
}
Expand All @@ -113,6 +116,7 @@ private function handleMapResults(?Collection $maps): void
$analytic->key = $this->analytic->key();
$analytic->value = (float) $map->{$this->analytic->property()};
$analytic->map()->associate($map);
$analytic->season()->associate($this->analytic->season);
$analytic->saveOrFail();
});
}
Expand Down
22 changes: 18 additions & 4 deletions app/Models/Analytic.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
* @property string $key
* @property ?int $game_id
* @property ?int $player_id
* @property ?int $map_id
* @property ?int $season_id
* @property float $value
* @property Carbon $created_at
* @property Carbon $updated_at
* @property-read ?Game $game
* @property-read ?Player $player
* @property-read ?Map $map
* @property-read ?Season $season
* @property-read AnalyticInterface $stat
*
* @method static AnalyticFactory factory(...$parameters)
Expand Down Expand Up @@ -80,11 +83,17 @@ public static function getStatFromEnum(?AnalyticKey $key): AnalyticInterface
};
}

public static function purgeKey(string $key): void
public static function purgeKey(string $key, ?Season $season = null): void
{
self::query()
->where('key', $key)
->delete();
$query = self::query()->where('key', $key);

if ($season) {
$query->where('season_id', $season->id);
} else {
$query->whereNull('season_id');
}

$query->delete();
}

public function game(): BelongsTo
Expand All @@ -101,4 +110,9 @@ public function map(): BelongsTo
{
return $this->belongsTo(Map::class);
}

public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
}
3 changes: 3 additions & 0 deletions app/Support/Analytics/AnalyticInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use App\Enums\AnalyticType;
use App\Models\Analytic;
use App\Models\Season;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

interface AnalyticInterface
{
public function __construct(?Season $season = null);

public function type(): AnalyticType;

public function key(): string;
Expand Down
6 changes: 6 additions & 0 deletions app/Support/Analytics/BaseGameStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

use App\Enums\AnalyticType;
use App\Models\GamePlayer;
use App\Models\Season;
use Illuminate\Database\Eloquent\Builder;

class BaseGameStat
{
public function __construct(public readonly ?Season $season = null)
{
//
}

public function type(): AnalyticType
{
return AnalyticType::GAME();
Expand Down
6 changes: 6 additions & 0 deletions app/Support/Analytics/BaseMapStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

use App\Enums\AnalyticType;
use App\Models\Map;
use App\Models\Season;
use Illuminate\Database\Eloquent\Builder;

class BaseMapStat
{
public function __construct(public readonly ?Season $season = null)
{
//
}

public function type(): AnalyticType
{
return AnalyticType::MAP();
Expand Down
6 changes: 6 additions & 0 deletions app/Support/Analytics/BaseOnlyGameStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

use App\Enums\AnalyticType;
use App\Models\Game;
use App\Models\Season;
use Illuminate\Database\Eloquent\Builder;

class BaseOnlyGameStat
{
public function __construct(public readonly ?Season $season = null)
{
//
}

public function type(): AnalyticType
{
return AnalyticType::ONLY_GAME();
Expand Down
6 changes: 6 additions & 0 deletions app/Support/Analytics/BasePlayerStat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
namespace App\Support\Analytics;

use App\Enums\AnalyticType;
use App\Models\Season;
use App\Models\ServiceRecord;
use Illuminate\Database\Eloquent\Builder;

class BasePlayerStat
{
public function __construct(public readonly ?Season $season = null)
{
//
}

public function type(): AnalyticType
{
return AnalyticType::PLAYER();
Expand Down
16 changes: 12 additions & 4 deletions app/Support/Analytics/Stats/BestAccuracyServiceRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,24 @@ public function displayProperty(Analytic $analytic): string

public function results(int $limit = 10): ?Collection
{
return $this->builder()
$seasonKey = $this->season?->key;

$query = $this->builder()
->select('service_records.*')
->with(['player'])
->leftJoin('players', 'players.id', '=', 'service_records.player_id')
->where('is_cheater', false)
->where('mode', Mode::MATCHMADE_PVP)
->whereNull('season_number')
->where('total_matches', '>=', 1000)
->orderByDesc($this->property())
->limit($limit)
->get();
->limit($limit);

if ($seasonKey) {
$query->where('season_key', $seasonKey);
} else {
$query->whereNull('season_key');
}

return $query->get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use App\Models\Season;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('analytics', function (Blueprint $table) {
$table->foreignIdFor(Season::class)->after('map_id')->nullable()->constrained();
});
}

public function down(): void
{
Schema::table('analytics', function (Blueprint $table) {
$table->dropForeignIdFor(Season::class);
});
}
};