|
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-2020 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.admin; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.game.admin.context.Context; |
|
23
|
|
|
import fr.quatrevieux.araknemu.game.admin.context.ContextResolver; |
|
24
|
|
|
import fr.quatrevieux.araknemu.game.admin.exception.AdminException; |
|
25
|
|
|
import fr.quatrevieux.araknemu.game.admin.exception.ContextException; |
|
26
|
|
|
import fr.quatrevieux.araknemu.game.player.GamePlayer; |
|
27
|
|
|
|
|
28
|
|
|
import java.util.Collection; |
|
29
|
|
|
import java.util.HashMap; |
|
30
|
|
|
import java.util.Map; |
|
31
|
|
|
import java.util.function.Function; |
|
32
|
|
|
import java.util.stream.Collectors; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Server for admin |
|
36
|
|
|
*/ |
|
37
|
|
|
public final class AdminService { |
|
38
|
|
|
private final Context globalContext; |
|
39
|
|
|
private final Map<String, ContextResolver> resolvers; |
|
40
|
|
|
private final AdminUser.Factory userFactory; |
|
41
|
|
|
|
|
42
|
1 |
|
private final Map<Integer, AdminUser> usersById = new HashMap<>(); |
|
43
|
|
|
|
|
44
|
1 |
|
public AdminService(Context globalContext, Collection<ContextResolver> resolvers, AdminUser.Factory userFactory) { |
|
45
|
1 |
|
this.globalContext = globalContext; |
|
46
|
1 |
|
this.userFactory = userFactory; |
|
47
|
1 |
|
this.resolvers = resolvers |
|
48
|
1 |
|
.stream() |
|
49
|
1 |
|
.collect( |
|
50
|
1 |
|
Collectors.toMap( |
|
51
|
|
|
ContextResolver::type, |
|
52
|
1 |
|
Function.identity() |
|
53
|
|
|
) |
|
54
|
|
|
) |
|
55
|
|
|
; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get an admin user from player |
|
60
|
|
|
* |
|
61
|
|
|
* @param player The admin player |
|
62
|
|
|
*/ |
|
63
|
|
|
public AdminUser user(GamePlayer player) throws AdminException { |
|
64
|
1 |
|
if (!usersById.containsKey(player.id())) { |
|
65
|
1 |
|
usersById.put(player.id(), userFactory.create(this, player)); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
return usersById.get(player.id()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
/** |
|
72
|
|
|
* Resolve a context |
|
73
|
|
|
* |
|
74
|
|
|
* For resolve a player : |
|
75
|
|
|
* resolve("player", "PlayerName"); |
|
76
|
|
|
* |
|
77
|
|
|
* @param type The context type name |
|
78
|
|
|
* @param argument The context argument |
|
79
|
|
|
* |
|
80
|
|
|
* @return The resolved context |
|
81
|
|
|
* @throws ContextException When the context type cannot be found |
|
82
|
|
|
*/ |
|
83
|
|
|
public Context context(String type, Object argument) throws ContextException { |
|
84
|
|
|
if (!resolvers.containsKey(type)) { |
|
85
|
|
|
throw new ContextException("Context type '" + type + "' not found"); |
|
86
|
|
|
} |
|
87
|
1 |
|
|
|
88
|
1 |
|
return resolvers.get(type).resolve(globalContext, argument); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
/** |
|
92
|
|
|
* Remove the admin session |
|
93
|
|
|
*/ |
|
94
|
|
|
void removeSession(AdminUser user) { |
|
95
|
|
|
usersById.remove(user.id()); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|