fr.quatrevieux.araknemu.game.handler.account.CreateCharacter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A CreateCharacter(CharactersService) 0 2 1
A handle(GameSession,AddCharacterRequest) 0 18 2
A packet() 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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.handler.account;
21
22
import fr.quatrevieux.araknemu.core.network.exception.ErrorPacket;
23
import fr.quatrevieux.araknemu.core.network.parser.PacketHandler;
24
import fr.quatrevieux.araknemu.game.account.AccountCharacter;
25
import fr.quatrevieux.araknemu.game.account.CharactersService;
26
import fr.quatrevieux.araknemu.game.account.GameAccount;
27
import fr.quatrevieux.araknemu.game.account.exception.CharacterCreationException;
28
import fr.quatrevieux.araknemu.network.game.GameSession;
29
import fr.quatrevieux.araknemu.network.game.in.account.AddCharacterRequest;
30
import fr.quatrevieux.araknemu.network.game.out.account.CharacterCreated;
31
import fr.quatrevieux.araknemu.network.game.out.account.CharacterCreationError;
32
import fr.quatrevieux.araknemu.network.game.out.account.CharactersList;
33
import org.checkerframework.checker.nullness.util.NullnessUtil;
34
35
/**
36
 * Handle character creation {@link AddCharacterRequest}
37
 */
38
public final class CreateCharacter implements PacketHandler<GameSession, AddCharacterRequest> {
39
    private final CharactersService service;
40
41 1
    public CreateCharacter(CharactersService service) {
42 1
        this.service = service;
43 1
    }
44
45
    @Override
46
    public void handle(GameSession session, AddCharacterRequest packet) throws Exception {
47 1
        final GameAccount account = NullnessUtil.castNonNull(session.account());
48
49
        try {
50 1
            service.create(AccountCharacter.fromRequest(account, packet));
51 1
        } catch (CharacterCreationException e) {
52 1
            throw new ErrorPacket(
53 1
                new CharacterCreationError(e.error()),
54 1
                e.getCause()
55
            );
56 1
        }
57
58 1
        session.send(new CharacterCreated());
59 1
        session.send(
60
            new CharactersList(
61 1
                account.remainingTime(),
62 1
                service.list(account)
63
            )
64
        );
65 1
    }
66
67
    @Override
68
    public Class<AddCharacterRequest> packet() {
69 1
        return AddCharacterRequest.class;
70
    }
71
}
72