Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Equidea;
- use Equidea\Core\Controller;
- use Equidea\Core\Response;
- use Equidea\Display\Template;
- /**
- * Controller that handles requests to the websites root.
- */
- class Index extends Controller
- {
- // @parent: $container, $request, $response
- // @construct: $template
- public function __construct(
- private Template $template
- ) {}
- /**
- * Renders the webpage and puts it inside of the response body.
- */
- private function renderWebsite(array $data = []) : Response
- {
- $view = $this->template->render('index', $data);
- return $this->response->withBody($view);
- }
- /**
- * Serves the index page.
- */
- public function getIndex() : Response {
- return $this->renderWebsite();
- }
- /**
- * Attempts to calculate foal exterieur on correct user input.
- */
- public function postExterieur() : Response
- {
- // GET AND CLEAN INPUT
- $stallion = $_POST['stallion'] ?? '';
- $stallion = trim($stallion);
- $mare = $_POST['mare'] ?? '';
- $mare = trim($mare);
- // CHECK FOR EVERY FIELD BEING FILLED OUT
- if (empty($stallion) || empty($mare)) {
- $error = 'Du hast nicht alle Felder ausgefüllt';
- return $this->renderWebsite(['error' => $error]);
- }
- // ON VALID INPUT CALCULATE EXTERIEUR
- if ($this->validateInput($mare) && $this->validateInput($stallion))
- {
- // BREAK GENE STRINGS INTO PARTS
- $stallionGenes = $this->importExterieur($stallion);
- $mareGenes = $this->importExterieur($mare);
- // CALCULATE EXTERIEUR ONE BY ONE
- $foal = [];
- $worst = $this->calculateWorst($stallionGenes, $mareGenes);
- $foal['worst']['genes'] = $this->exportExterieur($worst);
- $foal['worst']['grade'] = $this->calculateGrade($worst);
- $average = $this->calculateAverage($stallionGenes, $mareGenes);
- $foal['average']['genes'] = $this->exportExterieur($average);
- $foal['average']['grade'] = $this->calculateGrade($average);
- $best = $this->calculateBest($stallionGenes, $mareGenes);
- $foal['best']['genes'] = $this->exportExterieur($best);
- $foal['best']['grade'] = $this->calculateGrade($best);
- // $foal['average'] = null;
- return $this->renderWebsite([
- 'stallion' => [
- 'genes' => $stallion,
- 'grade' => $this->calculateGrade($stallionGenes)
- ],
- 'mare' => [
- 'genes' => $mare,
- 'grade' => $this->calculateGrade($mareGenes)
- ],
- 'foal' => $foal
- ]);
- }
- // OTHERWISE RENDER ERROR
- $error = 'Die Eingabe war fehlerhaft.';
- return $this->renderWebsite(['error' => $error]);
- }
- /**
- * Checks whether user input is valid.
- */
- private function validateInput(string $input) : bool
- {
- $regex = '#^([Hh]{2}\s){7}[Hh]{2}$#';
- return preg_match($regex, $input) == 1;
- }
- /**
- * Translates the gene string into an array of arrays
- */
- private function importExterieur(string $raw) : array
- {
- $rawGenes = explode(' ', $raw);
- $genes = [];
- foreach ($rawGenes as $key => $value) {
- $genes[$key] = str_split($value);
- }
- return $genes;
- }
- /**
- * Translates an array of genes into a string.
- */
- private function getArrayGenesAsString(array $raw) : array
- {
- $genes = [];
- foreach ($raw as $key => $value) {
- $genes[$key] = implode($value);
- }
- return $genes;
- }
- /**
- * Translates the gene array of array into a readable string
- */
- private function exportExterieur(array $genes) : string {
- return implode(' ', $this->getArrayGenesAsString($genes));
- }
- /**
- * Calculates the theoretical foal with the worst possible exterieur.
- */
- private function calculateWorst(array $stallion, array $mare) : array
- {
- $foal = [];
- for ($i = 0; $i < 4; $i++)
- {
- $left = in_array('h', $stallion[$i]) ? 'h' : 'H';
- $right = in_array('h', $mare[$i]) ? 'h' : 'H';
- $foal[$i] = [$left, $right];
- }
- for ($i = 4; $i < 8; $i++)
- {
- $left = in_array('H', $stallion[$i]) ? 'H' : 'h';
- $right = in_array('H', $mare[$i]) ? 'H' : 'h';
- $foal[$i] = [$left, $right];
- }
- return $foal;
- }
- /**
- * Calculates the average foal exterieur.
- */
- private function calculateAverage(array $stallion, array $mare) : array
- {
- $foal = [];
- $stallion = $this->getArrayGenesAsString($stallion);
- $mare = $this->getArrayGenesAsString($mare);
- for ($i = 0; $i < 8; $i++)
- {
- // HH + x
- if ($stallion[$i] == 'HH')
- {
- if ($mare[$i] == 'HH') {
- $foal[$i] = ['H', 'H'];
- }
- if ($mare[$i] == 'hh') {
- $foal[$i] = ['H', 'h'];
- }
- if ($mare[$i] == 'Hh' || $mare[$i] == 'hH')
- {
- if ($i > 3) {
- $foal[$i] = ['H', 'h'];
- } else {
- $foal[$i] = ['H', 'H'];
- }
- }
- }
- // hh + x
- if ($stallion[$i] == 'hh')
- {
- if ($mare[$i] == 'HH') {
- $foal[$i] = ['h', 'H'];
- }
- if ($mare[$i] == 'hh') {
- $foal[$i] = ['h', 'h'];
- }
- if ($mare[$i] == 'Hh' || $mare[$i] == 'hH')
- {
- if ($i > 3) {
- $foal[$i] = ['h', 'h'];
- } else {
- $foal[$i] = ['h', 'H'];
- }
- }
- }
- // Hh or hH + x
- if ($stallion[$i] == 'Hh' || $stallion[$i] == 'hH')
- {
- if ($mare[$i] == 'HH')
- {
- if ($i > 3) {
- $foal[$i] = ['h', 'H'];
- } else {
- $foal[$i] = ['H', 'H'];
- }
- }
- if ($mare[$i] == 'hh')
- {
- if ($i > 3) {
- $foal[$i] = ['h', 'h'];
- } else {
- $foal[$i] = ['H', 'h'];
- }
- }
- if ($mare[$i] == 'Hh' || $mare[$i] == 'hH')
- {
- $variants = [['H', 'h'], ['h', 'H']];
- $foal[$i] = $variants[mt_rand(0,1)];
- }
- }
- }
- return $foal;
- }
- /**
- * Calculates the theoretical foal with the best possible exterieur.
- */
- private function calculateBest(array $stallion, array $mare) : array
- {
- $foal = [];
- for ($i = 0; $i < 4; $i++)
- {
- $left = in_array('H', $stallion[$i]) ? 'H' : 'h';
- $right = in_array('H', $mare[$i]) ? 'H' : 'h';
- $foal[$i] = [$left, $right];
- }
- for ($i = 4; $i < 8; $i++)
- {
- $left = in_array('h', $stallion[$i]) ? 'h' : 'H';
- $right = in_array('h', $mare[$i]) ? 'h' : 'H';
- $foal[$i] = [$left, $right];
- }
- return $foal;
- }
- /**
- * Calculates the grade between 1 and 5 by interpreting the given
- * genetical code.
- */
- private function calculateGrade(array $genes) : int
- {
- $left = 0;
- $right = 0;
- // Calculate number of right genes
- for ($i = 0; $i < 4; $i++) {
- $left += in_array('H', $genes[$i]) ? 1 : 0;
- }
- for ($i = 4; $i < 8; $i++) {
- $right += in_array('H', $genes[$i]) ? 0 : 1;
- }
- $score = ($left < $right) ? $left : $right;
- // Calculate the grade
- switch ($score) {
- case 4: return 1;
- case 3: return 2;
- case 2: return 3;
- case 1: return 4;
- default: return 5;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement