add command and volume

This commit is contained in:
2026-09-10 12:18:37 +07:00
parent 883e0bc3b1
commit 4944ec26b7
7 changed files with 225 additions and 176 deletions
+6 -1
View File
@@ -281,6 +281,8 @@ func screenState(session *screen_sessions_repo.ScreenSession) *proto.ScreenState
Mode: proto.ScreenMode(session.Mode),
StateVersion: session.Version,
PositionMs: session.Position,
Command: proto.ScreenCommand(session.Command),
Volume: session.Volume,
}
if time.Since(session.LastSeen) <= 5*time.Second {
state.Status = "connected"
@@ -346,7 +348,10 @@ func (s *server) SendScreenCommand(ctx context.Context, req *proto.SendScreenCom
if req.PositionMs < 0 {
return nil, status.Error(codes.InvalidArgument, "некорректная позиция")
}
if err := s.screenRepo.SetPosition(ctx, int(req.GameId), req.PositionMs); err != nil {
if req.Volume < 0 || req.Volume > 100 {
return nil, status.Error(codes.InvalidArgument, "некорректная громкость")
}
if err := s.screenRepo.SetCommand(ctx, int(req.GameId), int32(req.Command), req.PositionMs, req.Volume); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить позицию экранов")
}
return &proto.SendScreenCommandRsp{}, nil
+11 -4
View File
@@ -19,6 +19,8 @@ type ScreenSession struct {
Mode int32
Version int64
Position int64
Command int32
Volume int32
}
type ScreenSessionsRepo struct {
@@ -41,10 +43,10 @@ func (r *ScreenSessionsRepo) Create(ctx context.Context, tokenHash string) (int,
func (r *ScreenSessionsRepo) Get(ctx context.Context, id int, tokenHash string) (*ScreenSession, error) {
session := &ScreenSession{ID: id}
err := r.pool.QueryRow(ctx, `
SELECT game_id, sound_enabled, last_seen_at, mode, state_version, position_ms
SELECT game_id, sound_enabled, last_seen_at, mode, state_version, position_ms, command, volume
FROM screen_sessions
WHERE id = $1 AND access_token_hash = $2`, id, tokenHash).
Scan(&session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position)
Scan(&session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position, &session.Command, &session.Volume)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrScreenNotFound
}
@@ -61,7 +63,7 @@ func (r *ScreenSessionsRepo) Get(ctx context.Context, id int, tokenHash string)
func (r *ScreenSessionsRepo) ListByGame(ctx context.Context, gameID int) ([]*ScreenSession, error) {
rows, err := r.pool.Query(ctx, `
SELECT id, game_id, sound_enabled, last_seen_at, mode, state_version, position_ms
SELECT id, game_id, sound_enabled, last_seen_at, mode, state_version, position_ms, command, volume
FROM screen_sessions
WHERE game_id = $1
ORDER BY id`, gameID)
@@ -73,7 +75,7 @@ func (r *ScreenSessionsRepo) ListByGame(ctx context.Context, gameID int) ([]*Scr
var sessions []*ScreenSession
for rows.Next() {
session := &ScreenSession{}
if err := rows.Scan(&session.ID, &session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position); err != nil {
if err := rows.Scan(&session.ID, &session.GameID, &session.Sound, &session.LastSeen, &session.Mode, &session.Version, &session.Position, &session.Command, &session.Volume); err != nil {
return nil, err
}
sessions = append(sessions, session)
@@ -91,6 +93,11 @@ func (r *ScreenSessionsRepo) SetPosition(ctx context.Context, gameID int, positi
return err
}
func (r *ScreenSessionsRepo) SetCommand(ctx context.Context, gameID int, command int32, position int64, volume int32) error {
_, err := r.pool.Exec(ctx, `UPDATE screen_sessions SET command = $2, position_ms = $3, volume = $4, state_version = state_version + 1, updated_at = NOW() WHERE game_id = $1`, gameID, command, position, volume)
return err
}
func (r *ScreenSessionsRepo) Bind(ctx context.Context, id, gameID int, tokenHash string) error {
result, err := r.pool.Exec(ctx, `
UPDATE screen_sessions