Factory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A Factory(BankService) 0 2 1
A create(ResponseAction) 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.exchange;
21
22
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.ResponseAction;
23
import fr.quatrevieux.araknemu.game.account.bank.Bank;
24
import fr.quatrevieux.araknemu.game.account.bank.BankService;
25
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
26
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.Action;
27
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.ActionFactory;
28
import fr.quatrevieux.araknemu.network.game.out.info.Information;
29
import fr.quatrevieux.araknemu.network.out.ServerMessage;
30
import fr.quatrevieux.araknemu.util.Asserter;
31
32
/**
33
 * Action for open the bank
34
 */
35
public final class OpenBank implements Action {
36
    private final BankService service;
37
38 1
    public OpenBank(BankService service) {
39 1
        this.service = service;
40 1
    }
41
42
    @Override
43
    public boolean check(ExplorationPlayer player) {
44
        // Always true ?
45 1
        return true;
46
    }
47
48
    @Override
49
    public void apply(ExplorationPlayer player) {
50 1
        final Bank bank = service.load(player.account());
51
52 1
        if (payBankTax(player, bank)) {
53 1
            player.interactions().start(bank.exchange(player).dialog());
54
        }
55 1
    }
56
57
    /**
58
     * Try to pay the bank tax
59
     *
60
     * Check, in order, if has enough kamas :
61
     * - The player
62
     * - The bank
63
     * - The bank + player kamas
64
     *
65
     * Send information message when payed
66
     *
67
     * @return True on success, or false if there not enough kamas
68
     */
69
    private boolean payBankTax(ExplorationPlayer player, Bank bank) {
70 1
        final long cost = bank.cost();
71
72 1
        if (cost == 0) {
73 1
            return true;
74
        }
75
76 1
        if (player.inventory().kamas() >= cost) {
77 1
            player.inventory().removeKamas(cost);
78 1
            player.send(Information.bankTaxPayed(cost));
79
80 1
            return true;
81
        }
82
83 1
        if (bank.kamas() >= cost) {
84 1
            bank.removeKamas(cost);
85
86 1
            return true;
87
        }
88
89 1
        if (player.inventory().kamas() + bank.kamas() < cost) {
90 1
            player.send(ServerMessage.notEnoughKamasForBank(cost));
91
92 1
            return false;
93
        }
94
95 1
        final long payedByPlayer = cost - bank.kamas();
96
97 1
        bank.removeKamas(Asserter.castPositive(bank.kamas()));
98 1
        player.inventory().removeKamas(payedByPlayer);
99 1
        player.send(Information.bankTaxPayed(payedByPlayer));
100
101 1
        return true;
102
    }
103
104
    public static final class Factory implements ActionFactory {
105
        private final BankService service;
106
107 1
        public Factory(BankService service) {
108 1
            this.service = service;
109 1
        }
110
111
        @Override
112
        public String type() {
113 1
            return "BANK";
114
        }
115
116
        @Override
117
        public Action create(ResponseAction entity) {
118 1
            return new OpenBank(service);
119
        }
120
    }
121
}
122