fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.dialog.NextQuestion   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 55
ccs 20
cts 20
cp 1
rs 10
wmc 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A Factory.create(ResponseAction) 0 7 1
A apply(ExplorationPlayer) 0 5 1
A questions() 0 6 2
A Factory.type() 0 3 1
A NextQuestion(DialogService,int[]) 0 3 1
A Factory.Factory(DialogService) 0 2 1
A check(ExplorationPlayer) 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.exploration.npc.dialog.action.dialog;
21
22
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.ResponseAction;
23
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
24
import fr.quatrevieux.araknemu.game.exploration.interaction.dialog.NpcDialog;
25
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.DialogService;
26
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.NpcQuestion;
27
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.Action;
28
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.ActionFactory;
29
import org.apache.commons.lang3.StringUtils;
30
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
31
32
import java.util.Arrays;
33
import java.util.Collection;
34
35
/**
36
 * Start next dialog question
37
 *
38
 * The check will failed if cannot found an available question (condition check)
39
 */
40
public final class NextQuestion implements Action {
41
    private final DialogService service;
42
    private final int[] questionIds;
43
44
    // Lazy loading of questions : prevent from stack overflow
45
    private @MonotonicNonNull Collection<NpcQuestion> questions;
46
47 1
    public NextQuestion(DialogService service, int[] questionIds) {
48 1
        this.service = service;
49 1
        this.questionIds = questionIds;
50 1
    }
51
52
    @Override
53
    public boolean check(ExplorationPlayer player) {
54 1
        return questions().stream().anyMatch(question -> question.check(player));
55
    }
56
57
    @Override
58
    public void apply(ExplorationPlayer player) {
59 1
        questions().stream()
60 1
            .filter(question -> question.check(player)).findFirst()
61 1
            .ifPresent(player.interactions().get(NpcDialog.class)::next)
62
        ;
63 1
    }
64
65
    /**
66
     * Lazy load questions : the first call will load questions, and next calls will always returns the same value
67
     */
68
    private Collection<NpcQuestion> questions() {
69 1
        if (questions != null) {
70 1
            return questions;
71
        }
72
73 1
        return questions = service.byIds(questionIds);
74
    }
75
76
    public static final class Factory implements ActionFactory {
77
        private final DialogService service;
78
79 1
        public Factory(DialogService service) {
80 1
            this.service = service;
81 1
        }
82
83
        @Override
84
        public String type() {
85 1
            return "NEXT";
86
        }
87
88
        @Override
89
        public Action create(ResponseAction entity) {
90 1
            return new NextQuestion(
91
                service,
92 1
                Arrays.stream(StringUtils.split(entity.arguments(), ";"))
93 1
                    .mapToInt(Integer::parseInt)
94 1
                    .toArray()
95
            );
96
        }
97
    }
98
}
99