Test Failed
Pull Request — master (#172)
by Vincent
09:48
created

fr.quatrevieux.araknemu.game.admin.account.Ban   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 81
dl 0
loc 138
ccs 65
cts 65
cp 1
rs 10
c 1
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A unban(AdminPerformer) 0 3 1
A Arguments.execute(AdminPerformer,Ban) 0 3 1
A createArguments() 0 3 1
A Arguments.UnbanArguments.execute(AdminPerformer,Ban) 0 3 1
A Arguments.ListArguments.execute(AdminPerformer,Ban) 0 3 1
A banFor(AdminPerformer,ForArguments) 0 9 2
A list(AdminPerformer) 0 18 4
A name() 0 3 1
A checkCanBan(AdminPerformer) 0 7 3
A build(Builder) 0 18 1
A Ban(GameAccount,BanishmentService) 0 3 1
A Arguments.ForArguments.execute(AdminPerformer,Ban) 0 3 1
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.account;
21
22
import fr.quatrevieux.araknemu.common.account.LivingAccount;
23
import fr.quatrevieux.araknemu.common.account.Permission;
24
import fr.quatrevieux.araknemu.common.account.banishment.BanEntry;
25
import fr.quatrevieux.araknemu.common.account.banishment.BanishmentService;
26
import fr.quatrevieux.araknemu.game.account.GameAccount;
27
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
28
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
29
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
30
import fr.quatrevieux.araknemu.game.admin.exception.CommandException;
31
import fr.quatrevieux.araknemu.game.admin.executor.argument.handler.ConcatRestOfArgumentsHandler;
32
import fr.quatrevieux.araknemu.game.admin.executor.argument.type.SubArguments;
33
import fr.quatrevieux.araknemu.game.admin.executor.argument.type.SubArgumentsCommandTrait;
34
import org.kohsuke.args4j.Argument;
35
import org.kohsuke.args4j.spi.SubCommand;
36
import org.kohsuke.args4j.spi.SubCommands;
37
38
import java.time.Duration;
39
import java.util.List;
40
41
/**
42
 * Handle banishment for an account
43
 */
44 1
public final class Ban extends AbstractCommand<Ban.Arguments> implements SubArgumentsCommandTrait<Ban.Arguments> {
45 1
    private final GameAccount account;
46 1
    private final BanishmentService<GameAccount> service;
47 1
48
    public Ban(GameAccount account, BanishmentService<GameAccount> service) {
49
        this.account = account;
50
        this.service = service;
51 1
    }
52 1
53 1
    @Override
54 1
    protected void build(Builder builder) {
55 1
        builder
56
            .description("Ban an account")
57 1
            .help(
58 1
                formatter -> formatter
59 1
                    .synopsis("[context] ban [for|list|unban] ARGUMENTS")
60
61 1
                    .options("for DURATION CAUSE", "Ban the account for the given duration.\nThe duration is in format [days]dT[hours]h[minutes]m[seconds]s\nNote: You cannot ban a game master account.")
62 1
                    .options("list", "List all banishment entries for the account")
63 1
                    .options("unban", "Remove current banishment for the account")
64 1
65
                    .example("${account:John} ban list", "Display all ban entries of the 'John' account")
66 1
                    .example("${account:John} ban for 5d", "Ban 'John' for 5 days")
67
                    .example("${player:Alan} ban for 10m", "Ban 'Alan' account for 10 minutes")
68 1
                    .example("${player:Alan} unban", "Unban 'Alan' account")
69
            )
70
            .requires(Permission.MANAGE_ACCOUNT)
71
        ;
72 1
    }
73
74
    @Override
75
    public String name() {
76
        return "ban";
77 1
    }
78 1
79
    /**
80
     * Unban the account
81 1
     */
82
    private void unban(AdminPerformer performer) {
83 1
        service.unban(account);
84 1
        performer.success("The account {} has been unbaned", account.pseudo());
85
    }
86
87 1
    /**
88 1
     * List all ban entries
89
     */
90
    private void list(AdminPerformer performer) {
91 1
        final List<BanEntry<GameAccount>> entries = service.list(account);
92 1
93
        if (entries.isEmpty()) {
94
            performer.success("No ban entries found");
95 1
            return;
96
        }
97 1
98
        performer.success("List of ban entries for {} :", account.pseudo());
99
100
        for (BanEntry<GameAccount> entry : entries) {
101
            performer.info(
102
                "{} - {} (by {}) : {}{}",
103 1
                entry.start(),
104 1
                entry.end(),
105 1
                entry.banisher().map(LivingAccount::pseudo).orElse("system"),
106
                entry.cause(),
107
                entry.active() ? " <b>active</b>" : ""
108
            );
109
        }
110
    }
111 1
112
    /**
113 1
     * Ban the account for a given duration
114 1
     */
115 1
    private void banFor(AdminPerformer performer, Arguments.ForArguments arguments) throws CommandException {
116
        checkCanBan(performer);
117
118 1
        final BanEntry<GameAccount> entry = performer.account().isPresent()
119
            ? service.ban(account, arguments.duration, arguments.cause, performer.account().get())
0 ignored issues
show
introduced by
Call "Optional#isPresent()" before accessing the value.
Loading history...
120 1
            : service.ban(account, arguments.duration, arguments.cause)
121 1
        ;
122
123 1
        performer.success("The account {} has been banned until {}", account.pseudo(), entry.end());
124 1
    }
125 1
126 1
    /**
127 1
     * Check if the account can be banned
128
     */
129 1
    private void checkCanBan(AdminPerformer performer) throws CommandException {
130 1
        if (performer.account().filter(performerAccount -> performerAccount.id() == account.id()).isPresent()) {
131
            throw new CommandException(name(), "Cannot ban yourself");
132
        }
133
134
        if (account.isMaster()) {
135
            throw new CommandException(name(), "Cannot ban a game master");
136 1
        }
137
    }
138 1
139 1
    @Override
140
    public Arguments createArguments() {
141
        return new Arguments();
142 1
    }
143 1
144 1
    public static final class Arguments implements SubArguments<Ban> {
145 1
        @Argument(required = true)
146
        @SubCommands({
147
            @SubCommand(name = "for", impl = ForArguments.class),
148 1
            @SubCommand(name = "list", impl = ListArguments.class),
149 1
            @SubCommand(name = "unban", impl = UnbanArguments.class),
150
        })
151
        private SubArguments<Ban> action;
152
153
        @Override
154
        public void execute(AdminPerformer performer, Ban command) throws AdminException {
155 1
            action.execute(performer, command);
156 1
        }
157
158
        public static final class ForArguments implements SubArguments<Ban> {
159 1
            @Argument(index = 0, required = true)
160
            private Duration duration;
161
162
            @Argument(index = 1, required = true, handler = ConcatRestOfArgumentsHandler.class)
163
            private String cause;
164
165
            @Override
166 1
            public void execute(AdminPerformer performer, Ban command) throws AdminException {
167 1
                command.banFor(performer, this);
168
            }
169
        }
170 1
171 1
        public static final class ListArguments implements SubArguments<Ban> {
172
            @Override
173 1
            public void execute(AdminPerformer performer, Ban command) throws AdminException {
174
                command.list(performer);
175
            }
176
        }
177
178
        public static final class UnbanArguments implements SubArguments<Ban> {
179
            @Override
180 1
            public void execute(AdminPerformer performer, Ban command) throws AdminException {
181
                command.unban(performer);
182 1
            }
183 1
        }
184 1
    }
185
}
186