Files
Thomas 655347dbf5 File Update
indentations
2025-10-06 21:16:06 +02:00

129 lines
4.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once __DIR__ . '/guard.php';
require_login();
$db = new PDO('sqlite:' . dirname(__DIR__) . '/data/app.db');
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Åbn nyt bet
if (isset($_POST['open'])) {
$o1 = trim($_POST['opt1'] ?? '');
$o2 = trim($_POST['opt2'] ?? '');
if ($o1 && $o2) {
$st = $db->prepare('INSERT INTO bets (status, option1, option2, created_ts) VALUES (?, ?, ?, ?)');
$st->execute(['open', $o1, $o2, gmdate('c')]);
$msg = 'Bet åbnet.';
}
}
// Luk indskud
if (isset($_POST['close'])) {
$db->exec("UPDATE bets SET status='closed', close_ts='" . gmdate('c') . "' WHERE status='open'");
$msg = 'Bet lukket.';
}
// Afslut og udbetal
if (isset($_POST['resolve'])) {
$win = (int)($_POST['winner'] ?? 1);
$win = ($win === 2) ? 2 : 1; // clamp til 1/2
$last = $db->query("SELECT * FROM bets WHERE status='closed' ORDER BY id DESC LIMIT 1")
->fetch(PDO::FETCH_ASSOC);
if ($last) {
// Find vindere
$st = $db->prepare('SELECT user_login, amount FROM bet_entries WHERE bet_id = ? AND option = ?');
$st->execute([$last['id'], $win]);
$wins = $st->fetchAll(PDO::FETCH_ASSOC);
// Udbetal 2x indsats (UPSERT på points-tabellen)
foreach ($wins as $w) {
$db->prepare(
'INSERT INTO points (user_login, display_name, points)
VALUES (?, ?, ?)
ON CONFLICT(user_login) DO UPDATE SET points = points + excluded.points'
)->execute([$w['user_login'], $w['user_login'], (int)$w['amount'] * 2]);
}
// Marker bet som resolved (STRINGSKONSTANT med enkeltanførselstegn)
$db->prepare("UPDATE bets SET status='resolved', resolved_option=? WHERE id=?")
->execute([$win, $last['id']]);
$msg = 'Bet resolved, vinder: option ' . $win;
}
}
}
$last = $db->query('SELECT * FROM bets ORDER BY id DESC LIMIT 1')->fetch(PDO::FETCH_ASSOC);
$entries = $last ? $db->query('SELECT * FROM bet_entries WHERE bet_id=' . (int)$last['id'])->fetchAll(PDO::FETCH_ASSOC) : [];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bets</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrap">
<div class="card">
<h2>💸 Bets</h2>
<p><a class="btn" href="index.php">← Tilbage</a></p>
<?php if ($msg): ?>
<p class="notice"><?php echo htmlspecialchars($msg); ?></p>
<?php endif; ?>
<form method="post" style="margin-bottom:1rem">
<label>
Option 1
<input name="opt1" placeholder="Team A">
</label>
<br>
<label>
Option 2
<input name="opt2" placeholder="Team B">
</label>
<br>
<button class="btn" name="open" value="1">Åbn nyt bet</button>
<button class="btn" name="close" value="1">Luk indskud</button>
<label>
Vinder (1/2)
<input name="winner" value="1">
</label>
<button class="btn" name="resolve" value="1">Afslut & udbetal</button>
</form>
<?php if ($last): ?>
<p>
Status:
<span class="badge"><?php echo htmlspecialchars($last['status']); ?></span>
#<?php echo (int)$last['id']; ?>
(<?php echo htmlspecialchars($last['option1'] . ' vs ' . $last['option2']); ?>)
</p>
<h3>Indskud</h3>
<table>
<tr>
<th>Bruger</th>
<th>Beløb</th>
<th>Option</th>
</tr>
<?php foreach ($entries as $e): ?>
<tr>
<td>@<?php echo htmlspecialchars($e['user_login']); ?></td>
<td><?php echo (int)$e['amount']; ?></td>
<td><?php echo (int)$e['option']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
</div>
</body>
</html>