-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddplayer.php
More file actions
126 lines (104 loc) · 3.61 KB
/
Copy pathaddplayer.php
File metadata and controls
126 lines (104 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
require_once __DIR__ . '/lib/config.php';
require_login();
$error = $_SESSION['error'] ?? null;
unset($_SESSION['error']); // Clear error after displaying it
// Submit.
if (count($_POST) > 0) {
// Validation.
if ($_POST['name'] == '') {
$_SESSION['error'] = 'Ohne Namen geht nix, oida!';
$valid = false;
} else {
$valid = true;
}
// Get params.
$name = $_POST['name'];
$bg = $_POST['bg'];
$color = $_POST['color'];
$contrast = contrast_ratio($bg, $color);
// Hallo Emu.
if ($contrast < 2) {
$_SESSION['error'] = 'Des kann ma ned lesen, oida!';
$valid = false;
}
// Check if name is too long.
if (strlen($name) > 14) {
$_SESSION['error'] = 'Nur Name, koan Roman, oida!';
$valid = false;
}
// Stop if not valid.
if (!$valid) {
header("Location: addplayer.php");
exit();
}
// Check if name already exists.
if ($dupe = $DB->get('players', '*', ['name' => $name])) {
$error = 'Dein Namen gibts scho, oida!';
// Temporary solution to change player color.
$DB->update("players", ['bg' => $bg, 'color' => $color], ['name' => $name]);
header("Location: players.php?time=session");
exit();
}
// Start transaction.
if ($valid) {
$DB->pdo->beginTransaction();
$player = [
'name' => $name,
'bg' => $bg,
'color' => $color,
'elo' => 1000, // Default ELO.
's_elo' => 1000, // Default ELO.
'joined' => time(),
];
$DB->insert("players", $player);
$DB->pdo->commit();
// Redirect.
header("Location: players.php");
exit();
}
}
// Create random background color.
$bgcolor = '#' . str_pad(dechex(mt_rand(0x0, 0x7FFFFF)), 6, '0', STR_PAD_LEFT);
?>
<body class="modal-page">
<h1>Add Player</h1>
<?php if (!empty($error)) echo '<p class="error">' . $error . '</p>'; ?>
<form method="post">
<div class="form inputform">
<div class="form-element">
<label>Name</label>
<input id="player-name-input" name="name"></input>
</div>
<div class="form-element color-picker-container">
<label>Colors</label>
<div class="color-picker">
<input type="color" class="color-wheel" name="color" id="color-wheel" value="#ffffff">
<input type="color" class="color-wheel" name="bg" id="bg-wheel" value="<?php echo $bgcolor ?>">
<div class="preview player-name" style="background-color: #7f7f7f;">Player</div>
</div>
</div>
<div class="footer">
<button class="button xl" type="submit">OK</button>
<a href="./players.php?time=session" class="button xl button-secondary">Cancel</a>
</div>
</div>
</form>
</body>
</html>
<!-- Color wheel, thx deepseek -->
<script>
const colorWheel = document.getElementById('color-wheel');
const bgWheel = document.getElementById('bg-wheel');
const preview = document.querySelector('.preview');
const nameInput = document.querySelector('#player-name-input');
function updatePreview() {
preview.style.color = colorWheel.value;
preview.style.backgroundColor = bgWheel.value;
preview.textContent = nameInput.value || 'Player';
}
colorWheel.addEventListener('input', updatePreview);
bgWheel.addEventListener('input', updatePreview);
nameInput.addEventListener('input', updatePreview);
updatePreview()
</script>