Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(points): add online subcommand #3090

Merged
merged 3 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/_master/commands/points.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
- add [number] points to specified [username]
- !points remove [username] [number]
- remove [number] points to specified [username]
- !points online [number]
- give or take [number] points to all **online** users
- !points all [number]
- add [number] points to all **online** users
- give or take [number] points to all users
- !points set [username] [number]
- set [number] points to specified [username]
- !points get [username]
Expand Down
10 changes: 9 additions & 1 deletion locales/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,14 @@
"success": {
"set": "$username ma nyni nastaveno $amount $pointsName",
"give": "$sender prave dal svych $amount $pointsName uzivateli $username",
"all": "Vsichni online uzivatele prave ziskali $amount $pointsName!",
"online": {
"positive": "Vsichni online uzivatele prave ziskali $amount $pointsName!",
"negative": "Vsem online uzivatelum prave bylo odebrano $amount $pointsName!"
},
"all": {
"positive": "Vsichni uzivatele prave ziskali $amount $pointsName!",
"negative": "Vsem uzivatelum prave bylo odebrano $amount $pointsName!"
},
"rain": "Jen at prsi! Vsichni online uzivatele prave ziskali az $amount $pointsName!",
"add": "$username ziskal $amount $pointsName!",
"remove": "A jeje, $amount $pointsName bylo odebrano uzivateli $username!"
Expand All @@ -891,6 +898,7 @@
"give": "{core.command-parse} $command [username] [amount]",
"giveNotEnough": "Promin, $sender, ale nemas $amount $pointsName abys je daroval uzivateli $username",
"get": "{core.command-parse} $command [username]",
"online": "{core.command-parse} $command [amount]",
"all": "{core.command-parse} $command [amount]",
"rain": "{core.command-parse} $command [amount]",
"add": "{core.command-parse} $command [username] [amount]",
Expand Down
10 changes: 9 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,14 @@
"success": {
"set": "$username was set to $amount $pointsName",
"give": "$sender just gave his $amount $pointsName to $username",
"all": "All online users just received $amount $pointsName!",
"online": {
"positive": "All online users just received $amount $pointsName!",
"negative": "All online users just lost $amount $pointsName!"
},
"all": {
"positive": "All users just received $amount $pointsName!",
"negative": "All users just lost $amount $pointsName!"
},
"rain": "Make it rain! All online users just received up to $amount $pointsName!",
"add": "$username just received $amount $pointsName!",
"remove": "Ouch, $amount $pointsName was removed from $username!"
Expand All @@ -881,6 +888,7 @@
"give": "{core.command-parse} $command [username] [amount]",
"giveNotEnough": "Sorry, $sender, you don't have $amount $pointsName to give it to $username",
"get": "{core.command-parse} $command [username]",
"online": "{core.command-parse} $command [amount]",
"all": "{core.command-parse} $command [amount]",
"rain": "{core.command-parse} $command [amount]",
"add": "{core.command-parse} $command [username] [amount]",
Expand Down
9 changes: 4 additions & 5 deletions src/bot/expects.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,20 @@ class Expects {

points (opts) {
opts = opts || {}
_.defaults(opts, { optional: false, all: false })
_.defaults(opts, { optional: false, all: false, negative: false })
if (!opts.optional) this.checkText()

let regexp
if (opts.all) regexp = XRegExp('(?<points> all|[0-9]+ )', 'ix')
else regexp = XRegExp('(?<points> [0-9]+ )', 'ix')
if (opts.all) regexp = XRegExp('(?<points> all|-?[0-9]+ )', 'ix')
else regexp = XRegExp('(?<points> -?[0-9]+ )', 'ix')
const match = XRegExp.exec(this.text, regexp)

if (!_.isNil(match)) {
if (match.points === 'all') {
this.match.push(match.points)
} else {
this.match.push(parseInt(
Number(match.points) <= Number.MAX_SAFE_INTEGER
? match.points
? (opts.negative ? match.points : Math.abs(match.points))
: Number.MAX_SAFE_INTEGER, 10)) // return only max safe
}
this.text = this.text.replace(match.points, '') // remove from text matched pattern
Expand Down
50 changes: 44 additions & 6 deletions src/bot/systems/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,55 @@ class Points extends System {
}
}

@command('!points online')
@default_permission(permission.CASTERS)
async online (opts: CommandOptions) {
try {
let points = new Expects(opts.parameters).points({ all: false, negative: true }).toArray()[0];

let message: string;
if (points >= 0) {
await getRepository(User).increment({}, 'points', points);
message = await prepare('points.success.online.positive', {
amount: points,
pointsName: await this.getPointsName(points),
});
} else {
points = Math.abs(points);
await this.decrement({}, points);
message = await prepare('points.success.online.negative', {
amount: -points,
pointsName: await this.getPointsName(points),
});
};

sendMessage(message, opts.sender, opts.attr);
} catch (err) {
sendMessage(translate('points.failed.online').replace('$command', opts.command), opts.sender, opts.attr);
}
}

@command('!points all')
@default_permission(permission.CASTERS)
async all (opts: CommandOptions) {
try {
const points = new Expects(opts.parameters).points({ all: false }).toArray()[0];
let points: number = new Expects(opts.parameters).points({ all: false, negative: true }).toArray()[0];
let message: string;
if (points >= 0) {
await getRepository(User).increment({}, 'points', points);
message = await prepare('points.success.all.positive', {
amount: points,
pointsName: await this.getPointsName(points),
});
} else {
points = Math.abs(points);
await this.decrement({}, points);
message = await prepare('points.success.all.negative', {
amount: -points,
pointsName: await this.getPointsName(points),
});
};

getRepository(User).increment({ isOnline: true }, 'points', points);
const message = await prepare('points.success.all', {
amount: points,
pointsName: await this.getPointsName(points),
});
sendMessage(message, opts.sender, opts.attr);
} catch (err) {
sendMessage(translate('points.failed.all').replace('$command', opts.command), opts.sender, opts.attr);
Expand Down
49 changes: 36 additions & 13 deletions test/tests/points/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ const owner = { userId: Math.floor(Math.random() * 100000), username: 'soge__' }
const user1 = { userId: Math.floor(Math.random() * 100000), username: 'user1', points: 100 };
const user2 = { userId: Math.floor(Math.random() * 100000), username: 'user2' };

async function setUsersOnline(users) {
await getRepository(User).update({}, { isOnline: false });
for (const username of users) {
await getRepository(User).update({ username }, { isOnline: true });
}
}

describe('Points - all()', () => {
before(async () => {
await db.cleanup();
Expand All @@ -33,10 +26,6 @@ describe('Points - all()', () => {
});

describe('Points should be correctly given', () => {
it('set users online', async () => {
await setUsersOnline(['soge__', 'user1', 'user2']);
});

it('!points get should return 0 for owner', async () => {
await points.get({ sender: owner, parameters: '' });
await message.isSent('points.defaults.pointsResponse', owner, {
Expand Down Expand Up @@ -64,10 +53,9 @@ describe('Points - all()', () => {
});
});


it('!points all 100', async () => {
await points.all({ sender: owner, parameters: '100' });
await message.isSent('points.success.all', { username: owner.username }, {
await message.isSent('points.success.all.positive', { username: owner.username }, {
amount: Math.floor(100),
pointsName: await points.getPointsName(Math.floor(100)),
});
Expand Down Expand Up @@ -99,5 +87,40 @@ describe('Points - all()', () => {
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points all -150', async () => {
await points.all({ sender: owner, parameters: '-150' });
await message.isSent('points.success.all.negative', { username: owner.username }, {
amount: Math.floor(-150),
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 0 for owner', async () => {
await points.get({ sender: owner, parameters: '' });
await message.isSent('points.defaults.pointsResponse', owner, {
amount: Math.floor(0),
username: owner.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});

it('!points get should return 50 for user1', async () => {
await points.get({ sender: user1, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user1, {
amount: Math.floor(50),
username: user1.username,
pointsName: await points.getPointsName(Math.floor(50)),
});
});

it('!points get should return 0 for user2', async () => {
await points.get({ sender: user2, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user2, {
amount: Math.floor(0),
username: user2.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});
});
});
2 changes: 1 addition & 1 deletion test/tests/points/give.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const points = (require('../../../dest/systems/points')).default;
const user1 = { username: 'user1', points: 100, userId: Number(_.random(999999, false)) };
const user2 = { username: 'user2', points: 100, userId: Number(_.random(999999, false)) };

describe('Points - get()', () => {
describe('Points - give()', () => {
describe('user1 will give 50 points to user2', () => {
before(async () => {
await db.cleanup();
Expand Down
137 changes: 137 additions & 0 deletions test/tests/points/online.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* global describe it before */

require('../../general.js');

const db = require('../../general.js').db;
const message = require('../../general.js').message;
const _ = require('lodash');

const { getRepository } = require('typeorm');
const { User } = require('../../../dest/database/entity/user');

const points = (require('../../../dest/systems/points')).default;

const owner = { userId: Math.floor(Math.random() * 100000), username: 'soge__' };
const user1 = { userId: Math.floor(Math.random() * 100000), username: 'user1', points: 100 };
const user2 = { userId: Math.floor(Math.random() * 100000), username: 'user2' };

async function setUsersOnline(users) {
await getRepository(User).update({}, { isOnline: false });
for (const username of users) {
await getRepository(User).update({ username }, { isOnline: true });
}
}

describe('Points - all()', () => {
before(async () => {
await db.cleanup();
await message.prepare();

await getRepository(User).save(owner);
await getRepository(User).save(user1);
await getRepository(User).save(user2);
});

describe('Points should be correctly given', () => {
it('set users online', async () => {
await setUsersOnline(['soge__', 'user1', 'user2']);
});

it('!points get should return 0 for owner', async () => {
await points.get({ sender: owner, parameters: '' });
await message.isSent('points.defaults.pointsResponse', owner, {
amount: Math.floor(0),
username: owner.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});

it('!points get should return 100 for user1', async () => {
await points.get({ sender: user1, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user1, {
amount: Math.floor(100),
username: user1.username,
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 0 for user2', async () => {
await points.get({ sender: user2, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user2, {
amount: Math.floor(0),
username: user2.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});

it('!points all 100', async () => {
await points.all({ sender: owner, parameters: '100' });
await message.isSent('points.success.all.positive', { username: owner.username }, {
amount: Math.floor(100),
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 100 for owner', async () => {
await points.get({ sender: owner, parameters: '' });
await message.isSent('points.defaults.pointsResponse', owner, {
amount: Math.floor(100),
username: owner.username,
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 200 for user1', async () => {
await points.get({ sender: user1, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user1, {
amount: Math.floor(100),
username: user1.username,
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 100 for user2', async () => {
await points.get({ sender: user2, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user2, {
amount: Math.floor(100),
username: user2.username,
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points all -150', async () => {
await points.all({ sender: owner, parameters: '-150' });
await message.isSent('points.success.all.negative', { username: owner.username }, {
amount: Math.floor(-150),
pointsName: await points.getPointsName(Math.floor(100)),
});
});

it('!points get should return 0 for owner', async () => {
await points.get({ sender: owner, parameters: '' });
await message.isSent('points.defaults.pointsResponse', owner, {
amount: Math.floor(0),
username: owner.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});

it('!points get should return 50 for user1', async () => {
await points.get({ sender: user1, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user1, {
amount: Math.floor(50),
username: user1.username,
pointsName: await points.getPointsName(Math.floor(50)),
});
});

it('!points get should return 0 for user2', async () => {
await points.get({ sender: user2, parameters: '' });
await message.isSent('points.defaults.pointsResponse', user2, {
amount: Math.floor(0),
username: user2.username,
pointsName: await points.getPointsName(Math.floor(0)),
});
});
});
});