Passed
Pull Request — master (#172)
by Vincent
13:00
created

player()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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.common.account.Permission;
23
import fr.quatrevieux.araknemu.game.account.GameAccount;
24
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
25
import fr.quatrevieux.araknemu.game.admin.exception.ContextException;
26
import fr.quatrevieux.araknemu.game.admin.exception.ExceptionHandler;
27
import fr.quatrevieux.araknemu.game.admin.executor.CommandExecutor;
28
import fr.quatrevieux.araknemu.game.listener.admin.RemoveAdminSession;
29
import fr.quatrevieux.araknemu.game.player.GamePlayer;
30
import fr.quatrevieux.araknemu.network.game.out.basic.admin.CommandResult;
31
import fr.quatrevieux.araknemu.util.LogFormatter;
32
import org.apache.logging.log4j.Level;
33
import org.apache.logging.log4j.Logger;
34
35
import java.util.Optional;
36
import java.util.Set;
37
38
/**
39
 * Admin user session
40
 */
41
public final class AdminUser implements AdminPerformer {
42
    private final AdminService service;
43
    private final GamePlayer player;
44
    private final CommandExecutor executor;
45
    private final Logger logger;
46
47
    private final AdminUserContext context;
48
    private final AdminUserCommandParser parser;
49
    private final ExceptionHandler errorHandler;
50
51 1
    public AdminUser(AdminService service, GamePlayer player, CommandExecutor executor, Logger logger) throws ContextException {
52 1
        this.service = service;
53 1
        this.player = player;
54 1
        this.executor = executor;
55 1
        this.logger = logger;
56
57 1
        this.context = new AdminUserContext(service, service.context("player", player));
58 1
        this.parser = new AdminUserCommandParser(this);
59 1
        this.errorHandler = new ExceptionHandler(this);
60
61 1
        player.dispatcher().add(new RemoveAdminSession(this));
62 1
    }
63
64
    /**
65
     * Get the admin user context handler
66
     */
67
    public AdminUserContext context() {
68 1
        return context;
69
    }
70
71
    @Override
72
    public Optional<GameAccount> account() {
73 1
        return Optional.of(player.account());
74
    }
75
76
    @Override
77
    public void execute(String command) throws AdminException {
78 1
        logger.log(Level.INFO, EXECUTE_MARKER, "[{}] {}", this, command);
79
80 1
        execute(parser.parse(command));
81 1
    }
82
83
    @Override
84
    public boolean isGranted(Set<Permission> permissions) {
85 1
        return player.account().isGranted(permissions);
86
    }
87
88
    @Override
89
    public void log(LogType type, String message, Object... arguments) {
90 1
        final String out = LogFormatter.format(message, arguments);
91
92 1
        logger.log(Level.INFO, OUTPUT_MARKER, "[{}; type={}] {}", this, type, out);
93 1
        player.send(new CommandResult(type, out));
94 1
    }
95
96
    /**
97
     * Send the exception error to the console
98
     */
99
    public void error(Throwable error) {
100 1
        errorHandler.handle(error);
101 1
    }
102
103
    /**
104
     * Destroy this user session
105
     */
106
    public void logout() {
107 1
        service.removeSession(this);
108 1
    }
109
110
    /**
111
     * Get the admin player id
112
     */
113
    public int id() {
114 1
        return player.id();
115
    }
116
117
    /**
118
     * Send packet to admin user
119
     *
120
     * @see AdminUser#log(LogType, String, Object...) to send a message to console
121
     */
122
    public void send(Object packet) {
123 1
        player.send(packet);
124 1
    }
125
126
    /**
127
     * Get the admin player
128
     */
129
    public GamePlayer player() {
130 1
        return player;
131
    }
132
133
    @Override
134
    public String toString() {
135 1
        return "account=" + player.account().id() + "; player=" + player.id();
136
    }
137
138
    private void execute(CommandParser.Arguments arguments) throws AdminException {
139 1
        final Command command = arguments.context().command(arguments.command());
140
141 1
        executor.execute(command, this, arguments);
142 1
    }
143
144
    /**
145
     * Factory create an AdminUser
146
     */
147
    public interface Factory {
148
        /**
149
         * Create the admin user
150
         *
151
         * @param service The admin service who handles the user
152
         * @param player The authorized game player
153
         *
154
         * @return The created AdminUser
155
         */
156
        public AdminUser create(AdminService service, GamePlayer player) throws AdminException;
157
    }
158
}
159