Skip to content

Commit 4f2df45

Browse files
feat: Updated lightning questions
1 parent 02e3f51 commit 4f2df45

File tree

11 files changed

+332
-457
lines changed

11 files changed

+332
-457
lines changed

app/Livewire/BuzzerListener.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class BuzzerListener extends Component
1515

1616
public ?Game $game = null;
1717

18-
protected $buzzerService;
19-
20-
public function boot(BuzzerService $buzzerService)
21-
{
22-
$this->buzzerService = $buzzerService;
23-
}
24-
2518
public function mount($gameId = null)
2619
{
2720
if ($gameId) {
@@ -45,7 +38,7 @@ public function enableBuzzers()
4538
$this->isListening = true;
4639
}
4740
$this->lockedOut = [];
48-
$this->buzzerService->resetAllBuzzers();
41+
app(BuzzerService::class)->resetAllBuzzers();
4942
}
5043

5144
#[On('open-buzzers')]
@@ -55,7 +48,7 @@ public function openBuzzers()
5548
// Called when host opens buzzers for all teams
5649
$this->isListening = true;
5750
$this->lockedOut = [];
58-
$this->buzzerService->resetAllBuzzers();
51+
app(BuzzerService::class)->resetAllBuzzers();
5952
}
6053

6154
#[On('clue-answered')]
@@ -68,7 +61,7 @@ public function disableBuzzers()
6861
public function resetBuzzers()
6962
{
7063
$this->lockedOut = [];
71-
$this->buzzerService->resetAllBuzzers();
64+
app(BuzzerService::class)->resetAllBuzzers();
7265
$this->isListening = true;
7366
}
7467

@@ -84,7 +77,8 @@ public function processBuzz($teamId, $timestamp)
8477
}
8578

8679
try {
87-
$buzzerEvent = $this->buzzerService->registerBuzz(
80+
$buzzerService = app(BuzzerService::class);
81+
$buzzerEvent = $buzzerService->registerBuzz(
8882
$teamId,
8983
$this->game->teams->find($teamId)->buzzer_pin,
9084
$timestamp
@@ -93,11 +87,8 @@ public function processBuzz($teamId, $timestamp)
9387
if ($buzzerEvent->is_first) {
9488
$this->dispatch('play-sound', sound: 'buzzer');
9589

96-
if ($this->game->status === 'main_game') {
97-
$this->dispatch('buzzer-pressed', teamId: $teamId);
98-
} else {
99-
$this->dispatch('lightning-buzzer-pressed', teamId: $teamId);
100-
}
90+
// Always dispatch buzzer-pressed event regardless of game mode
91+
$this->dispatch('buzzer-pressed', teamId: $teamId);
10192
}
10293
} catch (\Exception $e) {
10394
// Log error but don't crash
@@ -110,7 +101,8 @@ public function processBuzz($teamId, $timestamp)
110101

111102
public function testBuzzer($pin)
112103
{
113-
$result = $this->buzzerService->testBuzzer($pin);
104+
$buzzerService = app(BuzzerService::class);
105+
$result = $buzzerService->testBuzzer($pin);
114106

115107
if ($result['success']) {
116108
$this->dispatch('buzzer-test-success', result: $result);

app/Livewire/GameBoard.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public function handleGameStateChanged($state, $data = [])
155155
// Answer was incorrect - just refresh scores, keep clue open
156156
$this->refreshGame();
157157
// Don't close the modal - other teams can still buzz in
158+
} elseif ($state === 'lightning-round-started') {
159+
// Navigate to lightning round page
160+
$this->redirect(route('game.lightning', ['gameId' => $this->game->id]));
158161
}
159162
}
160163

app/Livewire/HostControl.php

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Livewire;
44

5+
use App\Events\BuzzerPressed;
56
use App\Events\ClueRevealed;
67
use App\Events\DailyDoubleTriggered;
78
use App\Events\GameStateChanged;
@@ -10,6 +11,7 @@
1011
use App\Models\Game;
1112
use App\Models\Team;
1213
use App\Services\BuzzerService;
14+
use App\Services\GameService;
1315
use App\Services\ScoringService;
1416
use Livewire\Attributes\On;
1517
use Livewire\Component;
@@ -26,6 +28,9 @@ class HostControl extends Component
2628
public ?Team $currentTeam = null;
2729

2830
public $teams = [];
31+
32+
// Lightning Round state
33+
public ?Team $lightningAnsweringTeam = null;
2934

3035
// Daily Double
3136
public bool $showDailyDoubleWager = false;
@@ -120,28 +125,44 @@ public function selectCurrentTeam($teamId)
120125
public function triggerBuzzer($teamId)
121126
{
122127
$team = Team::find($teamId);
123-
124-
if (!$team) {
128+
129+
if (! $team) {
125130
return;
126131
}
127132

128-
// Set the team as active
129-
$this->currentTeam = $team;
130-
$this->game->current_team_id = $teamId;
131-
$this->game->save();
133+
// Check if we're in lightning round
134+
if ($this->game->status === 'lightning_round') {
135+
// For lightning round, dispatch the buzzer event to the lightning round component
136+
broadcast(new GameStateChanged($this->game->id, 'buzzer-pressed', ['teamId' => $teamId]));
132137

133-
// Broadcast team selection to all clients
134-
broadcast(new GameStateChanged($this->game->id, 'team-selected', ['teamId' => $teamId]));
138+
// Also broadcast the buzzer sound
139+
broadcast(new BuzzerPressed($team));
135140

136-
// Broadcast buzzer event to trigger sound on game board
137-
broadcast(new \App\Events\BuzzerPressed($team));
138-
139-
Log::info('Buzzer triggered manually from host control', [
140-
'game_id' => $this->game->id,
141-
'team_id' => $teamId,
142-
'team_name' => $team->name,
143-
'set_as_active' => true,
144-
]);
141+
Log::info('Lightning round buzzer triggered manually from host control', [
142+
'game_id' => $this->game->id,
143+
'team_id' => $teamId,
144+
'team_name' => $team->name,
145+
]);
146+
} else {
147+
// Regular game mode
148+
// Set the team as active
149+
$this->currentTeam = $team;
150+
$this->game->current_team_id = $teamId;
151+
$this->game->save();
152+
153+
// Broadcast team selection to all clients
154+
broadcast(new GameStateChanged($this->game->id, 'team-selected', ['teamId' => $teamId]));
155+
156+
// Broadcast buzzer event to trigger sound on game board
157+
broadcast(new BuzzerPressed($team));
158+
159+
Log::info('Buzzer triggered manually from host control', [
160+
'game_id' => $this->game->id,
161+
'team_id' => $teamId,
162+
'team_name' => $team->name,
163+
'set_as_active' => true,
164+
]);
165+
}
145166
}
146167

147168
// Clue Control
@@ -241,27 +262,7 @@ private function calculateWagerOptions()
241262
// Sort options
242263
sort($options);
243264

244-
// Limit to 8 options for UI consistency
245-
if (count($options) > 8) {
246-
// Keep first few, some middle values, and the max
247-
$filtered = [];
248-
$filtered[] = $options[0]; // minimum
249-
250-
// Add evenly distributed values
251-
$step = (count($options) - 2) / 6; // We want 6 middle values
252-
for ($i = 1; $i <= 6; $i++) {
253-
$index = min(round($i * $step), count($options) - 2);
254-
if (! in_array($options[$index], $filtered)) {
255-
$filtered[] = $options[$index];
256-
}
257-
}
258-
259-
$filtered[] = $options[count($options) - 1]; // maximum
260-
261-
$this->wagerOptions = $filtered;
262-
} else {
263-
$this->wagerOptions = $options;
264-
}
265+
$this->wagerOptions = $options;
265266
}
266267

267268
public function markCorrect($teamId = null)
@@ -435,11 +436,42 @@ public function startLightningRound()
435436
return;
436437
}
437438

438-
$this->game->update(['status' => 'lightning_round']);
439+
// Transition to lightning round
440+
$gameService = app(GameService::class);
441+
$gameService->transitionToLightningRound($this->game->id);
442+
439443
$this->game->refresh();
440444

441-
// Redirect to lightning round
442-
return redirect()->route('game.lightning', ['gameId' => $this->game->id]);
445+
// Broadcast event to tell game board to navigate to lightning round
446+
broadcast(new GameStateChanged($this->game->id, 'lightning-round-started'));
447+
448+
// Host stays on the control page, no redirect
449+
$this->refreshGame();
450+
}
451+
452+
// Lightning Round Question Controls
453+
public function markLightningCorrect()
454+
{
455+
$this->dispatch('lightning-mark-correct')->to(LightningRound::class);
456+
$this->lightningAnsweringTeam = null; // Reset after marking
457+
}
458+
459+
public function markLightningIncorrect()
460+
{
461+
$this->dispatch('lightning-mark-incorrect')->to(LightningRound::class);
462+
// Don't reset here as another team might buzz in
463+
}
464+
465+
public function skipLightningQuestion()
466+
{
467+
$this->dispatch('lightning-skip-question')->to(LightningRound::class);
468+
$this->lightningAnsweringTeam = null; // Reset when skipping
469+
}
470+
471+
public function nextLightningQuestion()
472+
{
473+
$this->dispatch('lightning-next-question')->to(LightningRound::class);
474+
$this->lightningAnsweringTeam = null; // Reset for next question
443475
}
444476

445477
// Listen for buzzer events from main display
@@ -450,6 +482,15 @@ public function handleBuzzerWebhook($teamId)
450482
$this->currentTeam = Team::find($teamId);
451483
}
452484
}
485+
486+
#[On('buzzer-pressed')]
487+
public function handleBuzzerPressed($teamId)
488+
{
489+
// Track which team buzzed in during lightning round
490+
if ($this->game->status === 'lightning_round') {
491+
$this->lightningAnsweringTeam = Team::find($teamId);
492+
}
493+
}
453494

454495
private function refreshGame()
455496
{

app/Livewire/LightningRound.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Models\Game;
66
use App\Models\LightningQuestion;
77
use App\Models\Team;
8-
use App\Services\BuzzerService;
98
use App\Services\ScoringService;
109
use Livewire\Attributes\On;
1110
use Livewire\Component;
@@ -22,16 +21,6 @@ class LightningRound extends Component
2221

2322
public ?Team $currentAnsweringTeam = null;
2423

25-
protected $scoringService;
26-
27-
protected $buzzerService;
28-
29-
public function boot(ScoringService $scoringService, BuzzerService $buzzerService)
30-
{
31-
$this->scoringService = $scoringService;
32-
$this->buzzerService = $buzzerService;
33-
}
34-
3524
public function mount($gameId)
3625
{
3726
$this->game = Game::with('lightningQuestions', 'teams')->findOrFail($gameId);
@@ -63,8 +52,8 @@ private function loadCurrentQuestion()
6352
$this->currentAnsweringTeam = null;
6453
}
6554

66-
#[On('lightning-buzzer-pressed')]
67-
public function handleLightningBuzzer($teamId)
55+
#[On('buzzer-pressed')]
56+
public function handleBuzzer($teamId)
6857
{
6958
if ($this->currentAnsweringTeam) {
7059
return;
@@ -80,13 +69,46 @@ public function handleLightningBuzzer($teamId)
8069
}
8170
}
8271

72+
#[On('game-state-changed')]
73+
public function handleGameStateChanged($state, $data = [])
74+
{
75+
if ($state === 'buzzer-pressed' && isset($data['teamId'])) {
76+
$this->handleBuzzer($data['teamId']);
77+
}
78+
}
79+
80+
#[On('lightning-mark-correct')]
81+
public function handleMarkCorrect()
82+
{
83+
$this->markLightningCorrect();
84+
}
85+
86+
#[On('lightning-mark-incorrect')]
87+
public function handleMarkIncorrect()
88+
{
89+
$this->markLightningIncorrect();
90+
}
91+
92+
#[On('lightning-skip-question')]
93+
public function handleSkipQuestion()
94+
{
95+
$this->skipQuestion();
96+
}
97+
98+
#[On('lightning-next-question')]
99+
public function handleNextQuestion()
100+
{
101+
$this->nextQuestion();
102+
}
103+
83104
public function markLightningCorrect()
84105
{
85106
if (! $this->currentAnsweringTeam || ! $this->currentQuestion) {
86107
return;
87108
}
88109

89-
$this->scoringService->recordLightningAnswer(
110+
$scoringService = app(ScoringService::class);
111+
$scoringService->recordLightningAnswer(
90112
$this->currentQuestion->id,
91113
$this->currentAnsweringTeam->id,
92114
true

0 commit comments

Comments
 (0)