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() function __construct()
{ {
// $this->var_log($_SESSION, "session");
// starting/continuing a session // starting/continuing a session
session_start(); session_start();
@@ -39,29 +38,11 @@ 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->rankings = $this->createRankings(); $this->rankings = $this->createRankings();
$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"
);
// @todo Can be split. Some actions need the members to be already filled, some could be done earlier. // @todo Can be split. Some actions need the members to be already filled, some could be done earlier.
// check for action in post data // check for action in post data
@@ -72,10 +53,10 @@ class PmElo
$_SESSION["pmelo"]["fighterIds"] = $_SESSION["pmelo"]["fighterIds"] =
$_POST["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"]; $promote = $_POST["pmelo"]["promote"];
$this->promote( $this->promote(
$promote["promoteeId"], $promote["promoteeId"],
@@ -84,10 +65,9 @@ class PmElo
$promote["degradeeRank"] $promote["degradeeRank"]
); );
header("Location: ."); header("Location: pmelo");
} }
// $this->var_log($_SESSION, "session");
} }
// save the updated ranking // save the updated ranking
@@ -144,13 +124,6 @@ class PmElo
{ {
// 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"
);
// check if the ranked trainees still are in training // check if the ranked trainees still are in training
// - the ranking with // - the ranking with
@@ -179,13 +152,6 @@ class PmElo
? 1 ? 1
: 0); : 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) // now we do a mergesort (step)
$updatedRanking = []; $updatedRanking = [];
@@ -193,21 +159,15 @@ class PmElo
$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(
// [$this->trainees[$cleanRanking[$idxRanking]]->getStrBirthday(), $this->trainees[$newTraineesIds[$idxRanking]]->getStrBirthday()]
// );
if ( if (
$this->trainees[$cleanRanking[$idxRanking]]->getDateOfBirth() > $this->trainees[$cleanRanking[$idxRanking]]->getDateOfBirth() >
$this->trainees[ $this->trainees[
$newTraineesIds[$idxNewTrainees] $newTraineesIds[$idxNewTrainees]
]->getDateOfBirth() ]->getDateOfBirth()
) { ) {
// $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" );
array_unshift( array_unshift(
$updatedRanking, $updatedRanking,
$newTraineesIds[$idxNewTrainees] $newTraineesIds[$idxNewTrainees]
@@ -215,17 +175,11 @@ class PmElo
$idxNewTrainees -= 1; $idxNewTrainees -= 1;
} }
} }
// $this->var_log(
// ["idxR ".$idxRanking." idxNT ".$idxNewTrainees=>[$cleanRanking, $newTraineesIds]]
// , "after merge"
// );
while ($idxRanking >= 0) { while ($idxRanking >= 0) {
// $this->var_log( $this->trainees[$cleanRanking[$idxRanking]]->getFirstName(), "rest ranking insert" );
array_unshift($updatedRanking, $cleanRanking[$idxRanking]); array_unshift($updatedRanking, $cleanRanking[$idxRanking]);
$idxRanking -= 1; $idxRanking -= 1;
} }
while ($idxNewTrainees >= 0) { while ($idxNewTrainees >= 0) {
// $this->var_log( $this->trainees[$newTraineesIds[$idxNewTrainees]]->getFirstName(), "rest newTrainees insert" );
array_unshift($updatedRanking, $newTraineesIds[$idxNewTrainees]); array_unshift($updatedRanking, $newTraineesIds[$idxNewTrainees]);
$idxNewTrainees -= 1; $idxNewTrainees -= 1;
} }
@@ -259,20 +213,28 @@ class PmElo
$this->rankings[$promoteeRank] = $degradeeId; $this->rankings[$promoteeRank] = $degradeeId;
$this->rankings[$degradeeRank] = $promoteeId; $this->rankings[$degradeeRank] = $promoteeId;
$this->saveRankings(); $this->saveRankings($this->rankings);
} }
} }
} }
// save the updated ranking // save the updated ranking
$this->saveRankings($this->rankings);
} }
// promotion means switching the position with another user // promotion means switching the position with another user
// id-s would already be sufficient, rank is just for controlling // id-s would already be sufficient, rank is just for controlling
function htmlPromoteButton( public static function htmlPromoteButton(
int $promoteeId, int $promoteeId,
int $promoteeRank, int $promoteeRank,
int $degradeeId, int $degradeeId,
int $degradeeRank 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" => []]; 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> <!DOCTYPE html>
<html> <html>
<head> <head>
@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- shared imports (common css, MaterializeCss) --> <!-- shared imports (common css, MaterializeCss) -->
<?php readfile('shared/imports.php'); ?> <?php readfile("shared/imports.php"); ?>
<!-- inits for the materializeCss --> <!-- inits for the materializeCss -->
<script src="index.js"></script> <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" /> <img alt="cwsvJudoApps" style="max-width:100%;height:12vh;" class="responsive-img" src="http://cwsvjudo.bplaced.net/ressourcen/graphiken/logos/cwsvJudoLogoWappen.x256.png" />
</a> </a>
</li> </li>
<?php require_once 'sidenav/loginStatus.php'; ?><!-- brings its own li --> <?php require_once "sidenav/loginStatus.php"; ?><!-- brings its own li -->
</ul> </ul>
</header> </header>
@@ -43,11 +43,16 @@
<div class="input-field col s12"> <div class="input-field col s12">
<select name ="pmelo[fighterIds][]" id="pmelo-form-fighters-select" multiple> <select name ="pmelo[fighterIds][]" id="pmelo-form-fighters-select" multiple>
<option value="" disabled selected>Kämpfer auswählen</option> <option value="" disabled selected>Kämpfer auswählen</option>
<?php <?php foreach ($pmelo->trainees as $trainee) {
foreach($pmelo->trainees as $trainee){ echo "<option value=\"" .
echo("<option value=\"".$trainee->getId()."\">".$trainee->getFirstname()." ".$trainee->getName()."</option>".PHP_EOL); $trainee->getId() .
} "\">" .
?> $trainee->getFirstname() .
" " .
$trainee->getName() .
"</option>" .
PHP_EOL;
} ?>
</select> </select>
<label for="form-select-2">Kämpfer auswählen</label> <label for="form-select-2">Kämpfer auswählen</label>
<input type="submit" value="Liste erstellen"> <input type="submit" value="Liste erstellen">
@@ -59,46 +64,57 @@
<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 <?php if (!empty($pmelo->fighters)) {
if(!empty($pmelo->fighters)){ // remember the predecessor for the promoting form
foreach($pmelo->rankings as $rank=>$id){ $lastOne = null;
if(array_key_exists($id, $pmelo->fighters)){ foreach ($pmelo->rankings as $rank => $id) {
$fighter = $pmelo->fighters[$id]; if (array_key_exists($id, $pmelo->fighters)) {
// echo("<tr><td>".$rank."</td><td>".$fighter->getFirstName()."</td></tr>".PHP_EOL); $fighter = $pmelo->fighters[$id];
echo("<tr><td>".$rank."</td><td>".$fighter->getFirstName()." ".$fighter->getName()."</td><td>^</td></tr>".PHP_EOL); 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> </tbody>
</table> </table>
<ul class="collection with-header"> <ul class="collection with-header">
<li class="collection-header"><h3>Logs</h3></li> <li class="collection-header"><h3>Logs</h3></li>
<?php <?php foreach ($pmelo->log as $logLevel => $messages) {
foreach($pmelo->log as $logLevel=>$messages){ if (!empty($messages)) { ?>
if(!empty($messages)){
?>
<li class="collection-item"> <li class="collection-item">
<ul class="collection with-header"> <ul class="collection with-header">
<li class="collection-header"><?php echo($logLevel);?></li> <li class="collection-header"><?php echo $logLevel; ?></li>
<?php <?php foreach ($messages as $message) { ?>
foreach($messages as $message){
?>
<li> <li>
<?php echo($message); ?> <?php echo $message; ?>
</li> </li>
<?php <?php } ?>
}
?>
</ul> </ul>
</li> </li>
<?php } ?>
<?php <?php
} } ?>
?>
<?php
}
?>
</ul> </ul>
</main> </main>
</body> </body>