Add donation actions (rewards): menu editor + queue, donate-form buttons, overlay menu widget, alert lines
- Actions\Matcher (exact / every), ActionRepository, Recorder (idempotent via unique key) - Admin: Rewards screen with live preview, queue with Mark done, menu badge - Donate form shows actions as payment buttons; overlay 'actions' widget; alerts list triggered actions - Installer DB_VERSION 2 (sg_actions, sg_action_events) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c62ac06b27
commit
b0b5012698
25 files changed
+1227
-7
No files matched your search
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Actions;
|
||||
|
||||
/** Pure: which actions does a donation of $baseMinor trigger, and how many times each. */
|
||||
final class Matcher {
|
||||
|
||||
public const EXACT = 'exact';
|
||||
public const EVERY = 'every';
|
||||
|
||||
/**
|
||||
* @param array[] $actions Rows with keys id, title, amount_minor, mode. Malformed rows are skipped.
|
||||
* @return array<int,array{action_id:int,title:string,times:int}> In the order of $actions.
|
||||
*/
|
||||
public static function match( int $baseMinor, array $actions ): array {
|
||||
$out = array();
|
||||
if ( $baseMinor <= 0 ) {
|
||||
return $out;
|
||||
}
|
||||
foreach ( $actions as $a ) {
|
||||
if ( ! is_array( $a ) || ! isset( $a['id'], $a['amount_minor'], $a['mode'], $a['title'] ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( ! is_numeric( $a['id'] ) || ! is_numeric( $a['amount_minor'] ) || ! is_string( $a['mode'] ) || ! is_scalar( $a['title'] ) ) {
|
||||
continue;
|
||||
}
|
||||
$id = (int) $a['id'];
|
||||
$amount = (int) $a['amount_minor'];
|
||||
if ( $id <= 0 || $amount <= 0 ) {
|
||||
continue;
|
||||
}
|
||||
if ( self::EXACT === $a['mode'] ) {
|
||||
$times = $baseMinor === $amount ? 1 : 0;
|
||||
} elseif ( self::EVERY === $a['mode'] ) {
|
||||
$times = intdiv( $baseMinor, $amount );
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if ( $times >= 1 ) {
|
||||
$out[] = array( 'action_id' => $id, 'title' => (string) $a['title'], 'times' => $times );
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user