22
33namespace App \Livewire ;
44
5- use App \Events \BuzzerPressed ;
65use App \Events \ClueRevealed ;
76use App \Events \DailyDoubleTriggered ;
87use App \Events \GameStateChanged ;
@@ -28,7 +27,6 @@ class HostControl extends Component
2827 public ?Team $ currentTeam = null ;
2928
3029 public $ teams = [];
31-
3230
3331 // Daily Double
3432 public bool $ showDailyDoubleWager = false ;
@@ -128,39 +126,12 @@ public function triggerBuzzer($teamId)
128126 return ;
129127 }
130128
131- // Set the team as active (applies to both regular and lightning round)
132- $ this ->currentTeam = $ team ;
133- $ this ->game ->current_team_id = $ teamId ;
134- $ this ->game ->save ();
135-
136- // Check if we're in lightning round
137- if ($ this ->game ->status === 'lightning_round ' ) {
138- // For lightning round, dispatch the buzzer event to the lightning round component
139- broadcast (new GameStateChanged ($ this ->game ->id , 'buzzer-pressed ' , ['teamId ' => $ teamId ]));
140-
141- // Also broadcast the buzzer sound
142- broadcast (new BuzzerPressed ($ team ));
129+ // Use centralized buzzer handling logic
130+ $ this ->buzzerService ->handleBuzzerPress ($ team );
143131
144- Log::info ('Lightning round buzzer triggered manually from host control ' , [
145- 'game_id ' => $ this ->game ->id ,
146- 'team_id ' => $ teamId ,
147- 'team_name ' => $ team ->name ,
148- ]);
149- } else {
150- // Regular game mode
151- // Broadcast team selection to all clients
152- broadcast (new GameStateChanged ($ this ->game ->id , 'team-selected ' , ['teamId ' => $ teamId ]));
153-
154- // Broadcast buzzer event to trigger sound on game board
155- broadcast (new BuzzerPressed ($ team ));
156-
157- Log::info ('Buzzer triggered manually from host control ' , [
158- 'game_id ' => $ this ->game ->id ,
159- 'team_id ' => $ teamId ,
160- 'team_name ' => $ team ->name ,
161- 'set_as_active ' => true ,
162- ]);
163- }
132+ // Update local state
133+ $ this ->currentTeam = $ team ;
134+ $ this ->game ->refresh ();
164135 }
165136
166137 // Clue Control
@@ -453,29 +424,29 @@ public function markLightningCorrect()
453424 if ($ this ->currentTeam ) {
454425 // Award points directly here
455426 $ scoringService = app (ScoringService::class);
456-
427+
457428 // Need to refresh to get the latest lightning questions
458429 $ this ->game ->refresh ();
459430 $ currentQuestion = $ this ->game ->lightningQuestions
460431 ->where ('is_current ' , true )
461432 ->first ();
462-
433+
463434 if ($ currentQuestion ) {
464435 $ scoringService ->recordLightningAnswer (
465436 $ currentQuestion ->id ,
466437 $ this ->currentTeam ->id ,
467438 true
468439 );
469-
440+
470441 // Mark question as answered and not current
471442 $ currentQuestion ->update ([
472443 'is_current ' => false ,
473444 'is_answered ' => true ,
474445 ]);
475-
446+
476447 // Refresh team to get updated score
477448 $ this ->currentTeam ->refresh ();
478-
449+
479450 // Broadcast score update
480451 broadcast (new ScoreUpdated (
481452 $ this ->game ->id ,
@@ -484,29 +455,29 @@ public function markLightningCorrect()
484455 200 ,
485456 true
486457 ));
487-
458+
488459 // Get next question
489460 $ nextQuestion = $ this ->game ->lightningQuestions
490461 ->where ('is_answered ' , false )
491462 ->sortBy ('order_position ' )
492463 ->first ();
493-
464+
494465 if ($ nextQuestion ) {
495466 $ nextQuestion ->update (['is_current ' => true ]);
496467 // Broadcast that we've moved to the next question
497468 broadcast (new GameStateChanged ($ this ->game ->id , 'lightning-next-question ' ));
498469 }
499470 }
500471 }
501-
472+
502473 // Clear current team for next question
503474 $ this ->currentTeam = null ;
504475 $ this ->game ->current_team_id = null ;
505476 $ this ->game ->save ();
506-
477+
507478 // Now tell the lightning round component to refresh
508479 $ this ->dispatch ('lightning-refresh ' )->to (LightningRound::class);
509-
480+
510481 // Refresh our own game state
511482 $ this ->refreshGame ();
512483 }
@@ -517,10 +488,10 @@ public function markLightningIncorrect()
517488 // Deduct points from current team
518489 $ scoringService = app (ScoringService::class);
519490 $ scoringService ->deductPoints ($ this ->currentTeam ->id , 200 );
520-
491+
521492 // Refresh team to get updated score
522493 $ this ->currentTeam ->refresh ();
523-
494+
524495 // Broadcast score update
525496 broadcast (new ScoreUpdated (
526497 $ this ->game ->id ,
@@ -529,12 +500,12 @@ public function markLightningIncorrect()
529500 -200 ,
530501 false
531502 ));
532-
503+
533504 // Clear current team to allow others to buzz in
534505 $ this ->currentTeam = null ;
535506 $ this ->game ->current_team_id = null ;
536507 $ this ->game ->save ();
537-
508+
538509 // Broadcast that buzzers are open again
539510 broadcast (new GameStateChanged ($ this ->game ->id , 'buzzers-opened ' ));
540511 }
@@ -564,7 +535,7 @@ public function handleBuzzerWebhook($teamId)
564535 $ this ->currentTeam = Team::find ($ teamId );
565536 }
566537 }
567-
538+
568539 #[On('buzzer-pressed ' )]
569540 public function handleBuzzerPressed ($ teamId )
570541 {
@@ -575,17 +546,18 @@ public function handleBuzzerPressed($teamId)
575546 $ this ->game ->save ();
576547 }
577548 }
578-
549+
579550 #[On('game-state-changed ' )]
580551 public function handleGameStateChanged ($ state , $ data = [])
581552 {
582- // Refresh current team when team is selected in lightning round
583- if ($ state === 'team-selected ' && isset ($ data ['teamId ' ]) && $ this -> game -> status === ' lightning_round ' ) {
553+ // Handle team selection events from both manual triggers and buzzer API
554+ if (in_array ( $ state, [ 'team-selected ' , ' buzzer-pressed ' ]) && isset ($ data ['teamId ' ])) {
584555 $ this ->currentTeam = Team::find ($ data ['teamId ' ]);
585- $ this ->game ->refresh ();
556+ $ this ->game ->current_team_id = $ data ['teamId ' ];
557+ $ this ->refreshGame ();
586558 }
587559 }
588-
560+
589561 #[On('score-updated ' )]
590562 public function handleScoreUpdated ()
591563 {
@@ -598,7 +570,7 @@ private function refreshGame()
598570 $ this ->game ->refresh ();
599571 $ this ->categories = $ this ->game ->categories ->sortBy ('position ' );
600572 $ this ->teams = $ this ->game ->teams ()->get ();
601-
573+
602574 // Refresh current team if it exists
603575 if ($ this ->currentTeam ) {
604576 $ this ->currentTeam = $ this ->teams ->find ($ this ->currentTeam ->id );
0 commit comments