Browse Source

initial commit

main
openlegends 9 months ago
parent
commit
95dfa23b2e
  1. 1
      .gitignore
  2. 17
      composer.json
  3. 142
      src/Config.php
  4. 14
      src/Test/Cards/name-1.json
  5. 15
      src/Test/Cards/name-2.json
  6. 14
      src/Test/Cards/name-3.json

1
.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
/vendor/

17
composer.json

@ -0,0 +1,17 @@ @@ -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": {}
}

142
src/Config.php

@ -0,0 +1,142 @@ @@ -0,0 +1,142 @@
<?php
namespace OpenLegends\Asset;
class Config
{
private string $_asset;
public function __construct(string $asset)
{
if (is_dir(__DIR__ . DIRECTORY_SEPARATOR . $asset))
{
$this->_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;
}
}

14
src/Test/Cards/name-1.json

@ -0,0 +1,14 @@ @@ -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
}

15
src/Test/Cards/name-2.json

@ -0,0 +1,15 @@ @@ -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
}

14
src/Test/Cards/name-3.json

@ -0,0 +1,14 @@ @@ -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
}
Loading…
Cancel
Save