TournamentBean.java
8.11 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package fi.codecrew.moya.beans;
import java.util.Date;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import fi.codecrew.moya.enums.TournamentStatus;
import fi.codecrew.moya.enums.TournamentTeamMemberRole;
import fi.codecrew.moya.enums.apps.TournamentPermission;
import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.facade.TournamentFacade;
import fi.codecrew.moya.facade.TournamentGameFacade;
import fi.codecrew.moya.facade.TournamentParticipantFacade;
import fi.codecrew.moya.facade.TournamentRuleFacade;
import fi.codecrew.moya.facade.TournamentTeamMemberFacade;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Tournament;
import fi.codecrew.moya.model.TournamentGame;
import fi.codecrew.moya.model.TournamentParticipant;
import fi.codecrew.moya.model.TournamentRule;
import fi.codecrew.moya.model.TournamentTeamMember;
/**
* Session Bean implementation class TournamentBean
*/
@Stateless
@LocalBean
public class TournamentBean implements TournamentBeanLocal {
@EJB
private TournamentRuleFacade tournamentRuleFacade;
@EJB
private TournamentGameFacade tournamentGameFacade;
@EJB
private TournamentFacade tournamentFacade;
@EJB
private TournamentParticipantFacade tournamentParticipantFacade;
@EJB
private EventUserFacade eventUserFacade;
@EJB
private EventBean eventBean;
@EJB
private PermissionBean permissionBean;
@EJB
private TournamentTeamMemberFacade tournamentTeamMemberFacade;
/**
* Default constructor.
*/
public TournamentBean() {
// TODO Auto-generated constructor stub
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentRule> getRulesByGame(TournamentGame tg) {
return tournamentRuleFacade.getRulesByGame(tg);
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentGame> getGames() {
return tournamentGameFacade.getGames();
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public TournamentGame createGame(TournamentGame tg) {
tournamentGameFacade.create(tg);
return tg;
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public TournamentRule createRule(TournamentRule tr) {
tournamentRuleFacade.create(tr);
return tr;
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public TournamentGame findGame(Integer id) {
return tournamentGameFacade.find(id);
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public TournamentRule findRule(Integer id) {
return tournamentRuleFacade.find(id);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void createTournament(Tournament tournament) throws Exception {
// Assert correct event
if (eventBean.getCurrentEvent().equals(tournament.getLanEvent()))
tournamentFacade.create(tournament);
else
throw new Exception("tournament.invalid_event");
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void updateTournamentRules(TournamentRule tr) throws Exception {
// Assert correct event
if (eventBean.getCurrentEvent().equals(tr.getTournamentGame().getLanEvent())) {
tournamentRuleFacade.merge(tr);
} else {
throw new Exception("tournament.invalid_event");
}
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<Tournament> getTournamentsInStatus(TournamentStatus status, boolean useTimeConstraints, boolean invertMatch) {
if (useTimeConstraints)
if (!invertMatch)
return tournamentFacade.getTournamentsInStatusWithParticipationTimeIn(status, eventBean.getCurrentEvent());
else
return tournamentFacade.getTournamentsInStatusWithParticipationTimeNotIn(status, eventBean.getCurrentEvent());
else
return tournamentFacade.getTournamentsInStatus(status, eventBean.getCurrentEvent());
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<Tournament> getActiveTournaments() {
return tournamentFacade.getTournamentsNotInStatus(TournamentStatus.COMPLETED, eventBean.getCurrentEvent());
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public Tournament getTournamentById(Integer tournamentId) {
return tournamentFacade.find(tournamentId);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void editTournament(Tournament tournament) {
tournamentFacade.merge(tournament);
}
@Override
@RolesAllowed(TournamentPermission.S_MANAGE_ALL)
public void deleteTournament(Tournament tournament) {
tournament = tournamentFacade.merge(tournament);
tournamentFacade.remove(tournament);
}
@Override
@RolesAllowed(TournamentPermission.S_PARTICIPATE)
public void createParticipation(TournamentParticipant tournamentParticipant) throws Exception {
Tournament t = tournamentFacade.find(tournamentParticipant.getTournament().getId());
Date currentTime = new Date();
// Assert registration time is correct
if (t.getRegistrationOpensAt() != null && t.getRegistrationClosesAt() != null && currentTime.after(t.getRegistrationOpensAt()) && currentTime.before(t.getRegistrationClosesAt())) {
// Assert participant size is smaller than max
if (t.getParticipants().size() < t.getMaxParticipants()) {
TournamentTeamMember capt = null;
for (TournamentTeamMember ttm : tournamentParticipant.getTeamMembers())
if (ttm.getRole() == TournamentTeamMemberRole.CAPTAIN)
capt = ttm;
// Assert team has a captain
if (capt != null) {
// Assert team has the correct number of players for a match
if (tournamentParticipant.getTeamMembers().size() >= tournamentParticipant.getTournament().getPlayersPerMatch()) {
tournamentParticipantFacade.create(tournamentParticipant);
t.getParticipants().add(tournamentParticipant);
} else {
throw new Exception("tournament.not_enough_players");
}
} else {
throw new Exception("tournament.no_captain");
}
} else {
throw new Exception("tournament.participation_full");
}
} else {
throw new Exception("tournament.not_within_participation_time");
}
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public boolean hasParticipations(EventUser currentUser, Tournament tournament) {
for (TournamentParticipant tp : tournament.getParticipants()) {
for (TournamentTeamMember tm : tp.getTeamMembers()) {
EventUser eu = tm.getEventUser();
if (eu.equals(currentUser)) {
return true;
}
}
}
return false;
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public List<TournamentParticipant> findOwnParticipations() {
EventUser currentUser = permissionBean.getCurrentUser();
return tournamentParticipantFacade.findByParticipator(currentUser);
}
@Override
@RolesAllowed(TournamentPermission.S_VIEW)
public EventUser findAvailablePlayerForTournamentByLogin(Tournament t, String login) throws Exception {
EventUser u = eventUserFacade.findByLogin(login.toLowerCase().trim());
if (u != null) {
if (!hasParticipations(u, t))
return u;
else
throw new Exception("tournaments.participation_already_exists");
} else {
throw new Exception("tournaments.participated_user_not_found");
}
}
@Override
@RolesAllowed(TournamentPermission.S_REMOVE_OWN_PARTICIPATION)
public void deleteParticipationById(Integer tpid) throws Exception {
TournamentParticipant tp = tournamentParticipantFacade.find(tpid);
EventUser executor = permissionBean.getCurrentUser();
// Assert we have permission to remove participant
// TODO: this will include check for remove any participation too!
if (!permissionBean.hasPermission(TournamentPermission.REMOVE_OWN_PARTICIPATION) || !tp.getParticipator().equals(executor)) {
throw new Exception("tournaments.can_only_remove_own_participations");
}
// Assert that tournament has not closed participation
// TODO: admin exception!
if (!tp.getTournament().getRegistrationClosesAt().after(new Date())) {
throw new Exception("tournaments.cannot_remove_participation_from_closed_tournament");
}
// All ok, do nukage
for (TournamentTeamMember ttm : tp.getTeamMembers()) {
tournamentTeamMemberFacade.remove(ttm);
}
// Let's not leave anything in cache :)
tp.getTournament().getParticipants().remove(tp);
tournamentParticipantFacade.remove(tp);
}
}