- Alerts\Resolver/Media/Template/AlertMedia/DefInput/DefRepository; Alerts admin screen with Media Library picker and live preview - Overlay: media resolved server-side from attachment IDs (MIME whitelist), playback with timeouts and autoplay-failure fallbacks - Workflow: pick the next free minute if the version tag already exists (two pushes in one minute failed 'Create release') Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
55 lines
2.1 KiB
PHP
55 lines
2.1 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Alerts;
|
|
|
|
/** Picks the single alert definition that applies to one donation. Pure. */
|
|
final class Resolver {
|
|
|
|
/**
|
|
* @param int[] $triggeredActionIds Rewards triggered by this donation.
|
|
* @param array<int,array<string,mixed>> $defs Raw sg_alert_defs rows (any state; filtered here).
|
|
* @param array<int,int> $actionAmounts action id => reward amount (minor); ties fall back to sort, id.
|
|
* @return array<string,mixed>|null the winning row (with ints normalised) or null
|
|
*/
|
|
public static function pick( int $goalId, int $baseMinor, array $triggeredActionIds, array $defs, array $actionAmounts = array() ): ?array {
|
|
$triggered = array_flip( array_map( 'intval', $triggeredActionIds ) );
|
|
$reward = array();
|
|
$amount = array();
|
|
foreach ( $defs as $d ) {
|
|
if ( ! is_array( $d ) || empty( $d['enabled'] ) ) {
|
|
continue;
|
|
}
|
|
$dg = (int) ( $d['goal_id'] ?? 0 );
|
|
if ( 0 !== $dg && $dg !== $goalId ) {
|
|
continue;
|
|
}
|
|
$d['id'] = (int) ( $d['id'] ?? 0 );
|
|
$d['sort'] = (int) ( $d['sort'] ?? 0 );
|
|
$d['min_minor'] = (int) ( $d['min_minor'] ?? 0 );
|
|
$d['action_id'] = (int) ( $d['action_id'] ?? 0 );
|
|
$d['goal_id'] = $dg;
|
|
if ( 'reward' === ( $d['trigger_type'] ?? '' ) ) {
|
|
if ( $d['action_id'] > 0 && isset( $triggered[ $d['action_id'] ] ) ) {
|
|
$reward[] = $d;
|
|
}
|
|
} elseif ( 'amount' === ( $d['trigger_type'] ?? '' ) && $baseMinor >= $d['min_minor'] ) {
|
|
$amount[] = $d;
|
|
}
|
|
}
|
|
if ( $reward ) {
|
|
usort( $reward, static function ( array $a, array $b ) use ( $actionAmounts ): int {
|
|
return ( (int) ( $actionAmounts[ $b['action_id'] ] ?? 0 ) <=> (int) ( $actionAmounts[ $a['action_id'] ] ?? 0 ) )
|
|
?: ( $a['sort'] <=> $b['sort'] )
|
|
?: ( $a['id'] <=> $b['id'] );
|
|
} );
|
|
return $reward[0];
|
|
}
|
|
if ( $amount ) {
|
|
usort( $amount, static fn( array $a, array $b ): int => ( $b['min_minor'] <=> $a['min_minor'] ) ?: ( $a['sort'] <=> $b['sort'] ) ?: ( $a['id'] <=> $b['id'] ) );
|
|
return $amount[0];
|
|
}
|
|
return null;
|
|
}
|
|
}
|