| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Games play-time store. | |
| 3 | + * OpenStation — Games play-time store. | |
| 4 | 4 | * |
| 5 | 5 | * Accumulates how long each user has spent playing each game. The |
| 6 | 6 | * framework's launcher measures active window time client-side (the |
| 7 | 7 | * clock pauses while the game window is minimized) and flushes |
| @@ -14,13 +14,12 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * Trust model matches scores (arcade honesty): increments are |
| 16 | 16 | * client-asserted, the server clamps each flush to a filterable cap |
| 17 | 17 | * so a hostile client can't mint years of play in one request, and |
| 18 | - * the `desktop_mode_game_playtime_pre_record` filter is the hook for | |
| 18 | + * the `openstation_game_playtime_pre_record` filter is the hook for | |
| 19 | 19 | * stricter policies. |
| 20 | 20 | * |
| 21 | - * @package WPDesktopMode | |
| 22 | - * @since 0.9.7 | |
| 21 | + * @package OpenStation | |
| 23 | 22 | */ |
| 24 | 23 | |
| 25 | 24 | defined( 'ABSPATH' ) || exit; |
| 26 | 25 | |
| @@ -26,11 +25,15 @@ | ||
| 26 | 25 | |
| 27 | 26 | /** |
| 28 | 27 | * The user-meta key holding the per-game play-time map. |
| 29 | 28 | * |
| 30 | - * @since 0.9.7 | |
| 29 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 30 | + * persisted or externally-visible identifier, so renaming it would | |
| 31 | + * orphan data already written by live installs (or break a live | |
| 32 | + * URL). The mismatch between this constant's name and its value is | |
| 33 | + * deliberate — it is NOT a half-finished rename. | |
| 31 | 34 | */ |
| 32 | -define( 'DESKTOP_MODE_GAMES_PLAYTIME_META', 'desktop_mode_game_playtime' ); | |
| 35 | +define( 'OPENSTATION_GAMES_PLAYTIME_META', 'desktop_mode_game_playtime' ); | |
| 33 | 36 | |
| 34 | 37 | /** |
| 35 | 38 | * The user-meta key holding the per-game DAILY play-time map: |
| 36 | 39 | * `game id => array( 'YYYY-MM-DD' => seconds )`. Backs the |
| @@ -35,24 +38,26 @@ | ||
| 35 | 38 | * The user-meta key holding the per-game DAILY play-time map: |
| 36 | 39 | * `game id => array( 'YYYY-MM-DD' => seconds )`. Backs the |
| 37 | 40 | * Steam-style "last two weeks" figure; days are bucketed in the |
| 38 | 41 | * site's timezone and pruned past a rolling window (see |
| 39 | - * `desktop_mode_games_playtime_history_days`). The lifetime totals | |
| 40 | - * in {@see DESKTOP_MODE_GAMES_PLAYTIME_META} are authoritative and | |
| 42 | + * `openstation_games_playtime_history_days`). The lifetime totals | |
| 43 | + * in {@see OPENSTATION_GAMES_PLAYTIME_META} are authoritative and | |
| 41 | 44 | * never pruned. |
| 42 | 45 | * |
| 43 | - * @since 0.9.7 | |
| 46 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 47 | + * persisted or externally-visible identifier, so renaming it would | |
| 48 | + * orphan data already written by live installs (or break a live | |
| 49 | + * URL). The mismatch between this constant's name and its value is | |
| 50 | + * deliberate — it is NOT a half-finished rename. | |
| 44 | 51 | */ |
| 45 | -define( 'DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META', 'desktop_mode_game_playtime_days' ); | |
| 52 | +define( 'OPENSTATION_GAMES_PLAYTIME_DAYS_META', 'desktop_mode_game_playtime_days' ); | |
| 46 | 53 | |
| 47 | 54 | /** |
| 48 | 55 | * Today's daily-bucket key (`YYYY-MM-DD`, site timezone). |
| 49 | 56 | * |
| 50 | - * @since 0.9.7 | |
| 51 | - * | |
| 52 | 57 | * @return string |
| 53 | 58 | */ |
| 54 | -function desktop_mode_games_playtime_today_key() { | |
| 59 | +function openstation_games_playtime_today_key() { | |
| 55 | 60 | return current_datetime()->format( 'Y-m-d' ); |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | /** |
| @@ -57,17 +62,15 @@ | ||
| 57 | 62 | |
| 58 | 63 | /** |
| 59 | 64 | * Read a user's accumulated play time. |
| 60 | 65 | * |
| 61 | - * @since 0.9.7 | |
| 62 | - * | |
| 63 | 66 | * @param int $user_id Player. |
| 64 | 67 | * @param string $game Optional game id. Empty returns the full map. |
| 65 | 68 | * @return int|array<string,int> Seconds for one game, or the whole |
| 66 | 69 | * `game id => seconds` map. |
| 67 | 70 | */ |
| 68 | -function desktop_mode_games_get_playtime( $user_id, $game = '' ) { | |
| 69 | - $map = get_user_meta( (int) $user_id, DESKTOP_MODE_GAMES_PLAYTIME_META, true ); | |
| 71 | +function openstation_games_get_playtime( $user_id, $game = '' ) { | |
| 72 | + $map = get_user_meta( (int) $user_id, OPENSTATION_GAMES_PLAYTIME_META, true ); | |
| 70 | 73 | if ( ! is_array( $map ) ) { |
| 71 | 74 | $map = array(); |
| 72 | 75 | } |
| 73 | 76 | $clean = array(); |
| @@ -87,17 +90,15 @@ | ||
| 87 | 90 | |
| 88 | 91 | /** |
| 89 | 92 | * Read a user's daily play-time buckets. |
| 90 | 93 | * |
| 91 | - * @since 0.9.7 | |
| 92 | - * | |
| 93 | 94 | * @param int $user_id Player. |
| 94 | 95 | * @param string $game Optional game id. Empty returns the full map. |
| 95 | 96 | * @return array Day buckets (`'YYYY-MM-DD' => seconds`) for one game, |
| 96 | 97 | * or the whole `game id => buckets` map. |
| 97 | 98 | */ |
| 98 | -function desktop_mode_games_get_playtime_daily( $user_id, $game = '' ) { | |
| 99 | - $map = get_user_meta( (int) $user_id, DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META, true ); | |
| 99 | +function openstation_games_get_playtime_daily( $user_id, $game = '' ) { | |
| 100 | + $map = get_user_meta( (int) $user_id, OPENSTATION_GAMES_PLAYTIME_DAYS_META, true ); | |
| 100 | 101 | if ( ! is_array( $map ) ) { |
| 101 | 102 | $map = array(); |
| 102 | 103 | } |
| 103 | 104 | $clean = array(); |
| @@ -124,24 +125,22 @@ | ||
| 124 | 125 | |
| 125 | 126 | /** |
| 126 | 127 | * Add seconds to a user's play-time total for a game. |
| 127 | 128 | * |
| 128 | - * @since 0.9.7 | |
| 129 | - * | |
| 130 | 129 | * @param string $game Registered game id. |
| 131 | 130 | * @param int $user_id Player. |
| 132 | 131 | * @param int $seconds Seconds to add. Clamped to |
| 133 | - * `[1, desktop_mode_games_playtime_max_increment]`. | |
| 132 | + * `[1, openstation_games_playtime_max_increment]`. | |
| 134 | 133 | * @return int|WP_Error The new total for the game on success. |
| 135 | 134 | */ |
| 136 | -function desktop_mode_games_add_playtime( $game, $user_id, $seconds ) { | |
| 135 | +function openstation_games_add_playtime( $game, $user_id, $seconds ) { | |
| 137 | 136 | $game = sanitize_key( (string) $game ); |
| 138 | 137 | $user_id = (int) $user_id; |
| 139 | 138 | $seconds = (int) $seconds; |
| 140 | 139 | |
| 141 | - if ( ! desktop_mode_games_is_registered( $game ) ) { | |
| 140 | + if ( ! openstation_games_is_registered( $game ) ) { | |
| 142 | 141 | return new WP_Error( |
| 143 | - 'desktop_mode_unknown_game', | |
| 142 | + 'openstation_unknown_game', | |
| 144 | 143 | __( 'Unknown game.', 'desktop-mode' ), |
| 145 | 144 | array( 'status' => 404 ) |
| 146 | 145 | ); |
| 147 | 146 | } |
| @@ -146,9 +145,9 @@ | ||
| 146 | 145 | ); |
| 147 | 146 | } |
| 148 | 147 | if ( $user_id <= 0 ) { |
| 149 | 148 | return new WP_Error( |
| 150 | - 'desktop_mode_invalid_user', | |
| 149 | + 'openstation_invalid_user', | |
| 151 | 150 | __( 'A valid user is required to record play time.', 'desktop-mode' ), |
| 152 | 151 | array( 'status' => 400 ) |
| 153 | 152 | ); |
| 154 | 153 | } |
| @@ -153,9 +152,9 @@ | ||
| 153 | 152 | ); |
| 154 | 153 | } |
| 155 | 154 | if ( $seconds < 1 ) { |
| 156 | 155 | return new WP_Error( |
| 157 | - 'desktop_mode_invalid_playtime', | |
| 156 | + 'openstation_invalid_playtime', | |
| 158 | 157 | __( 'Play time must be a positive number of seconds.', 'desktop-mode' ), |
| 159 | 158 | array( 'status' => 400 ) |
| 160 | 159 | ); |
| 161 | 160 | } |
| @@ -165,15 +164,13 @@ | ||
| 165 | 164 | * The framework flushes roughly once a minute, so anything far |
| 166 | 165 | * past that is either a background-throttled tab catching up or a |
| 167 | 166 | * hostile client; the clamp bounds the damage either way. |
| 168 | 167 | * |
| 169 | - * @since 0.9.7 | |
| 170 | - * | |
| 171 | 168 | * @param int $max_seconds Default 900 (15 minutes). |
| 172 | 169 | * @param string $game Game id. |
| 173 | 170 | * @param int $user_id Player. |
| 174 | 171 | */ |
| 175 | - $max = max( 1, (int) apply_filters( 'desktop_mode_games_playtime_max_increment', 900, $game, $user_id ) ); | |
| 172 | + $max = max( 1, (int) apply_filters( 'openstation_games_playtime_max_increment', 900, $game, $user_id ) ); | |
| 176 | 173 | $seconds = min( $seconds, $max ); |
| 177 | 174 | |
| 178 | 175 | /** |
| 179 | 176 | * Short-circuit / veto filter for play-time recording. Return a |
| @@ -179,41 +176,37 @@ | ||
| 179 | 176 | * Short-circuit / veto filter for play-time recording. Return a |
| 180 | 177 | * `WP_Error` to reject the increment (surfaced to the client), or |
| 181 | 178 | * `null` to proceed. |
| 182 | 179 | * |
| 183 | - * @since 0.9.7 | |
| 184 | - * | |
| 185 | 180 | * @param null|WP_Error $pre Null to proceed. |
| 186 | 181 | * @param string $game Game id. |
| 187 | 182 | * @param int $user_id Player. |
| 188 | 183 | * @param int $seconds Clamped increment. |
| 189 | 184 | */ |
| 190 | - $pre = apply_filters( 'desktop_mode_game_playtime_pre_record', null, $game, $user_id, $seconds ); | |
| 185 | + $pre = apply_filters( 'openstation_game_playtime_pre_record', null, $game, $user_id, $seconds ); | |
| 191 | 186 | if ( is_wp_error( $pre ) ) { |
| 192 | 187 | return $pre; |
| 193 | 188 | } |
| 194 | 189 | |
| 195 | - $map = desktop_mode_games_get_playtime( $user_id ); | |
| 190 | + $map = openstation_games_get_playtime( $user_id ); | |
| 196 | 191 | $map[ $game ] = ( isset( $map[ $game ] ) ? $map[ $game ] : 0 ) + $seconds; |
| 197 | - update_user_meta( $user_id, DESKTOP_MODE_GAMES_PLAYTIME_META, $map ); | |
| 192 | + update_user_meta( $user_id, OPENSTATION_GAMES_PLAYTIME_META, $map ); | |
| 198 | 193 | |
| 199 | 194 | // Daily bucket (site timezone) for the recent-activity figure, |
| 200 | 195 | // pruned to a rolling window so the meta row stays bounded. The |
| 201 | 196 | // lifetime total above is the source of truth and never shrinks. |
| 202 | - $today = desktop_mode_games_playtime_today_key(); | |
| 197 | + $today = openstation_games_playtime_today_key(); | |
| 203 | 198 | |
| 204 | 199 | /** |
| 205 | 200 | * Filter how many days of daily play-time buckets are retained. |
| 206 | 201 | * The Games hub needs 14 for its "last two weeks" figure. |
| 207 | 202 | * |
| 208 | - * @since 0.9.7 | |
| 209 | - * | |
| 210 | 203 | * @param int $days Default 30. |
| 211 | 204 | */ |
| 212 | - $window = max( 1, (int) apply_filters( 'desktop_mode_games_playtime_history_days', 30 ) ); | |
| 205 | + $window = max( 1, (int) apply_filters( 'openstation_games_playtime_history_days', 30 ) ); | |
| 213 | 206 | $cutoff = current_datetime()->modify( '-' . ( $window - 1 ) . ' days' )->format( 'Y-m-d' ); |
| 214 | 207 | |
| 215 | - $daily = desktop_mode_games_get_playtime_daily( $user_id ); | |
| 208 | + $daily = openstation_games_get_playtime_daily( $user_id ); | |
| 216 | 209 | $days = isset( $daily[ $game ] ) ? $daily[ $game ] : array(); |
| 217 | 210 | |
| 218 | 211 | $days[ $today ] = ( isset( $days[ $today ] ) ? $days[ $today ] : 0 ) + $seconds; |
| 219 | 212 | foreach ( array_keys( $days ) as $day ) { |
| @@ -222,20 +215,18 @@ | ||
| 222 | 215 | unset( $days[ $day ] ); |
| 223 | 216 | } |
| 224 | 217 | } |
| 225 | 218 | $daily[ $game ] = $days; |
| 226 | - update_user_meta( $user_id, DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META, $daily ); | |
| 219 | + update_user_meta( $user_id, OPENSTATION_GAMES_PLAYTIME_DAYS_META, $daily ); | |
| 227 | 220 | |
| 228 | 221 | /** |
| 229 | 222 | * Fires after a play-time increment is recorded. |
| 230 | 223 | * |
| 231 | - * @since 0.9.7 | |
| 232 | - * | |
| 233 | 224 | * @param string $game Game id. |
| 234 | 225 | * @param int $user_id Player. |
| 235 | 226 | * @param int $seconds The recorded increment. |
| 236 | 227 | * @param int $total The user's new total for the game. |
| 237 | 228 | */ |
| 238 | - do_action( 'desktop_mode_game_playtime_recorded', $game, $user_id, $seconds, $map[ $game ] ); | |
| 229 | + do_action( 'openstation_game_playtime_recorded', $game, $user_id, $seconds, $map[ $game ] ); | |
| 239 | 230 | |
| 240 | 231 | return $map[ $game ]; |
| 241 | 232 | } |