Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Einfache Punktetabelle für vier Spieler zur Ansicht im Webbrowser, mit Speicher/Ladefunktion
<?php
// Punktetabelle
// made in 2017 with love - dasschaf
//
// readme:
// save to a php-able server; access the file first with ...punktetabelle.php?initialize, then with ...punktetabelle.php?setnames and afterwards access it normally and enjoy! ^_^
// during setup ignore the error messages.
//
// create helper classes
$punktetabelle = new tabelle;
$config = new configuration;
if (isset($_GET["setnames"], $_GET["name1"], $_GET["name2"], $_GET["name3"], $_GET["name4"])) {
$name1 = $_GET["name1"];
$name2 = $_GET["name2"];
$name3 = $_GET["name3"];
$name4 = $_GET["name4"];
$array = array(
"1" => $name1,
"2" => $name2,
"3" => $name3,
"4" => $name4
);
$config->set($array);
$config->save();
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = "punktetabelle.php";
header("Location: http://$host/$uri$extra");
}
// initialize the table's class
if (!isset($_GET["setnames"])) {
$config->init();
$punktetabelle->playernames = $config->playernames;
// execute table processor
$punktetabelle->executeme();
}
// check for reset
if (isset($_GET['initialize']))
{
$punktetabelle->resetpoints();
$punktetabelle->savescore();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Punktetabelle</title>
<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.1/build/pure-min.css" integrity="sha384-CCTZv2q9I9m3UOxRLaJneXrrqKwUNOzZ6NGEUMwHtShDJ+nCoiXJCAgi05KfkLGY" crossorigin="anonymous">
</head>
<body>
<br>
<div class="pure-g" <?php if(isset($_GET['setnames'])) echo 'hidden';?>>
<div class="pure-u-1-5"></div> <!-- Empty!-->
<div class="pure-u-3-5">
<table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Teilnehmer</th>
<th>Punktzahl</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
if (!isset($_GET['setnames'])) {
// create table from helper class
foreach ($punktetabelle->points as $spieler => $punktzahl)
{
$playername = $punktetabelle->playernames[$spieler];
echo "<tr>";
echo "<td>$playername</td>";
echo "<td>$punktzahl</td>";
echo "<td><a href=\"punktetabelle.php?applyscore&player=$spieler&amount=-1\"><button class=\"pure-button pure-button-primary\">-1</button></a></td>";
echo "<td><a href=\"punktetabelle.php?applyscore&player=$spieler&amount=-0.5\"><button class=\"pure-button pure-button-primary\">-0.5</button></a></td>";
echo "<td><a href=\"punktetabelle.php?applyscore&player=$spieler&amount=0.5\"><button class=\"pure-button pure-button-primary\">+0.5</button></a></td>";
echo "<td><a href=\"punktetabelle.php?applyscore&player=$spieler&amount=1\"><button class=\"pure-button pure-button-primary\">+1</button></a></td>";
echo "</tr>
";
}
} // just to avoid shit
?>
</tbody>
</table>
</div>
<div class="pure-u-1-5">
<a href="punktetabelle.php"><button class="pure-button pure-button-primary">neu laden!</button></a><br><br>
<a href="punktetabelle.php?initialize" onclick="return confirm('Ganz sicher?')"><button class="pure-button pure-button-primary">Zurücksetzen</button></a><br><br>
<a href="punktetabelle.php?setnames"><button class="pure-button pure-button-primary">Namen ändern</button></a>
</div>
</div>
<div class="pure-g" <?php if(!isset($_GET['setnames'])) echo 'hidden';?>>
<form action="punktetabelle.php" method="get" class="pure-form">
<input type="text" name="name1" placeholder="Spieler 1"><br>
<input type="text" name="name2" placeholder="Spieler 2"><br>
<input type="text" name="name3" placeholder="Spieler 3"><br>
<input type="text" name="name4" placeholder="Spieler 4"><br>
<input type="hidden" name="setnames" value=" ">
<br><br>
<button type="submit" class="pure-button pure-button-primary"><h3>Speichern!</h3></button>
</form>
</div>
</body>
</html>
<?php
// classes come here!
class configuration
{
public $playernames;
function init() // reading
{
$json = file_get_contents("punktetabelle_cfg.json");
$this->playernames = json_decode($json, true);
}
function save() // saving
{
$json = json_encode($this->playernames);
file_put_contents("punktetabelle_cfg.json", $json);
}
function set($names) // setting names
{
$this->playernames = array(
"1" => $names["1"],
"2" => $names["2"],
"3" => $names["3"],
"4" => $names["4"]
);
$this->save();
}
}
class tabelle
{
public $points;
public $playernames;
function resetpoints()
{
$this->points = array(
'1' => 0,
'2' => 0,
'3' => 0,
'4' => 0
);
}
function pointoperation($player, $amount)
{
$this->points[$player] = $this->points[$player] + $amount;
}
function executeme()
{
if (!isset($_GET['initialize'])) $this->readscore();
if (isset($_GET['applyscore']) == true && isset($_GET['player']) == true && isset($_GET['amount']) == true)
{
if ($_GET['player'] >= 1 && $_GET['player'] <= 4)
{
$this->pointoperation($_GET['player'], $_GET['amount']);
}
}
$this->savescore();
}
function getpoints(int $player)
{
return $this->points[player];
}
function getplayername(int $player)
{
return $this->playernames[player];
}
function readscore()
{
$temp = file_get_contents('points.txt');
$this->points = unserialize($temp);
}
function savescore()
{
$fp = fopen('points.txt','w');
fwrite($fp,serialize($this->points));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment