generated from VLADIMIR/template
add game statuses
This commit is contained in:
@@ -539,6 +539,70 @@ func (s *server) UpdateGame(ctx context.Context, req *proto.UpdateGameReq) (*pro
|
||||
return &proto.UpdateGameRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) StartGame(ctx context.Context, req *proto.StartGameReq) (*proto.StartGameRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.gameService.StartGame(ctx, int(req.Id))
|
||||
if err != nil {
|
||||
return &proto.StartGameRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.StartGameRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) PauseGame(ctx context.Context, req *proto.PauseGameReq) (*proto.PauseGameRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.gameService.PauseGame(ctx, int(req.Id))
|
||||
if err != nil {
|
||||
return &proto.PauseGameRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.PauseGameRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) FinishGame(ctx context.Context, req *proto.FinishGameReq) (*proto.FinishGameRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.gameService.FinishGame(ctx, int(req.Id))
|
||||
if err != nil {
|
||||
return &proto.FinishGameRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.FinishGameRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) ResetGame(ctx context.Context, req *proto.ResetGameReq) (*proto.ResetGameRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.gameService.ResetGame(ctx, int(req.Id))
|
||||
if err != nil {
|
||||
return &proto.ResetGameRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.ResetGameRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) AddTeam(ctx context.Context, req *proto.AddTeamReq) (*proto.AddTeamRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
||||
|
||||
@@ -177,3 +177,83 @@ func (r *GamesRepo) UpdateGame(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *GamesRepo) StartGame(ctx context.Context, id int) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE games
|
||||
SET
|
||||
status = 'run',
|
||||
start_at = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *GamesRepo) PauseGame(ctx context.Context, id int) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE games
|
||||
SET
|
||||
status = 'pause',
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *GamesRepo) FinishGame(ctx context.Context, id int) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE games
|
||||
SET
|
||||
status = 'finish',
|
||||
ended_at = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *GamesRepo) ResetGame(ctx context.Context, id int) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE games
|
||||
SET
|
||||
status = 'draft',
|
||||
started_at = DEFAULT,
|
||||
ended_at = DEFAULT,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -148,6 +148,22 @@ func (s *GameService) UpdateGame(
|
||||
return s.gamesRepo.UpdateGame(ctx, id, name, description, startAt, scenarioId)
|
||||
}
|
||||
|
||||
func (s *GameService) StartGame(ctx context.Context, id int) error {
|
||||
return s.gamesRepo.StartGame(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GameService) PauseGame(ctx context.Context, id int) error {
|
||||
return s.gamesRepo.PauseGame(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GameService) FinishGame(ctx context.Context, id int) error {
|
||||
return s.gamesRepo.FinishGame(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GameService) ResetGame(ctx context.Context, id int) error {
|
||||
return s.gamesRepo.ResetGame(ctx, id)
|
||||
}
|
||||
|
||||
func (s *GameService) AddTeam(
|
||||
ctx context.Context,
|
||||
creatorId int,
|
||||
|
||||
Reference in New Issue
Block a user