| 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.handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import fr.quatrevieux.araknemu.core.network.exception.CloseImmediately; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | import fr.quatrevieux.araknemu.core.network.parser.Packet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import fr.quatrevieux.araknemu.core.network.parser.PacketHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import fr.quatrevieux.araknemu.game.fight.Fight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import fr.quatrevieux.araknemu.network.game.GameSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Ensure that the session contains a valid fighter or spectator, and execute the corresponding packet handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @param <P> Packet to handle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | public final class EnsureFightingOrSpectator<P extends Packet> implements PacketHandler<GameSession, P> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private final PacketHandler<GameSession, P> fighting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private final PacketHandler<GameSession, P> spectator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |     public EnsureFightingOrSpectator(PacketHandler<GameSession, P> fighting, PacketHandler<GameSession, P> spectator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         this.fighting = fighting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         this.spectator = spectator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public void handle(GameSession session, P packet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         final Fight fight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         if (session.fighter() != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             fight = session.fighter().fight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         } else if (session.spectator() != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             fight = session.spectator().fight(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             throw new CloseImmediately("Not in fight"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         fight.execute(() -> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |                 if (session.fighter() != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |                     fighting.handle(session, packet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                 } else if (session.spectator() != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |                     spectator.handle(session, packet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } catch (Exception e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 session.exception(e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     public Class<P> packet() { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |         return fighting.packet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |