This commit is contained in:
2026-01-03 02:27:20 +01:00
parent 6d6f18c471
commit ee501b0b1d
3 changed files with 149 additions and 40 deletions

View File

@@ -1,23 +1,36 @@
<?php <?php
return [ return [
// Points per action 'actions' => [
'points' => [ 'event' => [
'event_participation' => 0.3, // war 0.1 'create' => [
'event_create' => 1.5, // war 1.0 'points' => 1.5,
'forum_question' => 0.4, // leicht runter 'caps' => ['daily' => null, 'total' => null],
'forum_answer' => 1.0, // bleibt Leitwert 'bonuses' => ['first' => 2.0],
'invite' => 0.3, // etwas entschärft
], ],
'caps' => [ 'participation' => [
'event_participation_per_day' => 1.0, 'points' => 0.3,
'forum_answer_per_day' => 5.0, 'caps' => ['daily' => 1.0, 'total' => null],
'invite_total' => 5.0, 'bonuses' => ['first' => 1.0],
],
],
'forum' => [
'question' => [
'points' => 0.4,
'caps' => ['daily' => null, 'total' => null],
'bonuses' => [],
],
'answer' => [
'points' => 1.0,
'caps' => ['daily' => 5.0, 'total' => null],
'bonuses' => ['first_helpful_5' => 2.0],
],
],
'invite' => [
'points' => 0.3,
'caps' => ['daily' => null, 'total' => 5.0],
'bonuses' => [],
], ],
'bonuses' => [
'first_event_join' => 1.0,
'first_event_create' => 2.0,
'first_helpful_5' => 2.0,
], ],
// Levels with thresholds and optional icons // Levels with thresholds and optional icons
'levels' => [ 'levels' => [

View File

@@ -122,6 +122,26 @@ CREATE TABLE forum_posts (
INDEX idx_fp_thread (thread_id) INDEX idx_fp_thread (thread_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Punkte-Tracking (persistent, config-Änderungen wirken nur auf neue Einträge)
CREATE TABLE user_points (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
action VARCHAR(64) NOT NULL,
amount DECIMAL(10,2) NOT NULL,
meta JSON NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_up_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX idx_up_user_action (user_id, action),
INDEX idx_up_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE user_points_totals (
user_id BIGINT UNSIGNED PRIMARY KEY,
total DECIMAL(12,2) NOT NULL DEFAULT 0,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_upt_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Session-Handling (neutral, keine sensiblen Inhalte) -- Session-Handling (neutral, keine sensiblen Inhalte)
CREATE TABLE sessions ( CREATE TABLE sessions (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

View File

@@ -88,18 +88,94 @@ final class Community
public function computePoints(int $userId): float public function computePoints(int $userId): float
{ {
$pointsCfg = $this->config['points'] ?? []; // Primär: aggregierte Werte aus user_points_totals, Fallback: Summe aus user_points
$eventCreated = (int)$this->pdo->query("SELECT COUNT(*) FROM events WHERE created_by = {$userId}")->fetchColumn(); $stmt = $this->pdo->prepare('SELECT total FROM user_points_totals WHERE user_id = :uid');
$eventParticipants = (float)$this->pdo->query("SELECT COUNT(*) FROM event_participants WHERE user_id = {$userId}")->fetchColumn(); $stmt->execute([':uid' => $userId]);
$threadCount = (int)$this->pdo->query("SELECT COUNT(*) FROM forum_threads WHERE user_id = {$userId}")->fetchColumn(); $total = $stmt->fetchColumn();
$answerCount = (int)$this->pdo->query("SELECT COUNT(*) FROM forum_posts WHERE user_id = {$userId}")->fetchColumn(); if ($total !== false && $total !== null) {
$invites = 0; return (float)$total;
}
return $threadCount * ($pointsCfg['forum_question'] ?? 0.5) $stmt = $this->pdo->prepare('SELECT COALESCE(SUM(amount),0) FROM user_points WHERE user_id = :uid');
+ $answerCount * ($pointsCfg['forum_answer'] ?? 1.0) $stmt->execute([':uid' => $userId]);
+ $eventCreated * ($pointsCfg['event_create'] ?? 1.0) return (float)$stmt->fetchColumn();
+ $eventParticipants * ($pointsCfg['event_participation'] ?? 0.1) }
+ $invites * ($pointsCfg['invite'] ?? 0.5);
/**
* Vergibt Punkte persistent und berücksichtigt Caps/Bonis gemäß config actions.
*/
public function addPoints(int $userId, string $group, string $key, array $meta = []): float
{
$actions = $this->config['actions'][$group][$key] ?? null;
if (!$actions || empty($actions['points'])) {
return 0.0;
}
$basePoints = (float)$actions['points'];
// Boni (einfacher first-Check)
$bonusPoints = 0.0;
if (!empty($actions['bonuses'])) {
if (isset($actions['bonuses']['first'])) {
$bonusPoints += (float)$actions['bonuses']['first'];
}
if (isset($actions['bonuses']['first_helpful_5']) && isset($meta['helpful_count']) && (int)$meta['helpful_count'] >= 5) {
$bonusPoints += (float)$actions['bonuses']['first_helpful_5'];
}
}
$amount = $basePoints + $bonusPoints;
if ($amount <= 0) {
return 0.0;
}
$caps = $actions['caps'] ?? [];
$capDaily = $caps['daily'] ?? null;
$capTotal = $caps['total'] ?? null;
$todayStart = (new \DateTimeImmutable('today'))->format('Y-m-d 00:00:00');
$todayEnd = (new \DateTimeImmutable('today'))->format('Y-m-d 23:59:59');
$actionKey = $group . '.' . $key;
if ($capDaily !== null) {
$stmt = $this->pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM user_points WHERE user_id = :uid AND action = :action AND created_at BETWEEN :s AND :e");
$stmt->execute([
':uid' => $userId,
':action' => $actionKey,
':s' => $todayStart,
':e' => $todayEnd,
]);
$usedToday = (float)$stmt->fetchColumn();
$remaining = max(0.0, (float)$capDaily - $usedToday);
if ($remaining <= 0) {
return 0.0;
}
$amount = min($amount, $remaining);
}
if ($capTotal !== null) {
$stmt = $this->pdo->prepare("SELECT COALESCE(SUM(amount),0) FROM user_points WHERE user_id = :uid AND action = :action");
$stmt->execute([':uid' => $userId, ':action' => $actionKey]);
$usedTotal = (float)$stmt->fetchColumn();
$remaining = max(0.0, (float)$capTotal - $usedTotal);
if ($remaining <= 0) {
return 0.0;
}
$amount = min($amount, $remaining);
}
$stmt = $this->pdo->prepare('INSERT INTO user_points (user_id, action, amount, meta) VALUES (:uid, :action, :amount, :meta)');
$stmt->execute([
':uid' => $userId,
':action' => $actionKey,
':amount' => $amount,
':meta' => $meta ? json_encode($meta) : null,
]);
$stmt = $this->pdo->prepare('INSERT INTO user_points_totals (user_id, total) VALUES (:uid, :amt) ON DUPLICATE KEY UPDATE total = total + VALUES(total)');
$stmt->execute([':uid' => $userId, ':amt' => $amount]);
return $amount;
} }
public function membershipLevel(float $points): array public function membershipLevel(float $points): array