|
1
|
|
|
/* |
|
2
|
|
|
* This file is part of Araknemu. |
|
3
|
|
|
* |
|
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU Lesser General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2017-2019 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.realm.host; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.event.Dispatcher; |
|
23
|
|
|
import fr.quatrevieux.araknemu.data.living.repository.player.PlayerRepository; |
|
24
|
|
|
import fr.quatrevieux.araknemu.data.value.ServerCharacters; |
|
25
|
|
|
import fr.quatrevieux.araknemu.realm.authentication.AuthenticationAccount; |
|
26
|
|
|
import fr.quatrevieux.araknemu.realm.host.event.HostsUpdated; |
|
27
|
|
|
|
|
28
|
|
|
import java.util.ArrayList; |
|
29
|
|
|
import java.util.Collection; |
|
30
|
|
|
import java.util.Collections; |
|
31
|
|
|
import java.util.HashSet; |
|
32
|
|
|
import java.util.Set; |
|
33
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
34
|
|
|
import java.util.concurrent.ConcurrentMap; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Handle game server hosts |
|
38
|
|
|
*/ |
|
39
|
|
|
public final class HostService { |
|
40
|
|
|
private final PlayerRepository playerRepository; |
|
41
|
|
|
private final Dispatcher dispatcher; |
|
42
|
|
|
|
|
43
|
1 |
|
private final ConcurrentMap<Integer, GameHost> hosts = new ConcurrentHashMap<>(); |
|
44
|
|
|
|
|
45
|
1 |
|
public HostService(PlayerRepository playerRepository, Dispatcher dispatcher) { |
|
46
|
1 |
|
this.playerRepository = playerRepository; |
|
47
|
1 |
|
this.dispatcher = dispatcher; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get all declared hosts |
|
52
|
|
|
*/ |
|
53
|
|
|
public Collection<GameHost> all() { |
|
54
|
1 |
|
return hosts.values(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Declare an host |
|
59
|
|
|
*/ |
|
60
|
|
|
public void declare(GameHost host) { |
|
61
|
1 |
|
hosts.put(host.id(), host); |
|
62
|
1 |
|
dispatcher.dispatch(new HostsUpdated(hosts.values())); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Update the host status |
|
67
|
|
|
* |
|
68
|
|
|
* @param id Host race |
|
69
|
|
|
* @param state Host state |
|
70
|
|
|
* @param canLog Can log into server |
|
71
|
|
|
*/ |
|
72
|
|
|
public void updateHost(int id, GameHost.State state, boolean canLog) { |
|
73
|
1 |
|
final GameHost host = hosts.get(id); |
|
74
|
|
|
|
|
75
|
1 |
|
host.setState(state); |
|
76
|
1 |
|
host.setCanLog(canLog); |
|
77
|
1 |
|
dispatcher.dispatch(new HostsUpdated(hosts.values())); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check if the requested host is available for login |
|
82
|
|
|
* @param id The host race |
|
83
|
|
|
*/ |
|
84
|
|
|
public boolean isAvailable(int id) { |
|
85
|
1 |
|
return hosts.containsKey(id) && hosts.get(id).canLog(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get a game host |
|
90
|
|
|
* @param id The host race |
|
91
|
|
|
*/ |
|
92
|
|
|
public GameHost get(int id) { |
|
93
|
1 |
|
return hosts.get(id); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Check if the account is logged into one of the game servers |
|
98
|
|
|
* |
|
99
|
|
|
* @param account Account to check |
|
100
|
|
|
* @param response The response callback |
|
101
|
|
|
*/ |
|
102
|
|
|
public void checkLogin(AuthenticationAccount account, GameConnector.HostResponse<Boolean> response) { |
|
103
|
1 |
|
if (hosts.isEmpty()) { |
|
104
|
1 |
|
response.response(false); |
|
105
|
1 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
new Runnable() { |
|
109
|
1 |
|
private final Set<GameHost> pending = Collections.synchronizedSet(new HashSet<>(all())); |
|
110
|
1 |
|
private boolean result = false; |
|
111
|
|
|
|
|
112
|
|
|
@Override |
|
113
|
|
|
public void run() { |
|
114
|
1 |
|
for (GameHost host : new ArrayList<>(pending)) { |
|
115
|
1 |
|
host.connector().checkLogin(account, r -> { |
|
116
|
1 |
|
result |= r; |
|
117
|
|
|
|
|
118
|
1 |
|
pending.remove(host); |
|
119
|
|
|
|
|
120
|
1 |
|
if (pending.isEmpty()) { |
|
121
|
1 |
|
response.response(result); |
|
122
|
|
|
} |
|
123
|
1 |
|
}); |
|
124
|
1 |
|
} |
|
125
|
1 |
|
} |
|
126
|
1 |
|
}.run(); |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get characters count for each hosts |
|
131
|
|
|
*/ |
|
132
|
|
|
public Collection<ServerCharacters> charactersByHost(AuthenticationAccount account) { |
|
133
|
1 |
|
return playerRepository.accountCharactersCount( |
|
134
|
1 |
|
account.id() |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Search for the friend servers |
|
140
|
|
|
* |
|
141
|
|
|
* @param pseudo The friend account pseudo |
|
142
|
|
|
*/ |
|
143
|
|
|
public Collection<ServerCharacters> searchFriendServers(String pseudo) { |
|
144
|
1 |
|
return playerRepository.serverCharactersCountByAccountPseudo(pseudo); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|