This repository was archived by the owner on Jul 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (149 loc) · 5.99 KB
/
Copy pathindex.js
File metadata and controls
174 lines (149 loc) · 5.99 KB
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
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var fs = require('fs');
var path = require('path');
var os = require('os');
var extend = require('extend');
var defaultOptions = {
endOfLineChar: os.EOL
};
var debug = require('debug');
// Define some debug logging functions for easy and readable debug messages.
var log = {
main: debug('HLW'),
gameStart: debug('HLW:game-start'),
zoneChange: debug('HLW:zone-change'),
gameOver: debug('HLW:game-over')
};
// Determine the default location of the config and log files.
if (/^win/.test(os.platform())) {
log.main('Windows platform detected.');
var programFiles = 'Program Files';
if (/64/.test(os.arch())) {
programFiles += ' (x86)';
}
defaultOptions.logFile = path.join('C:', programFiles, 'Hearthstone', 'Hearthstone_Data', 'output_log.txt');
defaultOptions.configFile = path.join(process.env.LOCALAPPDATA, 'Blizzard', 'Hearthstone', 'log.config');
} else {
log.main('OS X platform detected.');
defaultOptions.logFile = path.join(process.env.HOME, 'Library', 'Logs', 'Unity', 'Player.log');
defaultOptions.configFile = path.join(process.env.HOME, 'Library', 'Preferences', 'Blizzard', 'Hearthstone', 'log.config');
}
// The watcher is an event emitter so we can emit events based on what we parse in the log.
function LogWatcher(options) {
this.options = extend({}, defaultOptions, options);
log.main('config file path: %s', this.options.configFile);
log.main('log file path: %s', this.options.logFile);
// Copy local config file to the correct location.
// We're just gonna do this every time.
var localConfigFile = path.join(__dirname, 'log.config');
fs.createReadStream(localConfigFile).pipe(fs.createWriteStream(this.options.configFile));
log.main('Copied log.config file to force Hearthstone to write to its log file.');
}
util.inherits(LogWatcher, EventEmitter);
LogWatcher.prototype.start = function () {
var self = this;
var parserState = new ParserState;
log.main('Log watcher started.');
// Begin watching the Hearthstone log file.
var fileSize = fs.statSync(self.options.logFile).size;
fs.watchFile(self.options.logFile, function (current, previous) {
if (current.mtime <= previous.mtime) { return; }
// We're only going to read the portion of the file that we have not read so far.
var newFileSize = fs.statSync(self.options.logFile).size;
var sizeDiff = newFileSize - fileSize;
if (sizeDiff < 0) {
fileSize = 0;
sizeDiff = newFileSize;
}
var buffer = new Buffer(sizeDiff);
var fileDescriptor = fs.openSync(self.options.logFile, 'r');
fs.readSync(fileDescriptor, buffer, 0, sizeDiff, fileSize);
fs.closeSync(fileDescriptor);
fileSize = newFileSize;
self.parseBuffer(buffer, parserState);
});
self.stop = function () {
fs.unwatchFile(self.options.logFile);
delete self.stop;
};
};
LogWatcher.prototype.stop = function () {};
LogWatcher.prototype.parseBuffer = function (buffer, parserState) {
var self = this;
if (!parserState) {
parserState = new ParserState;
}
// Iterate over each line in the buffer.
buffer.toString().split(this.options.endOfLineChar).forEach(function (line) {
// Check if a card is changing zones.
var zoneChangeRegex = /^\[Zone\] ZoneChangeList.ProcessChanges\(\) - id=\d* local=.* \[name=(.*) id=(\d*) zone=.* zonePos=\d* cardId=(.*) player=(\d)\] zone from ?(FRIENDLY|OPPOSING)? ?(.*)? -> ?(FRIENDLY|OPPOSING)? ?(.*)?$/
if (zoneChangeRegex.test(line)) {
var parts = zoneChangeRegex.exec(line);
var data = {
cardName: parts[1],
entityId: parseInt(parts[2]),
cardId: parts[3],
playerId: parseInt(parts[4]),
fromTeam: parts[5],
fromZone: parts[6],
toTeam: parts[7],
toZone: parts[8]
};
log.zoneChange('%s moved from %s %s to %s %s.', data.cardName, data.fromTeam, data.fromZone, data.toTeam, data.toZone);
self.emit('zone-change', data);
// Only zone transitions show both the player ID and the friendly or opposing zone type. By tracking entities going into
// the "PLAY (Hero)" zone we can then set the player's team to FRIENDLY or OPPOSING. Once both players are associated with
// a team we can emite the game-start event.
if (data.toZone === 'PLAY (Hero)') {
parserState.players.forEach(function (player) {
if (player.id === data.playerId) {
player.team = data.toTeam;
parserState.playerCount++;
if (parserState.playerCount === 2) {
log.gameStart('A game has started.');
self.emit('game-start', parserState.players);
}
}
});
}
}
// Check for players entering play and track their team IDs.
var newPlayerRegex = /\[Power\] GameState\.DebugPrintPower\(\) - TAG_CHANGE Entity=(.*) tag=PLAYER_ID value=(.)$/;
if (newPlayerRegex.test(line)) {
var parts = newPlayerRegex.exec(line);
parserState.players.push({
name: parts[1],
id: parseInt(parts[2])
});
}
// Check if the game is over.
var gameOverRegex = /\[Power\] GameState\.DebugPrintPower\(\) - TAG_CHANGE Entity=(.*) tag=PLAYSTATE value=(LOST|WON|TIED)$/;
if (gameOverRegex.test(line)) {
var parts = gameOverRegex.exec(line);
// Set the status for the appropriate player.
parserState.players.forEach(function (player) {
if (player.name === parts[1]) {
player.status = parts[2];
}
});
parserState.gameOverCount++;
// When both players have lost, emit a game-over event.
if (parserState.gameOverCount === 2) {
log.gameOver('The current game has ended.');
self.emit('game-over', parserState.players);
parserState.reset();
}
}
});
};
function ParserState() {
this.reset();
}
ParserState.prototype.reset = function () {
this.players = [];
this.playerCount = 0;
this.gameOverCount = 0;
};
// Set the entire module to our emitter.
module.exports = LogWatcher;