Repository navigation
Expand file tree
/
Copy pathGame.java
More file actions
153 lines (126 loc) · 5.33 KB
/
Copy pathGame.java
File metadata and controls
153 lines (126 loc) · 5.33 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package de.marvinbrieger.toothbrushgame.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import de.marvinbrieger.toothbrushgame.exceptions.GameInWrongStateException;
import de.marvinbrieger.toothbrushgame.exceptions.MurderAssignmentNotFoundException;
import de.marvinbrieger.toothbrushgame.exceptions.PlayerAlreadyExistsException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Game {
@Id
@GeneratedValue
private Long id;
@Size(min = 3)
private String title;
@NotNull
@OneToOne(cascade = CascadeType.ALL)
private GamePreferences preferences;
private String gameCode;
@OneToOne(cascade = CascadeType.ALL)
private Player owner;
private GameStatus gameStatus;
@OneToMany(mappedBy = "game")
private List<Player> players;
@Embedded
private AssignmentList murderAssignments;
/**
* Creates a new game with the given game code, the title and preferences as the given game, the status {@link GameStatus#PREPARATION},
* an empty assignments list and a player list containing only the owner.
* The owner is a new player (see {@link Player#Player(Player, Game, ApplicationUser)}) based on the owner of the given game,
* belonging to the user {@code creator}.
*
* @param game An object providing some of the information reused in the created game.
* @param gameCode The game code of the created game.
* @param creator The user creating the game. Will be the user of the owner property of the created game.
*/
public Game(Game game, String gameCode, ApplicationUser creator) {
this.title = game.getTitle();
this.preferences = game.getPreferences();
this.gameCode = gameCode;
this.gameStatus = GameStatus.PREPARATION;
this.players = new ArrayList<>();
this.owner = new Player(game.getOwner(), this, creator);
this.murderAssignments = game.getMurderAssignments();
// owner is also a player
this.players.add(this.owner);
}
public boolean notInPreparation() {
return gameStatus != GameStatus.PREPARATION;
}
@JsonIgnore
public boolean notRunning() {
return gameStatus != GameStatus.RUNNING;
}
/**
* Fulfills the assignment with given ID.
*
* @param assignmentId ID of the assignment that is fulfilled
* @throws GameInWrongStateException if the game is not running
* @throws MurderAssignmentNotFoundException if there is no assignment with that ID that is part of the game
* @see MurderAssignment#commitMurder()
*/
public void commitMurder(Long assignmentId) {
// ensure game is running
if (this.notRunning())
throw new GameInWrongStateException(GameStatus.RUNNING, getGameStatus());
// get the assignment
MurderAssignment currentAssignment = murderAssignments.findAssignment(assignmentId);
// commit murder and add new assignment for the killer
currentAssignment.commitMurder();
currentAssignment.getTarget().setFailed();
murderAssignments.newAssignemntForSuccessfullKiller(currentAssignment);
}
/**
* Lets the given player join the game.
*
* @param player the joining player
* @throws GameInWrongStateException if the game is not in preparation
* @throws PlayerAlreadyExistsException if there is already a player with the same name or user in the game
*/
public void addPlayer(Player player) {
// check game state
if (notInPreparation())
throw new GameInWrongStateException(GameStatus.PREPARATION, getGameStatus());
// check neither player with same name nor with same user has already joined
boolean playerNameAlreadyExists = players.parallelStream()
.anyMatch(p -> p.getPlayerName().equalsIgnoreCase(player.getPlayerName()));
if (playerNameAlreadyExists)
throw new PlayerAlreadyExistsException(player.getPlayerName());
boolean userAlreadyJoined = players.parallelStream()
.anyMatch(p -> p.getUser().equals(player.getUser()));
if (userAlreadyJoined)
throw new PlayerAlreadyExistsException(player.getUser());
// add player
players.add(player);
}
/**
* Starts the game if it currently is in preparation and sets the assignments to the given value.
* @param assignments the new value of the {@link #murderAssignments} property
* @throws GameInWrongStateException if the game is not in preparation
*/
public void start(List<MurderAssignment> assignments) {
if (notInPreparation())
throw new GameInWrongStateException(GameStatus.PREPARATION, getGameStatus());
setGameStatus(GameStatus.RUNNING);
setMurderAssignments(new AssignmentList(this, assignments));
}
/**
* Ends the game.
*
* @throws GameInWrongStateException if the game is not running
*/
public void end() {
if (notRunning())
throw new GameInWrongStateException(GameStatus.RUNNING, getGameStatus());
setGameStatus(GameStatus.FINISHED);
}
}