This commit is contained in:
2026-10-01 16:12:38 +07:00
parent f8d78b04ff
commit 06c657f593
8 changed files with 152 additions and 28 deletions
@@ -0,0 +1,43 @@
package screen_service
import (
"time"
"evening_detective_server/internal/repos/screen_sessions_repo"
)
type ScreenSession struct {
ID int
GameID *int
Sound bool
LastSeen time.Time
Mode int32
Version int64
Position int64
Command int32
Volume int32
TimerLabel string
TimerDurationSeconds int64
TimerRemainingSeconds int64
TimerState int32
TimerEndsAt *time.Time
}
func mapSession(session *screen_sessions_repo.ScreenSession) *ScreenSession {
return &ScreenSession{
ID: session.ID,
GameID: session.GameID,
Sound: session.Sound,
LastSeen: session.LastSeen,
Mode: session.Mode,
Version: session.Version,
Position: session.Position,
Command: session.Command,
Volume: session.Volume,
TimerLabel: session.TimerLabel,
TimerDurationSeconds: session.TimerDurationSeconds,
TimerRemainingSeconds: session.TimerRemainingSeconds,
TimerState: session.TimerState,
TimerEndsAt: session.TimerEndsAt,
}
}
@@ -0,0 +1,77 @@
package screen_service
import (
"context"
"errors"
"evening_detective_server/internal/repos/screen_sessions_repo"
)
var ErrScreenNotFound = errors.New("Экран не найден")
type ScreenService struct {
repo *screen_sessions_repo.ScreenSessionsRepo
}
func NewScreenService(repo *screen_sessions_repo.ScreenSessionsRepo) *ScreenService {
return &ScreenService{repo: repo}
}
func (s *ScreenService) Create(ctx context.Context, tokenHash string) (int, error) {
return s.repo.Create(ctx, tokenHash)
}
func (s *ScreenService) Get(ctx context.Context, id int, tokenHash string) (*ScreenSession, error) {
session, err := s.repo.Get(ctx, id, tokenHash)
if err != nil {
return nil, mapError(err)
}
return mapSession(session), nil
}
func (s *ScreenService) ListByGame(ctx context.Context, gameID int) ([]*ScreenSession, error) {
sessions, err := s.repo.ListByGame(ctx, gameID)
if err != nil {
return nil, err
}
result := make([]*ScreenSession, 0, len(sessions))
for _, session := range sessions {
result = append(result, mapSession(session))
}
return result, nil
}
func (s *ScreenService) SetMode(ctx context.Context, gameID int, mode int32) error {
return s.repo.SetMode(ctx, gameID, mode)
}
func (s *ScreenService) SetTimer(ctx context.Context, gameID int, duration int64, label string) error {
return s.repo.SetTimer(ctx, gameID, duration, label)
}
func (s *ScreenService) SetTimerCommand(ctx context.Context, gameID int, command int32) error {
return s.repo.SetTimerCommand(ctx, gameID, command)
}
func (s *ScreenService) SetCommand(ctx context.Context, gameID int, command int32, position int64, volume int32) error {
return s.repo.SetCommand(ctx, gameID, command, position, volume)
}
func (s *ScreenService) Bind(ctx context.Context, id, gameID int, tokenHash string) error {
return mapError(s.repo.Bind(ctx, id, gameID, tokenHash))
}
func (s *ScreenService) Unbind(ctx context.Context, id int) error {
return mapError(s.repo.Unbind(ctx, id))
}
func (s *ScreenService) SetSound(ctx context.Context, id int, enabled bool) error {
return mapError(s.repo.SetSound(ctx, id, enabled))
}
func mapError(err error) error {
if errors.Is(err, screen_sessions_repo.ErrScreenNotFound) {
return ErrScreenNotFound
}
return err
}