Passed
Pull Request — master (#195)
by Vincent
16:08 queued 04:32
created

toString()   B

Complexity

Conditions 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 6
rs 8.6666
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.network.game;
21
22
import fr.quatrevieux.araknemu.common.session.SessionLog;
23
import fr.quatrevieux.araknemu.core.event.Dispatcher;
24
import fr.quatrevieux.araknemu.core.network.session.AbstractDelegatedSession;
25
import fr.quatrevieux.araknemu.core.network.session.Session;
26
import fr.quatrevieux.araknemu.game.account.GameAccount;
27
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
28
import fr.quatrevieux.araknemu.game.fight.fighter.player.PlayerFighter;
29
import fr.quatrevieux.araknemu.game.fight.spectator.Spectator;
30
import fr.quatrevieux.araknemu.game.player.GamePlayer;
31
import fr.quatrevieux.araknemu.network.AccountSession;
32
33
/**
34
 * Session wrapper for game server
35
 */
36
public final class GameSession extends AbstractDelegatedSession implements AccountSession<GameAccount>, Dispatcher {
37
    private GameAccount account;
38
    private GamePlayer player;
39
    private ExplorationPlayer exploration;
40
    private PlayerFighter fighter;
41
    private Spectator spectator;
42
    private SessionLog log;
43
44
    public GameSession(Session session) {
45 1
        super(session);
46 1
    }
47
48
    @Override
49
    public void attach(GameAccount account) {
50 1
        this.account = account;
51 1
    }
52
53
    @Override
54
    public GameAccount account() {
55 1
        return account;
56
    }
57
58
    @Override
59
    public boolean isLogged() {
60 1
        return account != null;
61
    }
62
63
    @Override
64
    public GameAccount detach() {
65 1
        return account = null;
66
    }
67
68
    /**
69
     * Set the logged player
70
     *
71
     * @throws IllegalStateException When a player is already set
72
     */
73
    public void setPlayer(GamePlayer player) {
74 1
        if (this.player != null && player != null) {
75 1
            throw new IllegalStateException("A player is already loaded");
76
        }
77
78 1
        this.player = player;
79 1
    }
80
81
    /**
82
     * Get the logged player
83
     *
84
     * @return The player instance, or null is not in game
85
     */
86
    public GamePlayer player() {
87 1
        return player;
88
    }
89
90
    /**
91
     * Get the exploration player
92
     *
93
     * @return The player instance, or null if not on exploration
94
     */
95
    public ExplorationPlayer exploration() {
96 1
        return exploration;
97
    }
98
99
    /**
100
     * Set the exploration player
101
     */
102
    public void setExploration(ExplorationPlayer exploration) {
103 1
        this.exploration = exploration;
104 1
    }
105
106
    /**
107
     * Get the fighter
108
     *
109
     * @return The fighter or null is not fighting
110
     */
111
    public PlayerFighter fighter() {
112 1
        return fighter;
113
    }
114
115
    /**
116
     * Set the fighter
117
     */
118
    public void setFighter(PlayerFighter fighter) {
119 1
        this.fighter = fighter;
120 1
    }
121
122
    /**
123
     * Get the current active spectator session
124
     */
125
    public Spectator spectator() {
126 1
        return spectator;
127
    }
128
129
    /**
130
     * Define the spectator session
131
     */
132
    public void setSpectator(Spectator spectator) {
133 1
        this.spectator = spectator;
134 1
    }
135
136
    /**
137
     * Get the session log
138
     */
139
    public SessionLog log() {
140 1
        return log;
141
    }
142
143
    /**
144
     * Define the current session log related to the current session
145
     */
146
    public void setLog(SessionLog log) {
147 1
        this.log = log;
148 1
    }
149
150
    @Override
151
    public void dispatch(Object event) {
152 1
        if (player != null) {
153 1
            player.dispatcher().dispatch(event);
154
        }
155
156 1
        if (exploration != null) {
157 1
            exploration.dispatcher().dispatch(event);
158
        }
159
160 1
        if (fighter != null) {
161 1
            fighter.dispatcher().dispatch(event);
162
        }
163
164 1
        if (spectator != null) {
165 1
            spectator.dispatcher().dispatch(event);
166
        }
167 1
    }
168
169
    @Override
170
    public String toString() {
171 1
        String str = "ip=" + channel().address().getAddress().getHostAddress();
172
173 1
        if (account != null) {
174 1
            str += "; account=" + account.id();
175
        }
176
177 1
        if (player != null) {
178 1
            str += "; player=" + player.id() + "; position=" + player.position();
179
        }
180
181 1
        if (exploration != null) {
182 1
            str += "; state=exploring";
183
        }
184
185 1
        if (fighter != null) {
186 1
            str += "; state=fighting";
187
        }
188
189 1
        if (spectator != null) {
190 1
            str += "; state=spectator";
191
        }
192
193 1
        return str;
194
    }
195
}
196