This commit is contained in:
2026-09-10 03:51:22 +07:00
parent 3b85484701
commit d0c8700a7a
10 changed files with 1351 additions and 378 deletions
+70
View File
@@ -17,6 +17,7 @@ import (
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/modules/scenario_archive"
"evening_detective_server/internal/modules/string_tools"
"evening_detective_server/internal/repos/game_map_repo"
"evening_detective_server/internal/repos/scenarios_repo"
"evening_detective_server/internal/repos/screen_sessions_repo"
"evening_detective_server/internal/repos/users_repo"
@@ -47,6 +48,7 @@ type server struct {
scenarioService *scenarios_service.ScenarioService
gameService *game_service.GameService
screenRepo *screen_sessions_repo.ScreenSessionsRepo
mapRepo *game_map_repo.Repo
}
func NewServer(
@@ -56,6 +58,7 @@ func NewServer(
scenarioService *scenarios_service.ScenarioService,
gameService *game_service.GameService,
screenRepo *screen_sessions_repo.ScreenSessionsRepo,
mapRepo *game_map_repo.Repo,
) proto.EveningDetectiveServerServer {
return &server{
usersService: usersService,
@@ -64,6 +67,7 @@ func NewServer(
scenarioService: scenarioService,
gameService: gameService,
screenRepo: screenRepo,
mapRepo: mapRepo,
}
}
@@ -183,6 +187,72 @@ func (s *server) SetScreenSound(ctx context.Context, req *proto.SetScreenSoundRe
return &proto.SetScreenSoundRsp{}, nil
}
func (s *server) GetGameMap(ctx context.Context, req *proto.GetGameMapReq) (*proto.GetGameMapRsp, error) {
if !canManageScreens(ctx) {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
if req == nil || req.GameId <= 0 {
return nil, status.Error(codes.InvalidArgument, "некорректный идентификатор игры")
}
game, err := s.gameService.GetFullGame(ctx, int(req.GameId))
if err != nil {
return nil, status.Error(codes.NotFound, "игра не найдена")
}
for _, team := range game.Teams {
if err := s.mapRepo.Ensure(ctx, int(req.GameId), "team", team.ID); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить карту")
}
}
screens, err := s.screenRepo.ListByGame(ctx, int(req.GameId))
if err != nil {
return nil, status.Error(codes.Internal, "не удалось получить экраны")
}
for _, screen := range screens {
if err := s.mapRepo.Ensure(ctx, int(req.GameId), "screen", screen.ID); err != nil {
return nil, status.Error(codes.Internal, "не удалось сохранить карту")
}
}
items, err := s.mapRepo.List(ctx, int(req.GameId))
if err != nil {
return nil, status.Error(codes.Internal, "не удалось получить карту")
}
teamNames := map[int]string{}
for _, team := range game.Teams {
teamNames[team.ID] = team.Name
}
screenStates := map[int]*proto.ScreenState{}
for _, screen := range screens {
screenStates[screen.ID] = screenState(screen)
}
result := make([]*proto.MapItem, 0, len(items))
for _, item := range items {
mapped := &proto.MapItem{Kind: item.Kind, ItemId: int32(item.ItemID), X: int32(item.X), Y: int32(item.Y), Hidden: item.Hidden}
if item.Kind == "team" {
mapped.Label = teamNames[item.ItemID]
}
if state := screenStates[item.ItemID]; item.Kind == "screen" && state != nil {
mapped.Label = "Экран " + state.Id
mapped.Status = state.Status
mapped.SoundEnabled = state.SoundEnabled
}
result = append(result, mapped)
}
return &proto.GetGameMapRsp{Items: result}, nil
}
func (s *server) UpdateGameMapItem(ctx context.Context, req *proto.UpdateGameMapItemReq) (*proto.UpdateGameMapItemRsp, error) {
if !canManageScreens(ctx) {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
if req == nil || req.GameId <= 0 || req.ItemId <= 0 || (req.Kind != "team" && req.Kind != "screen") {
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 {
return nil, status.Error(codes.Internal, "не удалось изменить карту")
}
return &proto.UpdateGameMapItemRsp{}, nil
}
func canManageScreens(ctx context.Context) bool {
claims, ok := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !ok || claims == nil {