|
4 | 4 | import util.Executable;
|
5 | 5 | import util.Sleep;
|
6 | 6 |
|
| 7 | +import java.util.stream.Stream; |
| 8 | + |
7 | 9 | public abstract class Banking extends Executable {
|
8 | 10 |
|
9 |
| - protected static final Area[] bankAreas = Bank.getAreas(); |
| 11 | + private static final Area[] ALL_BANK_AND_DEPOSIT_BOX_AREAS = Stream.concat(Stream.of(Bank.AREAS), Stream.of(DepositBox.AREAS)).toArray(Area[]::new); |
| 12 | + |
| 13 | + private final boolean useDepositBoxes; |
| 14 | + |
| 15 | + protected enum BankType { |
| 16 | + BANK, |
| 17 | + DEPOSIT_BOX |
| 18 | + } |
| 19 | + |
| 20 | + private BankType currentBankType; |
10 | 21 | public boolean succeeded;
|
11 | 22 |
|
| 23 | + public Banking() { |
| 24 | + this.useDepositBoxes = false; |
| 25 | + } |
| 26 | + |
| 27 | + public Banking(final boolean useDepositBoxes) { |
| 28 | + this.useDepositBoxes = useDepositBoxes; |
| 29 | + } |
| 30 | + |
12 | 31 | @Override
|
13 | 32 | public void run() throws InterruptedException {
|
14 |
| - if (!Bank.inAnyBank(myPosition())) { |
15 |
| - getWalking().webWalk(bankAreas); |
16 |
| - } else if (!getBank().isOpen()) { |
17 |
| - openBank(); |
| 33 | + if (!playerInBank()) { |
| 34 | + walkToBank(); |
| 35 | + } else if (!isBankOpen()) { |
| 36 | + currentBankType = openBank(); |
18 | 37 | } else {
|
19 |
| - succeeded = bank(); |
| 38 | + succeeded = bank(currentBankType); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private boolean playerInBank() { |
| 43 | + if (useDepositBoxes) { |
| 44 | + return Stream.of(ALL_BANK_AND_DEPOSIT_BOX_AREAS).anyMatch(area -> area.contains(myPosition())); |
20 | 45 | }
|
| 46 | + return Stream.of(Bank.AREAS).anyMatch(area -> area.contains(myPosition())); |
21 | 47 | }
|
22 | 48 |
|
23 |
| - void openBank() throws InterruptedException { |
| 49 | + private boolean walkToBank() { |
| 50 | + if (useDepositBoxes) { |
| 51 | + return getWalking().webWalk(ALL_BANK_AND_DEPOSIT_BOX_AREAS); |
| 52 | + } |
| 53 | + return getWalking().webWalk(Bank.AREAS); |
| 54 | + } |
| 55 | + |
| 56 | + boolean isBankOpen() { |
| 57 | + if (useDepositBoxes) { |
| 58 | + return getDepositBox().isOpen(); |
| 59 | + } |
| 60 | + return getBank().isOpen(); |
| 61 | + } |
| 62 | + |
| 63 | + BankType openBank() throws InterruptedException { |
| 64 | + if (useDepositBoxes && getDepositBox() != null) { |
| 65 | + if (getDepositBox().open()) { |
| 66 | + Sleep.sleepUntil(() -> getDepositBox().isOpen(), 5000); |
| 67 | + } |
| 68 | + return BankType.DEPOSIT_BOX; |
| 69 | + } |
| 70 | + |
24 | 71 | if (getBank().open()) {
|
25 | 72 | Sleep.sleepUntil(() -> getBank().isOpen(), 5000);
|
26 | 73 | }
|
| 74 | + return BankType.BANK; |
27 | 75 | }
|
28 | 76 |
|
29 | 77 | /**
|
30 | 78 | * Execute banking operation
|
31 | 79 | *
|
32 | 80 | * @return whether the operation was a success or not
|
33 | 81 | */
|
34 |
| - protected abstract boolean bank(); |
| 82 | + protected abstract boolean bank(final BankType currentBankType); |
35 | 83 |
|
36 | 84 | @Override
|
37 | 85 | public void onEnd() {
|
38 |
| - if (getBank().isOpen()) { |
39 |
| - closeBank(); |
40 |
| - } |
| 86 | + closeBank(); |
41 | 87 | }
|
42 | 88 |
|
43 | 89 | private void closeBank() {
|
44 |
| - if (getBank().close()) { |
| 90 | + if (currentBankType == BankType.DEPOSIT_BOX) { |
| 91 | + if (getDepositBox().isOpen() && getDepositBox().close()) { |
| 92 | + Sleep.sleepUntil(() -> !getDepositBox().isOpen(), 2500); |
| 93 | + } |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (getBank().isOpen() && getBank().close()) { |
45 | 98 | Sleep.sleepUntil(() -> !getBank().isOpen(), 2500);
|
46 | 99 | }
|
47 | 100 | }
|
|
0 commit comments