Skip to content

Commit

Permalink
Merge pull request #22 from yamikuronue/feature-globaltime-#3
Browse files Browse the repository at this point in the history
Feature globaltime #3
  • Loading branch information
yamikuronue committed Apr 13, 2017
2 parents f0b3da2 + 7dd21ac commit e4fa14d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- '4'
- '5'
- '6'
- '7'
env:
global:
service_name: travis-ci
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sockmafia-autogm",
"version": "1.0.0",
"version": "1.1.0",
"description": "Auto gamemaster for sockmafia",
"main": "src/autoGM.js",
"directories": {
Expand All @@ -26,7 +26,7 @@
"handlebars": "^4.0.5",
"moment": "^2.14.1",
"rimraf-promise": "^2.0.0",
"sockmafia": "sockdrawer/SockMafia"
"sockmafia": ">=4.0.2"
},
"peerDependencies": {
"sockbot": "^6.2.0"
Expand Down
20 changes: 13 additions & 7 deletions src/autoGM.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ exports.createGame = function() {
internals.game = g;
})
.then(() => internals.game.addModerator(internals.myName))
.then(() => internals.forum.Post.reply(threadID, undefined, 'Signups are now open!\n To join the game, please type `!join`.\n The game will start in ' + internals.config.phases.init))
.then(() => internals.forum.Post.reply(threadID, undefined, 'Signups are now open!\n To join the game, please type `!join`.\n The game will start on ' + formatTimeLink(internals.config.phases.init)))
.then(() => exports.setTimer(internals.config.phases.init, exports.startGame));
};

Expand Down Expand Up @@ -244,6 +244,12 @@ exports.postFlip = function postFlip(username, type) {
return internals.forum.Post.reply(internals.game.topicId, undefined, message);
};

function formatTimeLink(offset) {
const timeString = viewHelper.relativeToAbsoluteTime(offset);
const time = Moment(timeString, 'MMM Do [at] HH:mm [UTC]');
return `<a href="${viewHelper.getDateTimeLink(time)}">${timeString}</a>`;
}

exports.startGame = function startGame() {
debug('Running game start routine');
const players = internals.game.livePlayers;
Expand Down Expand Up @@ -293,9 +299,9 @@ exports.startGame = function startGame() {
internals.game.addChat(chatroom.id);
})
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined, flavorText[internals.flavor].openingScene))
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined, 'Let the game begin! It is now day. The day will end in ' + internals.config.phases.day))
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined, 'Let the game begin! It is now day. The day will end on ' + formatTimeLink(internals.config.phases.day)))
.then(() => exports.setTimer(internals.config.phases.day, exports.onDayEnd))
.then(() => internals.game.setValue('phaseEnd', viewHelper.relativeToAbsoluteTime(internals.config.phases.day)))
.then(() => internals.game.setValue('phaseEnd', formatTimeLink(internals.config.phases.day)))
.catch((err) => {
debug(err);
return internals.forum.Post.reply(internals.game.topicId, undefined, ':wtf: Sorry folks, I need to cancel this one; I\'ve hit an error. \n Error was: ' + err)
Expand All @@ -313,9 +319,9 @@ exports.startGame = function startGame() {
exports.onDayEnd = function onDayEnd() {
debug('running Day End routine');
return internals.game.nextPhase()
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined, 'It is now night. Night will end in ' + internals.config.phases.night))
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined, 'It is now night. Night will end on ' + formatTimeLink(internals.config.phases.night)))
.then(() => exports.setTimer(internals.config.phases.night, exports.onNightEnd))
.then(() => internals.game.setValue('phaseEnd', viewHelper.relativeToAbsoluteTime(internals.config.phases.night)));
.then(() => internals.game.setValue('phaseEnd', formatTimeLink(internals.config.phases.night)));
};

exports.onLynch = function(username) {
Expand Down Expand Up @@ -373,15 +379,15 @@ exports.onNightEnd = function onNightEnd() {
} else {
return internals.game.newDay()
.then(() => internals.forum.Post.reply(internals.game.topicId, undefined,
'It is now day. Day will end in ' + internals.config.phases.day))
'It is now day. Day will end on ' + formatTimeLink(internals.config.phases.day)))
.then(() => {
if (warn) {
return internals.forum.Post.reply(internals.game.topicId, undefined,
'WARNING! It is now ' + warn);
}
})
.then(() => exports.setTimer(internals.config.phases.day, exports.onDayEnd))
.then(() => internals.game.setValue('phaseEnd', viewHelper.relativeToAbsoluteTime(internals.config.phases.day)));
.then(() => internals.game.setValue('phaseEnd', formatTimeLink(internals.config.phases.day)));
}
});
};
Expand Down
4 changes: 4 additions & 0 deletions src/viewHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@ module.exports = {
const parts = relative.split(' ');
const timestamp = new Moment().add(parts[0], parts[1]);
return timestamp.utc().format('MMM Do [at] HH:mm [UTC]');
},

getDateTimeLink: function(time) {
return `https://www.timeanddate.com/worldclock/fixedtime.html?year=${time.year()}&month=${time.format('MMM')}&day=${time.date()}&hour=${time.hour()}&min=${time.minutes()}&sec=${time.seconds()}`;
}
};
29 changes: 26 additions & 3 deletions test/unitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,10 @@ describe('AutoGM', () => {
AutoGM.internals.game.livePlayers = [player('one'), player('two'), player('three'), player('four'), player('five'), player('six')];
sandbox.spy(AutoGM.internals.game, 'setValue');
sandbox.stub(viewHelper, 'relativeToAbsoluteTime').returns('Jan 1st at 2pm');
sandbox.stub(viewHelper, 'getDateTimeLink').returns('a_hyperlink');

return AutoGM.startGame().then(() => {
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', 'Jan 1st at 2pm');
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', '<a href="a_hyperlink">Jan 1st at 2pm</a>');
});
});

Expand Down Expand Up @@ -697,10 +698,11 @@ describe('AutoGM', () => {
sandbox.stub(AutoGM, 'setTimer').resolves();
sandbox.spy(AutoGM.internals.game, 'setValue');
sandbox.stub(viewHelper, 'relativeToAbsoluteTime').returns('Jan 1st at 2pm');
sandbox.stub(viewHelper, 'getDateTimeLink').returns('a_hyperlink');


return AutoGM.onDayEnd().then(() => {
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', 'Jan 1st at 2pm');
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', '<a href="a_hyperlink">Jan 1st at 2pm</a>');
});
});
});
Expand Down Expand Up @@ -802,11 +804,12 @@ describe('AutoGM', () => {
sandbox.stub(AutoGM, 'checkWin').returns(false);
sandbox.stub(AutoGM, 'setTimer').resolves();
sandbox.stub(viewHelper, 'relativeToAbsoluteTime').returns('Jan 1st at 2pm');
sandbox.stub(viewHelper, 'getDateTimeLink').returns('a_hyperlink');

sandbox.spy(AutoGM.internals.game, 'setValue');

return AutoGM.onNightEnd().then(() => {
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', 'Jan 1st at 2pm');
AutoGM.internals.game.setValue.should.have.been.calledWith('phaseEnd', '<a href="a_hyperlink">Jan 1st at 2pm</a>');
});
});

Expand Down Expand Up @@ -1396,4 +1399,24 @@ describe('viewHelper', () => {
it('should work for hours', () => viewHelper.relativeToAbsoluteTime('1 hour').should.equal('Jan 1st at 01:00 UTC'));
it('should work for days', () => viewHelper.relativeToAbsoluteTime('1 day').should.equal('Jan 2nd at 00:00 UTC'));
});

describe('globalDateTimeLink', () => {
//https://www.timeanddate.com/worldclock/fixedtime.html?year=&month=&day=&hour=&min=&sec=0
let clock;

beforeEach(() => {
//Set Date to unix epoch (0)
clock = sinon.useFakeTimers();
});

afterEach(() => {
clock.restore();
});

it('should return a string', () => viewHelper.getDateTimeLink(new Moment()).should.be.a('string'));
it('should contain a hyperlink', () => viewHelper.getDateTimeLink(new Moment()).should.contain('https://www.timeanddate.com/worldclock/fixedtime.html'));
it('should return the date passed in', () => viewHelper.getDateTimeLink(new Moment()).should.equal('https://www.timeanddate.com/worldclock/fixedtime.html?year=1970&month=Jan&day=1&hour=0&min=0&sec=0'));
it('should return the date passed in 2', () => viewHelper.getDateTimeLink(new Moment().add(7, 'days')).should.equal('https://www.timeanddate.com/worldclock/fixedtime.html?year=1970&month=Jan&day=8&hour=0&min=0&sec=0'));
});

});

0 comments on commit e4fa14d

Please sign in to comment.