Passed
Pull Request — master (#179)
by Vincent
10:13
created

Colors.parse(String)   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
cc 2
crap 2
rs 10
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-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.admin;
21
22
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
23
import fr.quatrevieux.araknemu.game.admin.executor.argument.handler.ConcatRestOfArgumentsHandler;
24
import fr.quatrevieux.araknemu.game.admin.help.CommandHelp;
25
import fr.quatrevieux.araknemu.network.game.out.chat.ServerMessage;
26
import org.kohsuke.args4j.Argument;
27
import org.kohsuke.args4j.Option;
28
29
/**
30
 * Command for send a server message
31
 */
32 1
public abstract class AbstractMessageCommand extends AbstractCommand<AbstractMessageCommand.Arguments> {
33
    /**
34
     * Configure the help page
35
     */
36
    protected abstract void configureHelp(CommandHelp.Builder builder);
37
38
    /**
39
     * Send the message packet
40
     */
41
    protected abstract void send(AdminPerformer performer, ServerMessage packet);
42
43
    @Override
44
    protected final void build(Builder builder) {
45 1
        builder
46 1
            .help(help -> {
47 1
                configureHelp(help);
48 1
                help.with("colors.enum", Colors.class);
49 1
            })
50 1
            .arguments(Arguments::new)
51
        ;
52 1
    }
53
54
    @Override
55
    public final String name() {
56 1
        return "msg";
57
    }
58
59
    @Override
60
    public final void execute(AdminPerformer performer, Arguments arguments) throws AdminException {
61 1
        final ServerMessage message = new ServerMessage(buildMessage(arguments));
62
63 1
        send(performer, message);
64 1
    }
65
66
    private String buildMessage(Arguments arguments) {
67 1
        String message = arguments.message;
68
69 1
        if (!arguments.noMarkdown) {
70 1
            message = applyMarkdown(message);
71
        }
72
73 1
        if (arguments.color != null) {
74 1
            message = "<font color=\"" + Colors.parse(arguments.color) + "\">" + message + "</font>";
75
        }
76
77 1
        return message;
78
    }
79
80
    private String applyMarkdown(String message) {
81 1
        return message
82 1
            .replaceAll("\\*\\*([^*]+?)\\*\\*", "<b>$1</b>")
83 1
            .replaceAll("\\*([^*]+?)\\*", "<i>$1</i>")
84 1
            .replaceAll("__([^_]+?)__", "<u>$1</u>")
85 1
            .replaceAll("_([^_]+?)_", "<i>$1</i>")
86 1
            .replaceAll("\\[(.+?)\\]\\((.+?)\\)", "<u><a href=\"$2\">$1</a></u>")
87
        ;
88
    }
89
90 1
    public static final class Arguments {
91
        @Argument(
92
            metaVar = "MESSAGE", handler = ConcatRestOfArgumentsHandler.class, required = true,
93
            usage = "The message to send.\n" +
94
                "If --no-md option is not set, the message will be parsed as markdown format.\n" +
95
                "Available formats :\n" +
96
                "- *message* or _message_ for <i>italic</i>\n" +
97
                "- **message** for <i>bold</i>\n" +
98
                "- __message__ for <u>underline</u>\n" +
99
                "- [test](http://link) for display a clickable link\n" +
100
                "- Multiple formats can be applied like : ___**message**___ for <i><u><b>italic + underline + bold</b></u></i>\n" +
101
                "<font color=\"#CC0000\">Note: The message must be the last argument, <b>after all options</b></font>"
102
        )
103
        private String message;
104
105
        @Option(
106
            name = "--color", metaVar = "COLOR",
107
            usage = "Define a color. The value can be an hexadecimal value, without #, in form RRGGBB, or one of the defined color : {{colors.enum}}."
108
        )
109
        private String color;
110
111
        @Option(name = "--no-md", usage = "Disable markdown parsing on the message")
112
        private boolean noMarkdown;
113
    }
114
115 1
    public static enum Colors {
116 1
        RED("C10000"),
117 1
        GREEN("009900"),
118 1
        BLUE("0066FF"),
119 1
        ORANGE("DD7700"),
120
        ;
121
122
        private final String color;
123
124 1
        Colors(String color) {
125 1
            this.color = color;
126 1
        }
127
128
        /**
129
         * Get the hexadecimal color from a color name, if defined
130
         */
131
        public static String parse(String color) {
132
            try {
133 1
                return "#" + valueOf(color.toUpperCase()).color;
134 1
            } catch (IllegalArgumentException e) {
135 1
                return "#" + color;
136
            }
137
        }
138
    }
139
}
140