diff --git a/app/Helpers/HelperFunctions.php b/app/Helpers/HelperFunctions.php index 7647af7..249d894 100644 --- a/app/Helpers/HelperFunctions.php +++ b/app/Helpers/HelperFunctions.php @@ -2,6 +2,7 @@ use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\ImageManager; +use App\Models\ChampionImage; function getRoleIcon($roleName): string { @@ -64,3 +65,21 @@ function getRoleIconSvg($roleName): string return $roleIcons[$roleName]; } + +/** + * Get the champion image + * + * @param string $full_id + * @param enum $type splash, uncentered_splash, loading, tile, icon, ability, video + * @return string + */ +function getChampionImage($full_id, $type): string +{ + $championImage = ChampionImage::where('full_id', $full_id)->where('type', $type)->first(); + + if (!$championImage) { + return ''; + } + + return $championImage->url; +} diff --git a/app/Models/Champion.php b/app/Models/Champion.php index b2165be..658a484 100644 --- a/app/Models/Champion.php +++ b/app/Models/Champion.php @@ -101,19 +101,12 @@ class Champion extends Model public function getChampionImageAttribute(bool $uncentered = false): string { - $baseUrl = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/assets/characters/'; - $championName = strtolower(str_replace([' ', "'", '.'], ['', '', ''], $this->name)); - $imagePath = 'base/images/'; - $imageUrl = $baseUrl.$championName.'/skins/'.$imagePath.$championName.'_splash_'; - $imageUrl .= $uncentered ? 'uncentered' : 'centered'; - $imageUrl .= '_0.jpg'; - - return $imageUrl; + return getChampionImage($this->champion_id . '000', $uncentered ? 'uncentered_splash' : 'splash'); } public function getChampionImageTileAttribute(): string { - return 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-tiles/'.$this->champion_id.'/'.$this->champion_id.'000.jpg'; + return getChampionImage($this->champion_id . '000', 'tile'); } public function getChampionSquareImageAttribute(): string @@ -121,6 +114,7 @@ class Champion extends Model return 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/'.$this->champion_id.'.png'; } + // TODO: Implement Ability Icons in the DB and get them from there. public function getChampionAbilityIconQAttribute(): string { return 'https://cdn.communitydragon.org/latest/champion/'.$this->champion_id.'/ability-icon/q'; diff --git a/app/Models/ChampionImage.php b/app/Models/ChampionImage.php new file mode 100644 index 0000000..e26818a --- /dev/null +++ b/app/Models/ChampionImage.php @@ -0,0 +1,23 @@ +belongsTo(Champion::class); + } +} diff --git a/app/Models/ChampionSkin.php b/app/Models/ChampionSkin.php index 07789ec..2441fe1 100644 --- a/app/Models/ChampionSkin.php +++ b/app/Models/ChampionSkin.php @@ -74,11 +74,7 @@ class ChampionSkin extends Model public function getSkinImageAttribute(bool $uncentered = false): string { - $championName = strtolower(str_replace([' ', "'"], ['', ''], $this->champion->name)); - $imagePath = $uncentered ? 'uncentered_' : 'centered_'; - $imageUrl = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/assets/characters/'.$championName.'/skins/skin'.str_pad($this->skin_id, 2, '0', STR_PAD_LEFT).'/images/'.$championName.'_splash_'.$imagePath.$this->skin_id.'.jpg'; - - return $imageUrl; + return getChampionImage($this->full_skin_id, $uncentered ? 'uncentered_splash' : 'splash'); } public function getSkinImageLoadingAttribute(): string diff --git a/database/migrations/2024_05_01_234838_create_champion_images_table.php b/database/migrations/2024_05_01_234838_create_champion_images_table.php new file mode 100644 index 0000000..59835ca --- /dev/null +++ b/database/migrations/2024_05_01_234838_create_champion_images_table.php @@ -0,0 +1,33 @@ +id(); + $table->text('full_id'); + $table->integer('champion_id'); + $table->enum('type', ['splash', 'uncentered_splash', 'loading', 'tile', 'icon', 'ability', 'video']); + $table->text('url'); + $table->foreign('champion_id')->references('champion_id')->on('champions')->onDelete('cascade'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('champion_images'); + } +}; diff --git a/database/seeders/ChampionImageSeeder.php b/database/seeders/ChampionImageSeeder.php new file mode 100644 index 0000000..007185c --- /dev/null +++ b/database/seeders/ChampionImageSeeder.php @@ -0,0 +1,56 @@ +each(function ($champion) { + $dataJson = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champions/' . $champion->champion_id . '.json'; + + $data = json_decode(file_get_contents($dataJson), true); + + $championSkins = $data['skins']; + + foreach ($championSkins as $skin) { + $splash = new ChampionImage(); + $splash->full_id = $skin['id']; + $splash->champion_id = $champion->champion_id; + $splash->type = 'splash'; + $splash->url = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/' . strtolower(substr($skin['splashPath'], strpos($skin['splashPath'], 'ASSETS'))); + $splash->save(); + + $uncenteredSplash = new ChampionImage(); + $uncenteredSplash->full_id = $skin['id']; + $uncenteredSplash->champion_id = $champion->champion_id; + $uncenteredSplash->type = 'uncentered_splash'; + $uncenteredSplash->url = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/' . strtolower(substr($skin['uncenteredSplashPath'], strpos($skin['uncenteredSplashPath'], 'ASSETS'))); + $uncenteredSplash->save(); + + $loading = new ChampionImage(); + $loading->full_id = $skin['id']; + $loading->champion_id = $champion->champion_id; + $loading->type = 'loading'; + $loading->url = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/' . strtolower(substr($skin['loadScreenPath'], strpos($skin['loadScreenPath'], 'ASSETS'))); + $loading->save(); + + $tile = new ChampionImage(); + $tile->full_id = $skin['id']; + $tile->champion_id = $champion->champion_id; + $tile->type = 'tile'; + $tile->url = 'https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/' . strtolower(substr($skin['tilePath'], strpos($skin['tilePath'], 'ASSETS'))); + $tile->save(); + } + }); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index c6ee975..f7db022 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -6,6 +6,7 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Artisan; class DatabaseSeeder extends Seeder { @@ -20,8 +21,10 @@ class DatabaseSeeder extends Seeder $this->call(ChampionRolesSeeder::class); $this->call(SummonerIconSeeder::class); $this->call(SummonerEmoteSeeder::class); + $this->call(ChampionImageSeeder::class); Cache::flush(); + Artisan::call('cloudflare:purge'); Log::info('Seeding complete at '.date('Y-m-d H:i:s')); } diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 4fdfb32..e5fac87 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -55,10 +55,6 @@
-We're aware of an issue where certain images may not display properly. We're currently working on a solution.
-