fix: 打手自操作改为查找打手身份

This commit is contained in:
zetaloop
2026-04-04 04:34:08 +08:00
parent 32d58ef462
commit e7dff1fc1a
2 changed files with 42 additions and 4 deletions
@@ -7,11 +7,13 @@ import (
"context" "context"
"errors" "errors"
"juwan-backend/app/player/rpc/playerservice" "juwan-backend/app/player/rpc/playerservice"
"juwan-backend/common/utils/contextj"
"juwan-backend/app/player/api/internal/svc" "juwan-backend/app/player/api/internal/svc"
"juwan-backend/app/player/api/internal/types" "juwan-backend/app/player/api/internal/types"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/status"
) )
type CreateServiceLogic struct { type CreateServiceLogic struct {
@@ -30,8 +32,28 @@ func NewCreateServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cre
} }
func (l *CreateServiceLogic) CreateService(req *types.CreateServiceReq) (resp *types.PlayerService, err error) { func (l *CreateServiceLogic) CreateService(req *types.CreateServiceReq) (resp *types.PlayerService, err error) {
_, err = l.svcCtx.PlayerRpc.AddPlayerServices(l.ctx, &playerservice.AddPlayerServicesReq{ userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
playerResp, err := l.svcCtx.PlayerRpc.GetPlayerByUserId(l.ctx, &playerservice.SearchPlayersReq{UserId: &userID})
if err != nil {
st, _ := status.FromError(err)
if st.Code().String() == "NotFound" {
return nil, errors.New("player not initialized")
}
logx.Errorf("failed to get player by user id: %v", err)
return nil, errors.New("get player failed")
}
player := playerResp.GetPlayers()
if player == nil {
return nil, errors.New("player not initialized")
}
_, err = l.svcCtx.PlayerRpc.AddPlayerServices(l.ctx, &playerservice.AddPlayerServicesReq{
PlayerId: player.Id,
GameId: req.GameId, GameId: req.GameId,
Title: req.Title, Title: req.Title,
Description: req.Description, Description: req.Description,
@@ -41,7 +63,7 @@ func (l *CreateServiceLogic) CreateService(req *types.CreateServiceReq) (resp *t
Availability: req.Availability, Availability: req.Availability,
}) })
if err != nil { if err != nil {
logx.Errorf("failed to create player service: " + err.Error()) logx.Errorf("failed to create player service: %v", err)
return nil, errors.New("failed to create player service") return nil, errors.New("failed to create player service")
} }
return return
@@ -5,6 +5,7 @@ package player
import ( import (
"context" "context"
"errors"
"juwan-backend/app/player/rpc/pb" "juwan-backend/app/player/rpc/pb"
"juwan-backend/common/utils/contextj" "juwan-backend/common/utils/contextj"
@@ -12,6 +13,7 @@ import (
"juwan-backend/app/player/api/internal/types" "juwan-backend/app/player/api/internal/types"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/status"
) )
type UpdatePlayerStatusLogic struct { type UpdatePlayerStatusLogic struct {
@@ -30,13 +32,27 @@ func NewUpdatePlayerStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext)
} }
func (l *UpdatePlayerStatusLogic) UpdatePlayerStatus(req *types.UpdatePlayerStatusReq) (resp *types.EmptyResp, err error) { func (l *UpdatePlayerStatusLogic) UpdatePlayerStatus(req *types.UpdatePlayerStatusReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextj.UserIDFrom(l.ctx) userId, err := contextj.UserIDFrom(l.ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
playerResp, err := l.svcCtx.PlayerRpc.GetPlayerByUserId(l.ctx, &pb.SearchPlayersReq{UserId: &userId})
if err != nil {
st, _ := status.FromError(err)
if st.Code().String() == "NotFound" {
return nil, errors.New("player not initialized")
}
return nil, err
}
player := playerResp.GetPlayers()
if player == nil {
return nil, errors.New("player not initialized")
}
_, err = l.svcCtx.PlayerRpc.UpdatePlayers(l.ctx, &pb.UpdatePlayersReq{ _, err = l.svcCtx.PlayerRpc.UpdatePlayers(l.ctx, &pb.UpdatePlayersReq{
Id: userId, Id: player.Id,
Status: &req.Status, Status: &req.Status,
}) })
if err != nil { if err != nil {