Skip to content

Commit

Permalink
Move the files from the logger and into the game lib instead. Also up…
Browse files Browse the repository at this point in the history
…date the test.
  • Loading branch information
heavysixer committed Mar 2, 2015
1 parent 38e4962 commit 404eff4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Player = require('./player');
var slugify = require('underscore.string/slugify');

class Game {
constructor(mode="unknown") {
Expand Down Expand Up @@ -58,7 +59,7 @@ class Game {
var secondPlayerName = secondPlayer ? secondPlayer.name : "UNKNOWN";
var gameStartEvent = this.events.find(e => e[0] == "tag_change" && e[1]['type'] == "game_start")
var time = gameStartEvent ? (gameStartEvent[1]['timestamp']) : (new Date().valueOf()/1000);
return `${time}_${this.mode}_${firstPlayerName}_v_${secondPlayerName}`;
return `${time}_${this.mode}_${slugify(firstPlayerName)}_v_${slugify(secondPlayerName)}`;
}

toObject() {
Expand Down
3 changes: 1 addition & 2 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var path = require('path');
var Parser = require('./parser');
var Tail = require('tail').Tail;
var Game = require('./game');
var slugify = require('underscore.string/slugify');

class Logger {
constructor(parser, outputPath) {
Expand Down Expand Up @@ -54,7 +53,7 @@ class Logger {
saveGame() {
if (this.game) {
var jsonStr = JSON.stringify(this.game.toObject());
var filename = `slugify(${this.game.filename()}.json)`;
var filename = `${this.game.filename()}.json`;
var fullpath = path.join(this.outputPath, filename);
fs.writeFileSync(fullpath, jsonStr);
console.log(`Saved match: ${filename}`)
Expand Down
18 changes: 9 additions & 9 deletions test/game_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('Game', function(){
var game;

beforeEach(function(){
game = new Game();
});
game = new Game();
});

describe("addEvent", function(){
describe('tag_change', function(){
Expand Down Expand Up @@ -70,12 +70,12 @@ describe('Game', function(){
game.addEvent(["tag_change", {type: "game_over", name: "cho", state: "LOST"}])

expect(game.toObject()).to.deep.equal({
mode: "ranked",
mode: "ranked",
players: [
{id: 1, name: "siuying", first_player: true, result: "WON"},
{id: 1, name: "siuying", first_player: true, result: "WON"},
{id: 2, name: "cho", first_player: false, result: "LOST"}
],
events: [ ["tag_change", {type: "player_id", name: "siuying", player_id: 1}],
],
events: [ ["tag_change", {type: "player_id", name: "siuying", player_id: 1}],
["tag_change", {type: "player_id", name: "cho", player_id: 2}],
["tag_change", {type: "first_player", name: "siuying"}],
["tag_change", {type: "game_over", name: "siuying", state: "WON"}],
Expand All @@ -88,11 +88,11 @@ describe('Game', function(){
it("should return filename", function(){
game.mode = "ranked";
game.addEvent(["tag_change", {type: "player_id", name: "siuying", player_id: 1}]);
game.addEvent(["tag_change", {type: "player_id", name: "cho", player_id: 2}]);
game.addEvent(["tag_change", {type: "player_id", name: "cho.foo bar baz!", player_id: 2}]);
game.addEvent(["tag_change", {type: "game_start", timestamp: 1419332929}]);
expect(game.filename()).to.equal("1419332929_ranked_siuying_v_cho");
expect(game.filename()).to.equal("1419332929_ranked_siuying_v_cho-foo-bar-baz");
});
});
});

});

0 comments on commit 404eff4

Please sign in to comment.