-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
131 lines (120 loc) · 4.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { app, ipcMain } from "electron";
import path from "path";
import fs from "fs";
import { ApplicationStore } from "../applicationStore";
import { actions } from "../../redux/slice";
import { notifyGameFound } from "./notification";
import { locateWarningsFile } from "./locateWarningsDialog";
import { parseLogFileReverse } from "./parseLogFile";
import { refineLogFileData } from "./refineLogFileData";
import { events } from "../mixpanel";
import { Unsubscribe } from "@reduxjs/toolkit";
export class GameWatcher {
currentIntervalTime: number;
nodeInterval: NodeJS.Timer;
lastGameId: string;
isFirstScan: boolean;
applicationStore: ApplicationStore;
unsubscriber: Unsubscribe;
constructor(applicationStore: ApplicationStore) {
this.applicationStore = applicationStore;
this.unsubscriber = this.applicationStore.runtimeStore.subscribe(this.runtimeStoreSubscriber);
this.isFirstScan = true;
const settings = this.applicationStore.getState().settings;
if (!settings.coh2LogFileFound) {
// check for warnings.log file in expected folder
const theoreticalLogPath = path.resolve(
app.getPath("documents"),
"My Games",
"Company of Heroes 2",
"warnings.log",
);
if (fs.existsSync(theoreticalLogPath)) {
this.applicationStore.dispatch(actions.setLogFileFound(true));
this.applicationStore.dispatch(actions.setLogFilePath(theoreticalLogPath));
}
}
// start interval
this.lastGameId = "";
this.currentIntervalTime = settings.updateInterval;
this.setInterval(this.currentIntervalTime);
ipcMain.on("locateLogFile", async () => {
const filePath = await locateWarningsFile();
if (fs.existsSync(filePath)) {
this.applicationStore.dispatch(actions.setLogFileFound(true));
this.applicationStore.dispatch(actions.setLogFilePath(filePath));
}
});
ipcMain.on("scanForLogFile", () => {
const theoreticalLogPath = path.resolve(
app.getPath("documents"),
"My Games",
"Company of Heroes 2",
"warnings.log",
);
if (fs.existsSync(theoreticalLogPath)) {
this.applicationStore.dispatch(actions.setLogFileFound(true));
this.applicationStore.dispatch(actions.setLogFilePath(theoreticalLogPath));
}
});
ipcMain.on("reloadStats", () => {
this.lastGameId = "";
this.isFirstScan = true; // do not notify
this.intervalHandler();
});
// listen to settings changes
this.unsubscriber = this.applicationStore.runtimeStore.subscribe(this.runtimeStoreSubscriber);
}
protected runtimeStoreSubscriber = (): void => {
const settings = this.applicationStore.getState().settings;
if (settings.updateInterval !== this.currentIntervalTime) {
this.updateInterval(settings.updateInterval);
}
};
protected intervalHandler = (): void => {
const settings = this.applicationStore.getState().settings;
if (settings.coh2LogFileFound && fs.existsSync(settings.coh2LogFileLocation)) {
parseLogFileReverse(settings.coh2LogFileLocation, this.lastGameId).then((result) => {
if (result.new) {
this.lastGameId = result.newGameId;
// send a notification
if (!this.isFirstScan && this.applicationStore.getState().settings.gameNotification) {
notifyGameFound();
}
this.isFirstScan = false;
refineLogFileData(
result.game,
result.newGameId,
this.applicationStore.getState().cache.mapStats,
).then(
(gameData) => {
events.new_match_found(gameData.map);
this.applicationStore.dispatch(actions.setGameData(gameData));
},
() => {
this.lastGameId = ""; // retry in next interval
this.isFirstScan = true; // do not notify more than once when request fail
},
);
} else {
const currentGame = this.applicationStore.getState().game;
if (currentGame.state !== result.game.state) {
this.applicationStore.dispatch(actions.setGameState(result.game.state));
}
}
});
}
};
protected setInterval(checkInterval: number): void {
this.nodeInterval = setInterval(this.intervalHandler, checkInterval * 1000);
}
protected updateInterval(newCheckInterval: number): void {
clearInterval(this.nodeInterval);
this.currentIntervalTime = newCheckInterval;
this.setInterval(newCheckInterval);
}
public destroy(): void {
this.unsubscriber();
clearInterval(this.nodeInterval);
}
}