Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Fixes #588: Level Import: Random ISO Code #612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ function($a, $b) {
return self::countryFromRow($result->mapRows()[0]);
}

// Get a random, unused country.
public static async function genRandomAvailableCountry(): Awaitable<Country> {
$countries = await genAllAvailableCountries();
return $countries[array_rand($countries)];
}

// Get a random enabled, unused country ID
public static async function genRandomAvailableCountryId(): Awaitable<int> {
$db = await self::genDb();
Expand Down
2 changes: 1 addition & 1 deletion src/models/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static function levelFromRow(Map<string, string> $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();
Copy link

@azjezz azjezz Apr 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert.

  1. always use || instead of or
  2. this is a syntax error, we can't have await in there.
  3. must_have_string will always return, there's no case where it returns a "falsy" value, so the or await Country::genRandomAvailableCountry()->getIsoCode(); will never be executed.

$c = must_have_string($level, 'category');
$exist = await self::genAlreadyExist($type, $title, $entity_iso_code);
list($entity_exist, $category_exist) = await \HH\Asio\va(
Expand Down
6 changes: 2 additions & 4 deletions src/scripts/liveimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down