Passed
Push — main ( 4741f4...6edd0c )
by Etienne
01:57 queued 13s
created

de.kleiner3.lasertag.lasertaggame.timing.GameTickTimerTask   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 35 6
A GameTickTimerTask(ITickable,IGameModeManager,ISettingsManager) 0 4 1
1
package de.kleiner3.lasertag.lasertaggame.timing;
2
3
import de.kleiner3.lasertag.lasertaggame.ITickable;
4
import de.kleiner3.lasertag.lasertaggame.settings.SettingDescription;
5
import de.kleiner3.lasertag.lasertaggame.state.management.server.synced.IGameModeManager;
6
import de.kleiner3.lasertag.lasertaggame.state.management.server.synced.ISettingsManager;
7
8
import java.util.TimerTask;
9
10
/**
11
 * Timer task to implement the ticking of the game
12
 *
13
 * @author Étienne Muser
14
 */
15
public class GameTickTimerTask extends TimerTask {
16
17
    private final ITickable game;
18
    private final IGameModeManager gameModeManager;
19
    private final ISettingsManager settingsManager;
20
21
    private int seconds = -1;
22
23
    public GameTickTimerTask(ITickable game, IGameModeManager gameModeManager, ISettingsManager settingsManager) {
24
        this.game = game;
25
        this.gameModeManager = gameModeManager;
26
        this. settingsManager = settingsManager;
27
    }
28
29
    @Override
30
    public void run() {
31
        ++seconds;
32
33
        // Get the game mode
34
        var gameMode = gameModeManager.getGameMode();
35
36
        // If game mode has infinite time
37
        if (gameMode.hasInfiniteTime()) {
38
39
            // Do regular tick every 60 seconds
40
            if (seconds % 60 == 0) {
41
                game.doTick(false);
42
            }
43
        } else {
44
45
            // Get the total duration of the game
46
            var gameDurationSeconds = settingsManager.<Long>get(SettingDescription.PLAY_TIME) * 60;
47
48
            // If is 30 seconds before end
49
            if (gameDurationSeconds - seconds == 30) {
50
                game.thirtySecondsTick();
51
                return;
52
            }
53
54
            // If is last tick
55
            if (seconds == gameDurationSeconds) {
56
                seconds = -1;
57
                game.endTick();
58
                return;
59
            }
60
61
            // Else do regular tick every 60 seconds
62
            if (seconds % 60 == 0) {
63
                game.doTick(gameDurationSeconds - seconds == 60);
64
            }
65
        }
66
    }
67
}
68