diff --git a/htdocs/content/research.php b/htdocs/content/research.php index b3595537b..7fe45e871 100644 --- a/htdocs/content/research.php +++ b/htdocs/content/research.php @@ -58,6 +58,35 @@ $use_img_filter = false; } +function check_building_requirement(EtoA\Requirement\ObjectRequirement $requirement, array $buildlist): bool +{ + return isset($buildlist[$requirement->requiredBuildingId]) && $buildlist[$requirement->requiredBuildingId] >= $requirement->requiredLevel; +} + +function check_technology_requirement(EtoA\Requirement\ObjectRequirement $requirement, array $techlist): bool +{ + return isset($techlist[$requirement->requiredTechnologyId]) && $techlist[$requirement->requiredTechnologyId]->currentLevel >= $requirement->requiredLevel; +} + +function check_requirements(EtoA\Requirement\RequirementsCollection $requirements, array $buildlist, array $techlist, int $techid): bool +{ + // check building prerequisites + foreach ($requirements->getBuildingRequirements($techid) as $requirement) { + if (!check_building_requirement($requirement, $buildlist)) { + return false; + } + } + + // check technology prerequisites + foreach ($requirements->getTechnologyRequirements($techid) as $requirement) { + if (!check_technology_requirement($requirement, $techlist)) { + return false; + } + } + + return true; +} + if (isset($cp)) { $planet = $planetRepo->find($cp->id); @@ -528,18 +557,7 @@ // Check requirements for this building - $requirements_passed = true; - foreach ($requirements->getBuildingRequirements($technology->id) as $requirement) { - if (!isset($buildlist[$requirement->requiredBuildingId]) || $buildlist[$requirement->requiredBuildingId] < $requirement->requiredLevel) { - $requirements_passed = false; - } - } - - foreach ($requirements->getTechnologyRequirements($technology->id) as $requirement) { - if (!isset($techlist[$requirement->requiredTechnologyId]) || $techlist[$requirement->requiredTechnologyId]->currentLevel < $requirement->requiredLevel) { - $requirements_passed = false; - } - } + $requirements_passed = check_requirements($requirements, $buildlist, $techlist, $technology->id); // // BaumenĂ¼ @@ -767,28 +785,7 @@ function setCountdown() } // Check requirements for this tech - $requirements_passed = true; - $b_req_info = array(); - $t_req_info = array(); - if (isset($b_req[$tech->id]['t']) && count($b_req[$tech->id]['t']) > 0) { - foreach ($b_req[$tech->id]['t'] as $b => $l) { - if (!isset($techlist[$b]->currentLevel) || $techlist[$b]->currentLevel < $l) { - $t_req_info[] = array($b, $l, false); - $requirements_passed = false; - } else - $t_req_info[] = array($b, $l, true); - } - } - if (isset($b_req[$tech->id]['b']) && count($b_req[$tech->id]['b']) > 0) { - foreach ($b_req[$tech->id]['b'] as $id => $level) { - if (!isset($buildlist[$id]) || $buildlist[$id] < $level) { - $requirements_passed = false; - $b_req_info[] = array($id, $level, false); - } else { - $b_req_info[] = array($id, $level, true); - } - } - } + $requirements_passed = check_requirements($requirements, $buildlist, $techlist, $tech->id); $filterStyleClass = ""; if (!$tech->show && $b_level > 0) { @@ -808,12 +805,13 @@ function setCountdown() /** @var BuildingDataRepository $buildingRepository */ $buildingRepository = $app[BuildingDataRepository::class]; $buildingNames = $buildingRepository->getBuildingNames(true); - foreach ($b_req_info as $v) { - $tmtext .= "
" . $buildingNames[$v[0]] . " Stufe " . $v[1] . "
"; + + foreach ($requirements->getBuildingRequirements($tech->id) as $breq) { + $tmtext .= "
" . $buildingNames[$breq->requiredBuildingId] . " Stufe " . $breq->requiredLevel . "
"; } - foreach ($t_req_info as $v) { - $tmtext .= "
" . $technologyNames[$v[0]] . " Stufe " . $v[1] . "
"; + foreach ($requirements->getTechnologyRequirements($tech->id) as $treq) { + $tmtext .= "
" . $technologyNames[$treq->requiredTechnologyId] . " Stufe " . $treq->requiredLevel . "
"; } $color = '#999';