Skip to content

Commit e936ca0

Browse files
committedNov 29, 2015
fix wrong parser issue
1 parent bc3c3bf commit e936ca0

13 files changed

+188
-113
lines changed
 

‎bin/hslog

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
var cli = require('cli');
44
var fs = require('fs');
5-
var Hearthstone = require('../hearthstone');
5+
var touch = require('touch');
66

7+
var Hearthstone = require('../hearthstone');
78
var Configurator = Hearthstone.Configurator;
89
var Parser = Hearthstone.Parser;
10+
var Parsers = Hearthstone.Parsers;
911
var Logger = Hearthstone.Logger;
1012
var Reporter = Hearthstone.Reporter;
1113

@@ -34,11 +36,23 @@ cli.main(function(args, options) {
3436
return 1;
3537
}
3638

39+
if (!inputPath.match(/\/$/)) {
40+
inputPath = inputPath + "/"
41+
}
42+
43+
if (!config.match(/\/$/)) {
44+
config = config + "/"
45+
}
46+
3747
if (!fs.existsSync(output)) {
3848
this.ok("Create output directory: " + output)
3949
fs.mkdir(output);
4050
}
4151

52+
for (var parser in Parsers) {
53+
touch(`${this.inputPath}${parser.filename}`);
54+
}
55+
4256
var parser = new Parser();
4357
var logger = new Logger(parser, output);
4458
var reporter = new Reporter(parser);

‎build/Parsers.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var Power = require('./parsers/Power');
4+
var Zone = require('./parsers/Zone');
5+
var Party = require('./parsers/Party');
6+
7+
module.exports = [Power, Zone, Party];

‎build/arsers.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.export = [];

‎build/configurator.js

+54-20
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,104 @@
1-
"use strict";
1+
'use strict';
22

33
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
44

55
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
66

77
var fs = require('fs');
8+
var touch = require('touch');
89

910
var Configurator = (function () {
10-
function Configurator(path) {
11+
function Configurator(configPath, inputPath) {
1112
_classCallCheck(this, Configurator);
1213

13-
this.path = path ? path : Configurator.defaultConfigPath();
14+
this.configPath = configPath ? configPath : Configurator.defaultConfigPath();
15+
this.inputPath = inputPath ? inputPath : Configurator.defaultInputPath();
1416
}
1517

1618
_createClass(Configurator, [{
17-
key: "configNeeded",
19+
key: 'configNeeded',
1820
value: function configNeeded() {
19-
if (!this.path) {
21+
if (!this.configPath) {
2022
throw "Path cannot be nil";
2123
}
2224

23-
if (!fs.existsSync(this.path)) {
25+
if (!fs.existsSync(this.configPath)) {
2426
return true;
2527
}
2628

27-
var data = fs.readFileSync(this.path).toString();
29+
var data = fs.readFileSync(this.configPath).toString();
2830
return !data.match(/\[Zone\]/) && !data.match(/\[Power\]/) && !data.match(/\[LoadingScreen\]/);
2931
}
3032
}, {
31-
key: "config",
33+
key: 'config',
3234
value: function config() {
33-
if (!this.path) {
34-
throw "Path cannot be nil";
35+
if (!this.configPath) {
36+
throw "Config Path cannot be nil";
37+
}
38+
if (!this.inputPath) {
39+
throw "Input Path cannot be nil";
3540
}
3641

37-
var config = "[ZONE]\n" + "LogLevel=1\n" + "FilePrinting=false\n" + "ConsolePrinting=true\n" + "ScreenPrinting=false\n\n" + "[Power]\n" + "LogLevel=1\n" + "ConsolePrinting=true\n" + "[LoadingScreen]\n" + "LogLevel=1\n" + "FilePrinting=false\n" + "ConsolePrinting=true\n" + "ScreenPrinting=false";
42+
var config = "[Zone]\
43+
LogLevel=1\
44+
FilePrinting=true\
45+
ConsolePrinting=false\
46+
ScreenPrinting=false\
47+
[Bob]\
48+
LogLevel=1\
49+
FilePrinting=true\
50+
ConsolePrinting=false\
51+
ScreenPrinting=false\
52+
[Power]\
53+
LogLevel=1\
54+
FilePrinting=true\
55+
ConsolePrinting=false\
56+
ScreenPrinting=false\
57+
[Asset]\
58+
LogLevel=1\
59+
FilePrinting=true\
60+
ConsolePrinting=false\
61+
ScreenPrinting=false\
62+
[Rachelle]\
63+
LogLevel=1\
64+
FilePrinting=true\
65+
ConsolePrinting=false\
66+
ScreenPrinting=false\
67+
[Arena]\
68+
LogLevel=1\
69+
FilePrinting=true\
70+
ConsolePrinting=false\
71+
ScreenPrinting=false";
3872

3973
var mode = "w";
40-
if (fs.existsSync(this.path)) {
74+
if (fs.existsSync(this.configPath)) {
4175
mode = "a";
4276
}
43-
fs.writeFileSync(this.path, config, { flag: mode });
77+
fs.writeFileSync(this.configPath, config, { flag: mode });
4478
}
4579
}], [{
46-
key: "getUserHome",
80+
key: 'getUserHome',
4781
value: function getUserHome() {
4882
var isWindows = process.platform === 'win32';
4983
return isWindows ? process.env.USERPROFILE : process.env.HOME;
5084
}
5185
}, {
52-
key: "defaultConfigPath",
86+
key: 'defaultConfigPath',
5387
value: function defaultConfigPath() {
5488
var home = this.getUserHome();
55-
return home + "/Library/Preferences/Blizzard/Hearthstone/log.config";
89+
return home + '/Library/Preferences/Blizzard/Hearthstone/log.config';
5690
}
5791
}, {
58-
key: "defaultInputPath",
92+
key: 'defaultInputPath',
5993
value: function defaultInputPath() {
6094
var home = this.getUserHome();
61-
return "/Applications/Hearthstone/Logs/";
95+
return '/Applications/Hearthstone/Logs/';
6296
}
6397
}, {
64-
key: "defaultOutputPath",
98+
key: 'defaultOutputPath',
6599
value: function defaultOutputPath() {
66100
var home = this.getUserHome();
67-
return home + "/Documents/hearthstone";
101+
return home + '/Documents/hearthstone';
68102
}
69103
}]);
70104

‎build/game.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var Game = (function () {
104104
var gameStartEvent = this.events.filter(function (e) {
105105
return e[0] == "tag_change" && e[1]['type'] == "game_start";
106106
})[0];
107-
var time = this.gameStartTime().valueOf();
107+
var time = this.gameStartTime.valueOf();
108108
return time + '_' + slugify(firstPlayerName) + '_v_' + slugify(secondPlayerName);
109109
}
110110
}, {

‎build/parser.js

+44-36
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
1010

1111
var EventEmitter = require('events').EventEmitter;
1212
var Tail = require('file-tail');
13-
var ls = require('ls');
14-
15-
var Power = require('./parsers/Power');
16-
var Zone = require('./parsers/Zone');
17-
var Party = require('./parsers/Party');
13+
var Parsers = require('./parsers');
1814

1915
var Parser = (function (_EventEmitter) {
2016
_inherits(Parser, _EventEmitter);
@@ -28,64 +24,76 @@ var Parser = (function (_EventEmitter) {
2824
_createClass(Parser, [{
2925
key: 'parse',
3026
value: function parse(path) {
31-
var parser = this;
32-
var parsers = [Power, Zone, Party];
27+
var _this2 = this;
3328

34-
if (!path.match(/\/$/)) {
35-
path = path + "/";
36-
}
29+
var _iteratorNormalCompletion = true;
30+
var _didIteratorError = false;
31+
var _iteratorError = undefined;
32+
33+
try {
34+
var _loop = function _loop() {
35+
parser = _step.value;
3736

38-
ls(path + "*.log", function () {
39-
// find a parser for the file, if found, watch it
40-
var file = this.file;
41-
var logParser = parsers.filter(function (f) {
42-
return f.filename == file;
43-
})[0];
44-
var fullpath = this.full;
45-
if (logParser) {
46-
console.log("Watching", file);
37+
var fullpath = '' + path + parser.filename;
4738
var tail = new Tail.startTailing(fullpath);
39+
var parsers = parser.parsers;
4840
tail.on("line", function (line) {
49-
var result = parser.parseLine(logParser.parsers, line);
50-
if (result) {
51-
console.log(result);
52-
}
41+
_this2.parseLine(parsers, line);
5342
});
5443
tail.on('error', function (data) {
5544
console.error("error:", data);
5645
});
46+
};
47+
48+
for (var _iterator = Parsers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
49+
var parser;
50+
51+
_loop();
52+
}
53+
} catch (err) {
54+
_didIteratorError = true;
55+
_iteratorError = err;
56+
} finally {
57+
try {
58+
if (!_iteratorNormalCompletion && _iterator.return) {
59+
_iterator.return();
60+
}
61+
} finally {
62+
if (_didIteratorError) {
63+
throw _iteratorError;
64+
}
5765
}
58-
});
66+
}
5967
}
6068
}, {
6169
key: 'parseLine',
6270
value: function parseLine(parsers, line) {
6371
var match = null;
64-
var _iteratorNormalCompletion = true;
65-
var _didIteratorError = false;
66-
var _iteratorError = undefined;
72+
var _iteratorNormalCompletion2 = true;
73+
var _didIteratorError2 = false;
74+
var _iteratorError2 = undefined;
6775

6876
try {
69-
for (var _iterator = parsers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
70-
var parser = _step.value;
77+
for (var _iterator2 = parsers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
78+
var parser = _step2.value;
7179

72-
var result = parser.bind(this)(line);
80+
var result = parser(line);
7381
if (result) {
7482
this.emit.apply(this, result);
7583
return result;
7684
}
7785
}
7886
} catch (err) {
79-
_didIteratorError = true;
80-
_iteratorError = err;
87+
_didIteratorError2 = true;
88+
_iteratorError2 = err;
8189
} finally {
8290
try {
83-
if (!_iteratorNormalCompletion && _iterator.return) {
84-
_iterator.return();
91+
if (!_iteratorNormalCompletion2 && _iterator2.return) {
92+
_iterator2.return();
8593
}
8694
} finally {
87-
if (_didIteratorError) {
88-
throw _iteratorError;
95+
if (_didIteratorError2) {
96+
throw _iteratorError2;
8997
}
9098
}
9199
}

‎hearthstone.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Hearthstone = {
77
Player: require('./build/player'),
88
Game: require('./build/game'),
99
Parser: require('./build/parser'),
10+
Parsers: require('./build/parsers'),
1011
Reporter: require('./build/reporter'),
1112
Configurator: require('./build/configurator')
1213
};

‎lib/configurator.js

+44-22
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,69 @@
11
var fs = require('fs');
2+
var touch = require('touch');
23

34
class Configurator {
4-
constructor(path) {
5-
this.path = path ? path : Configurator.defaultConfigPath();
5+
constructor(configPath, inputPath) {
6+
this.configPath = configPath ? configPath : Configurator.defaultConfigPath();
7+
this.inputPath = inputPath ? inputPath : Configurator.defaultInputPath();
68
}
79

810
configNeeded() {
9-
if (!this.path) {
11+
if (!this.configPath) {
1012
throw "Path cannot be nil";
1113
}
1214

13-
if (!fs.existsSync(this.path)) {
15+
if (!fs.existsSync(this.configPath)) {
1416
return true;
1517
}
1618

17-
var data = fs.readFileSync(this.path).toString();
19+
var data = fs.readFileSync(this.configPath).toString();
1820
return !data.match(/\[Zone\]/) && !data.match(/\[Power\]/) && !data.match(/\[LoadingScreen\]/);
1921
}
2022

2123
config() {
22-
if (!this.path) {
23-
throw "Path cannot be nil";
24+
if (!this.configPath) {
25+
throw "Config Path cannot be nil";
26+
}
27+
if (!this.inputPath) {
28+
throw "Input Path cannot be nil";
2429
}
2530

26-
var config = "[ZONE]\n" +
27-
"LogLevel=1\n" +
28-
"FilePrinting=false\n" +
29-
"ConsolePrinting=true\n" +
30-
"ScreenPrinting=false\n\n" +
31-
"[Power]\n" +
32-
"LogLevel=1\n" +
33-
"ConsolePrinting=true\n" +
34-
"[LoadingScreen]\n" +
35-
"LogLevel=1\n" +
36-
"FilePrinting=false\n" +
37-
"ConsolePrinting=true\n" +
38-
"ScreenPrinting=false"
31+
var config = "[Zone]\
32+
LogLevel=1\
33+
FilePrinting=true\
34+
ConsolePrinting=false\
35+
ScreenPrinting=false\
36+
[Bob]\
37+
LogLevel=1\
38+
FilePrinting=true\
39+
ConsolePrinting=false\
40+
ScreenPrinting=false\
41+
[Power]\
42+
LogLevel=1\
43+
FilePrinting=true\
44+
ConsolePrinting=false\
45+
ScreenPrinting=false\
46+
[Asset]\
47+
LogLevel=1\
48+
FilePrinting=true\
49+
ConsolePrinting=false\
50+
ScreenPrinting=false\
51+
[Rachelle]\
52+
LogLevel=1\
53+
FilePrinting=true\
54+
ConsolePrinting=false\
55+
ScreenPrinting=false\
56+
[Arena]\
57+
LogLevel=1\
58+
FilePrinting=true\
59+
ConsolePrinting=false\
60+
ScreenPrinting=false"
3961

4062
var mode = "w"
41-
if (fs.existsSync(this.path)) {
63+
if (fs.existsSync(this.configPath)) {
4264
mode = "a"
4365
}
44-
fs.writeFileSync(this.path, config, {flag: mode});
66+
fs.writeFileSync(this.configPath, config, {flag: mode});
4567
}
4668

4769
static getUserHome() {

‎lib/game.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Game {
7474
var firstPlayerName = firstPlayer ? firstPlayer.name : "UNKNOWN";
7575
var secondPlayerName = secondPlayer ? secondPlayer.name : "UNKNOWN";
7676
var gameStartEvent = this.events.filter(e => e[0] == "tag_change" && e[1]['type'] == "game_start")[0]
77-
var time = this.gameStartTime().valueOf()
77+
var time = this.gameStartTime.valueOf()
7878
return `${time}_${slugify(firstPlayerName)}_v_${slugify(secondPlayerName)}`;
7979
}
8080

‎lib/parser.js

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,26 @@
11
var EventEmitter = require('events').EventEmitter;
22
var Tail = require('file-tail');
3-
var ls = require('ls');
4-
5-
var Power = require('./parsers/Power');
6-
var Zone = require('./parsers/Zone');
7-
var Party = require('./parsers/Party');
3+
var Parsers = require('./parsers');
84

95
class Parser extends EventEmitter {
106
parse(path) {
11-
let parser = this
12-
let parsers = [Power, Zone, Party];
13-
14-
if (!path.match(/\/$/)) {
15-
path = path + "/"
7+
for (var parser of Parsers) {
8+
let fullpath = `${path}${parser.filename}`;
9+
let tail = new Tail.startTailing(fullpath);
10+
let parsers = parser.parsers;
11+
tail.on("line", (line) => {
12+
this.parseLine(parsers, line);
13+
});
14+
tail.on('error', (data) => {
15+
console.error("error:", data);
16+
});
1617
}
17-
18-
ls(path + "*.log", function() {
19-
// find a parser for the file, if found, watch it
20-
let file = this.file
21-
let logParser = parsers.filter((f) => { return f.filename == file })[0];
22-
let fullpath = this.full;
23-
if (logParser) {
24-
console.log("Watching", file)
25-
let tail = new Tail.startTailing(fullpath);
26-
tail.on("line", function(line) {
27-
var result = parser.parseLine(logParser.parsers, line);
28-
if (result) {
29-
console.log(result)
30-
}
31-
});
32-
tail.on('error', function(data) {
33-
console.error("error:", data);
34-
});
35-
}
36-
})
3718
}
3819

3920
parseLine(parsers, line) {
4021
var match = null;
4122
for (let parser of parsers) {
42-
var result = parser.bind(this)(line);
23+
var result = parser(line);
4324
if (result) {
4425
this.emit.apply(this, result);
4526
return result;

‎lib/parsers.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Power = require('./parsers/Power');
2+
var Zone = require('./parsers/Zone');
3+
var Party = require('./parsers/Party');
4+
5+
module.exports = [Power, Zone, Party];

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"cli": "^0.11.1",
2020
"colors": "^1.1.2",
2121
"file-tail": "^0.3.0",
22-
"ls": "^0.1.0",
2322
"tail": "^0.4.0",
23+
"touch": "^1.0.0",
2424
"underscore.string": "^3.0.3"
2525
},
2626
"devDependencies": {

‎undefinedundefined

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.