Skip to contentMethod: toVierConnectsGame(Game)
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-VierConnects-gui.
5:  *
6:  * ipspiel24-VierConnects-gui is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * ipspiel24-VierConnects-gui is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * ipspiel24-VierConnects-gui. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.VierConnects.gui.impl;
20: 
21: import java.lang.ref.Reference;
22: import java.lang.ref.WeakReference;
23: import java.util.LinkedHashMap;
24: import java.util.Map;
25: import java.util.Optional;
26: import java.util.concurrent.atomic.AtomicReference;
27: 
28: import de.fhdw.gaming.core.domain.Game;
29: import de.fhdw.gaming.gui.GuiObserver;
30: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsPlayer;
31: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsState;
32: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsStrategy;
33: import de.fhdw.gaming.ipspiel24.VierConnects.core.moves.VierConnectsMove;
34: import de.fhdw.gaming.ipspiel24.VierConnects.gui.VierConnectsBoardEventProvider;
35: import javafx.scene.Node;
36: 
37: /**
38:  * Implements {@link GuiObserver}.
39:  */
40: final class VierConnectsGuiObserverImpl implements GuiObserver {
41: 
42:     /**
43:      * The primary (i.e. first created) {@link VierConnectsGuiObserverImpl} instance of this class.
44:      */
45:     private static final AtomicReference<WeakReference<VierConnectsGuiObserverImpl>> INSTANCE = new AtomicReference<>();
46: 
47:     /**
48:      * The {@link VierConnectsBoardView} objects per Vier Connects game ID.
49:      */
50:     private final Map<Integer, VierConnectsBoardView> boardViews;
51: 
52:     /**
53:      * Creates an {@link VierConnectsGuiObserverImpl}.
54:      */
55:     VierConnectsGuiObserverImpl() {
56:         VierConnectsGuiObserverImpl.INSTANCE.compareAndSet(null, new WeakReference<>(this));
57:         this.boardViews = new LinkedHashMap<>();
58:     }
59: 
60:     /**
61:      * Returns a {@link VierConnectsBoardEventProvider} for a given game.
62:      *
63:      * @param gameId The game ID.
64:      * @return The {@link VierConnectsBoardEventProvider}.
65:      */
66:     static Optional<VierConnectsBoardEventProvider> getEventProvider(final int gameId) {
67:         final VierConnectsGuiObserverImpl instance = Optional.ofNullable(VierConnectsGuiObserverImpl.INSTANCE.get())
68:                 .map(Reference::get).orElse(null);
69:         if (instance == null) {
70:             return Optional.empty();
71:         }
72:         final VierConnectsBoardView boardView = instance.boardViews.get(gameId);
73:         return boardView == null ? Optional.empty() : Optional.of(new VierConnectsBoardEventProviderImpl(boardView));
74:     }
75: 
76:     @Override
77:     public Optional<Node> gameCreated(final Game<?, ?, ?, ?> game) {
78:         final Optional<Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove,
79:                 VierConnectsStrategy>> vierConnectsGame = toVierConnectsGame(game);
80:         if (vierConnectsGame.isPresent()) {
81:             final VierConnectsBoardView boardView = new VierConnectsBoardView(vierConnectsGame.get());
82:             this.boardViews.put(game.getId(), boardView);
83:             return Optional.of(boardView.getNode());
84:         }
85:         return Optional.empty();
86:     }
87: 
88:     @Override
89:     public void gamePaused(final Game<?, ?, ?, ?> game) {
90:         final Optional<Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove,
91:                 VierConnectsStrategy>> vierConnectsGame = toVierConnectsGame(game);
92:         if (vierConnectsGame.isPresent()) {
93:             final VierConnectsBoardView boardView = this.boardViews.get(game.getId());
94:             if (boardView != null) {
95:                 boardView.gamePaused(vierConnectsGame.get());
96:             }
97:         }
98:     }
99: 
100:     @Override
101:     public void gameResumed(final Game<?, ?, ?, ?> game) {
102:         final Optional<Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove,
103:                 VierConnectsStrategy>> vierConnectsGame = toVierConnectsGame(game);
104:         if (vierConnectsGame.isPresent()) {
105:             final VierConnectsBoardView boardView = this.boardViews.get(game.getId());
106:             if (boardView != null) {
107:                 boardView.gameResumed(vierConnectsGame.get());
108:             }
109:         }
110:     }
111: 
112:     @Override
113:     public void gameDestroyed(final Game<?, ?, ?, ?> game) {
114:         final Optional<Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove,
115:                 VierConnectsStrategy>> vierConnectsGame = toVierConnectsGame(game);
116:         if (vierConnectsGame.isPresent()) {
117:             final VierConnectsBoardView boardView = this.boardViews.remove(game.getId());
118:             if (boardView != null) {
119:                 boardView.destroy(vierConnectsGame.get());
120:             }
121:         }
122:     }
123: 
124:     /**
125:      * Casts the game to the right type if it is a Vier Connects game. Otherwise an empty {@link Optional} is returned.
126:      *
127:      * @param game The game to check.
128:      */
129:     private static Optional<
130:             Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove, VierConnectsStrategy>> toVierConnectsGame(
131:             final Game<?, ?, ?, ?> game) {
132:•        if (game.getState() instanceof VierConnectsState) {
133:             @SuppressWarnings("unchecked")
134:             final Game<VierConnectsPlayer, VierConnectsState, VierConnectsMove,
135:                     VierConnectsStrategy> vierConnectsGame = (Game<
136:                     VierConnectsPlayer, VierConnectsState, VierConnectsMove, VierConnectsStrategy>) game;
137:             return Optional.of(vierConnectsGame);
138:         }
139:         return Optional.empty();
140:     }
141: }