WIP: added promote function, still missing: promote form

This commit is contained in:
marko
2024-02-03 11:59:38 +01:00
parent e04de32c56
commit 7dac619cf1

View File

@@ -3,17 +3,18 @@ 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/");
// 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";
// Libs // Libs
require_once("participoLib/user.php"); require_once "participoLib/user.php";
participo::init($cwsvJudoConfig); participo::init($cwsvJudoConfig);
$pmelo = new PmElo(); $pmelo = new PmElo();
class PmElo{ class PmElo
{
function __construct() function __construct()
{ {
// $this->var_log($_SESSION, "session"); // $this->var_log($_SESSION, "session");
@@ -21,52 +22,91 @@ class PmElo{
session_start(); session_start();
// create 'pmelo' section in the sessions data // create 'pmelo' section in the sessions data
if(!array_key_exists('pmelo', $_SESSION)){ if (!array_key_exists("pmelo", $_SESSION)) {
$_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['pmelo'])){ if (!array_key_exists("fighterIds", $_SESSION["pmelo"])) {
$_SESSION['pmelo']['fighterIds'] = []; $_SESSION["pmelo"]["fighterIds"] = [];
}
// check for action in post data
if(array_key_exists('pmelo', $_POST)){
if(array_key_exists('fighterIds', $_POST['pmelo'])){
$_SESSION['pmelo']['fighterIds']=$_POST['pmelo']['fighterIds'];
}
// $this->var_log($_SESSION, "session");
} }
// load/recreate ranking data // load/recreate ranking data
if (!is_file(self::$pmeloJson)) { if (!is_file(self::$pmeloJson)) {
$this->log['info'][] = "Couldn't find `".self::$pmeloJson."`. Create a new one!"; $this->log["info"][] =
"Couldn't find `" . self::$pmeloJson . "`. Create a new one!";
file_put_contents(self::$pmeloJson, json_encode([])); file_put_contents(self::$pmeloJson, json_encode([]));
} }
// 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 = $this->createRankings(); $this->rankings = $this->createRankings();
$this->var_log(array_map(function($id){return $this->trainees[$id]->getFirstName();},$this->rankings), "current ranking"); $this->var_log(
array_map(function ($id) {
return $this->trainees[$id]->getFirstName();
}, $this->rankings),
"current ranking"
);
$this->fighters = []; $this->fighters = [];
foreach($_SESSION['pmelo']['fighterIds'] as $id){ foreach ($_SESSION["pmelo"]["fighterIds"] as $id) {
$this->fighters[$id] = $this->trainees[$id]; $this->fighters[$id] = $this->trainees[$id];
}; }
$this->var_log(array_map(function($user){return $user->getFirstName();},$this->fighters), "fighters"); $this->var_log(
array_map(function ($user) {
return $user->getFirstName();
}, $this->fighters),
"fighters"
);
// @todo Can be split. Some actions need the members to be already filled, some could be done earlier.
// check for action in post data
if (array_key_exists("pmelo", $_POST)) {
// setting the current fighters
// - meaning: fighter list is automatically updated by posted fighters
if (array_key_exists("fighterIds", $_POST["pmelo"])) {
$_SESSION["pmelo"]["fighterIds"] =
$_POST["pmelo"]["fighterIds"];
header("Location: .");
}
if (array_key_exist("promote", $_POST["pmelo"])) {
$promote = $_POST["pmelo"]["promote"];
$this->promote(
$promote["promoteeId"],
$promote["promoteeRank"],
$promote["degradeeId"],
$promote["degradeeRank"]
);
header("Location: .");
}
// $this->var_log($_SESSION, "session");
}
// save the updated ranking // save the updated ranking
// @todo should be in destructor // @todo should be in destructor
self::saveRankings($this->rankings); self::saveRankings($this->rankings);
} }
function __destruct(){ function __destruct()
{
// sad, the destructor is not allowed to use file_put_contents // 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
public static function getTrainees(){ public static function getTrainees()
$traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4)); {
$traineeList = array_map(
"User::fromDbArray",
User::dbSelectWithAttribute(4)
);
$traineeAssocArray = []; $traineeAssocArray = [];
foreach ($traineeList as $trainee) { foreach ($traineeList as $trainee) {
$traineeAssocArray[$trainee->getId()] = $trainee; $traineeAssocArray[$trainee->getId()] = $trainee;
@@ -75,29 +115,42 @@ class PmElo{
} }
// load ranking from json file // load ranking from json file
public static function loadRankings(){ public static function loadRankings()
{
return json_decode(file_get_contents(self::$pmeloJson)); return json_decode(file_get_contents(self::$pmeloJson));
} }
// save a ranking to json file // save a ranking to json file
public static function saveRankings(array $rankings){ public static function saveRankings(array $rankings)
{
file_put_contents(self::$pmeloJson, json_encode($rankings)); file_put_contents(self::$pmeloJson, json_encode($rankings));
} }
// simple logger to a logging buffer // simple logger to a logging buffer
function var_log($variable, string $prefix=null, string $logChannel='info'){ function var_log(
if(!in_array($logChannel, ['info', 'warning', 'error'])){ $variable,
$logChannel='info'; string $prefix = null,
string $logChannel = "info"
) {
if (!in_array($logChannel, ["info", "warning", "error"])) {
$logChannel = "info";
} }
$prefix = (!empty($prefix) ? $prefix.": " : ""); $prefix = !empty($prefix) ? $prefix . ": " : "";
$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) // 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(array_map(function($id){$user=$this->trainees[$id]; return $user->getFirstName();}, $loadedRanking), "loaded 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 // - the ranking with
@@ -114,13 +167,18 @@ class PmElo{
} }
// get the ids of the to be inserted trainees sorted by the birthday // 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) {
usort( return $u->getId();
$newTraineesIds }, $toBeInsertedTrainees);
, function($lhs, $rhs){ 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); return $this->trainees[$lhs]->getStrBirthday() <
} $this->trainees[$rhs]->getStrBirthday()
); ? -1
: ($this->trainees[$lhs]->getStrBirthday() >
$this->trainees[$rhs]->getStrBirthday()
? 1
: 0);
});
// $this->var_log( // $this->var_log(
// array_map( // array_map(
// function($id){return $this->trainees[$id]->getStrBirthday()." ".$this->trainees[$id]->getFirstName();} // function($id){return $this->trainees[$id]->getStrBirthday()." ".$this->trainees[$id]->getFirstName();}
@@ -134,21 +192,26 @@ class PmElo{
$idxNewTrainees = count($newTraineesIds) - 1; $idxNewTrainees = count($newTraineesIds) - 1;
$idxRanking = count($cleanRanking) - 1; $idxRanking = count($cleanRanking) - 1;
// - while we can merge, we merge // - while we can merge, we merge
while( ($idxRanking >= 0) && ($idxNewTrainees >= 0) ){ while ($idxRanking >= 0 && $idxNewTrainees >= 0) {
// $this->var_log( [$idxRanking.", ".$idxNewTrainees=>$updatedRanking] ); // $this->var_log( [$idxRanking.", ".$idxNewTrainees=>$updatedRanking] );
// $this->var_log( // $this->var_log(
// [$this->trainees[$cleanRanking[$idxRanking]]->getStrBirthday(), $this->trainees[$newTraineesIds[$idxRanking]]->getStrBirthday()] // [$this->trainees[$cleanRanking[$idxRanking]]->getStrBirthday(), $this->trainees[$newTraineesIds[$idxRanking]]->getStrBirthday()]
// ); // );
if ( if (
$this->trainees[$cleanRanking[$idxRanking]]->getDateOfBirth() $this->trainees[$cleanRanking[$idxRanking]]->getDateOfBirth() >
> $this->trainees[$newTraineesIds[$idxNewTrainees]]->getDateOfBirth() $this->trainees[
$newTraineesIds[$idxNewTrainees]
]->getDateOfBirth()
) { ) {
// $this->var_log( $this->trainees[$cleanRanking[$idxRanking]]->getFirstName(), "merge ranking insert" ); // $this->var_log( $this->trainees[$cleanRanking[$idxRanking]]->getFirstName(), "merge ranking insert" );
array_unshift($updatedRanking, $cleanRanking[$idxRanking]); array_unshift($updatedRanking, $cleanRanking[$idxRanking]);
$idxRanking -= 1; $idxRanking -= 1;
} else { } else {
// $this->var_log( $this->trainees[$newTraineesIds[$idxNewTrainees]]->getFirstName(), "merge trainees insert" ); // $this->var_log( $this->trainees[$newTraineesIds[$idxNewTrainees]]->getFirstName(), "merge trainees insert" );
array_unshift($updatedRanking, $newTraineesIds[$idxNewTrainees]); array_unshift(
$updatedRanking,
$newTraineesIds[$idxNewTrainees]
);
$idxNewTrainees -= 1; $idxNewTrainees -= 1;
} }
} }
@@ -169,7 +232,50 @@ class PmElo{
return $updatedRanking; return $updatedRanking;
} }
public $log = ['error'=>[], 'warning'=>[], 'info'=>[]]; private function promote(
int $promoteeId,
int $promoteeRank,
int $degradeeId,
int $degradeeRank
) {
// input sanitation
filterId($promoteeId);
filterId($promoteeRank);
filterId($degradeeId);
filterId($degradeeRank);
// check if both id are in the current fighter list
if (
array_key_exists($promoteeId, $this->fighters) &&
array_key_exists($degradeeId, $this->fighters)
) {
// check their current rank
if (
$this->rankings[$promoteeRank] == $promoteeId &&
$this->rankings[$degradeeRank] == $degradeeId
) {
// @todo check if they are rank neighbors in the fighter list
if (true) {
// do the actual switch
$this->rankings[$promoteeRank] = $degradeeId;
$this->rankings[$degradeeRank] = $promoteeId;
$this->saveRankings();
}
}
}
// save the updated ranking
}
// promotion means switching the position with another user
// id-s would already be sufficient, rank is just for controlling
function htmlPromoteButton(
int $promoteeId,
int $promoteeRank,
int $degradeeId,
int $degradeeRank
) {
}
public $log = ["error" => [], "warning" => [], "info" => []];
public $fighters = null; public $fighters = null;
public $trainees = null; public $trainees = null;
// list of id-s in the order of the current ranking // list of id-s in the order of the current ranking