Skip to content

Commit 3f80eaa

Browse files
committed
Update playtime statistics on the fly
1 parent d159665 commit 3f80eaa

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

src/main/java/tibetiroka/esmanager/instance/SessionHelper.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010

1111
package tibetiroka.esmanager.instance;
1212

13+
import javafx.animation.KeyFrame;
14+
import javafx.animation.Timeline;
1315
import javafx.application.Platform;
1416
import javafx.beans.property.SimpleBooleanProperty;
17+
import javafx.event.ActionEvent;
18+
import javafx.event.EventHandler;
19+
import javafx.util.Duration;
1520
import org.jetbrains.annotations.NotNull;
1621
import org.slf4j.Logger;
1722
import org.slf4j.LoggerFactory;
@@ -75,9 +80,20 @@ public static void start(@NotNull Instance instance, boolean debug) {
7580
//starting process
7681
new Thread(() -> {
7782
Main.configureThread(Thread.currentThread(), false);
78-
Instant start = Instant.now();
83+
Timeline timer = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<>() {
84+
Instant last = Instant.now();
85+
86+
@Override
87+
public void handle(ActionEvent event) {
88+
Instant now = Instant.now();
89+
instance.getStatistics().advanceActiveTimeCounter(now.toEpochMilli() - last.toEpochMilli());
90+
last = now;
91+
}
92+
}), new KeyFrame(new Duration(100)));
93+
timer.setCycleCount(Timeline.INDEFINITE);
7994
try {
8095
Process process = builder.start();
96+
timer.play();
8197
LogUtils.logAsync(process.getInputStream(), Level.DEBUG);
8298
LogUtils.logAsync(process.getErrorStream(), Level.WARN);
8399
instance.getStatistics().advanceLaunchCounter();

src/main/java/tibetiroka/esmanager/ui/MainApplication.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010

1111
package tibetiroka.esmanager.ui;
1212

13+
import javafx.animation.KeyFrame;
14+
import javafx.animation.Timeline;
1315
import javafx.application.Application;
1416
import javafx.application.Platform;
1517
import javafx.collections.ObservableList;
1618
import javafx.css.PseudoClass;
19+
import javafx.event.ActionEvent;
1720
import javafx.event.EventHandler;
1821
import javafx.fxml.FXMLLoader;
1922
import javafx.scene.Node;
@@ -29,7 +32,7 @@
2932
import javafx.scene.input.MouseEvent;
3033
import javafx.stage.Modality;
3134
import javafx.stage.Stage;
32-
import javafx.stage.WindowEvent;
35+
import javafx.util.Duration;
3336
import org.slf4j.Logger;
3437
import org.slf4j.LoggerFactory;
3538
import tibetiroka.esmanager.Main;
@@ -244,25 +247,19 @@ public void start(Stage primaryStage) throws Exception {
244247
primaryStage.setMaximized(true);
245248
primaryStage.setMinHeight(200);
246249
primaryStage.setMinWidth(200);
247-
primaryStage.setOnCloseRequest(new EventHandler<>() {
248-
private final Instant launcherLoaded = Instant.now();
249-
250-
@Override
251-
public void handle(WindowEvent event) {
252-
event.consume();
253-
//prevent exiting if updates are in progress
254-
if(PluginManager.updateInProgressProperty().get()) {
250+
primaryStage.setOnCloseRequest(event -> {
251+
event.consume();
252+
//prevent exiting if updates are in progress
253+
if(PluginManager.updateInProgressProperty().get()) {
254+
return;
255+
}
256+
for(Instance instance : Instance.getInstances()) {
257+
if(instance.getTracker().isWorkingProperty().get()) {
255258
return;
256259
}
257-
for(Instance instance : Instance.getInstances()) {
258-
if(instance.getTracker().isWorkingProperty().get()) {
259-
return;
260-
}
261-
}
262-
GlobalStatistics.getGlobalStatistics().advanceActiveTimeCounter(Instant.now().toEpochMilli() - launcherLoaded.toEpochMilli());
263-
AppConfiguration.saveAll();
264-
primaryStage.close();
265260
}
261+
AppConfiguration.saveAll();
262+
primaryStage.close();
266263
});
267264
MAIN_WINDOW_STYLESHEETS = scene.getRoot().getStylesheets();
268265
STYLE_SHEET_LISTS.add(MAIN_WINDOW_STYLESHEETS);
@@ -332,6 +329,18 @@ public void handle(WindowEvent event) {
332329
}
333330
Platform.runLater(() -> MainController.getController().getPluginListBox().setDisable(false));
334331
}, "Plugin Query Thread").start();
332+
Timeline timer = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<>() {
333+
Instant last = Instant.now();
334+
335+
@Override
336+
public void handle(ActionEvent event) {
337+
Instant now = Instant.now();
338+
GlobalStatistics.getGlobalStatistics().advanceActiveTimeCounter(now.toEpochMilli() - last.toEpochMilli());
339+
last = now;
340+
}
341+
}), new KeyFrame(new Duration(100)));
342+
timer.setCycleCount(Timeline.INDEFINITE);
343+
timer.play();
335344
} catch(Exception e) {
336345
e.printStackTrace();
337346
throw new Error(e);

0 commit comments

Comments
 (0)