mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 18:32:09 +00:00
Implement instant websocket room updates
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Rooms;
|
||||
|
||||
use App\Models\Room;
|
||||
|
||||
class RoomRealtimeStateService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RoomStatisticsService $statistics,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* current_round: int,
|
||||
* revealed: bool,
|
||||
* state_version: int,
|
||||
* statistics: array{average: string|null, median: string|null}|null,
|
||||
* players: array<int, array{
|
||||
* id: int,
|
||||
* name: string,
|
||||
* is_moderator: bool,
|
||||
* is_owner: bool,
|
||||
* has_voted: bool,
|
||||
* vote: string|null
|
||||
* }>
|
||||
* }
|
||||
*/
|
||||
public function for(Room $room): array
|
||||
{
|
||||
$room->refresh();
|
||||
$room->load([
|
||||
'players' => fn ($query) => $query
|
||||
->active()
|
||||
->select(['id', 'room_id', 'user_id', 'name', 'is_moderator', 'created_at'])
|
||||
->orderByDesc('is_moderator')
|
||||
->orderBy('created_at'),
|
||||
'players.votes' => fn ($query) => $query
|
||||
->where('room_id', $room->id)
|
||||
->where('round', $room->current_round),
|
||||
]);
|
||||
|
||||
return [
|
||||
'current_round' => $room->current_round,
|
||||
'revealed' => $room->revealed_at !== null,
|
||||
'state_version' => $room->state_version,
|
||||
'statistics' => $this->statistics->currentRound($room),
|
||||
'players' => $room->players
|
||||
->map(function ($player) use ($room): array {
|
||||
$vote = $player->votes->first();
|
||||
|
||||
return [
|
||||
'id' => $player->id,
|
||||
'name' => $player->name,
|
||||
'is_moderator' => $player->is_moderator,
|
||||
'is_owner' => $player->user_id === $room->owner_id,
|
||||
'has_voted' => $vote !== null,
|
||||
'vote' => $room->revealed_at !== null ? $vote?->value : null,
|
||||
];
|
||||
})
|
||||
->all(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user