Skip to content

Commit

Permalink
Add globals weakness and resistances to teams
Browse files Browse the repository at this point in the history
  • Loading branch information
noa-devv committed Dec 5, 2024
1 parent 092f917 commit 88f3fa4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
8 changes: 7 additions & 1 deletion app/Http/Controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\TeamRequest;
use App\Models\Pokemon;
use App\Models\Team;
use App\Models\Type;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -73,7 +74,12 @@ public function update(TeamRequest $request, $id): \Illuminate\Http\RedirectResp
public function show($id): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application
{
$team = Team::findOrFail($id);
return view('teams.show', compact('team'));
$types = Type::all();
$typeColors = $types->pluck('color', 'name');

$groupedResistances = $team->categorizedResistances();

return view('teams.show', compact('team', 'groupedResistances', 'types', 'typeColors'));
}

public function destroy($id): \Illuminate\Http\RedirectResponse
Expand Down
34 changes: 34 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,39 @@ public function isPokemonInTheTeam(Pokemon $pokemon): bool
{
return $this->pokemons->contains($pokemon->id);
}

public function categorizedResistances()
{
$globalRatios = [];
$pokemonCount = $this->pokemons()->count();

if ($pokemonCount == 0) {
return ['Globaly Weak' => [], 'Globaly Resistant' => []];
}

foreach ($this->pokemons as $pokemon) {
$pokemonResistances = $pokemon->resistances();

foreach ($pokemonResistances as $type => $ratio) {
if (!isset($globalRatios[$type])) {
$globalRatios[$type] = 0;
}
$globalRatios[$type] += $ratio;
}
}

$categorized = ['Globaly Weak' => [], 'Globaly Resistant' => []];

foreach ($globalRatios as $type => $totalRatio) {
$averageRatio = $totalRatio / $pokemonCount;

if ($averageRatio > 1) {
$categorized['Globaly Weak'][$type] = $averageRatio;
} else {
$categorized['Globaly Resistant'][$type] = $averageRatio;
}
}
return $categorized;
}
}

5 changes: 0 additions & 5 deletions resources/views/layout/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@

<footer class="bg-dark text-white text-center text-lg-start mt-5 py-4">
<div class="container">
<div class="row">
<div class="col-md-6 mb-3 mb-md-0">
<h5>PokeTeams</h5>
</div>
</div>
<div class="text-center py-3">
&copy; {{ date('Y') }} PokeTeams. All rights reserved.
</div>
Expand Down
4 changes: 1 addition & 3 deletions resources/views/pokemons/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class="mx-auto"
@endswitch
</span>
@foreach ($types as $type)
<span class="card-type" style="background-color: {{ $typeColors[$type] ?? '#CCCCCC' }};">
{{ strtoupper($type) }}
</span>
<span class="card-type" style="background-color: {{ $typeColors[$type] ?? '#CCCCCC' }};">{{ strtoupper($type) }}</span>
&nbsp;
@endforeach
<br>
Expand Down
20 changes: 20 additions & 0 deletions resources/views/teams/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,25 @@
</div>
@endforeach
</div>
<br>
<br>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-4">
@foreach ($groupedResistances as $category => $types)
@if (!empty($types))
<div class="col card shadow-sm card-body">
<h5 class="card-title text-center">
{{ $category == 'Globaly Weak' ? 'Globaly Weak' : 'Globaly Resistant' }}
</h5>
<div class="d-flex flex-wrap justify-content-center">
@foreach ($types as $type => $resistance)
<span class="card-type p-2 mx-1 mb-2 text-white rounded" style="background-color: {{ $typeColors[$type] ?? '#CCCCCC' }};">
{{ strtoupper($type) }} {{ round($resistance, 2) }}
</span>
@endforeach
</div>
</div>
@endif
@endforeach
</div>
</div>
@endsection

0 comments on commit 88f3fa4

Please sign in to comment.