/** * Import landing summary data and update game stats, ensuring robust empty-net goal detection. * * @param int $game_id * @param array $landing_data * @return bool */ function otg_import_landing_summary($game_id, $landing_data) { global $wpdb; $table_goals = $wpdb->prefix . 'otg_game_goals'; if (empty($landing_data) || !isset($landing_data['summary']['scoring'])) { return false; } $scoring_periods = $landing_data['summary']['scoring']; foreach ($scoring_periods as $period) { if (empty($period['goals'])) { continue; } foreach ($period['goals'] as $goal) { $goal_id = intval($goal['eventId'] ?? 0); $goal_modifier = sanitize_text_field($goal['goalModifier'] ?? ''); $landing_modifier = sanitize_text_field($goal['landingModifier'] ?? ''); // Robust direct empty net evaluation using the official API goal node properties $is_empty_net = 0; if (isset($goal['emptyNet']) && $goal['emptyNet'] === true) { $is_empty_net = 1; } elseif (in_array(strtolower($goal_modifier), ['empty-net', 'en', 'empty-net-goal'], true) || in_array(strtolower($landing_modifier), ['empty-net', 'en', 'empty-net-goal'], true)) { $is_empty_net = 1; } // Update database record for the specific goal event $wpdb->update( $table_goals, ['is_empty_net' => $is_empty_net], [ 'game_id' => $game_id, 'goal_id' => $goal_id ], ['%d'], ['%d', '%d'] ); } } return true; } /** * Backfill season empty-net goals using play-by-play data payloads. * * @param int $season * @return void */ function otg_backfill_season_empty_net_goals($season) { global $wpdb; $table_goals = $wpdb->prefix . 'otg_game_goals'; $table_games = $wpdb->prefix . 'otg_games'; // Retrieve completed games for the designated season $games = $wpdb->get_results($wpdb->prepare( "SELECT game_id, pbp_json FROM $table_games WHERE season = %d AND status = 'OFF'", $season )); foreach ($games as $game) { $game_id = intval($game->game_id); $pbp_data = json_decode($game->pbp_json, true); if (empty($pbp_data) || !isset($pbp_data['plays'])) { continue; } foreach ($pbp_data['plays'] as $play) { if (($play['typeCode'] ?? 0) !== 505) { // 505 represents a goal event in the NHL API continue; } $details = $play['details'] ?? []; $goal_id = intval($play['eventId'] ?? 0); $is_empty_net = false; if (!empty($details['emptyNet']) && $details['emptyNet'] === true) { $is_empty_net = true; } elseif (in_array(strtolower($play['goalModifier'] ?? ''), ['empty-net', 'en', 'empty-net-goal'], true)) { $is_empty_net = true; } if ($is_empty_net) { $wpdb->update( $table_goals, ['is_empty_net' => 1], [ 'game_id' => $game_id, 'goal_id' => $goal_id ], ['%d'], ['%d', '%d'] ); } } } }