124 lines
4.3 KiB
PHP
124 lines
4.3 KiB
PHP
<?php
|
|
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . "./lib/");
|
|
|
|
// start a session
|
|
session_start();
|
|
|
|
// Configs
|
|
require_once("config/participo.php");
|
|
require_once $config['basePath'] . "/config/cwsvJudo.config.php";
|
|
|
|
// Libs
|
|
require_once("participoLib/user.php");
|
|
|
|
participo::init($cwsvJudoConfig);
|
|
|
|
$pmelo = new PmElo();
|
|
|
|
class PmElo{
|
|
function __construct()
|
|
{
|
|
// starting/continuing a session
|
|
session_start();
|
|
|
|
// create 'pmelo' section in the sessions data
|
|
if(!array_key_exists('pmelo', $_SESSION)){
|
|
$_SESSION['pmelo'] = [];
|
|
}
|
|
// - data storage for id-s of chosen fighters
|
|
if(!array_key_exists('fighterIds', $_SESSION)){
|
|
$_SESSION['pmelo']['fighterIds'] = null;
|
|
}
|
|
|
|
// check for action in post data
|
|
if(array_key_exists('pmelo', $_POST)){
|
|
if(array_key_exists('fighterIds', $_POST['pmelo'])){
|
|
$this->fighters = array_map('User::loadFromDb', $_POST['pmelo']['fighterIds']);
|
|
}
|
|
}
|
|
|
|
// load/recreate ranking data
|
|
if(!is_file(self::$pmeloJson)){
|
|
$this->log['info'][] = "Couldn't find `".self::$pmeloJson."`. Create a new one!";
|
|
file_put_contents(self::$pmeloJson, json_encode([]));
|
|
}
|
|
|
|
// init members
|
|
$this->trainees = self::getTrainees();
|
|
$this->var_log(array_map(function($user){return $user->getFirstName();},$this->trainees), "trainees from db");
|
|
$this->rankings = self::createRankings();
|
|
|
|
}
|
|
|
|
// load all active trainees into a id=>User assoc array
|
|
public static function getTrainees(){
|
|
$traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4));
|
|
$traineeAssocArray = [];
|
|
foreach($traineeList as $trainee){
|
|
$traineeAssocArray[$trainee->getId()] = $trainee;
|
|
}
|
|
return $traineeAssocArray;
|
|
}
|
|
|
|
// load ranking from json file
|
|
public static function loadRankings(){
|
|
return json_decode(file_get_contents(self::$pmeloJson));
|
|
}
|
|
|
|
// save a ranking to json file
|
|
public static function saveRankings(array $rankings){
|
|
file_put_contents(self::$pmeloJson, json_encode($rankings));
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public function createRankings(){
|
|
// load the last state of the ranking
|
|
$loadedRanking = self::loadRankings();
|
|
$this->var_log($loadedRanking,"ranking");
|
|
|
|
// check if the ranked trainees still are in training
|
|
$cleanRanking = [];
|
|
$toBeInsertedTrainees = $this->trainees;
|
|
foreach($loadedRanking as $rank=>$id){
|
|
if(array_key_exists($id, $this->trainees)){
|
|
$cleanRanking[] = $id;
|
|
// id of the not in the ranking, we have to insert
|
|
unset($toBeInsertedTrainees[$id]);
|
|
}
|
|
}
|
|
|
|
$newTraineesIds = array_map(function($u){return $u->getId();},$toBeInsertedTrainees);
|
|
usort(
|
|
$newTraineesIds
|
|
, function($lhs, $rhs){
|
|
return $this->trainees[$lhs]->getStrBirthday() < $this->trainees[$rhs]->getStrBirthday() ? -1 : ($this->trainees[$lhs]->getStrBirthday() > $this->trainees[$rhs]->getStrBirthday() ? 1 : 0);
|
|
}
|
|
);
|
|
$this->var_log(array_map(function($id){return $this->trainees[$id]->getStrBirthday();},$newTraineesIds), "newTrainieesIdsByAge");
|
|
$this->var_log($newTraineesIds, "newTrainieesIdsByAge");
|
|
$this->var_log(array_map(function($user){return $user->getFirstName();}, $toBeInsertedTrainees), "newTrainees");
|
|
|
|
// 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;
|
|
|
|
static $pmeloJson="pmeloRanking.json";
|
|
} |