prepare( 'INSERT INTO activity_settings (check_id, points_present, deadline_at) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE points_present = VALUES(points_present), deadline_at = VALUES(deadline_at)' )->execute([$checkId, $points, date('Y-m-d H:i:s', $deadline)]); } /** @return array{points_present: int, processed_at: ?string, result: ?string}|null */ function activity_points_get(int $checkId): ?array { try { $stmt = db()->prepare('SELECT points_present, processed_at, result FROM activity_settings WHERE check_id = ?'); $stmt->execute([$checkId]); $row = $stmt->fetch(); } catch (PDOException $e) { return null; } return $row ? ['points_present' => (int) $row['points_present'], 'processed_at' => $row['processed_at'], 'result' => $row['result']] : null; } /** * Offene Punkte vergeben: alle Checks, deren Frist vorbei ist (oder den einen, der gerade auf der * Website beendet wurde), beim Bot nachsehen und bei "abgeschlossen" die Punkte gutschreiben. * * @param int|null $checkId nur diesen Check (sofort, ohne auf die Frist zu warten) * @param array|null $detail schon geladene Daten des Checks (spart die Abfrage beim Bot) * @return string[] Meldungen je verarbeitetem Check */ function activity_process_points(?int $checkId = null, ?array $detail = null): array { try { $pending = $checkId !== null ? db()->prepare('SELECT check_id, points_present FROM activity_settings WHERE processed_at IS NULL AND check_id = ?') : db()->prepare('SELECT check_id, points_present FROM activity_settings WHERE processed_at IS NULL AND deadline_at <= NOW()'); $pending->execute($checkId !== null ? [$checkId] : []); $rows = $pending->fetchAll(); } catch (PDOException $e) { return []; // Tabelle kommt erst mit dem naechsten Schema-Abgleich } $messages = []; foreach ($rows as $row) { $id = (int) $row['check_id']; try { $check = ($detail !== null && (int) ($detail['check_id'] ?? 0) === $id) ? $detail : (activity_api_request('GET', '/' . $id)['check'] ?? null); } catch (Throwable $e) { continue; // Bot nicht erreichbar - beim naechsten Aufruf erneut } $message = activity_award_points($id, (int) $row['points_present'], $check); if ($message !== '') { $messages[] = $message; } } return $messages; } /** Punkte fuer einen Check gutschreiben (einmalig, per Zeilensperre gegen Doppelvergabe). */ function activity_award_points(int $checkId, int $points, ?array $check): string { $status = (string) ($check['status'] ?? ''); if ($check !== null && $status === 'open') { return ''; // laeuft noch (Frist gerade erst vorbei, der Bot schliesst gleich) } $pdo = db(); $pdo->beginTransaction(); $lock = $pdo->prepare('SELECT processed_at FROM activity_settings WHERE check_id = ? FOR UPDATE'); $lock->execute([$checkId]); if ($lock->fetchColumn() !== null) { $pdo->commit(); return ''; } $finish = static function (string $result) use ($pdo, $checkId): string { $pdo->prepare('UPDATE activity_settings SET processed_at = NOW(), result = ? WHERE check_id = ?') ->execute([mb_substr($result, 0, 255), $checkId]); $pdo->commit(); return $result; }; if ($check === null) { return $finish('Keine Punkte: Der Check existiert beim Bot nicht mehr.'); } $title = (string) ($check['title'] ?? 'Aktivitätscheck'); if ($status === 'cancelled') { return $finish('Keine Punkte für „' . $title . '“: Der Check wurde abgebrochen.'); } // Wer "Ich bin da" gewaehlt hat und ein Website-Konto mit Discord-Verknuepfung besitzt $discordIds = []; foreach ($check['responses'] ?? [] as $response) { if (($response['status'] ?? '') === 'present' && !empty($response['user_id'])) { $discordIds[] = (string) $response['user_id']; } } $userIds = []; if ($discordIds) { $stmt = $pdo->prepare("SELECT id FROM users WHERE status = 'active' AND discord_id IN (" . implode(',', array_fill(0, count($discordIds), '?')) . ')'); $stmt->execute(array_values(array_unique($discordIds))); $userIds = array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN)); } if (!$userIds) { return $finish('Keine Punkte für „' . $title . '“: niemand mit „Ich bin da“ und Website-Konto.'); } $closedAt = (int) ($check['closed_at'] ?? 0); $pdo->prepare( "INSERT INTO contribution_awards (category, title, note, awarded_at, created_by) VALUES ('activity', ?, ?, ?, NULL)" )->execute([ mb_substr($title, 0, 150), 'Automatisch für „Ich bin da“ beim Aktivitätscheck', date('Y-m-d H:i:s', $closedAt > 0 ? $closedAt : time()), ]); $awardId = (int) $pdo->lastInsertId(); $insert = $pdo->prepare('INSERT INTO contribution_entries (award_id, user_id, points) VALUES (?, ?, ?)'); foreach ($userIds as $userId) { $insert->execute([$awardId, $userId, $points]); } return $finish('Lumipunkte für „' . $title . '“ vergeben: ' . count($userIds) . '× +' . $points . '.'); }