This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (180 loc) · 5.32 KB
/
index.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env node
import { RefreshingAuthProvider } from '@twurple/auth';
import { promises as fs } from 'fs';
import path from 'path';
import { Tail } from 'tail';
import { ApiClient } from '@twurple/api';
import OBSWebSocket from 'obs-websocket-js';
import config from 'config';
// Game State Constants
const GAME_STATE_LOBBY = 0;
const GAME_STATE_BATTLE_LOBBY = 1;
const GAME_STATE_BATTLE = 2;
// Twitch API config
const twitchEnabled = config.get('twitch.enable');
console.info(`Twitch integration is ${twitchEnabled ? 'enabled' : 'disabled'}`);
const clientId = config.get('twitch.client_id');
const clientSecret = config.get('twitch.client_secret');
const userId = +config.get('twitch.user_id');
const commercialSeconds = +config.get('twitch.commercial_time_seconds');
// OBS Config
const obsEnabled = config.get('obs.enable');
console.info(`OBS integration is ${obsEnabled ? 'enabled' : 'disabled'}`);
// Path to log file to watch for game state changes
const logPath = path.join(
process.env.APPDATA,
'../LocalLow/24Entertainment/Naraka/Player.log'
);
(async () => {
// Init Twitch Integration
let twitch = null;
if (twitchEnabled) {
// Read last Twitch token
const tokenData = JSON.parse(
await fs.readFile(`./tokens.${userId}.json`, 'UTF-8')
);
// Authenticate to Twitch API and refresh token if necessary
const authProvider = new RefreshingAuthProvider({
clientId,
clientSecret,
onRefresh: async (userId, newTokenData) =>
await fs.writeFile(
`./tokens.${userId}.json`,
JSON.stringify(newTokenData, null, 4),
'UTF-8'
),
});
await authProvider.addUserForToken(tokenData);
twitch = new ApiClient({ authProvider });
}
// Init OBS Integration
let obs = new OBSWebSocket();
let isObsConnected = false;
const obsUrl = config.get('obs.websocket_url');
const obsPassword = config.get('obs.password');
async function obsConnect() {
try {
const { obsWebSocketVersion, negotiatedRpcVersion } =
await obs.connect(obsUrl, obsPassword);
console.log(
`OBS Integration: connected to server ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`
);
isObsConnected = true;
} catch (error) {
console.error(
'OBS Integration: failed to connect',
error.code,
error.message
);
console.info('OBS Integration: retrying in 5s.');
setTimeout(obsConnect, 5000);
}
}
if (obsEnabled) {
obsConnect();
}
let lastSceneName = '';
async function switchScene(sceneName) {
if (!obsEnabled) {
return;
}
if (!isObsConnected) {
console.warn(
'OBS Integration: OBS not connected yet, unable to switch scene.'
);
lastSceneName = '';
return;
}
if (lastSceneName === sceneName) {
console.info(
`OBS Integration: Already on scene ${sceneName}. Skipping change.`
);
return;
}
console.info(`OBS Integration: switching to OBS Scene "${sceneName}"`);
try {
await obs.call('SetCurrentProgramScene', { sceneName });
lastSceneName = sceneName;
} catch (error) {
console.error(
'OBS Integration: failed to switch scene. Is OBS running? Does the scene exist?'
);
lastSceneName = '';
}
}
let gameState = GAME_STATE_LOBBY;
let lastGameStateChange = 0;
// Init log file watch
const tail = new Tail(logPath, { useWatchFile: true, fromBeginning: true });
tail.on('line', (line) => {
if (line.includes('vivox OnEnterLobby')) {
if (gameState === GAME_STATE_LOBBY) {
return;
}
gameState = GAME_STATE_LOBBY;
lastGameStateChange = Date.now();
console.info('Game State: Lobby');
switchScene(config.get('obs.lobby_scene'));
} else if (line.includes('vivox OnEnterBattle')) {
if (gameState === GAME_STATE_BATTLE_LOBBY) {
return;
}
gameState = GAME_STATE_BATTLE_LOBBY;
lastGameStateChange = Date.now();
console.info('Game State: Battle Lobby');
switchScene(config.get('obs.battle_lobby_scene'));
} else if (line.includes('DoHideTeamOffLoadingPage')) {
if (gameState === GAME_STATE_BATTLE) {
return;
}
gameState = GAME_STATE_BATTLE;
lastGameStateChange = Date.now();
console.info('Game State: Battle');
switchScene(config.get('obs.battle_scene'));
}
});
tail.on('error', function (error) {
console.error('ERROR while attempting to read game state: ', error);
});
// Twitch Integration Loop
if (twitchEnabled) {
let lastCommercial = 0;
async function twitchLoop() {
if (
gameState !== GAME_STATE_LOBBY ||
Date.now() - lastCommercial < 900000 || // 15 minutes
Date.now() - lastGameStateChange < 20000 // wait at least 20 seconds before starting a commercial
) {
setTimeout(twitchLoop, 1000);
return;
}
const stream = await twitch.streams.getStreamByUserId(userId);
if (stream === null) {
console.info(
'Twitch Integration: not streaming, waiting 1 minute.'
);
setTimeout(twitchLoop, 60000);
return;
}
console.info(
`Twitch Integration: stream is live, starting ${commercialSeconds}s commercial.`
);
try {
await twitch.channels.startChannelCommercial(
userId,
commercialSeconds
);
console.info(
`Twitch Integration: started ${commercialSeconds}s commercial.`
);
} catch (error) {
console.error(
'Twitch Integration: failed to start commercial. Is a commercial running already?'
);
}
lastCommercial = Date.now();
setTimeout(twitchLoop, 1000);
}
twitchLoop();
}
})();