This commit is contained in:
2026-10-01 16:16:43 +07:00
parent 06c657f593
commit 9f081eeee5
6 changed files with 106 additions and 16 deletions
+3 -2
View File
@@ -31,6 +31,7 @@ import (
"evening_detective_server/internal/services/game_review_service" "evening_detective_server/internal/services/game_review_service"
"evening_detective_server/internal/services/game_service" "evening_detective_server/internal/services/game_service"
"evening_detective_server/internal/services/gameplay_service" "evening_detective_server/internal/services/gameplay_service"
"evening_detective_server/internal/services/map_service"
"evening_detective_server/internal/services/mcp_service" "evening_detective_server/internal/services/mcp_service"
"evening_detective_server/internal/services/participation_service" "evening_detective_server/internal/services/participation_service"
"evening_detective_server/internal/services/scenarios_service" "evening_detective_server/internal/services/scenarios_service"
@@ -139,7 +140,7 @@ func main() {
answersRepo := team_answers_repo.NewTeamAnswersRepo(dbpool) answersRepo := team_answers_repo.NewTeamAnswersRepo(dbpool)
screenRepo := screen_sessions_repo.NewScreenSessionsRepo(dbpool) screenRepo := screen_sessions_repo.NewScreenSessionsRepo(dbpool)
screenService := screen_service.NewScreenService(screenRepo) screenService := screen_service.NewScreenService(screenRepo)
mapRepo := game_map_repo.New(dbpool) mapService := map_service.NewMapService(game_map_repo.NewGameMapRepo(dbpool))
reviewsRepo := game_reviews_repo.New(dbpool) reviewsRepo := game_reviews_repo.New(dbpool)
gameService := game_service.NewGameService( gameService := game_service.NewGameService(
gameRepo, gameRepo,
@@ -220,7 +221,7 @@ func main() {
ticketService, ticketService,
participationService, participationService,
screenService, screenService,
mapRepo, mapService,
), ),
) )
+15 -8
View File
@@ -4,7 +4,7 @@ import (
"context" "context"
"errors" "errors"
"evening_detective_server/internal/repos/game_map_repo" "evening_detective_server/internal/services/map_service"
proto "evening_detective_server/proto" proto "evening_detective_server/proto"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@@ -22,14 +22,14 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro
if err != nil { if err != nil {
return nil, status.Error(codes.NotFound, "игра не найдена") return nil, status.Error(codes.NotFound, "игра не найдена")
} }
if err := s.mapRepo.Ensure(ctx, int(req.GameID), "organizer_desk", 1); err != nil { if err := s.mapService.Ensure(ctx, int(req.GameID), "organizer_desk", 1); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить карту") return nil, status.Error(codes.Internal, "не удалось сохранить карту")
} }
for _, team := range game.Teams { for _, team := range game.Teams {
if team.ParticipationStatus == "cancelled" { if team.ParticipationStatus == "cancelled" {
continue continue
} }
if err := s.mapRepo.Ensure(ctx, int(req.GameID), "team", team.ID); err != nil { if err := s.mapService.Ensure(ctx, int(req.GameID), "team", team.ID); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить карту") return nil, status.Error(codes.Internal, "не удалось сохранить карту")
} }
} }
@@ -38,11 +38,11 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro
return nil, status.Error(codes.Internal, "не удалось получить экраны") return nil, status.Error(codes.Internal, "не удалось получить экраны")
} }
for _, screen := range screens { for _, screen := range screens {
if err := s.mapRepo.Ensure(ctx, int(req.GameID), "screen", screen.ID); err != nil { if err := s.mapService.Ensure(ctx, int(req.GameID), "screen", screen.ID); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить карту") return nil, status.Error(codes.Internal, "не удалось сохранить карту")
} }
} }
items, err := s.mapRepo.List(ctx, int(req.GameID)) items, err := s.mapService.List(ctx, int(req.GameID))
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, "не удалось получить карту") return nil, status.Error(codes.Internal, "не удалось получить карту")
} }
@@ -61,7 +61,7 @@ func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*pro
for _, item := range items { for _, item := range items {
_, teamExists := teamNames[item.ItemID] _, teamExists := teamNames[item.ItemID]
if (item.Kind == "team" && !teamExists) || (item.Kind == "screen" && screenStates[item.ItemID] == nil) { if (item.Kind == "team" && !teamExists) || (item.Kind == "screen" && screenStates[item.ItemID] == nil) {
if err := s.mapRepo.Delete(ctx, item); err != nil { if err := s.mapService.Delete(ctx, item); err != nil {
return nil, status.Error(codes.Internal, "не удалось обновить карту") return nil, status.Error(codes.Internal, "не удалось обновить карту")
} }
continue continue
@@ -90,8 +90,15 @@ func (s *server) UpdateGameMapItem(ctx context.Context, req *proto.UpdateGameMap
if req == nil || req.GameID <= 0 || req.ItemID <= 0 || (req.Kind != "team" && req.Kind != "screen" && req.Kind != "organizer_desk") { if req == nil || req.GameID <= 0 || req.ItemID <= 0 || (req.Kind != "team" && req.Kind != "screen" && req.Kind != "organizer_desk") {
return nil, status.Error(codes.InvalidArgument, "некорректный объект карты") return nil, status.Error(codes.InvalidArgument, "некорректный объект карты")
} }
if err := s.mapRepo.Update(ctx, game_map_repo.Item{GameID: int(req.GameID), Kind: req.Kind, ItemID: int(req.ItemID), X: int(req.X), Y: int(req.Y), Hidden: req.Hidden}); err != nil { if err := s.mapService.Update(ctx, &map_service.MapItem{
if errors.Is(err, game_map_repo.ErrItemNotFound) { GameID: int(req.GameID),
Kind: req.Kind,
ItemID: int(req.ItemID),
X: int(req.X),
Y: int(req.Y),
Hidden: req.Hidden,
}); err != nil {
if errors.Is(err, map_service.ErrItemNotFound) {
return nil, status.Error(codes.NotFound, "объект карты не найден") return nil, status.Error(codes.NotFound, "объект карты не найден")
} }
return nil, status.Error(codes.Internal, "не удалось изменить карту") return nil, status.Error(codes.Internal, "не удалось изменить карту")
+4 -4
View File
@@ -1,12 +1,12 @@
package app package app
import ( import (
"evening_detective_server/internal/repos/game_map_repo"
"evening_detective_server/internal/services/answer_service" "evening_detective_server/internal/services/answer_service"
"evening_detective_server/internal/services/file_service" "evening_detective_server/internal/services/file_service"
"evening_detective_server/internal/services/game_review_service" "evening_detective_server/internal/services/game_review_service"
"evening_detective_server/internal/services/game_service" "evening_detective_server/internal/services/game_service"
"evening_detective_server/internal/services/gameplay_service" "evening_detective_server/internal/services/gameplay_service"
"evening_detective_server/internal/services/map_service"
"evening_detective_server/internal/services/participation_service" "evening_detective_server/internal/services/participation_service"
"evening_detective_server/internal/services/scenarios_service" "evening_detective_server/internal/services/scenarios_service"
"evening_detective_server/internal/services/screen_service" "evening_detective_server/internal/services/screen_service"
@@ -32,7 +32,7 @@ type server struct {
ticketService *ticket_service.Service ticketService *ticket_service.Service
participationService *participation_service.ParticipationService participationService *participation_service.ParticipationService
screenService *screen_service.ScreenService screenService *screen_service.ScreenService
mapRepo *game_map_repo.Repo mapService *map_service.MapService
} }
func NewServer( func NewServer(
@@ -48,7 +48,7 @@ func NewServer(
ticketService *ticket_service.Service, ticketService *ticket_service.Service,
participationService *participation_service.ParticipationService, participationService *participation_service.ParticipationService,
screenService *screen_service.ScreenService, screenService *screen_service.ScreenService,
mapRepo *game_map_repo.Repo, mapService *map_service.MapService,
) proto.EveningDetectiveServerServer { ) proto.EveningDetectiveServerServer {
return &server{ return &server{
usersService: usersService, usersService: usersService,
@@ -63,6 +63,6 @@ func NewServer(
ticketService: ticketService, ticketService: ticketService,
participationService: participationService, participationService: participationService,
screenService: screenService, screenService: screenService,
mapRepo: mapRepo, mapService: mapService,
} }
} }
+2 -2
View File
@@ -25,8 +25,8 @@ type Repo struct {
pool *pgxpool.Pool pool *pgxpool.Pool
} }
// New создаёт repository объектов карты. // NewGameMapRepo создаёт repository объектов карты.
func New(pool *pgxpool.Pool) *Repo { func NewGameMapRepo(pool *pgxpool.Pool) *Repo {
return &Repo{pool: pool} return &Repo{pool: pool}
} }
+34
View File
@@ -0,0 +1,34 @@
package map_service
import "evening_detective_server/internal/repos/game_map_repo"
type MapItem struct {
GameID int
Kind string
ItemID int
X int
Y int
Hidden bool
}
func mapItem(item game_map_repo.Item) *MapItem {
return &MapItem{
GameID: item.GameID,
Kind: item.Kind,
ItemID: item.ItemID,
X: item.X,
Y: item.Y,
Hidden: item.Hidden,
}
}
func toRepoItem(item *MapItem) game_map_repo.Item {
return game_map_repo.Item{
GameID: item.GameID,
Kind: item.Kind,
ItemID: item.ItemID,
X: item.X,
Y: item.Y,
Hidden: item.Hidden,
}
}
+48
View File
@@ -0,0 +1,48 @@
package map_service
import (
"context"
"errors"
"evening_detective_server/internal/repos/game_map_repo"
)
var ErrItemNotFound = errors.New("объект карты не найден")
type MapService struct {
repo *game_map_repo.Repo
}
func NewMapService(repo *game_map_repo.Repo) *MapService {
return &MapService{repo: repo}
}
func (s *MapService) Ensure(ctx context.Context, gameID int, kind string, itemID int) error {
return s.repo.Ensure(ctx, gameID, kind, itemID)
}
func (s *MapService) List(ctx context.Context, gameID int) ([]*MapItem, error) {
items, err := s.repo.List(ctx, gameID)
if err != nil {
return nil, err
}
result := make([]*MapItem, 0, len(items))
for _, item := range items {
result = append(result, mapItem(item))
}
return result, nil
}
func (s *MapService) Update(ctx context.Context, item *MapItem) error {
if err := s.repo.Update(ctx, toRepoItem(item)); err != nil {
if errors.Is(err, game_map_repo.ErrItemNotFound) {
return ErrItemNotFound
}
return err
}
return nil
}
func (s *MapService) Delete(ctx context.Context, item *MapItem) error {
return s.repo.Delete(ctx, toRepoItem(item))
}