mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 18:32:09 +00:00
67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?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,
|
|
* allow_participants_to_manage_rounds: bool,
|
|
* 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,
|
|
'allow_participants_to_manage_rounds' => $room->allow_participants_to_manage_rounds,
|
|
'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(),
|
|
];
|
|
}
|
|
}
|