mirror of
https://github.com/scummvm/scummvm-web.git
synced 2026-05-21 05:40:47 +00:00
WEB: Add error handling about missing required fields
In the new data model, some field operate as keys for other parts of the model. This now performs some validations and will throw an error if fields are missing. For example: a Game must have an Engine, but does not require datafiles
This commit is contained in:
+290
-290
File diff suppressed because it is too large
Load Diff
+6
-1
@@ -372,4 +372,9 @@
|
||||
id: zvision
|
||||
name: ZVision
|
||||
alt_name: ""
|
||||
enabled: true
|
||||
enabled: true
|
||||
-
|
||||
id: testbed
|
||||
name: Testbed
|
||||
alt_name: ""
|
||||
enabled: false
|
||||
+4
-4
@@ -2562,7 +2562,7 @@
|
||||
datafiles: ""
|
||||
-
|
||||
id: pass
|
||||
name: 'Passport to Adventure'
|
||||
name: 'Passport to Adventure (Indiana Jones and the Last Crusade, The Secret of Monkey Island, Loom)'
|
||||
engine_id: scumm
|
||||
company_id: lucasarts
|
||||
moby_id: ""
|
||||
@@ -3341,7 +3341,7 @@
|
||||
id: chivalry
|
||||
name: 'Chivalry is Not Dead'
|
||||
engine_id: wintermute
|
||||
company_id: ""
|
||||
company_id: squinky
|
||||
moby_id: ""
|
||||
datafiles: 'https://wiki.scummvm.org/index.php?title=Datafiles#Chivalry_is_Not_Dead'
|
||||
-
|
||||
@@ -3369,7 +3369,7 @@
|
||||
id: dreaming
|
||||
name: 'Des Reves Elastiques Avec Mille Insectes Nommes Georges'
|
||||
engine_id: wintermute
|
||||
company_id: ""
|
||||
company_id: squinky
|
||||
moby_id: ""
|
||||
datafiles: ""
|
||||
-
|
||||
@@ -3628,7 +3628,7 @@
|
||||
id: pigeons
|
||||
name: 'Pigeons in the Park'
|
||||
engine_id: wintermute
|
||||
company_id: ""
|
||||
company_id: squinky
|
||||
moby_id: ""
|
||||
datafiles: ""
|
||||
-
|
||||
|
||||
@@ -60,4 +60,43 @@ abstract class CompatibilityModel extends BasicModel
|
||||
|
||||
return $all_games[$target];
|
||||
}
|
||||
|
||||
public static function getAllDataGroups($version) {
|
||||
$data = self::getAllData($version);
|
||||
$compat_data = [];
|
||||
foreach ($data as $compat) {
|
||||
$engine = $compat->getGame()->getEngine();
|
||||
$company = $compat->getGame()->getCompany();
|
||||
|
||||
if ($engine->getEnabled()) {
|
||||
$engineName = $engine->getName();
|
||||
if (is_string($company)) {
|
||||
$companyName = "Unknown";
|
||||
} else {
|
||||
$companyName = $company->getName();
|
||||
}
|
||||
if (!array_key_exists($companyName, $compat_data)) {
|
||||
$compat_data[$companyName] = [];
|
||||
}
|
||||
|
||||
$compat_data[$companyName][] = $compat->toLegacyCompatGame();
|
||||
|
||||
}
|
||||
}
|
||||
$compat_data['Other'] = [];
|
||||
foreach ($compat_data as $key => $company) {
|
||||
\sort($compat_data[$key], SORT_STRING);
|
||||
if (count($compat_data[$key]) < 3) {
|
||||
$compat_data['Other'] = \array_merge($compat_data['Other'], $company);
|
||||
unset($compat_data[$key]);
|
||||
}
|
||||
}
|
||||
\sort($compat_data['Other'], SORT_STRING);
|
||||
return $compat_data;
|
||||
}
|
||||
|
||||
private static function compatibilitySorter($compat1, $compat2)
|
||||
{
|
||||
return strnatcmp($compat1->getName(), $compat2->getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,18 +57,13 @@ abstract class GameDemosModel extends BasicModel
|
||||
'demos' => []
|
||||
];
|
||||
foreach ($groups as $key => $group) {
|
||||
\usort($groups[$key]['demos'], "\ScummVM\Models\GameDemosModel::demoSorter");
|
||||
\sort($groups[$key]['demos'], SORT_STRING);
|
||||
if (count($groups[$key]['demos']) <= 15) {
|
||||
$groups['other']['demos'] = \array_merge($groups['other']['demos'], $groups[$key]['demos']);
|
||||
unset($groups[$key]);
|
||||
}
|
||||
}
|
||||
// \usort($groups['other']['demos'], "\ScummVM\Models\GameDemosModel::demoSorter");
|
||||
\sort($groups['other']['demos'], SORT_STRING);
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private static function demoSorter($demo1, $demo2)
|
||||
{
|
||||
return strnatcmp($demo1->getName(), $demo2->getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,11 @@ abstract class BasicObject
|
||||
$this->name = $data['name'];
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getName();
|
||||
}
|
||||
|
||||
/* Get the name. */
|
||||
public function getName()
|
||||
{
|
||||
|
||||
@@ -14,8 +14,8 @@ class Company extends DataObject
|
||||
public function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->name = $data['name'];
|
||||
$this->alt_name = $data['alt_name'];
|
||||
$this->name = $this->assignFromArray('name', $data, true);
|
||||
$this->alt_name = $this->assignFromArray('alt_name', $data);
|
||||
}
|
||||
|
||||
/* Get the company name. */
|
||||
|
||||
@@ -21,14 +21,14 @@ class Compatibility extends DataObject
|
||||
public function __construct($data, $games, $platforms)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->supportLevel = $data['support'];
|
||||
$this->notes = $data['notes'];
|
||||
$this->version = $data['version'];
|
||||
$this->supportLevel = $this->assignFromArray('support', $data, true);
|
||||
$this->notes = $this->assignFromArray('notes', $data);
|
||||
$this->version = $this->assignFromArray('version', $data, true);
|
||||
$this->stablePlatforms =
|
||||
$this->processPlatforms($data['stable_platforms'], $platforms);
|
||||
$this->processPlatforms($this->assignFromArray('stable_platforms', $data, true), $platforms);
|
||||
$this->unstablePlatforms =
|
||||
$this->processPlatforms($data['unstable_platforms'], $platforms);
|
||||
$this->game = $this->assignFromArray($data['game_id'], $games);
|
||||
$this->processPlatforms($this->assignFromArray('unstable_platforms', $data), $platforms);
|
||||
$this->game = $this->assignFromArray($data['id'], $games, true);
|
||||
}
|
||||
|
||||
private function processPlatforms($values, $platforms)
|
||||
@@ -42,12 +42,6 @@ class Compatibility extends DataObject
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
/* Get the target name. */
|
||||
public function getTarget()
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
/* Get the support level. */
|
||||
public function getSupportLevel()
|
||||
{
|
||||
|
||||
@@ -8,13 +8,16 @@ namespace ScummVM\Objects;
|
||||
abstract class DataObject
|
||||
{
|
||||
protected $id;
|
||||
const NO_ID = 'Data object %s is missing an ID field';
|
||||
const BAD_KEY = 'Field %s is required and cannot be empty for %s';
|
||||
const NO_KEY = "Key is required and can not be blank";
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
if (array_key_exists('id', $data)) {
|
||||
$this->id = $data['id'];
|
||||
} elseif (array_key_exists('support', $data)) { // Compatibility
|
||||
$this->id = $data['game_id'];
|
||||
try {
|
||||
$this->id = $this->assignFromArray('id', $data);
|
||||
} catch (\ErrorException $ex) {
|
||||
throw new \ErrorException(\sprintf(self::NO_ID,\get_class($this)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,19 +29,24 @@ abstract class DataObject
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return strval($this->id);
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to safely retrieve an object
|
||||
* from an array.
|
||||
*/
|
||||
protected function assignFromArray($key, $array)
|
||||
protected function assignFromArray($key, $array, $required = false)
|
||||
{
|
||||
if (array_key_exists($key, $array)) {
|
||||
return $array[$key];
|
||||
} else {
|
||||
return $key;
|
||||
if ($required) {
|
||||
if ($key === '') {
|
||||
throw new \ErrorException(\sprintf(self::NO_KEY, $key, \get_class($this)));
|
||||
}
|
||||
|
||||
if (!isset($array[$key]) || $array[$key] === '') {
|
||||
throw new \ErrorException(\sprintf(self::BAD_KEY, $key, \get_class($this)));
|
||||
}
|
||||
}
|
||||
return $array[$key];
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,9 @@ class Engine extends DataObject
|
||||
public function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->name = $data['name'];
|
||||
$this->alt_name = $data['alt_name'];
|
||||
$this->enabled = $data['enabled'];
|
||||
$this->name = $this->assignFromArray('name', $data, true);
|
||||
$this->alt_name = $this->assignFromArray('alt_name', $data);
|
||||
$this->enabled = $this->assignFromArray('enabled', $data);
|
||||
}
|
||||
|
||||
/* Get the engine name. */
|
||||
|
||||
@@ -16,11 +16,16 @@ class Game extends DataObject
|
||||
public function __construct($data, $engines, $companies)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->name = $data['name'];
|
||||
$this->moby_id = $data['moby_id'];
|
||||
$this->datafiles = $data['datafiles'];
|
||||
$this->name = $this->assignFromArray('name', $data, true);
|
||||
$this->moby_id = $this->assignFromArray('moby_id', $data);
|
||||
$this->datafiles = $this->assignFromArray('datafiles', $data);
|
||||
$this->company = $this->assignFromArray($data['company_id'], $companies);
|
||||
$this->engine = $this->assignFromArray($data['engine_id'], $engines);
|
||||
$this->engine = $this->assignFromArray($data['engine_id'], $engines, true);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getName();
|
||||
}
|
||||
|
||||
/* Get the game name. */
|
||||
|
||||
@@ -8,7 +8,6 @@ class GameDemo extends DataObject
|
||||
{
|
||||
|
||||
private $url;
|
||||
private $name;
|
||||
private $category;
|
||||
private $platform;
|
||||
private $game;
|
||||
@@ -17,10 +16,15 @@ class GameDemo extends DataObject
|
||||
public function __construct($data, $games, $platforms)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->url = $data['url'];
|
||||
$this->platform = $this->assignFromArray($data['platform'], $platforms);
|
||||
$this->game = $this->assignFromArray($data['id'], $games);
|
||||
$this->category = isset($data['category']) ? $data['category'] : "";
|
||||
$this->url = $this->assignFromArray('url', $data, true);
|
||||
$this->platform = $this->assignFromArray($data['platform'], $platforms, true);
|
||||
$this->game = $this->assignFromArray($data['id'], $games, true);
|
||||
$this->category = $this->assignFromArray('category', $data);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getName();
|
||||
}
|
||||
|
||||
/* Get the download URL for the demo. */
|
||||
|
||||
@@ -12,7 +12,7 @@ class Platform extends DataObject
|
||||
public function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->name = $data['name'];
|
||||
$this->name = $this->assignFromArray('name', $data, true);
|
||||
}
|
||||
|
||||
/* Get the platform name. */
|
||||
|
||||
@@ -12,7 +12,7 @@ class Version extends DataObject
|
||||
public function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
$this->date = $data['date'];
|
||||
$this->date = $this->assignFromArray('date', $data);
|
||||
}
|
||||
|
||||
/* Get the version release name. */
|
||||
|
||||
@@ -133,37 +133,7 @@ class CompatibilityPage extends Controller
|
||||
$compat_data = LegacyCompatibilityModel::getAllData($version);
|
||||
} else {
|
||||
$filename = DIR_DATA . "/compatibility.yaml";
|
||||
$compat_data = [];
|
||||
$data = CompatibilityModel::getAllData($version);
|
||||
|
||||
foreach ($data as $compat) {
|
||||
$engine = $compat->getGame()->getEngine();
|
||||
$company = $compat->getGame()->getCompany();
|
||||
|
||||
if ($engine->getEnabled()) {
|
||||
$engineName = $engine->getName();
|
||||
if (is_string($company)) {
|
||||
$companyName = "Unknown";
|
||||
} else {
|
||||
$companyName = $company->getName();
|
||||
}
|
||||
if (!array_key_exists($companyName, $compat_data)) {
|
||||
$compat_data[$companyName] = [];
|
||||
}
|
||||
|
||||
$compat_data[$companyName][] = $compat->toLegacyCompatGame();
|
||||
|
||||
}
|
||||
}
|
||||
$compat_data['Other'] = [];
|
||||
foreach ($compat_data as $key => $company) {
|
||||
\usort($compat_data[$key], array($this,"compatibilitySorter"));
|
||||
if (count($compat_data[$key]) < 3) {
|
||||
$compat_data['Other'] = \array_merge($compat_data['Other'], $company);
|
||||
unset($compat_data[$key]);
|
||||
}
|
||||
}
|
||||
\usort($compat_data['Other'], array($this,"compatibilitySorter"));
|
||||
$compat_data = CompatibilityModel::getAllDataGroups($version);
|
||||
}
|
||||
|
||||
$last_updated = filemtime($filename);
|
||||
@@ -188,9 +158,4 @@ class CompatibilityPage extends Controller
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function compatibilitySorter($compat1, $compat2)
|
||||
{
|
||||
return strnatcmp($compat1->getName(), $compat2->getName());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user