Passed
Push — master ( d5a095...990124 )
by Vincent
04:15 queued 12s
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.context.Context;
25
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
26
import fr.quatrevieux.araknemu.game.admin.exception.ContextException;
27
import fr.quatrevieux.araknemu.game.admin.exception.ExceptionHandler;
28
import fr.quatrevieux.araknemu.game.admin.executor.CommandExecutor;
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 GamePlayer player;
43
    private final CommandExecutor executor;
44
    private final CommandParser parser;
45
    private final Context self;
46
    private final ExceptionHandler errorHandler;
47
    private final Logger logger;
48
49 1
    public AdminUser(GamePlayer player, CommandExecutor executor, CommandParser parser, Context self, ExceptionHandler errorHandler, Logger logger) throws ContextException {
50 1
        this.player = player;
51 1
        this.executor = executor;
52 1
        this.parser = parser;
53 1
        this.self = self;
54 1
        this.errorHandler = errorHandler;
55 1
        this.logger = logger;
56 1
    }
57
58
    @Override
59
    public Optional<GameAccount> account() {
60 1
        return Optional.of(player.account());
61
    }
62
63
    @Override
64
    public void execute(String command) throws AdminException {
65 1
        logger.log(Level.INFO, EXECUTE_MARKER, "[{}] {}", this, command);
66
67 1
        execute(parser.parse(this, command));
68 1
    }
69
70
    @Override
71
    public boolean isGranted(Set<Permission> permissions) {
72 1
        return player.account().isGranted(permissions);
73
    }
74
75
    @Override
76
    public Context self() {
77 1
        return self;
78
    }
79
80
    @Override
81
    public void log(LogType type, String message, Object... arguments) {
82 1
        final String out = LogFormatter.format(message, arguments);
83
84 1
        logger.log(Level.INFO, OUTPUT_MARKER, "[{}; type={}] {}", this, type, out);
85 1
        player.send(new CommandResult(type, out));
86 1
    }
87
88
    /**
89
     * Send the exception error to the console
90
     */
91
    public void error(Throwable error) {
92 1
        errorHandler.handle(this, error);
93 1
    }
94
95
    /**
96
     * Get the admin player id
97
     */
98
    public int id() {
99 1
        return player.id();
100
    }
101
102
    /**
103
     * Send packet to admin user
104
     *
105
     * @see AdminUser#log(LogType, String, Object...) to send a message to console
106
     */
107
    public void send(Object packet) {
108 1
        player.send(packet);
109 1
    }
110
111
    /**
112
     * Get the admin player
113
     */
114
    public GamePlayer player() {
115 1
        return player;
116
    }
117
118
    @Override
119
    public String toString() {
120 1
        return "account=" + player.account().id() + "; player=" + player.id();
121
    }
122
123
    private void execute(CommandParser.Arguments arguments) throws AdminException {
124 1
        final Command command = arguments.context().command(arguments.command());
125
126 1
        executor.execute(command, this, arguments);
127 1
    }
128
129
    /**
130
     * Factory create an AdminUser
131
     */
132
    public interface Factory {
133
        /**
134
         * Create the admin user
135
         *
136
         * @param player The authorized game player
137
         *
138
         * @return The created AdminUser
139
         */
140
        public AdminUser create(GamePlayer player) throws AdminException;
141
    }
142
}
143