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.game.handler.account; |
||
21 | |||
22 | import fr.quatrevieux.araknemu.common.session.SessionLog; |
||
23 | import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException; |
||
24 | import fr.quatrevieux.araknemu.core.network.exception.CloseWithPacket; |
||
25 | import fr.quatrevieux.araknemu.core.network.parser.PacketHandler; |
||
26 | import fr.quatrevieux.araknemu.game.player.GamePlayer; |
||
27 | import fr.quatrevieux.araknemu.game.player.PlayerService; |
||
28 | import fr.quatrevieux.araknemu.game.player.event.GameJoined; |
||
29 | import fr.quatrevieux.araknemu.network.game.GameSession; |
||
30 | import fr.quatrevieux.araknemu.network.game.in.account.ChoosePlayingCharacter; |
||
31 | import fr.quatrevieux.araknemu.network.game.out.account.CharacterSelected; |
||
32 | import fr.quatrevieux.araknemu.network.game.out.account.CharacterSelectionError; |
||
33 | import fr.quatrevieux.araknemu.network.game.out.info.Error; |
||
34 | import fr.quatrevieux.araknemu.network.game.out.info.Information; |
||
35 | |||
36 | /** |
||
37 | * Handle character select for entering game |
||
38 | */ |
||
39 | public final class SelectCharacter implements PacketHandler<GameSession, ChoosePlayingCharacter> { |
||
40 | private final PlayerService service; |
||
41 | |||
42 | 1 | public SelectCharacter(PlayerService service) { |
|
43 | 1 | this.service = service; |
|
44 | 1 | } |
|
45 | |||
46 | @Override |
||
47 | @SuppressWarnings("contracts.precondition") // Cannot prove that account is not null here |
||
48 | public void handle(GameSession session, ChoosePlayingCharacter packet) { |
||
49 | final GamePlayer player; |
||
50 | |||
51 | 1 | synchronized (session) { |
|
0 ignored issues
–
show
|
|||
52 | 1 | if (session.player() != null) { |
|
53 | 1 | throw new CloseWithPacket(new CharacterSelectionError()); |
|
54 | } |
||
55 | |||
56 | try { |
||
57 | 1 | player = service.load(session, packet.id()); |
|
58 | 1 | player.register(session); |
|
59 | 1 | } catch (EntityNotFoundException e) { |
|
60 | 1 | throw new CloseWithPacket(new CharacterSelectionError()); |
|
61 | 1 | } |
|
62 | 1 | } |
|
63 | |||
64 | 1 | session.send(new CharacterSelected(player)); |
|
65 | 1 | player.dispatch(new GameJoined()); |
|
66 | 1 | session.log().ifPresent(log -> log.setPlayerId(player.id())); |
|
67 | |||
68 | 1 | session.send(Error.welcome()); |
|
69 | 1 | session.log().flatMap(SessionLog::last).ifPresent(log -> session.send(Information.lastLogin(log.startDate(), log.ipAddress()))); |
|
70 | 1 | session.send(Information.currentIpAddress(session.channel().address().getAddress().getHostAddress())); |
|
71 | 1 | } |
|
72 | |||
73 | @Override |
||
74 | public Class<ChoosePlayingCharacter> packet() { |
||
75 | 1 | return ChoosePlayingCharacter.class; |
|
76 | } |
||
77 | } |
||
78 |
For an in-depth explanation of how locks work, you may refer to Chapter 20 Thread Synchronization of Inside the Java Virtual Machine by Bill Venners available online.