diff --git a/src/models/Country.php b/src/models/Country.php index 75fad5cc..a9a2f993 100644 --- a/src/models/Country.php +++ b/src/models/Country.php @@ -278,6 +278,12 @@ function($a, $b) { return self::countryFromRow($result->mapRows()[0]); } + // Get a random, unused country. + public static async function genRandomAvailableCountry(): Awaitable { + $countries = await genAllAvailableCountries(); + return $countries[array_rand($countries)]; + } + // Get a random enabled, unused country ID public static async function genRandomAvailableCountryId(): Awaitable { $db = await self::genDb(); diff --git a/src/models/Level.php b/src/models/Level.php index e559621b..5c00db31 100644 --- a/src/models/Level.php +++ b/src/models/Level.php @@ -151,7 +151,7 @@ private static function levelFromRow(Map $row): Level { foreach ($elements as $level) { $title = must_have_string($level, 'title'); $type = must_have_string($level, 'type'); - $entity_iso_code = must_have_string($level, 'entity_iso_code'); + $entity_iso_code = must_have_string($level, 'entity_iso_code') or await Country::genRandomAvailableCountry()->getIsoCode(); $c = must_have_string($level, 'category'); $exist = await self::genAlreadyExist($type, $title, $entity_iso_code); list($entity_exist, $category_exist) = await \HH\Asio\va( diff --git a/src/scripts/liveimport.php b/src/scripts/liveimport.php index 9b138d05..aeab2472 100644 --- a/src/scripts/liveimport.php +++ b/src/scripts/liveimport.php @@ -123,8 +123,7 @@ class LiveSyncImport { $level->type = 'flag'; } if (property_exists($level, 'entity_iso_code') === false) { - $countries = await Country::genAllAvailableCountries(); - $country = $countries[array_rand($countries)]; + $country = await Country::genRandomAvailableCountry(); $country_id = $country->getId(); $level->entity_iso_code = $country->getIsoCode(); $level->random_country = true; @@ -261,8 +260,7 @@ class LiveSyncImport { intval($level->points), ); if ($level_exists === false) { - $countries = await Country::genAllAvailableCountries(); - $new_country = $countries[array_rand($countries)]; + $new_country = await Country::genRandomAvailableCountry(); self::debug( $debug, $url,