Skip to content

Commit 6ffe2ef

Browse files
fix: Bugs
1 parent bfb43aa commit 6ffe2ef

File tree

5 files changed

+50
-169
lines changed

5 files changed

+50
-169
lines changed

app/Livewire/HostControl.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function loadGame($gameId)
7373
}
7474

7575
// If game just started and no team is selected, select Team Illuminate
76-
if ($this->game->status === 'main_game' && ! $this->currentTeam) {
76+
if ($this->game->status === 'main_game' && !$this->currentTeam) {
7777
$teamIlluminate = $this->teams->where('name', 'Team Illuminate')->first();
7878
if ($teamIlluminate) {
7979
$this->selectCurrentTeam($teamIlluminate->id);
@@ -122,7 +122,7 @@ public function triggerBuzzer($teamId)
122122
{
123123
$team = Team::find($teamId);
124124

125-
if (! $team) {
125+
if (!$team) {
126126
return;
127127
}
128128

@@ -183,7 +183,7 @@ public function setDailyDoubleWager($amount)
183183

184184
private function getMaximumWager()
185185
{
186-
if (! $this->currentTeam) {
186+
if (!$this->currentTeam) {
187187
return 400; // Default max if no team selected
188188
}
189189

@@ -202,7 +202,7 @@ private function getMaximumWager()
202202

203203
private function calculateWagerOptions()
204204
{
205-
if (! $this->currentTeam) {
205+
if (!$this->currentTeam) {
206206
$this->wagerOptions = [];
207207

208208
return;
@@ -218,13 +218,13 @@ private function calculateWagerOptions()
218218
$increments = [200, 300, 400, 500, 600, 800, 1000, 1200, 1500, 2000];
219219

220220
foreach ($increments as $amount) {
221-
if ($amount <= $maxWager && ! in_array($amount, $options)) {
221+
if ($amount <= $maxWager && !in_array($amount, $options)) {
222222
$options[] = $amount;
223223
}
224224
}
225225

226226
// If team score is positive and not already in options, add it as "True Daily Double"
227-
if ($this->currentTeam->score > 0 && ! in_array($this->currentTeam->score, $options)) {
227+
if ($this->currentTeam->score > 0 && !in_array($this->currentTeam->score, $options)) {
228228
$options[] = $this->currentTeam->score;
229229
}
230230

@@ -238,7 +238,7 @@ public function markCorrect($teamId = null)
238238
{
239239
$team = $teamId ? Team::find($teamId) : $this->currentTeam;
240240

241-
if (! $team || ! $this->selectedClue) {
241+
if (!$team || !$this->selectedClue) {
242242
return;
243243
}
244244

@@ -302,7 +302,7 @@ public function markIncorrect($teamId = null)
302302
{
303303
$team = $teamId ? Team::find($teamId) : $this->currentTeam;
304304

305-
if (! $team || ! $this->selectedClue) {
305+
if (!$team || !$this->selectedClue) {
306306
return;
307307
}
308308

@@ -319,7 +319,7 @@ public function markIncorrect($teamId = null)
319319
}
320320

321321
// Clear current team to open buzzers for others (unless Daily Double)
322-
if (! $this->selectedClue->is_daily_double) {
322+
if (!$this->selectedClue->is_daily_double) {
323323
$this->currentTeam = null;
324324
$this->game->current_team_id = null;
325325
$this->game->save();
@@ -384,7 +384,7 @@ public function closeClue()
384384
public function adjustScore($teamId, $amount)
385385
{
386386
$team = Team::find($teamId);
387-
if (! $team) {
387+
if (!$team) {
388388
return;
389389
}
390390

@@ -409,7 +409,7 @@ public function startLightningRound()
409409
$gameService = app(GameService::class);
410410
$gameService->transitionToLightningRound($this->game->id);
411411

412-
$this->game->refresh();
412+
$this->currentTeam = null; // Clear current team
413413

414414
// Broadcast event to tell game board to navigate to lightning round
415415
broadcast(new GameStateChanged($this->game->id, 'lightning-round-started'));
@@ -457,16 +457,7 @@ public function markLightningCorrect()
457457
));
458458

459459
// Get next question
460-
$nextQuestion = $this->game->lightningQuestions
461-
->where('is_answered', false)
462-
->sortBy('order_position')
463-
->first();
464-
465-
if ($nextQuestion) {
466-
$nextQuestion->update(['is_current' => true]);
467-
// Broadcast that we've moved to the next question
468-
broadcast(new GameStateChanged($this->game->id, 'lightning-next-question'));
469-
}
460+
$this->nextLightningQuestion();
470461
}
471462
}
472463

@@ -511,27 +502,11 @@ public function markLightningIncorrect()
511502
}
512503
}
513504

514-
public function skipLightningQuestion()
515-
{
516-
$this->dispatch('lightning-skip-question')->to(LightningRound::class);
517-
$this->currentTeam = null; // Reset when skipping
518-
$this->game->current_team_id = null;
519-
$this->game->save();
520-
}
521-
522-
public function nextLightningQuestion()
523-
{
524-
$this->dispatch('lightning-next-question')->to(LightningRound::class);
525-
$this->currentTeam = null; // Reset for next question
526-
$this->game->current_team_id = null;
527-
$this->game->save();
528-
}
529-
530505
// Listen for buzzer events from main display
531506
#[On('buzzer-webhook-received')]
532507
public function handleBuzzerWebhook($teamId)
533508
{
534-
if ($this->showClueModal && ! $this->currentTeam && ! $this->selectedClue->is_daily_double) {
509+
if ($this->showClueModal && !$this->currentTeam && !$this->selectedClue->is_daily_double) {
535510
$this->currentTeam = Team::find($teamId);
536511
}
537512
}
@@ -551,7 +526,7 @@ public function handleBuzzerPressed($teamId)
551526
public function handleGameStateChanged($state, $data = [])
552527
{
553528
// Handle team selection events from both manual triggers and buzzer API
554-
if (in_array($state, ['team-selected', 'buzzer-pressed']) && isset($data['teamId'])) {
529+
if (in_array($state, ['team-selected', 'buzzer-pressed', 'lightning-round-started']) && isset($data['teamId'])) {
555530
$this->currentTeam = Team::find($data['teamId']);
556531
$this->game->current_team_id = $data['teamId'];
557532
$this->refreshGame();
@@ -582,4 +557,31 @@ public function render()
582557
return view('livewire.host-control')
583558
->layout('layouts.game');
584559
}
560+
561+
/**
562+
* @return void
563+
*/
564+
public function nextLightningQuestion(): void
565+
{
566+
$nextQuestion = $this->game->lightningQuestions
567+
->where('is_answered', false)
568+
->sortBy('order_position')
569+
->first();
570+
571+
if ($nextQuestion) {
572+
$nextQuestion->update(['is_current' => true]);
573+
// Broadcast that we've moved to the next question
574+
broadcast(new GameStateChanged($this->game->id, 'lightning-next-question'));
575+
} else {
576+
// Lightning round complete
577+
$this->game->update(['status' => 'finished', 'current_team_id' => null]);
578+
$this->game->refresh();
579+
580+
// Dispatch to browser for redirect - this will trigger the JavaScript listener
581+
$this->dispatch('lightning-round-complete');
582+
583+
// Also broadcast to all clients
584+
broadcast(new GameStateChanged($this->game->id, 'lightning-round-complete', []));
585+
}
586+
}
585587
}

app/Livewire/LightningRound.php

Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace App\Livewire;
44

5+
use App\Events\GameStateChanged;
56
use App\Models\Game;
67
use App\Models\LightningQuestion;
78
use App\Models\Team;
8-
use App\Services\ScoringService;
99
use Livewire\Attributes\On;
1010
use Livewire\Component;
1111

@@ -31,7 +31,7 @@ public function mount($gameId)
3131
$this->game->refresh();
3232

3333
// Broadcast to clear team selection on all clients
34-
broadcast(new \App\Events\GameStateChanged($this->game->id, 'team-deselected'));
34+
broadcast(new GameStateChanged($this->game->id, 'team-deselected'));
3535
}
3636

3737
$this->loadCurrentQuestion();
@@ -43,7 +43,7 @@ private function loadCurrentQuestion()
4343
->where('is_current', true)
4444
->first();
4545

46-
if (! $this->currentQuestion) {
46+
if (!$this->currentQuestion) {
4747
$this->currentQuestion = $this->game->lightningQuestions
4848
->where('is_answered', false)
4949
->sortBy('order_position')
@@ -69,7 +69,7 @@ public function handleBuzzer($teamId)
6969
return;
7070
}
7171

72-
if (! in_array($teamId, $this->buzzerOrder)) {
72+
if (!in_array($teamId, $this->buzzerOrder)) {
7373
$this->buzzerOrder[] = $teamId;
7474
}
7575

@@ -83,7 +83,7 @@ public function handleBuzzer($teamId)
8383
$this->dispatch('buzzer-accepted', teamId: $teamId);
8484

8585
// Broadcast to all clients that a team has buzzed in
86-
broadcast(new \App\Events\GameStateChanged($this->game->id, 'team-selected', ['teamId' => $teamId]));
86+
broadcast(new GameStateChanged($this->game->id, 'team-selected', ['teamId' => $teamId]));
8787
}
8888
}
8989

@@ -98,30 +98,6 @@ public function handleGameStateChanged($state, $data = [])
9898
}
9999
}
100100

101-
#[On('lightning-mark-correct')]
102-
public function handleMarkCorrect()
103-
{
104-
$this->markLightningCorrect();
105-
}
106-
107-
#[On('lightning-mark-incorrect')]
108-
public function handleMarkIncorrect()
109-
{
110-
$this->markLightningIncorrect();
111-
}
112-
113-
#[On('lightning-skip-question')]
114-
public function handleSkipQuestion()
115-
{
116-
$this->skipQuestion();
117-
}
118-
119-
#[On('lightning-next-question')]
120-
public function handleNextQuestion()
121-
{
122-
$this->nextQuestion();
123-
}
124-
125101
#[On('lightning-refresh')]
126102
public function handleRefresh()
127103
{
@@ -130,92 +106,6 @@ public function handleRefresh()
130106
$this->loadCurrentQuestion();
131107
}
132108

133-
public function markLightningCorrect()
134-
{
135-
if (! $this->currentAnsweringTeam || ! $this->currentQuestion) {
136-
return;
137-
}
138-
139-
$scoringService = app(ScoringService::class);
140-
$scoringService->recordLightningAnswer(
141-
$this->currentQuestion->id,
142-
$this->currentAnsweringTeam->id,
143-
true
144-
);
145-
146-
$this->dispatch('play-sound', sound: 'correct');
147-
$this->nextQuestion();
148-
}
149-
150-
public function markLightningIncorrect()
151-
{
152-
if (! $this->currentAnsweringTeam || ! $this->currentQuestion) {
153-
return;
154-
}
155-
156-
// Remove team from current question and try next in buzzer order
157-
array_shift($this->buzzerOrder);
158-
159-
if (! empty($this->buzzerOrder)) {
160-
$nextTeamId = $this->buzzerOrder[0];
161-
$this->currentAnsweringTeam = Team::find($nextTeamId);
162-
163-
// Update game state to set next active team
164-
$this->game->update(['current_team_id' => $nextTeamId]);
165-
$this->game->refresh();
166-
167-
$this->dispatch('buzzer-accepted', teamId: $nextTeamId);
168-
broadcast(new \App\Events\GameStateChanged($this->game->id, 'team-selected', ['teamId' => $nextTeamId]));
169-
} else {
170-
// No more teams buzzed, clear active team and move to next question
171-
$this->currentAnsweringTeam = null;
172-
$this->game->update(['current_team_id' => null]);
173-
$this->game->refresh();
174-
$this->nextQuestion();
175-
}
176-
177-
$this->dispatch('play-sound', sound: 'incorrect');
178-
}
179-
180-
public function nextQuestion()
181-
{
182-
if ($this->currentQuestion) {
183-
$this->currentQuestion->update([
184-
'is_current' => false,
185-
'is_answered' => true,
186-
]);
187-
}
188-
189-
// Clear active team when moving to next question
190-
$this->game->update(['current_team_id' => null]);
191-
192-
$nextQuestion = $this->game->lightningQuestions
193-
->where('is_answered', false)
194-
->sortBy('order_position')
195-
->first();
196-
197-
if ($nextQuestion) {
198-
$nextQuestion->update(['is_current' => true]);
199-
$this->loadCurrentQuestion();
200-
$this->dispatch('reset-buzzers');
201-
} else {
202-
// Lightning round complete
203-
$this->game->update(['status' => 'finished', 'current_team_id' => null]);
204-
$this->game->refresh();
205-
206-
// Dispatch to browser for redirect - this will trigger the JavaScript listener
207-
$this->dispatch('lightning-round-complete');
208-
209-
// Also broadcast to all clients
210-
broadcast(new \App\Events\GameStateChanged($this->game->id, 'lightning-round-complete', []));
211-
}
212-
}
213-
214-
public function skipQuestion()
215-
{
216-
$this->nextQuestion();
217-
}
218-
219109
public function render()
220110
{
221111
return view('livewire.lightning-round')

resources/js/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ if (window.gameId) {
3232
.listen('GameStateChanged', (e) => {
3333
console.log('Game state changed:', e);
3434
Livewire.dispatch('game-state-changed', e);
35+
36+
if (e.state === 'lightning-round-complete') {
37+
window.location.href = '/game/' + window.gameId + '/leaderboard';
38+
}
3539
})
3640
.listen('DailyDoubleTriggered', (e) => {
3741
console.log('Daily double triggered:', e);

resources/views/livewire/host-control.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ class="cursor-pointer px-6 py-3 bg-red-600 hover:bg-red-700 rounded-lg font-bold
146146
@endif
147147

148148
<!-- Skip/Next buttons always available -->
149-
<button wire:click="skipLightningQuestion"
150-
class="cursor-pointer px-6 py-3 bg-gray-600 hover:bg-gray-700 rounded-lg font-bold text-white transition-all">
151-
Skip Question
152-
</button>
153149
<button wire:click="nextLightningQuestion"
154150
class="cursor-pointer px-6 py-3 bg-purple-600 hover:bg-purple-700 rounded-lg font-bold text-white transition-all hover:scale-105">
155151
→ Next Question

resources/views/livewire/lightning-round.blade.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,4 @@ class="text-center backdrop-blur-lg bg-black/50 rounded-2xl p-12 border border-p
185185
</div>
186186
</div>
187187
@endif
188-
189-
<script>
190-
document.addEventListener('livewire:init', function () {
191-
Livewire.on('lightning-round-complete', function () {
192-
// Redirect immediately to leaderboard
193-
@if($game)
194-
window.location.href = '{{ route("game.leaderboard", ["gameId" => $game->id]) }}';
195-
@endif
196-
});
197-
});
198-
</script>
199188
</div>

0 commit comments

Comments
 (0)