wip pmelo: still promoting missing

This commit is contained in:
marko
2024-01-23 19:49:41 +01:00
parent 62bd7cb14b
commit e04de32c56
3 changed files with 115 additions and 50 deletions

View File

@@ -159,13 +159,13 @@ class User
/** List of users kids */ /** List of users kids */
private $kids = null; private $kids = null;
public function kidIds(bool $forceLoading = false) // public function kidIds(bool $forceLoading = false)
{ // {
if (is_null($this->kidIds) || $forceLoading) { // if (is_null($this->kidIds) || $forceLoading) {
$this->kidIds = self::getKidIds($id); // $this->kidIds = self::getKidIds($id);
} // }
return self::$kidIds; // return self::$kidIds;
} // }
public function kids(bool $forceLoading = false) public function kids(bool $forceLoading = false)
{ {
@@ -175,26 +175,26 @@ class User
return $this->kids; return $this->kids;
} }
private static function getKidIds(int $id) // private static function getKidIds(int $id)
{ // {
$response = dbConnector::query( // $response = dbConnector::query(
'SELECT * FROM `wkParticipo_Users` WHERE `' . $name . '` = :' . $name, // 'SELECT * FROM `wkParticipo_Users` WHERE `' . $name . '` = :' . $name,
[$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]] // [$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]]
); // );
$query = <<<SQL // $query = <<<SQL
SELECT * // SELECT *
FROM `wkParticipo_Users` // FROM `wkParticipo_Users`
JOIN `vormundschaft` // JOIN `vormundschaft`
ON `wkParticipo_Users`.`id` = `vormundschaft`.`kidId` // ON `wkParticipo_Users`.`id` = `vormundschaft`.`kidId`
WHERE `vormundschaft`.`userId` = :userId; // WHERE `vormundschaft`.`userId` = :userId;
SQL; // SQL;
$params = [ // $params = [
':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT] // ':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]
]; // ];
$response = dbConnector::query($query, $params); // $response = dbConnector::query($query, $params);
return $response; // return $response;
} // }
/** Export the User data into an associative array /** Export the User data into an associative array
* *
@@ -248,9 +248,12 @@ SQL;
public function getConfig() public function getConfig()
{ {
return $this->$config; return $this->config;
} }
public function getDateOfBirth(){
return $this->dateOfBirth;
}
public function getStrBirthday(){ public function getStrBirthday(){
return $this->dateOfBirth->format("Y-m-d"); return $this->dateOfBirth->format("Y-m-d");
} }

View File

@@ -2,9 +2,6 @@
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge"); setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
set_include_path(get_include_path() . PATH_SEPARATOR . "./lib/"); set_include_path(get_include_path() . PATH_SEPARATOR . "./lib/");
// start a session
session_start();
// Configs // Configs
require_once("config/participo.php"); require_once("config/participo.php");
require_once $config['basePath'] . "/config/cwsvJudo.config.php"; require_once $config['basePath'] . "/config/cwsvJudo.config.php";
@@ -19,6 +16,7 @@ $pmelo = new PmElo();
class PmElo{ class PmElo{
function __construct() function __construct()
{ {
// $this->var_log($_SESSION, "session");
// starting/continuing a session // starting/continuing a session
session_start(); session_start();
@@ -27,15 +25,16 @@ class PmElo{
$_SESSION['pmelo'] = []; $_SESSION['pmelo'] = [];
} }
// - data storage for id-s of chosen fighters // - data storage for id-s of chosen fighters
if(!array_key_exists('fighterIds', $_SESSION)){ if(!array_key_exists('fighterIds', $_SESSION['pmelo'])){
$_SESSION['pmelo']['fighterIds'] = null; $_SESSION['pmelo']['fighterIds'] = [];
} }
// check for action in post data // check for action in post data
if(array_key_exists('pmelo', $_POST)){ if(array_key_exists('pmelo', $_POST)){
if(array_key_exists('fighterIds', $_POST['pmelo'])){ if(array_key_exists('fighterIds', $_POST['pmelo'])){
$this->fighters = array_map('User::loadFromDb', $_POST['pmelo']['fighterIds']); $_SESSION['pmelo']['fighterIds']=$_POST['pmelo']['fighterIds'];
} }
// $this->var_log($_SESSION, "session");
} }
// load/recreate ranking data // load/recreate ranking data
@@ -47,8 +46,22 @@ class PmElo{
// init members // init members
$this->trainees = self::getTrainees(); $this->trainees = self::getTrainees();
$this->var_log(array_map(function($user){return $user->getFirstName();},$this->trainees), "trainees from db"); $this->var_log(array_map(function($user){return $user->getFirstName();},$this->trainees), "trainees from db");
$this->rankings = self::createRankings(); $this->rankings = $this->createRankings();
$this->var_log(array_map(function($id){return $this->trainees[$id]->getFirstName();},$this->rankings), "current ranking");
$this->fighters = [];
foreach($_SESSION['pmelo']['fighterIds'] as $id){
$this->fighters[$id] = $this->trainees[$id];
};
$this->var_log(array_map(function($user){return $user->getFirstName();},$this->fighters), "fighters");
// save the updated ranking
// @todo should be in destructor
self::saveRankings($this->rankings);
}
function __destruct(){
// sad, the destructor is not allowed to use file_put_contents
} }
// load all active trainees into a id=>User assoc array // load all active trainees into a id=>User assoc array
@@ -71,6 +84,7 @@ class PmElo{
file_put_contents(self::$pmeloJson, json_encode($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'){ function var_log($variable, string $prefix=null, string $logChannel='info'){
if(!in_array($logChannel, ['info', 'warning', 'error'])){ if(!in_array($logChannel, ['info', 'warning', 'error'])){
$logChannel='info'; $logChannel='info';
@@ -79,22 +93,27 @@ class PmElo{
$this->log[$logChannel][]=$prefix.var_export($variable, true); $this->log[$logChannel][]=$prefix.var_export($variable, true);
} }
// create the ranking for the current session (load saved ranking from file, remove old trainees, add new trainees)
public function createRankings(){ public function createRankings(){
// load the last state of the ranking // load the last state of the ranking
$loadedRanking = self::loadRankings(); $loadedRanking = self::loadRankings();
$this->var_log($loadedRanking,"ranking"); $this->var_log(array_map(function($id){$user=$this->trainees[$id]; return $user->getFirstName();}, $loadedRanking), "loaded ranking");
// check if the ranked trainees still are in training // check if the ranked trainees still are in training
// - the ranking with
$cleanRanking = []; $cleanRanking = [];
// - trainees that aren't currently in the ranking for later inserting
$toBeInsertedTrainees = $this->trainees; $toBeInsertedTrainees = $this->trainees;
foreach($loadedRanking as $rank=>$id){ foreach($loadedRanking as $id){
if(array_key_exists($id, $this->trainees)){ if(array_key_exists($id, $this->trainees)){
// userId is still in training, so append it to the cleaned up ranking
$cleanRanking[] = $id; $cleanRanking[] = $id;
// id of the not in the ranking, we have to insert // trainees already in the ranking we won't have to insert manual
unset($toBeInsertedTrainees[$id]); unset($toBeInsertedTrainees[$id]);
} }
} }
// get the ids of the to be inserted trainees sorted by the birthday
$newTraineesIds = array_map(function($u){return $u->getId();},$toBeInsertedTrainees); $newTraineesIds = array_map(function($u){return $u->getId();},$toBeInsertedTrainees);
usort( usort(
$newTraineesIds $newTraineesIds
@@ -102,16 +121,52 @@ class PmElo{
return $this->trainees[$lhs]->getStrBirthday() < $this->trainees[$rhs]->getStrBirthday() ? -1 : ($this->trainees[$lhs]->getStrBirthday() > $this->trainees[$rhs]->getStrBirthday() ? 1 : 0); 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(
$this->var_log($newTraineesIds, "newTrainieesIdsByAge"); // array_map(
$this->var_log(array_map(function($user){return $user->getFirstName();}, $toBeInsertedTrainees), "newTrainees"); // function($id){return $this->trainees[$id]->getStrBirthday()." ".$this->trainees[$id]->getFirstName();}
// , $newTraineesIds
// )
// , "newTraineesBy B-Day"
// );
// insert new trainees in the ranking // now we do a mergesort (step)
// foreach($this->trainees as $trainee){ $updatedRanking = [];
// if(empty($this->rankings)){ $idxNewTrainees = count($newTraineesIds)-1;
$idxRanking = count($cleanRanking)-1;
// } // - while we can merge, we merge
// } while( ($idxRanking >= 0) && ($idxNewTrainees >= 0) ){
// $this->var_log( [$idxRanking.", ".$idxNewTrainees=>$updatedRanking] );
// $this->var_log(
// [$this->trainees[$cleanRanking[$idxRanking]]->getStrBirthday(), $this->trainees[$newTraineesIds[$idxRanking]]->getStrBirthday()]
// );
if(
$this->trainees[$cleanRanking[$idxRanking]]->getDateOfBirth()
> $this->trainees[$newTraineesIds[$idxNewTrainees]]->getDateOfBirth()
){
// $this->var_log( $this->trainees[$cleanRanking[$idxRanking]]->getFirstName(), "merge ranking insert" );
array_unshift($updatedRanking, $cleanRanking[$idxRanking]);
$idxRanking-=1;
}else{
// $this->var_log( $this->trainees[$newTraineesIds[$idxNewTrainees]]->getFirstName(), "merge trainees insert" );
array_unshift($updatedRanking, $newTraineesIds[$idxNewTrainees]);
$idxNewTrainees-=1;
}
}
// $this->var_log(
// ["idxR ".$idxRanking." idxNT ".$idxNewTrainees=>[$cleanRanking, $newTraineesIds]]
// , "after merge"
// );
while( $idxRanking >= 0 ){
// $this->var_log( $this->trainees[$cleanRanking[$idxRanking]]->getFirstName(), "rest ranking insert" );
array_unshift($updatedRanking, $cleanRanking[$idxRanking]);
$idxRanking-=1;
}
while( $idxNewTrainees >= 0 ){
// $this->var_log( $this->trainees[$newTraineesIds[$idxNewTrainees]]->getFirstName(), "rest newTrainees insert" );
array_unshift($updatedRanking, $newTraineesIds[$idxNewTrainees]);
$idxNewTrainees-=1;
}
return $updatedRanking;
} }
public $log = ['error'=>[], 'warning'=>[], 'info'=>[]]; public $log = ['error'=>[], 'warning'=>[], 'info'=>[]];
@@ -120,5 +175,5 @@ class PmElo{
// list of id-s in the order of the current ranking // list of id-s in the order of the current ranking
public $rankings = null; public $rankings = null;
static $pmeloJson="pmeloRanking.json"; static $pmeloJson="./pmeloRanking.json";
} }

View File

@@ -59,8 +59,15 @@
<tr><th>Platz</th><th>Name</th><th>Aufstieg</th></tr> <tr><th>Platz</th><th>Name</th><th>Aufstieg</th></tr>
</thead> </thead>
<tbody> <tbody>
<?php if(!empty($pmelo->fighters)) foreach($pmelo->fighters as $fighter){ <?php
echo("<tr><td>#</td><td>".$fighter->getFirstname()." ".$fighter->getName()."</td><td>^</td></tr>".PHP_EOL); if(!empty($pmelo->fighters)){
foreach($pmelo->rankings as $rank=>$id){
if(array_key_exists($id, $pmelo->fighters)){
$fighter = $pmelo->fighters[$id];
// echo("<tr><td>".$rank."</td><td>".$fighter->getFirstName()."</td></tr>".PHP_EOL);
echo("<tr><td>".$rank."</td><td>".$fighter->getFirstName()." ".$fighter->getName()."</td><td>^</td></tr>".PHP_EOL);
}
}
} }
?> ?>
</tbody> </tbody>