startGameTimer(long)   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
dl 12
loc 12
rs 10
c 0
b 0
f 0
1
package de.pewpewproject.lasertag.lasertaggame.state.management.client.implementation;
2
3
import de.pewpewproject.lasertag.common.util.ThreadUtil;
4
import de.pewpewproject.lasertag.lasertaggame.settings.SettingDescription;
5
import de.pewpewproject.lasertag.lasertaggame.state.management.client.IClientLasertagManager;
6
import de.pewpewproject.lasertag.lasertaggame.state.management.client.IGameModeManager;
7
import de.pewpewproject.lasertag.lasertaggame.state.management.client.ISettingsManager;
8
import de.pewpewproject.lasertag.lasertaggame.state.management.client.IUIStateManager;
9
10
import java.util.concurrent.ScheduledExecutorService;
11
import java.util.concurrent.TimeUnit;
12
13
/**
14
 * Implementation of IUIStateManager for the lasertag game
15
 *
16
 * @author Étienne Muser
17
 */
18 View Code Duplication
public class UIStateManager implements IUIStateManager {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
20
    private IClientLasertagManager clientManager;
21
22
    private ScheduledExecutorService gameTimer;
23
24
    private ScheduledExecutorService preGameTimer;
25
26
    private final IGameModeManager gameModeManager;
27
28
    private final ISettingsManager settingsManager;
29
30
    public UIStateManager(IGameModeManager gameModeManager, ISettingsManager settingsManager) {
31
32
        this.gameModeManager = gameModeManager;
33
        this.settingsManager = settingsManager;
34
    }
35
36
    public void setClientManager(IClientLasertagManager clientManager) {
37
        this.clientManager = clientManager;
38
    }
39
40
    public void startGameTimer(long gameTime) {
41
42
        synchronized (this) {
43
44
            if (gameTimer != null && !gameTimer.isShutdown()) {
45
                throw new IllegalStateException("Client gameTimer is already running.");
46
            }
47
48
            clientManager.getSyncedState().getUIState().gameTime = gameTime;
49
50
            gameTimer = ThreadUtil.createScheduledExecutor("client-lasertag-game-timer-thread-%d");
51
            gameTimer.scheduleAtFixedRate(this::gameCountDownTimerTask, 0, 1, TimeUnit.SECONDS);
52
        }
53
    }
54
55
    public void stopGameTimer() {
56
57
        synchronized (this) {
58
59
            if (gameTimer == null) {
60
                return;
61
            }
62
63
            gameTimer.shutdownNow();
64
            gameTimer = null;
65
            clientManager.getSyncedState().getUIState().gameTime = 0;
66
        }
67
    }
68
69
    public void startPreGameCountdownTimer(long startingIn) {
70
71
        synchronized (this) {
72
73
            if (preGameTimer != null && !preGameTimer.isShutdown()) {
74
                throw new IllegalStateException("Client preGameTimer is already running.");
75
            }
76
77
            clientManager.getSyncedState().getUIState().startingIn = startingIn;
78
79
            preGameTimer = ThreadUtil.createScheduledExecutor("client-lasertag-pregame-timer-thread-%d");
80
            preGameTimer.scheduleAtFixedRate(this::preGameCountDownTimerTask, 1, 1, TimeUnit.SECONDS);
81
        }
82
    }
83
84
    public void stopPreGameCountdownTimer() {
85
86
        synchronized (this) {
87
88
            if (preGameTimer == null) {
89
                return;
90
            }
91
92
            preGameTimer.shutdownNow();
93
            preGameTimer = null;
94
            clientManager.getSyncedState().getUIState().startingIn = -1;
95
        }
96
    }
97
98
    //endregion
99
100
    //region Private methods
101
102
    private void gameCountDownTimerTask() {
103
104
        // Increment the game time
105
        ++clientManager.getSyncedState().getUIState().gameTime;
106
107
        if (!gameModeManager.getGameMode().hasInfiniteTime() &&
108
                (settingsManager.<Long>get(SettingDescription.PLAY_TIME) * 60L) - clientManager.getSyncedState().getUIState().gameTime == 0) {
109
            stopGameTimer();
110
        }
111
    }
112
113
    private void preGameCountDownTimerTask() {
114
115
        // Decrement the starting in value
116
        --clientManager.getSyncedState().getUIState().startingIn;
117
118
        // If the count down is at the end
119
        if (clientManager.getSyncedState().getUIState().startingIn <= -1) {
120
121
            // Start game count down timer from the start
122
            startGameTimer(0);
123
124
            // Stop countdown timer
125
            stopPreGameCountdownTimer();
126
        }
127
    }
128
129
    //endregion
130
}
131