Skip to content

Commit

Permalink
Merge pull request #1407 from CatalysmsServerManager/timeDiff-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele authored Jan 28, 2023
2 parents 0cca7a1 + 58badc8 commit 5e4e08e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 25 deletions.
37 changes: 19 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"lusca": "^1.7.0",
"minimist": "^1.2.3",
"morgan": "^1.10.0",
"ms": "^2.1.3",
"mysql2": "^2.3.0",
"p-wait-for": "^4.1.0",
"passport": "^0.4.1",
Expand Down
66 changes: 59 additions & 7 deletions test/unit/helpers/sdtd/execute-custom-command.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { expect } = require('chai');
const faker = require('faker');
const wait = require('../../../../worker/util/wait');
const MockDate = require('mockdate');

describe('HELPER execute-custom-command', function () {
beforeEach(function () {
Expand Down Expand Up @@ -445,7 +446,7 @@ say "1 - 1 = {{subtract 1 1}}"
expect(sails.helpers.sdtdApi.executeConsoleCommand.getCall(3).lastArg).to.equal(`say "The var is: "`);
});

it('Can list variables', async() => {
it('Can list variables', async () => {
const names = [
'exchange:rate',
'exchange:max',
Expand Down Expand Up @@ -515,15 +516,15 @@ say "1 - 1 = {{subtract 1 1}}"
.map((string) => string.trim());
expect(result).to.have.length(1, 'simpleSearch: error running command');
expect(resultArray).to.have.length(3, 'simpleSearch: invalid length');
expect(resultArray).to.eql([ 'zk_id0', 'zk_id1', 'zk_id2' ], 'simpleSearch: eql');
expect(resultArray).to.eql(['zk_id0', 'zk_id1', 'zk_id2'], 'simpleSearch: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, startsWithSearch, {});
resultArray = sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg
.split('\n')
.map((string) => string.trim());
expect(result).to.have.length(1, 'startsWithSearch: error running command');
expect(resultArray).to.have.length(2, 'startsWithSearch: invalid length');
expect(resultArray).to.eql([ 'exchange:rate', 'exchange:max' ], 'startsWithSearch: eql');
expect(resultArray).to.eql(['exchange:rate', 'exchange:max'], 'startsWithSearch: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, endsWithSearch, {});
let resultString = sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg;
Expand All @@ -546,31 +547,31 @@ say "1 - 1 = {{subtract 1 1}}"
.map((string) => string.trim());
expect(result).to.have.length(1, 'searchWithSortDesc: error running command');
expect(resultArray).to.have.length(3, 'searchWithSortDesc: invalid length');
expect(resultArray).to.eql([ 'zk_id2', 'zk_id1', 'zk_id0' ], 'searchWithSortDesc: eql');
expect(resultArray).to.eql(['zk_id2', 'zk_id1', 'zk_id0'], 'searchWithSortDesc: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, searchWithSortAsc, {});
resultArray = sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg
.split('\n')
.map((string) => string.trim());
expect(result).to.have.length(1, 'searchWithSortAsc: error running command');
expect(resultArray).to.have.length(3, 'searchWithSortAsc: invalid length');
expect(resultArray).to.eql([ 'zk_id0', 'zk_id1', 'zk_id2' ], 'searchWithSortAsc: eql');
expect(resultArray).to.eql(['zk_id0', 'zk_id1', 'zk_id2'], 'searchWithSortAsc: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, searchWithTheLot, {});
resultArray = sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg
.split('\n')
.map((string) => string.trim());
expect(result).to.have.length(1, 'searchWithTheLot: error running command');
expect(resultArray).to.have.length(2, 'searchWithTheLot: invalid length');
expect(resultArray).to.eql([ 'zk_id0', 'zk_id1'], 'searchWithTheLot: eql');
expect(resultArray).to.eql(['zk_id0', 'zk_id1'], 'searchWithTheLot: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, searchWithNoQuery, {});
resultArray = sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg
.split('\n')
.map((string) => string.trim());
expect(result).to.have.length(1, 'searchWithNoQuery: error running command');
expect(resultArray).to.have.length(8, 'searchWithNoQuery: invalid length');
expect(resultArray).to.eql([ 'exchange:rate', 'exchange:max', 'zk_id0', 'zk_id1', 'zk_id2', 'pk_id0', 'pk_id1', 'pk_id2'], 'searchWithNoQuery: eql');
expect(resultArray).to.eql(['exchange:rate', 'exchange:max', 'zk_id0', 'zk_id1', 'zk_id2', 'pk_id0', 'pk_id1', 'pk_id2'], 'searchWithNoQuery: eql');

result = await sails.helpers.sdtd.executeCustomCmd(sails.testServer, searchWithNoResults, {});
expect(result).to.have.length(0, 'searchWithNoResults: wait what????');
Expand Down Expand Up @@ -628,6 +629,57 @@ say "1 - 1 = {{subtract 1 1}}"

});

describe('timeDiff helper', () => {
it('Can display the time left until a date', async () => {
MockDate.set('2020-01-01T00:00:00.000Z');
const template = [
'{{timeDiff "2020-01-02T00:00:00.000Z"}}',
].join(';');

await sails.helpers.sdtd.executeCustomCmd(sails.testServer, template, { player: sails.testPlayer });
expect(sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg).to.equal('86400');
});

it('Can display time passed for date in the past', async () => {
MockDate.set('2020-02-10T00:00:00.000Z');
const template = [
'{{timeDiff "2020-02-09T00:00:00.000Z"}}',
].join(';');

await sails.helpers.sdtd.executeCustomCmd(sails.testServer, template, { player: sails.testPlayer });
expect(sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg).to.equal('-86400');
});

it('can pretty-print a timediff in the future', async () => {
MockDate.set('2020-02-10T00:00:00.000Z');
const template = [
'{{timeDiffPretty "2020-02-11T00:00:00.000Z"}}',
].join(';');

await sails.helpers.sdtd.executeCustomCmd(sails.testServer, template, { player: sails.testPlayer });
expect(sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg).to.equal('1 day');
});

it('Can pretty-print a timediff thats two days away', async () => {
MockDate.set('2020-02-10T00:00:00.000Z');
const template = [
'{{timeDiffPretty "2020-02-12T00:00:00.000Z"}}',
].join(';');

await sails.helpers.sdtd.executeCustomCmd(sails.testServer, template, { player: sails.testPlayer });
expect(sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg).to.equal('2 days');
});

it('can pretty-print a timediff in the past', async () => {
MockDate.set('2020-02-10T00:00:00.000Z');
const template = [
'{{timeDiffPretty "2020-02-09T00:00:00.000Z"}}',
].join(';');

await sails.helpers.sdtd.executeCustomCmd(sails.testServer, template, { player: sails.testPlayer });
expect(sails.helpers.sdtdApi.executeConsoleCommand.lastCall.lastArg).to.equal('1 day ago');
});
});


});
Expand Down
22 changes: 22 additions & 0 deletions worker/util/Handlebars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const promisedHandlebars = require('promised-handlebars');
const ms = require('ms');
const PersistentVariablesManager = require('./CSMMCommand/persistentVariables');


Expand Down Expand Up @@ -310,4 +311,25 @@ Handlebars.registerHelper('datePassed', function (date) {
return ((Date.now() - Date.parse(date)) >= 0);
});

Handlebars.registerHelper('timeDiff', function (date) {
//Get milliseconds from Date.now()
let timeDiff = -1 * (Date.now() - Date.parse(date));
return timeDiff / 1000;
});

Handlebars.registerHelper('timeDiffPretty', function (date) {
//Get milliseconds from Date.now()
let timeDiff = Date.now() - Date.parse(date);

const isInPast = timeDiff > 0;

timeDiff = Math.abs(timeDiff);

if (isInPast) {
return ms(timeDiff, { long: true }) + ' ago';
}

return ms(timeDiff, { long: true });
});

module.exports = Handlebars;

0 comments on commit 5e4e08e

Please sign in to comment.