cost(GameAccount)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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.account.bank;
21
22
import fr.quatrevieux.araknemu.data.living.entity.account.AccountBank;
23
import fr.quatrevieux.araknemu.data.living.repository.account.AccountBankRepository;
24
import fr.quatrevieux.araknemu.data.living.repository.account.BankItemRepository;
25
import fr.quatrevieux.araknemu.game.GameConfiguration;
26
import fr.quatrevieux.araknemu.game.account.GameAccount;
27
import fr.quatrevieux.araknemu.game.item.ItemService;
28
import fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank;
29
import org.checkerframework.checker.index.qual.NonNegative;
30
31
import java.util.List;
32
import java.util.stream.Collectors;
33
34
/**
35
 * Handle the bank accounts
36
 */
37
public final class BankService {
38
    private final ItemService itemService;
39
    private final AccountBankRepository bankRepository;
40
    private final BankItemRepository itemRepository;
41
    private final GameConfiguration.EconomyConfiguration configuration;
42
43 1
    public BankService(ItemService itemService, AccountBankRepository bankRepository, BankItemRepository itemRepository, GameConfiguration.EconomyConfiguration configuration) {
44 1
        this.itemService = itemService;
45 1
        this.bankRepository = bankRepository;
46 1
        this.itemRepository = itemRepository;
47 1
        this.configuration = configuration;
48 1
    }
49
50
    /**
51
     * Load the bank for the given account
52
     */
53
    public Bank load(GameAccount account) {
54 1
        final Bank bank = new Bank(this, bankRepository.get(new AccountBank(account.id(), account.serverId(), 0)));
55
56 1
        bank.dispatcher().register(new SaveBank(itemRepository));
57
58 1
        return bank;
59
    }
60
61
    /**
62
     * Get the cost for open the bank account
63
     */
64
    public long cost(GameAccount account) {
65 1
        return cost(new AccountBank(account.id(), account.serverId(), 0));
66
    }
67
68
    void save(Bank bank) {
69 1
        bankRepository.add(bank.entity());
70 1
    }
71
72
    /**
73
     * Load bank items from the bank
74
     */
75
    List<BankEntry> loadItems(Bank bank) {
76 1
        return itemRepository
77 1
            .byBank(bank.entity())
78 1
            .stream()
79 1
            .map(entity -> new BankEntry(bank, entity, itemService.retrieve(entity.itemTemplateId(), entity.effects())))
80 1
            .collect(Collectors.toList())
81
        ;
82
    }
83
84
    /**
85
     * Get the cost for open the bank account
86
     */
87
    @SuppressWarnings("return") // bankCostPerEntry is positive, but cannot be annotated
88
    @NonNegative long cost(AccountBank bank) {
89 1
        return (long) (itemRepository.count(bank) * configuration.bankCostPerEntry());
90
    }
91
}
92