|
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.interaction.exchange.player; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer; |
|
23
|
|
|
import fr.quatrevieux.araknemu.game.exploration.exchange.ExchangeType; |
|
24
|
|
|
import fr.quatrevieux.araknemu.game.exploration.exchange.player.PlayerExchangeParty; |
|
25
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.Interaction; |
|
26
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.exchange.ExchangeInteraction; |
|
27
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.request.Invitation; |
|
28
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.request.InvitationHandler; |
|
29
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.request.RequestDialog; |
|
30
|
|
|
import fr.quatrevieux.araknemu.game.exploration.interaction.request.TargetRequestDialog; |
|
31
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap; |
|
32
|
|
|
import fr.quatrevieux.araknemu.network.game.out.exchange.ExchangeLeaved; |
|
33
|
|
|
import fr.quatrevieux.araknemu.network.game.out.exchange.ExchangeRequestError; |
|
34
|
|
|
import fr.quatrevieux.araknemu.network.game.out.exchange.ExchangeRequested; |
|
35
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Request for a player exchange |
|
39
|
|
|
* |
|
40
|
|
|
* The request store the two parties |
|
41
|
|
|
*/ |
|
42
|
|
|
public final class PlayerExchangeRequest implements ExchangeInteraction, InvitationHandler { |
|
43
|
|
|
private final Invitation invitation; |
|
44
|
|
|
|
|
45
|
|
|
@SuppressWarnings("method.invocation") |
|
46
|
1 |
|
public PlayerExchangeRequest(ExplorationPlayer initiator, ExplorationPlayer target) { |
|
47
|
1 |
|
invitation = invitation(initiator, target); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
@Override |
|
51
|
|
|
public @Nullable Interaction start() { |
|
52
|
1 |
|
return invitation.start(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
@Override |
|
56
|
|
|
public void stop() { |
|
57
|
1 |
|
invitation.stop(); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
@Override |
|
61
|
|
|
public void leave() { |
|
62
|
1 |
|
invitation.cancel(initiatorDialog(invitation)); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
@Override |
|
66
|
|
|
public void acknowledge(Invitation invitation) { |
|
67
|
1 |
|
invitation.send(new ExchangeRequested(invitation.initiator(), invitation.target(), ExchangeType.PLAYER_EXCHANGE)); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
@Override |
|
71
|
|
|
public void refuse(Invitation invitation, RequestDialog dialog) { |
|
72
|
1 |
|
invitation.send(ExchangeLeaved.cancelled()); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
@Override |
|
76
|
|
|
public void accept(Invitation invitation, TargetRequestDialog dialog) { |
|
77
|
1 |
|
for (PlayerExchangeParty party : PlayerExchangeParty.make(invitation.initiator(), invitation.target())) { |
|
78
|
1 |
|
party.start(); |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
@Override |
|
83
|
|
|
public RequestDialog initiatorDialog(Invitation invitation) { |
|
84
|
1 |
|
return new InitiatorExchangeRequestDialog(invitation); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
@Override |
|
88
|
|
|
public TargetRequestDialog targetDialog(Invitation invitation) { |
|
89
|
1 |
|
return new TargetExchangeRequestDialog(invitation); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
@Override |
|
93
|
|
|
public boolean check(Invitation invitation) { |
|
94
|
1 |
|
if (!invitation.initiator().restrictions().canExchange() || !invitation.target().restrictions().canExchange()) { |
|
95
|
1 |
|
return error(invitation, ExchangeRequestError.Error.CANT_EXCHANGE); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
if (invitation.target().interactions().busy()) { |
|
99
|
1 |
|
if (invitation.target().interactions().get(Interaction.class) instanceof ExchangeInteraction) { |
|
100
|
1 |
|
return error(invitation, ExchangeRequestError.Error.ALREADY_EXCHANGE); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return error(invitation, ExchangeRequestError.Error.CANT_EXCHANGE); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
final ExplorationMap map = invitation.initiator().map(); |
|
107
|
|
|
|
|
108
|
1 |
|
if (map == null || !map.equals(invitation.target().map())) { |
|
109
|
1 |
|
return error(invitation, ExchangeRequestError.Error.CANT_EXCHANGE); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
if (invitation.initiator().inventory().overweight()) { |
|
113
|
1 |
|
return error(invitation, ExchangeRequestError.Error.OVERWEIGHT); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Send error |
|
121
|
|
|
*/ |
|
122
|
|
|
private boolean error(Invitation invitation, ExchangeRequestError.Error error) { |
|
123
|
1 |
|
invitation.initiator().send(new ExchangeRequestError(error)); |
|
124
|
|
|
|
|
125
|
1 |
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|