-
Notifications
You must be signed in to change notification settings - Fork 19
/
InviteManager.java
69 lines (53 loc) · 2.26 KB
/
InviteManager.java
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
package com.cardshifter.server.model;
import com.cardshifter.api.*;
import com.cardshifter.api.outgoing.*;
import com.cardshifter.core.game.*;
import org.apache.log4j.*;
import java.util.*;
import java.util.concurrent.*;
public class InviteManager {
private static final Logger logger = LogManager.getLogger(InviteManager.class);
private final Server server;
private final ServerHandler<GameInvite> inviteHandler = new ServerHandler<>();
private final Set<Integer> currentInvitors = Collections.newSetFromMap(new ConcurrentHashMap<>());
public InviteManager(Server server) {
this.server = server;
}
private GameInvite createInvite(ClientIO sender, String gameType) {
// Can only have one open invite at a time, see https://github.com/Cardshifter/Cardshifter/issues/373
if (!currentInvitors.add(sender.getId())) {
logger.info("[" + sender + "] already has an open invitation");
return null;
}
ServerGame game = server.createGame(gameType);
GameInvite invite = new GameInvite(this, server.getMainChat(), sender, game, gameType, inviteHandler.newId());
inviteHandler.add(invite);
logger.info("Created [" + invite + "]");
return invite;
}
public void createAndSend(ClientIO sender, ClientIO receiver, String gameType) {
GameInvite invite = createInvite(sender, gameType);
if (invite != null) {
logger.info("Sending [" + invite + "] to [" + receiver + "]");
invite.sendInvite(receiver);
} else {
sender.sendToClient(new ServerErrorMessage("You already have a game invitation open."));
}
}
public void createAndAdd(ClientIO sender, ClientIO receiver, String gameType) {
GameInvite invite = createInvite(sender, gameType);
Objects.requireNonNull(invite);
invite.addPlayer(receiver);
}
public void remove(GameInvite invite) {
logger.info("Removing [" + invite + "]");
inviteHandler.remove(invite);
currentInvitors.remove(invite.getHost().getId());
}
public GameInvite getInvite(int id) {
return inviteHandler.get(id);
}
public Collection<GameInvite> getInvites() {
return inviteHandler.all().values();
}
}