Skip to content

Commit

Permalink
Adding a !list command (#16)
Browse files Browse the repository at this point in the history
* Adding list command

* Adding test

* Adding a total list
  • Loading branch information
alexjpaz authored Jan 18, 2021
1 parent 615e586 commit ac0b894
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ The shuffler can be configured by adding a `config.json` file (an example is pro

#### chatCommand

> Configures the chat command (e.g !swap) to respond to (default: ^swap$)
> Configures the swap chat command (e.g !swap) to respond to (default: ^swap$)
#### chatListCommand

> Configures the list chat command (e.g !list) to respond to (default: ^list$)
#### redemptionName

Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const getDefaultConfig = () => {
"port": 7070,
"host": "127.0.0.1",
"chatCommand": "^swap$",
"chatListCommand": "^list$",
"chatCooldownGlobal": "60000",
"chatCooldownUser": "60000",
"redemptionName": "^swap$",
Expand Down
32 changes: 30 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,39 @@ const startServer = async () => {
if(!rom) {
return;
}
const romName = rom.replace(/\./g, '_');
const romName = rom.replace(/\.[a-zA-Z]+$/, '')

twitchShufflerListener.say(`/me Swapping to "${romName}" (${cause})`);
sockets.forEach((sock) => {
sock.write(`switchRom\t${rom}\n`);
});
};

const list = async () => {
let roms = await romShuffler.fetchCurrentRoms();

let filteredRoms = roms
.map((rom) => rom.replace(/\.[a-zA-Z]+$/, ''))
.filter((rom) => rom !== 'DeleteMe')
;

let total = filteredRoms.length;

let partition = filteredRoms;

const join = () => {
return partition.join(', ');
};

while(join().length >= 500) {
partition.pop();
}

let chatText = `ExtraLife ${join()} (${total}/${total})`;

return chatText;
};

const swap = async (index, cause) => {
const rom = await romShuffler.shuffle(index);

Expand Down Expand Up @@ -78,7 +103,10 @@ const startServer = async () => {
};

const romShuffler = new RomShuffler();
const twitchShufflerListener = new TwitchShufflerListener({ swap });
const twitchShufflerListener = new TwitchShufflerListener({
swap,
list,
});

logger.info(chalk.blue(`TCP Server is starting on ${config.host} ${config.port}`));

Expand Down
8 changes: 8 additions & 0 deletions src/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ const ComfyJS = require("comfy.js");

const chatCommandRegExp = new RegExp(config.chatCommand);

const chatListCommandRegExp = new RegExp(config.chatListCommand);

const rewardNameRegExp = new RegExp(config.rewardNameRegExp);

class TwitchShufflerListener {
constructor(props = {}) {
this.swap = props.swap;

this.list = props.list;

this.lastCommandTimestamps = {};
}

Expand Down Expand Up @@ -55,6 +59,10 @@ class TwitchShufflerListener {
this.swap(message, `${user} via command`);
}
}

if(chatListCommandRegExp.test(command)) {
this.say(await this.list(message));
}
}

async onReward( user, reward, cost, extra ) {
Expand Down
24 changes: 24 additions & 0 deletions src/twitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ describe('twitch', () => {
listener = new TwitchShufflerListener();
});

describe('list', () => {
it('should list games on !list', () => {
listener.list = sinon.spy();

listener.isCoolingDown = sinon.spy(() => false);

listener.onCommand(
"fake_user",
"list", // TODO
"fake_message",
{},
{
sinceLastCommand: {
any: 0
}
}
);

// expect(listener.isCoolingDown.called).to.eql(true);
expect(listener.list.called).to.eql(true);
expect(listener.list.calledWith("fake_message")).to.eql(true);
});
});

it('should swap on !swap', () => {
listener.swap = sinon.spy();

Expand Down

0 comments on commit ac0b894

Please sign in to comment.