pmelo working version

This commit is contained in:
marko
2024-02-03 20:08:36 +01:00
parent 7dac619cf1
commit da2ccb3ba3
2 changed files with 65 additions and 87 deletions

View File

@@ -17,7 +17,6 @@ class PmElo
{
function __construct()
{
// $this->var_log($_SESSION, "session");
// starting/continuing a session
session_start();
@@ -39,29 +38,11 @@ class PmElo
// init members
$this->trainees = self::getTrainees();
$this->var_log(
array_map(function ($user) {
return $user->getFirstName();
}, $this->trainees),
"trainees from db"
);
$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"
);
// @todo Can be split. Some actions need the members to be already filled, some could be done earlier.
// check for action in post data
@@ -72,10 +53,10 @@ class PmElo
$_SESSION["pmelo"]["fighterIds"] =
$_POST["pmelo"]["fighterIds"];
header("Location: .");
header("Location: pmelo");
}
if (array_key_exist("promote", $_POST["pmelo"])) {
if (array_key_exists("promote", $_POST["pmelo"])) {
$promote = $_POST["pmelo"]["promote"];
$this->promote(
$promote["promoteeId"],
@@ -84,10 +65,9 @@ class PmElo
$promote["degradeeRank"]
);
header("Location: .");
header("Location: pmelo");
}
// $this->var_log($_SESSION, "session");
}
// save the updated ranking
@@ -144,13 +124,6 @@ class PmElo
{
// load the last state of the ranking
$loadedRanking = self::loadRankings();
$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
// - the ranking with
@@ -179,13 +152,6 @@ class PmElo
? 1
: 0);
});
// $this->var_log(
// array_map(
// function($id){return $this->trainees[$id]->getStrBirthday()." ".$this->trainees[$id]->getFirstName();}
// , $newTraineesIds
// )
// , "newTraineesBy B-Day"
// );
// now we do a mergesort (step)
$updatedRanking = [];
@@ -193,21 +159,15 @@ class PmElo
$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]
@@ -215,17 +175,11 @@ class PmElo
$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;
}
@@ -259,20 +213,28 @@ class PmElo
$this->rankings[$promoteeRank] = $degradeeId;
$this->rankings[$degradeeRank] = $promoteeId;
$this->saveRankings();
$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
function htmlPromoteButton(
public static function htmlPromoteButton(
int $promoteeId,
int $promoteeRank,
int $degradeeId,
int $degradeeRank
) {
return "<form action=\"pmelo\" method=\"post\">" .
"<input type= \"hidden\" name=\"pmelo[promote][promoteeId]\" value=\"" . $promoteeId . "\" />" .
"<input type= \"hidden\" name=\"pmelo[promote][promoteeRank]\" value=\"" . $promoteeRank . "\" />" .
"<input type= \"hidden\" name=\"pmelo[promote][degradeeId]\" value=\"" . $degradeeId . "\" />" .
"<input type= \"hidden\" name=\"pmelo[promote][degradeeRank]\" value=\"" . $degradeeRank . "\">" .
"<input class=\"btn\" type=\"submit\" value=\"^\">".
"</form>";
}
public $log = ["error" => [], "warning" => [], "info" => []];

View File

@@ -1,4 +1,4 @@
<?php require_once("pmelo.inc.php");?>
<?php require_once "pmelo.inc.php"; ?>
<!DOCTYPE html>
<html>
<head>
@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- shared imports (common css, MaterializeCss) -->
<?php readfile('shared/imports.php'); ?>
<?php readfile("shared/imports.php"); ?>
<!-- inits for the materializeCss -->
<script src="index.js"></script>
@@ -32,7 +32,7 @@
<img alt="cwsvJudoApps" style="max-width:100%;height:12vh;" class="responsive-img" src="http://cwsvjudo.bplaced.net/ressourcen/graphiken/logos/cwsvJudoLogoWappen.x256.png" />
</a>
</li>
<?php require_once 'sidenav/loginStatus.php'; ?><!-- brings its own li -->
<?php require_once "sidenav/loginStatus.php"; ?><!-- brings its own li -->
</ul>
</header>
@@ -43,11 +43,16 @@
<div class="input-field col s12">
<select name ="pmelo[fighterIds][]" id="pmelo-form-fighters-select" multiple>
<option value="" disabled selected>Kämpfer auswählen</option>
<?php
foreach($pmelo->trainees as $trainee){
echo("<option value=\"".$trainee->getId()."\">".$trainee->getFirstname()." ".$trainee->getName()."</option>".PHP_EOL);
}
?>
<?php foreach ($pmelo->trainees as $trainee) {
echo "<option value=\"" .
$trainee->getId() .
"\">" .
$trainee->getFirstname() .
" " .
$trainee->getName() .
"</option>" .
PHP_EOL;
} ?>
</select>
<label for="form-select-2">Kämpfer auswählen</label>
<input type="submit" value="Liste erstellen">
@@ -59,46 +64,57 @@
<tr><th>Platz</th><th>Name</th><th>Aufstieg</th></tr>
</thead>
<tbody>
<?php
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);
}
}
}
?>
<?php if (!empty($pmelo->fighters)) {
// remember the predecessor for the promoting form
$lastOne = null;
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() .
" " .
$fighter->getName() .
"</td>" .
"<td>" .
(!is_null($lastOne)
? $pmelo->htmlPromoteButton(
$fighter->getId(),
$rank,
$lastOne["id"],
$lastOne["rank"]
)
: "") .
"</td>" .
"</tr>" .
PHP_EOL;
$lastOne = ["id" => $fighter->getId(), "rank" => $rank];
}
}
} ?>
</tbody>
</table>
<ul class="collection with-header">
<li class="collection-header"><h3>Logs</h3></li>
<?php
foreach($pmelo->log as $logLevel=>$messages){
if(!empty($messages)){
?>
<?php foreach ($pmelo->log as $logLevel => $messages) {
if (!empty($messages)) { ?>
<li class="collection-item">
<ul class="collection with-header">
<li class="collection-header"><?php echo($logLevel);?></li>
<?php
foreach($messages as $message){
?>
<li class="collection-header"><?php echo $logLevel; ?></li>
<?php foreach ($messages as $message) { ?>
<li>
<?php echo($message); ?>
<?php echo $message; ?>
</li>
<?php
}
?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php
}
?>
<?php
}
?>
} ?>
</ul>
</main>
</body>