fighters = array_map('User::loadFromDb', $_POST['pmelo']['fighterIds']); } } if(!is_file("pmeloRanking.json")){ $this->log['info'][] = "Couldn't find `pmeloRanking.json`. Create a new one!"; file_put_contents("pmeloRanking.json", json_encode([])); } // init members $this->trainees = self::getTrainees(); $this->rankings = self::createRankings(); } public static function getTrainees(){ $traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4)); $traineeAssocArray = []; foreach($traineeList as $trainee){ $traineeAssocArray[$trainee->getId()] = $trainee; } return $traineeAssocArray; } public static function loadRankings(){ return json_decode(file_get_contents("pmeloRanking.json")); } public static function saveRankings(array $rankings){ file_put_contents("pmeloRankings.json", json_encode($rankings)); } public function createRankings(){ // load the last state of the ranking $loadedRanking = self::loadRankings(); // check if the ranked trainees still are in training $cleanRanking = []; $toBeInsertedTrainees = []; foreach($loadedRanking as $rank=>$id){ if(array_key_exists($id, $this->trainees)){ $cleanRanking[] = $id; continue; } // id of the not in the ranking, we have to insert $toBeInsertedTrainees[$id] = $this->trainees[$id]; } usort($toBeInsertedTrainees, function($lhs, $rhs){return $lhs < $rhs ? -1 : 1;}); var_dump($toBeInsertedTrainees); // insert new trainees in the ranking // foreach($this->trainees as $trainee){ // if(empty($this->rankings)){ // } // } } public $log = ['error'=>[], 'warning'=>[], 'info'=>[]]; public $fighters = null; public $trainees = null; // list of id-s in the order of the current ranking public $rankings = null; }