From b5d2655e8cd2c0b3102d75551029b6901c5d8b64 Mon Sep 17 00:00:00 2001 From: marko Date: Sun, 4 Feb 2024 19:32:30 +0100 Subject: [PATCH] pmelo cleaning up --- homepage/participo/pmelo.inc.php | 217 ++++++++++++++++++++++++++----- homepage/participo/pmelo.php | 80 +----------- 2 files changed, 189 insertions(+), 108 deletions(-) diff --git a/homepage/participo/pmelo.inc.php b/homepage/participo/pmelo.inc.php index 9a491dc..9db3b48 100644 --- a/homepage/participo/pmelo.inc.php +++ b/homepage/participo/pmelo.inc.php @@ -15,6 +15,7 @@ $pmelo = new PmElo(); class PmElo { + /** initialize the pmelo session */ function __construct() { // starting/continuing a session @@ -75,13 +76,31 @@ class PmElo self::saveRankings($this->rankings); } - function __destruct() - { - // sad, the destructor is not allowed to use file_put_contents + //// + // html code injection + /// + + /** Echo the fighterInputForm */ + public function htmlFighterInputForm(){ + echo($this->getFighterInputForm($this->trainees)); } - // load all active trainees into a id=>User assoc array - public static function getTrainees() + /** Echo the fighterTable */ + public function htmlFighterTable(){ + echo($this->getFighterTable($this->fighters, $this->rankings)); + } + + /** Echo logOutput */ + public function htmlLogOutput(){ + echo($this->getLogOutput($this->log)); + } + + //// + // saving and loading + //// + + /** load all active trainees into a id=>User assoc array */ + static private function getTrainees() { $traineeList = array_map( "User::fromDbArray", @@ -94,39 +113,34 @@ class PmElo return $traineeAssocArray; } - // load ranking from json file - public static function loadRankings() + /** load ranking from json file */ + static private function loadRankings() { return json_decode(file_get_contents(self::$pmeloJson)); } - // save a ranking to json file - public static function saveRankings(array $rankings) + /** save a ranking to json file */ + static private function saveRankings(array $rankings) { file_put_contents(self::$pmeloJson, json_encode($rankings)); } - // simple logger to a logging buffer - function var_log( - $variable, - string $prefix = null, - string $logChannel = "info" - ) { - if (!in_array($logChannel, ["info", "warning", "error"])) { - $logChannel = "info"; - } - $prefix = !empty($prefix) ? $prefix . ": " : ""; - $this->log[$logChannel][] = $prefix . var_export($variable, true); - } + //// + // member functions + //// - // create the ranking for the current session (load saved ranking from file, remove old trainees, add new trainees) - public function createRankings() + /** create the ranking for the current session + * + * - load saved ranking from file + * - remove old trainees + * - add new trainees + */ + private function createRankings() { // load the last state of the ranking $loadedRanking = self::loadRankings(); // check if the ranked trainees still are in training - // - the ranking with $cleanRanking = []; // - trainees that aren't currently in the ranking for later inserting $toBeInsertedTrainees = $this->trainees; @@ -186,6 +200,15 @@ class PmElo return $updatedRanking; } + /** promote a fighter + * + * Promotion means two fighter switch places + * + * $promoteeId (int>0) id to promote + * $promopteeRank (int>0) current rank of the one to be promoted + * $degradeeId (int>0) id to demote + * $degradeeRank (int>0) new rank of the promotee + */ private function promote( int $promoteeId, int $promoteeRank, @@ -212,17 +235,100 @@ class PmElo // do the actual switch $this->rankings[$promoteeRank] = $degradeeId; $this->rankings[$degradeeRank] = $promoteeId; - - $this->saveRankings($this->rankings); } } } // save the updated ranking $this->saveRankings($this->rankings); } - // promotion means switching the position with another user - // id-s would already be sufficient, rank is just for controlling - public static function htmlPromoteButton( + + //// + // helper for output generation + //// + + /** Assemble the Input form for the fighter selection + * + * $trainees (array(participo::user)) list of trainees that can be selected for fighting + * + * return fighterInputForm as html code + */ + static private function getFighterInputForm(array $trainees){ + $style = [ + 'classes'=>[ + 'input-field' => "input-field col s12" + ] + ]; + + $retHtml = + '
' + // selects have to be wrapped into an .input-field, limitation of materializeCss + .'
' + .''. + ''. + ''. + '
'. + '
'; + return $retHtml; + } + + /** Assemble the ranking table of the fighters + * + * $fighters (array(id=>participo::User)) list of fighters in the active round + * $ranking (array(rank=>id)) ranking of the current selected fighters + * + * return fighter ranking table as html code + */ + static private function getFighterTable(array $fighters, array $rankings){ + $retHtml = + ''. + ''. + ''; + if (!empty($fighters)) { + // cache the predecessor in the ranking for the promoting form + $lastOne = null; + foreach ($rankings as $rank => $id) { + if (array_key_exists($id, $fighters)) { + $fighter = $fighters[$id]; + $retHtml .= ''. + ''. + ''; + if (!is_null($lastOne)){ + $retHtml .= ''; + } + $retHtml .= ''; + $lastOne = ["id" => $fighter->getId(), "rank" => $rank]; + } + } + } + $retHtml .= + ''. + '
PlatzNameAufstieg
'.$rank.''.join(' ', [$fighter->getFirstName(), $fighter->getName()]).''.self::getPromoteButton( + $fighter->getId(),$rank, + $lastOne["id"],$lastOne["rank"] + ).'
'; + + return $retHtml; + } + + /** Assemble a Promote button + * + * $promoteeId (int>0) id to promote + * $promoteeRank (int>0) current rank of the promotee + * $degradeeId (int>0) id to demote + * $degradeeRank (int>0) rank of the to be demoted + */ + static private function getPromoteButton( int $promoteeId, int $promoteeRank, int $degradeeId, @@ -237,11 +343,60 @@ class PmElo ""; } - public $log = ["error" => [], "warning" => [], "info" => []]; + /** Assemble the log Output + * + * $log (array logLevel=>array(messages)) lists of log messages per logLevel + * + * return logOutput as html code + */ + private static function getLogOutput(array $log){ + $retHtml = + '