Skip to content

Commit

Permalink
Adding rng text for redemptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjpaz committed Jan 8, 2021
1 parent 289788d commit cfd6fc9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ The shuffler can be configured by adding a `config.json` file (an example is pro

> User chat cooldown (defaukt: 60000)
#### redepmtionRandomText

> Text that is used for a random game via redemption (default: ^rng$)
#### timer

> timer options
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const getDefaultConfig = () => {
"chatCooldownGlobal": "60000",
"chatCooldownUser": "60000",
"redemptionName": "^swap$",
"redepmtionRandomText": "^rng$",
"randomOnly": false,
"timer": {
"min": 5000,
Expand Down
6 changes: 6 additions & 0 deletions src/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const chalk = require('chalk');

const config = require('./config');

const isRNG = (t) => new RegExp(config.redepmtionRandomText).test(t);

class RomShuffler {
constructor() {
this.state = {};
Expand All @@ -25,6 +27,10 @@ class RomShuffler {
}

if(index) {
index = index.trim();
}

if(index && index !== '' && !isRNG(index)) {
roms = roms
.filter((rom) => rom.toLowerCase().startsWith(index.toLowerCase()));
} else {
Expand Down
79 changes: 79 additions & 0 deletions src/swap.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('./config');

const { expect } = require('chai');
const sinon = require('sinon');

Expand Down Expand Up @@ -114,4 +116,81 @@ describe('swap', () => {

expect(rom).to.eql('1_Foo.nes');
});


it('should be random on null', async () => {

const shuffler = new RomShuffler();

shuffler.state = {
currentRom: '1_Foo.nes'
};

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'2_Bar.nes'
]);

const rom = await shuffler.shuffle(null);

expect(rom).to.eql('2_Bar.nes');
});


it('should be random on empty', async () => {

const shuffler = new RomShuffler();

shuffler.state = {
currentRom: '1_Foo.nes'
};

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'2_Bar.nes'
]);

const rom = await shuffler.shuffle('');

expect(rom).to.eql('2_Bar.nes');
});


it('should be random on empty', async () => {

const shuffler = new RomShuffler();

shuffler.state = {
currentRom: '1_Foo.nes'
};

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'2_Bar.nes'
]);

const rom = await shuffler.shuffle(' ');

expect(rom).to.eql('2_Bar.nes');
});

it('should be random on "rng"', async () => {

const shuffler = new RomShuffler();

shuffler.state = {
currentRom: '1_Foo.nes'
};

shuffler.fetchCurrentRoms = sinon.spy(() => [
'1_Foo.nes',
'2_Bar.nes'
]);

const rng = "rng" // TODO

const rom = await shuffler.shuffle(rng);

expect(rom).to.eql('2_Bar.nes');
});
});

0 comments on commit cfd6fc9

Please sign in to comment.