From 95dfa23b2e69a164907e0090398bd238bceec0a6 Mon Sep 17 00:00:00 2001 From: openlegends Date: Thu, 14 Mar 2024 22:17:35 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + composer.json | 17 +++++ src/Config.php | 142 +++++++++++++++++++++++++++++++++++++ src/Test/Cards/name-1.json | 14 ++++ src/Test/Cards/name-2.json | 15 ++++ src/Test/Cards/name-3.json | 14 ++++ 6 files changed, 203 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Config.php create mode 100644 src/Test/Cards/name-1.json create mode 100644 src/Test/Cards/name-2.json create mode 100644 src/Test/Cards/name-3.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4d7f275 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "openlegends/asset", + "description": "Game Assets for OpenLegends", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "OpenLegends\\Asset\\": "src/" + } + }, + "authors": [ + { + "name": "OpenLegends" + } + ], + "require": {} +} diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..f897635 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,142 @@ +_asset = $asset; + } + + else + { + throw new Exception(); + } + } + + public function getCards( + ?string $folder = 'Cards', + ?array $type = null, + ?array $attributes = null, + ?array $title = null, + ?array $description = null, + ?array $cost = null, + ?array $attack = null, + ?array $health = null, + ?array $craft = null, + ?array $extract = null + ): array + { + $cards = []; + + foreach ( + $this->_getConfigs( + __DIR__ . DIRECTORY_SEPARATOR . $this->_asset . DIRECTORY_SEPARATOR . $folder + ) as $config) + { + if ($type && !in_array($config->type, $type)) + { + continue; + } + + if ($attributes && !in_array($config->attributes, $attributes)) + { + continue; + } + + if ($title && !in_array($config->title, $title)) + { + continue; + } + + if ($description && !in_array($config->description, $description)) + { + continue; + } + + if ($cost && !in_array($config->cost, $cost)) + { + continue; + } + + if ($attack && !in_array($config->attack, $attack)) + { + continue; + } + + if ($health && !in_array($config->health, $health)) + { + continue; + } + + if ($craft && !in_array($config->craft, $craft)) + { + continue; + } + + if ($extract && !in_array($config->extract, $extract)) + { + continue; + } + + $cards[] = json_decode( + $config + ); + } + + return $cards; + } + + private function _getConfigs( + string $location + ): array + { + $configs = []; + + if (!is_dir($location) || !is_readable($location)) + { + throw new Exception(); + } + + foreach (scandir($location) as $filename) + { + if (!str_ends_with($filename, '.json')) + { + continue; + } + + $configs[] = $this->_getConfig( + $filename + ); + } + + return $configs; + } + + private function _getConfig( + string $location + ): object + { + if (!is_file($location) || !is_readable($location)) + { + throw new Exception(); + } + + if (!$json = file_get_contents($location)) + { + throw new Exception(); + } + + if (!$config = json_decode($json)) + { + throw new Exception(); + } + + return $config; + } +} \ No newline at end of file diff --git a/src/Test/Cards/name-1.json b/src/Test/Cards/name-1.json new file mode 100644 index 0000000..39dd528 --- /dev/null +++ b/src/Test/Cards/name-1.json @@ -0,0 +1,14 @@ +{ + "attributes": + [ + "attribute 1" + ], + "type":"type 1", + "title":"title 1", + "description":"description 1", + "cost":1, + "attack":1, + "health":1, + "craft":1, + "extract":1 +} \ No newline at end of file diff --git a/src/Test/Cards/name-2.json b/src/Test/Cards/name-2.json new file mode 100644 index 0000000..8ef0a98 --- /dev/null +++ b/src/Test/Cards/name-2.json @@ -0,0 +1,15 @@ +{ + "attributes": + [ + "attribute 1", + "attribute 2" + ], + "type":"type 2", + "title":"title 2", + "description":"description 2", + "cost":1, + "attack":1, + "health":1, + "craft":1, + "extract":1 +} \ No newline at end of file diff --git a/src/Test/Cards/name-3.json b/src/Test/Cards/name-3.json new file mode 100644 index 0000000..61d4930 --- /dev/null +++ b/src/Test/Cards/name-3.json @@ -0,0 +1,14 @@ +{ + "attributes": + [ + "attribute 2" + ], + "type":"type 2", + "title":"title 3", + "description":"description 3", + "cost":2, + "attack":2, + "health":2, + "craft":2, + "extract":2 +} \ No newline at end of file