generated from VLADIMIR/template
add map
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package game_map_repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
GameID int
|
||||
Kind string
|
||||
ItemID int
|
||||
X int
|
||||
Y int
|
||||
Hidden bool
|
||||
}
|
||||
|
||||
type Repo struct{
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool) *Repo {
|
||||
return &Repo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *Repo) Ensure(ctx context.Context, gameID int, kind string, itemID int) error {
|
||||
_, err := r.pool.Exec(ctx, `
|
||||
INSERT INTO game_map_items (game_id, kind, item_id)
|
||||
VALUES ($1, $2, $3) ON CONFLICT (game_id, kind, item_id) DO NOTHING`, gameID, kind, itemID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repo) List(ctx context.Context, gameID int) ([]Item, error) {
|
||||
rows, err := r.pool.Query(ctx, `SELECT game_id, kind, item_id, x, y, hidden FROM game_map_items WHERE game_id = $1 ORDER BY kind, item_id`, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Item
|
||||
for rows.Next() {
|
||||
var item Item
|
||||
if err := rows.Scan(&item.GameID, &item.Kind, &item.ItemID, &item.X, &item.Y, &item.Hidden); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items, rows.Err()
|
||||
}
|
||||
|
||||
func (r *Repo) Update(ctx context.Context, item Item) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE game_map_items SET x = $4, y = $5, hidden = $6 WHERE game_id = $1 AND kind = $2 AND item_id = $3`, item.GameID, item.Kind, item.ItemID, item.X, item.Y, item.Hidden)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user