Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
- Tiers\TierPolicy (pure), TierRepository (atomic reached marking), Tiers\Recorder hooked before close-at-target - Admin tiers screen; goal target derived from the final tier - Overlay goal widget shows Tier N of M and fills within the tier; alert pill; donate form 'Next unlock' - Installer DB_VERSION 4 (sg_goal_tiers) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39840a20d7
commit
4c379a0245
20 files changed
+1089
-14
No files matched your search
@@ -4,6 +4,7 @@ declare( strict_types=1 );
|
||||
namespace StreamGoals\Overlay;
|
||||
|
||||
use StreamGoals\Core\Money;
|
||||
use StreamGoals\Tiers\TierPolicy;
|
||||
|
||||
/**
|
||||
* Pure, WordPress-free payload builders. Everything returned here is public-safe.
|
||||
@@ -31,7 +32,10 @@ final class Payloads {
|
||||
return max( 0, min( 100, intdiv( max( 0, $raised ) * 100, $target ) ) );
|
||||
}
|
||||
|
||||
public static function goal( ?array $goal, int $raised ): array {
|
||||
/**
|
||||
* @param array<int,array<string,mixed>>|null $tiers Raw sg_goal_tiers rows; null/empty = an ordinary (untiered) goal, payload unchanged.
|
||||
*/
|
||||
public static function goal( ?array $goal, int $raised, ?array $tiers = null ): array {
|
||||
if ( null === $goal ) {
|
||||
return array( 'active' => false, 'title' => '', 'raised_minor' => 0, 'target_minor' => 0, 'percent' => 0,
|
||||
'currency' => 'USD', 'raised_display' => '', 'target_display' => '' );
|
||||
@@ -39,6 +43,9 @@ final class Payloads {
|
||||
$cur = strtoupper( (string) ( $goal['currency'] ?? 'USD' ) );
|
||||
$target = max( 0, (int) ( $goal['target_minor'] ?? 0 ) );
|
||||
$raised = max( 0, $raised );
|
||||
if ( $tiers ) {
|
||||
return self::tieredGoal( $goal, $raised, $cur, $tiers );
|
||||
}
|
||||
return array(
|
||||
'active' => true,
|
||||
'title' => (string) ( $goal['title'] ?? '' ),
|
||||
@@ -51,6 +58,40 @@ final class Payloads {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tiered goal: raised stays cumulative; target/percent describe the CURRENT tier (percent is within the tier).
|
||||
* When every tier is reached the bar is full at the top tier's target.
|
||||
*/
|
||||
private static function tieredGoal( array $goal, int $raised, string $cur, array $tiers ): array {
|
||||
$sorted = TierPolicy::sorted( $tiers );
|
||||
$current = TierPolicy::currentTier( $sorted );
|
||||
$all = null === $current;
|
||||
$target = $all ? TierPolicy::derivedGoalTarget( $sorted ) : max( 0, (int) $current['target_minor'] );
|
||||
$floor = TierPolicy::floorMinor( $sorted );
|
||||
return array(
|
||||
'active' => true,
|
||||
'title' => (string) ( $goal['title'] ?? '' ),
|
||||
'raised_minor' => $raised,
|
||||
'target_minor' => $target,
|
||||
'percent' => TierPolicy::percentFor( $sorted, $raised ),
|
||||
'currency' => $cur,
|
||||
'raised_display' => self::money( $raised, $cur ),
|
||||
'target_display' => self::money( $target, $cur ),
|
||||
'tiers' => array(
|
||||
'count' => count( $sorted ),
|
||||
'index' => TierPolicy::currentIndex( $sorted ),
|
||||
'current' => $all ? null : array(
|
||||
'title' => self::text( $current['title'] ?? '', 120 ),
|
||||
'description' => self::text( $current['description'] ?? '', 255 ),
|
||||
'target_minor' => $target,
|
||||
'target_display' => self::money( $target, $cur ),
|
||||
'floor_minor' => $floor,
|
||||
),
|
||||
'all_reached' => $all,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $endsAt sg_state `timer_ends_at` (epoch seconds, or a UTC datetime string).
|
||||
* @param string|null $pausedRemaining sg_state `paused_remaining` seconds; non-null means paused.
|
||||
@@ -132,6 +173,7 @@ final class Payloads {
|
||||
'message' => (string) ( $r['message'] ?? '' ),
|
||||
'bonuses' => self::bonuses( $r['bonuses'] ?? array() ),
|
||||
'actions' => self::actionEvents( $r['actions'] ?? array() ),
|
||||
'tiers' => self::tierEvents( $r['tiers'] ?? array() ),
|
||||
'media' => $mediaFor ? $mediaFor( $r, $pub, $disp ) : null,
|
||||
);
|
||||
}
|
||||
@@ -200,6 +242,27 @@ final class Payloads {
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tiers a donation unlocked, for the "TIER UNLOCKED" pill: [ {title} ]. At most 5, titles capped at 80 chars, blanks dropped.
|
||||
*
|
||||
* @param mixed $events
|
||||
* @return array<int,array{title:string}>
|
||||
*/
|
||||
public static function tierEvents( mixed $events ): array {
|
||||
$out = array();
|
||||
foreach ( is_array( $events ) ? $events : array() as $e ) {
|
||||
$title = self::text( is_array( $e ) ? ( $e['title'] ?? '' ) : '', 80 );
|
||||
if ( '' === $title ) {
|
||||
continue;
|
||||
}
|
||||
$out[] = array( 'title' => $title );
|
||||
if ( count( $out ) >= 5 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The on-stream reward menu for the active goal. Empty list means "render nothing".
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user