From 6ad2ed7b29a268ef9a96f35ab7c37c85c2656b84 Mon Sep 17 00:00:00 2001 From: Dan Chadwick Date: Sun, 14 Apr 2024 21:54:51 -0700 Subject: [PATCH] new things --- web/modules/custom/ufc/js/src/index.jsx | 1 - .../Controller/FightTrainingController.php | 40 +++++++++++++++---- .../src/Controller/RecentFightsController.php | 8 ++-- .../ufc/src/Services/FighterImporter.php | 1 - web/themes/custom/dchadwick/js/global.js | 5 ++- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/web/modules/custom/ufc/js/src/index.jsx b/web/modules/custom/ufc/js/src/index.jsx index 1c93375..ba79b74 100644 --- a/web/modules/custom/ufc/js/src/index.jsx +++ b/web/modules/custom/ufc/js/src/index.jsx @@ -7,5 +7,4 @@ const container = document.getElementById('recent-fights'); const root = createRoot(container); root.render(); -// root.render(); // root.render(); diff --git a/web/modules/custom/ufc/src/Controller/FightTrainingController.php b/web/modules/custom/ufc/src/Controller/FightTrainingController.php index 3b1a7f4..f758955 100644 --- a/web/modules/custom/ufc/src/Controller/FightTrainingController.php +++ b/web/modules/custom/ufc/src/Controller/FightTrainingController.php @@ -184,23 +184,47 @@ private function extractValuesFromFields(int $id, array $field_names, string $pr * This needs to be between 0-1 */ private function normalizeData(array $data): array { + // @todo - this creates an issue where duration of fight is the largest number which isnt really correct + // and smaller duration would be better + // $min = min(array_values($data)); $max = max(array_values($data)); $normalized = []; foreach ($data as $key => $value) { - $norm_val = 0; - if ($max - $min == 0) { - $normalized[$key] = $norm_val; - } - else { - $norm_val = ($value - $min) / ($max - $min); - $normalized[$key] = $norm_val; - } + + $value = (float) $value; + $normalized[$key] = $this->convertToDecimal($value); + /* $norm_val = 0; */ + /* if ($max - $min == 0) { */ + /* $normalized[$key] = $norm_val; */ + /* } */ + /* else { */ + /* $norm_val = ($value - $min) / ($max - $min); */ + /* $normalized[$key] = $norm_val; */ + /* } */ } return $normalized; } + public function convertToDecimal(float $input) { + $output = 0; + if ($input > 0 && $input <= 1) { + $output = $input / 100; + } + elseif ($input > 1 && $input < 10) { + $output = $input / 100; + } + elseif ($input > 10 && $input < 100) { + $output = $input / 1000; + } + elseif ($input > 100 && $input < 1000) { + $output = $input / 10000; + } + + return $output; + } + /** * Gets the trained neural network. */ diff --git a/web/modules/custom/ufc/src/Controller/RecentFightsController.php b/web/modules/custom/ufc/src/Controller/RecentFightsController.php index 97bc139..9bc6e87 100644 --- a/web/modules/custom/ufc/src/Controller/RecentFightsController.php +++ b/web/modules/custom/ufc/src/Controller/RecentFightsController.php @@ -34,15 +34,16 @@ public static function create(ContainerInterface $container) { public function getRecentFights() { $query = $this->entityTypeManager->getStorage('node')->getQuery(); $query->accessCheck(TRUE); - $query->condition('type', 'fight')->sort('created', 'DESC')->sort('nid', 'DESC'); - $query->range(0, 13); + $query->condition('type', 'fight')->sort('field_fight_date', 'DESC'); + $query->range(0, 15); $nids = $query->execute(); $all_fights = Node::loadMultiple($nids); $fights = []; foreach ($all_fights as $fight) { $f1 = Node::load($fight->field_fighter_one->target_id); $f2 = Node::load($fight->field_fighter_two->target_id); - $result = Node::load($fight->field_result->target_id); + + /* $result = Node::load($fight->field_result->target_id) ?? FALSE; */ $alias = \Drupal::service('path_alias.manager')->getAliasByPath("/" . $fight->toUrl()->getInternalPath()); if ($f1 && $f2) { @@ -57,7 +58,6 @@ public function getRecentFights() { 'fighter_one_image' => $f1_pic_url, 'fighter_two' => $f2->getTitle(), 'fighter_two_image' => $f2_pic_url, - 'result' => $result->getTitle(), 'url' => $alias ]; } diff --git a/web/modules/custom/ufc/src/Services/FighterImporter.php b/web/modules/custom/ufc/src/Services/FighterImporter.php index 24434c5..a4b232c 100644 --- a/web/modules/custom/ufc/src/Services/FighterImporter.php +++ b/web/modules/custom/ufc/src/Services/FighterImporter.php @@ -178,7 +178,6 @@ public function loopThroughFighterPages($base_url): void { * Extract fighters from an html string. */ public function extractFighters(string $input): void { - // @todo REBUILD THIS NOW!! $fighter_list = []; $crawler = new Crawler($input); $athlete_flipcards = $crawler->filter('.c-listing-athlete-flipcard'); diff --git a/web/themes/custom/dchadwick/js/global.js b/web/themes/custom/dchadwick/js/global.js index 2bbbca6..bc750f8 100644 --- a/web/themes/custom/dchadwick/js/global.js +++ b/web/themes/custom/dchadwick/js/global.js @@ -4,9 +4,10 @@ document.addEventListener('readystatechange', event => { highlightFightHistory(); let train = false; if (train) { - trainTheBrain(); + setTimeout(() => { + trainTheBrain(); + }, 5000); } - } }, { passive: true });