Repository navigation
Expand file tree
/
Copy pathGameServiceImpl.java
More file actions
79 lines (69 loc) · 3.29 KB
/
Copy pathGameServiceImpl.java
File metadata and controls
79 lines (69 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package de.marvinbrieger.toothbrushgame.services;
import com.querydsl.core.types.dsl.BooleanExpression;
import de.marvinbrieger.toothbrushgame.domain.Game;
import de.marvinbrieger.toothbrushgame.domain.GameStatus;
import de.marvinbrieger.toothbrushgame.domain.MurderAssignment;
import de.marvinbrieger.toothbrushgame.domain.QGame;
import de.marvinbrieger.toothbrushgame.exceptions.GameNotFoundException;
import de.marvinbrieger.toothbrushgame.persistence.GameRepository;
import de.marvinbrieger.toothbrushgame.push.messagebuilders.GameEndedNotificationService;
import de.marvinbrieger.toothbrushgame.push.messagebuilders.MurderAssignmentNotificationService;
import de.marvinbrieger.toothbrushgame.services.interfaces.CurrentUserService;
import de.marvinbrieger.toothbrushgame.services.interfaces.EnsureGameOwnerService;
import de.marvinbrieger.toothbrushgame.services.interfaces.GameService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@AllArgsConstructor
public class GameServiceImpl implements GameService {
private final GameRepository gameRepository;
private final GameCodeService gameCodeService;
private final AssignmentGeneratorService assignmentHelperService;
private final CurrentUserService currentUserService;
private final MurderAssignmentNotificationService murderAssignmentNotificationService;
private final GameEndedNotificationService gameEndedNotificationService;
private final EnsureGameOwnerService ensureGameOwnerService;
@Override
public Game getGameById(Long id) {
return gameRepository.findById(id)
.orElseThrow(() -> new GameNotFoundException(id));
}
@Override
public Game getGameByGameCode(String gameCode) {
BooleanExpression pred = QGame
.game.gameCode.eq(gameCode)
.and(QGame.game
.gameStatus.ne(GameStatus.FINISHED));
return gameRepository.findOne(pred)
.orElseThrow(() -> new GameNotFoundException(gameCode));
}
@Override
public Game createGame(Game game) {
String gameCode = gameCodeService.getNewGameCode();
Game newGame = new Game(game, gameCode, currentUserService.getCurrentUser());
return gameRepository.save(newGame);
}
@Override
public Game startGame(Long id) {
return gameRepository.findById(id)
.map(game -> {
ensureGameOwnerService.ensureRequestedByGameOwner(game);
List<MurderAssignment> murderAssignments = assignmentHelperService.generateKillAssignments(game);
game.start(murderAssignments);
murderAssignmentNotificationService.pushMurderAssignments(murderAssignments);
return gameRepository.save(game);
})
.orElseThrow(() -> new GameNotFoundException(id));
}
@Override
public Game endGame(Long id) {
return gameRepository.findById(id)
.map(game -> {
ensureGameOwnerService.ensureRequestedByGameOwner(game);
game.end();
gameEndedNotificationService.pushGameEnding(game);
return gameRepository.save(game);})
.orElseThrow(() -> new GameNotFoundException(id));
}
}