takeone-youtube-clone/app/Http/Controllers/MatchEventController.php
ghassan 1c4e4986b5 Frame-accurate scrubbing + mobile highlights UX + collapsed action menu
Frame-accurate timestamps (end-to-end)
- Migration 2026_08_08_000002 promotes match_rounds.start_time_seconds,
  match_points.timestamp_seconds, coach_reviews.start_time_seconds and
  coach_reviews.end_time_seconds from INTEGER to decimal(10,3).
- Model $casts updated (float). MatchEventController validators relaxed
  from `integer` to `numeric|min:0` on all four fields.
- Blade SSR data-time-{start,end} attributes cast to (float) so the
  first paint carries frame precision.
- Point + review capture strips: slider step = 1/fps, snapToFrame()
  applied to every seek/nudge/input commit. Clock now shows SMPTE
  MM:SS.FF; parsePcbTimeInput accepts MM:SS.FF, MM:SS, MM.SS or seconds.
- Default fps is 30; override via window.matchFPS.
- confirmPointCapture no longer Math.round()s currentTime — sends the
  exact frame boundary.

Point + review UX
- Video pauses aggressively on scrubber grab (mousedown/pointerdown/
  touchstart) AND on every input, with a 30ms follow-up to beat HLS.js
  race conditions.
- Zoom buttons now scope by data-pcs-zoom / data-rcb-zoom so clicks on
  the review-strip zoom don't clobber the point-strip zoom (root cause
  of the "resets while button still on 20x" bug).
- Point cards now play a preroll/postroll clip at 1× then replay at
  the chosen slow-mo rate, then resume normal playback.
- REPLAY ×N badge shown in the top-left of the video during replays,
  swapping palette when slow-mo kicks in.
- Slow-mo picker (¼× ½× ¾×) in the tab-header — window.slowmoRate is
  the source of truth for both point and review replays.
- Clicking a card hides the player chrome (controls-hidden re-applied
  across 0/10/60/200/500ms to beat pause/seek/play showControls races);
  mousemove over the video brings them back instantly.
- Review-tools slow-mo button removed — the card click IS the trigger.

Coach review specific
- Dual-thumb scrubber on a shared track with a red fill between the
  thumbs (visualises the note's range). Start clamps to ≤ end and
  vice-versa; nudges act on the last-touched thumb.
- Note overlay live-previews as user types and is drag-to-position;
  overlay position persists (position_x/position_y decimal(6,4)).
- Overlay uses container-query units (cqi) so text scales with the
  player width and position stays proportional on any resize.
- Timeline card design (chapter-style time label + subtle card with
  emoji, note, coach name, tools) — nothing else selected.

Sidebar
- "Rounds & points" / "Private notes" section labels removed; only
  the owner-only + Add Round / + Add Note button remains.
- Points that share a timestamp within a round collapse into ONE card
  with a chip per side (equal-width Blue/Red pills), one edit/delete
  pair, and a coloured score line (yellow ROUND N, blue/red score
  badges — background pills with white numerals).
- On mobile the highlights sheet only closes via its own toggle now.
  Backdrop is pointer-events: none so taps pass through to the video.

Mobile capture strip
- Renders below the video on mobile portrait so the paused frame is
  visible while typing. Wider inner padding (10px 20px 14px) so
  content doesn't sit on the edges.
- .pcs-entry uses flex-wrap: nowrap and Action input shrinks to
  fit — the row stays on one line at any width.

Collapsed action menu (video-actions component)
- Every viewport now shows the single  Action dropdown; individual
  desktop-action buttons are hidden by CSS across the board.

SPA navigation fixes
- window.videoId + isOwner now live on window so navigation to a new
  match video via Up Next can update them via reloadMatchVideoState.
- Tabs, event-item clicks, highlights toggle and sidebar-height sync
  re-hydrate on each SPA swap.
- Blade point-time regression: "{{ '@' . $fmtTime(...) }}" instead of
  the misused "@{{ ... }}" escape directive.

Backend
- Point-score recompute (recomputeRoundScores) already sums by
  (timestamp, id) order — works fine with the new decimal columns.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-08-09 02:26:19 +03:00

312 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\CoachReview;
use App\Models\MatchPoint;
use App\Models\MatchRound;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class MatchEventController extends Controller
{
// ==================== ROUNDS ====================
public function storeRound(Request $request, Video $video)
{
$request->validate([
'round_number' => 'required|integer|min:1',
'name' => 'nullable|string|max:50',
'start_time_seconds' => 'nullable|numeric|min:0',
]);
// Check if user owns the video
if (Auth::id() !== $video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$round = MatchRound::create([
'video_id' => $video->id,
'round_number' => $request->round_number,
'name' => $request->name ?? 'ROUND '.$request->round_number,
'start_time_seconds' => $request->start_time_seconds,
]);
return response()->json([
'success' => true,
'round' => $round,
'message' => 'Round added successfully!',
]);
}
public function updateRound(Request $request, MatchRound $round)
{
$request->validate([
'round_number' => 'sometimes|integer|min:1',
'name' => 'required|string|max:50',
'start_time_seconds' => 'nullable|numeric|min:0',
]);
// Check if user owns the video
if (Auth::id() !== $round->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$round->update([
'round_number' => $request->round_number ?? $round->round_number,
'name' => $request->name,
'start_time_seconds' => $request->start_time_seconds,
]);
return response()->json([
'success' => true,
'round' => $round,
'message' => 'Round updated successfully!',
]);
}
public function destroyRound(MatchRound $round)
{
// Check if user owns the video
if (Auth::id() !== $round->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$round->delete();
return response()->json([
'success' => true,
'message' => 'Round deleted successfully!',
]);
}
// ==================== POINTS ====================
public function storePoint(Request $request, Video $video)
{
$request->validate([
'round_id' => 'required|exists:match_rounds,id',
'timestamp_seconds' => 'required|numeric|min:0',
'action' => 'required|string|max:255',
'points' => 'required|integer|min:1',
'competitor' => 'required|in:blue,red',
'notes' => 'nullable|string|max:500',
]);
// Check if user owns the video
if (Auth::id() !== $video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
// Create the row first with placeholder scores, then recompute the whole
// round's running scores. This is the only correct approach when two
// points can share a timestamp (Blue + Red at the same moment) or when
// a point is inserted between existing ones.
$point = MatchPoint::create([
'video_id' => $video->id,
'match_round_id' => $request->round_id,
'timestamp_seconds' => $request->timestamp_seconds,
'action' => $request->action,
'points' => $request->points,
'competitor' => $request->competitor,
'notes' => $request->notes,
'score_blue' => 0,
'score_red' => 0,
]);
$this->recomputeRoundScores($request->round_id);
return response()->json([
'success' => true,
'point' => $point->fresh(),
'message' => 'Point added successfully!',
]);
}
/**
* Recompute cumulative score_blue/score_red for every point in a round,
* ordered by (timestamp_seconds, id). Handles same-timestamp ties by
* insertion order, so Blue and Red saved at the same moment both end up
* reflecting the running score after that moment.
*/
private function recomputeRoundScores(int $roundId): void
{
$blue = 0;
$red = 0;
MatchPoint::where('match_round_id', $roundId)
->orderBy('timestamp_seconds', 'asc')
->orderBy('id', 'asc')
->get()
->each(function (MatchPoint $p) use (&$blue, &$red) {
if ($p->competitor === 'blue') $blue += (int) $p->points;
else $red += (int) $p->points;
if ((int) $p->score_blue !== $blue || (int) $p->score_red !== $red) {
$p->update(['score_blue' => $blue, 'score_red' => $red]);
}
});
}
public function updatePoint(Request $request, MatchPoint $point)
{
$request->validate([
'timestamp_seconds' => 'required|numeric|min:0',
'action' => 'required|string|max:255',
'points' => 'required|integer|min:1',
'competitor' => 'required|in:blue,red',
'notes' => 'nullable|string|max:500',
]);
// Check if user owns the video
if (Auth::id() !== $point->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$roundId = $point->match_round_id;
$point->update([
'timestamp_seconds' => $request->timestamp_seconds,
'action' => $request->action,
'points' => $request->points,
'competitor' => $request->competitor,
'notes' => $request->notes,
]);
$this->recomputeRoundScores($roundId);
return response()->json([
'success' => true,
'point' => $point->fresh(),
'message' => 'Point updated successfully!',
]);
}
public function destroyPoint(MatchPoint $point)
{
// Check if user owns the video
if (Auth::id() !== $point->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$roundId = $point->match_round_id;
$point->delete();
$this->recomputeRoundScores($roundId);
return response()->json([
'success' => true,
'message' => 'Point deleted successfully!',
]);
}
// ==================== COACH REVIEWS ====================
public function storeReview(Request $request, Video $video)
{
$request->validate([
'start_time_seconds' => 'required|numeric|min:0',
'end_time_seconds' => 'nullable|numeric|min:0',
'note' => 'required|string|max:1000',
'coach_name' => 'required|string|max:100',
'emoji' => 'nullable|string|max:10',
'position_x' => 'nullable|numeric|between:0,1',
'position_y' => 'nullable|numeric|between:0,1',
]);
// Check if user owns the video
if (Auth::id() !== $video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$review = CoachReview::create([
'video_id' => $video->id,
'user_id' => Auth::id(),
'start_time_seconds' => $request->start_time_seconds,
'end_time_seconds' => $request->end_time_seconds,
'note' => $request->note,
'coach_name' => $request->coach_name,
'emoji' => $request->emoji ?? '🔥',
'position_x' => $request->position_x,
'position_y' => $request->position_y,
]);
return response()->json([
'success' => true,
'review' => $review,
'message' => 'Coach note added successfully!',
]);
}
public function updateReview(Request $request, CoachReview $review)
{
$request->validate([
'start_time_seconds' => 'required|numeric|min:0',
'end_time_seconds' => 'nullable|numeric|min:0',
'note' => 'required|string|max:1000',
'coach_name' => 'required|string|max:100',
'emoji' => 'nullable|string|max:10',
'position_x' => 'nullable|numeric|between:0,1',
'position_y' => 'nullable|numeric|between:0,1',
]);
// Check if user owns the video
if (Auth::id() !== $review->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$review->update([
'start_time_seconds' => $request->start_time_seconds,
'end_time_seconds' => $request->end_time_seconds,
'note' => $request->note,
'coach_name' => $request->coach_name,
'emoji' => $request->emoji,
'position_x' => $request->position_x,
'position_y' => $request->position_y,
]);
return response()->json([
'success' => true,
'review' => $review,
'message' => 'Coach note updated successfully!',
]);
}
public function destroyReview(CoachReview $review)
{
// Check if user owns the video
if (Auth::id() !== $review->video->user_id) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$review->delete();
return response()->json([
'success' => true,
'message' => 'Coach note deleted successfully!',
]);
}
// ==================== GET DATA ====================
public function getMatchData(Video $video)
{
// Check if user can view this video
if (! $video->canView(Auth::user())) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
$rounds = MatchRound::where('video_id', $video->id)
->with('points')
->orderBy('round_number')
->get();
$reviews = CoachReview::where('video_id', $video->id)
->orderBy('start_time_seconds')
->get();
return response()->json([
'success' => true,
'rounds' => $rounds,
'reviews' => $reviews,
]);
}
}