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

GBL Notification bug #188

Merged
merged 3 commits into from
Jul 15, 2020
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
14 changes: 8 additions & 6 deletions api/hooks/countryBan/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const SdtdApi = require('7daystodie-api-wrapper');

/**
* @module 7dtdCountryBan
* @description Restrict certain countries access to a server
Expand Down Expand Up @@ -399,13 +397,16 @@ module.exports = function sdtdCountryBan(sails) {
} catch (error) {
sails.log.error(`HOOK:countryBan ${error}`);
}
}
},

handleCountryBan: handleCountryBan
};

async function handleCountryBan(connectedMessage) {
let country = connectedMessage.country;
let steamId = connectedMessage.steamId;
let serverId = this.server.id;
let serverId = connectedMessage.server.id;

try {
let server = await SdtdServer.findOne(serverId);

Expand All @@ -424,6 +425,7 @@ module.exports = function sdtdCountryBan(sails) {
});

let countryBanConfig = config[0].countryBanConfig;
console.log(countryBanConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

if (
countryBanConfig.bannedCountries.includes(country) &&
!countryBanConfig.whiteListedSteamIds.includes(steamId)
Expand All @@ -438,7 +440,7 @@ module.exports = function sdtdCountryBan(sails) {
});

if (countryBanConfig.ban) {
await SdtdApi.executeConsoleCommand(
await sails.helpers.sdtdApi.executeConsoleCommand(
{
ip: server.ip,
port: server.webPort,
Expand All @@ -448,7 +450,7 @@ module.exports = function sdtdCountryBan(sails) {
`ban add ${connectedMessage.steamId} 100 years "CSMM: Players from your country (${country}) are not allowed to connect to this server."`
);
} else {
await SdtdApi.executeConsoleCommand(
await sails.helpers.sdtdApi.executeConsoleCommand(
{
ip: server.ip,
port: server.webPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class CountrybanKick extends DiscordNotification {
super('countrybanKick');
}

async makeEmbed(event){
async makeEmbed(event) {
let client = sails.hooks.discordbot.getClient();
let embed = new client.customEmbed();

embed.setTitle(`:flag_${event.player.country.toLowerCase()}: Country ban kicked: ${event.player.playerName}`)
.setColor('ORANGE')
.addField('Steam ID', `[${event.player.steamID}](https://steamidfinder.com/lookup/${event.player.steamID}/)`, true)
.addField('Entity ID', `${event.player.entityID}`, true)
.addField('Steam ID', `[${event.player.steamId}](https://steamidfinder.com/lookup/${event.player.steamId}/)`, true)
.addField('Entity ID', `${event.player.entityId}`, true)
.setFooter(`${event.server.name}`)
.setURL(`${process.env.CSMM_HOSTNAME}/player/${event.player.id}/profile`);

Expand Down
4 changes: 3 additions & 1 deletion test/lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ before(function (done) {

let testServerConfig = await SdtdConfig.create({
server: testServer.id,
inactive: true
inactive: true,
countryBanConfig: { bannedCountries: ['BE'], whiteListedSteamIds: [] },

}).fetch();

sails.testUser = testUser;
Expand Down
67 changes: 67 additions & 0 deletions test/unit/hooks/countryBan/countryBan.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { expect } = require('chai');


describe('countryBan', () => {

beforeEach(() => {
sails.helpers.sdtdApi.executeConsoleCommand = sandbox.stub().callsFake();
});

it('Kicks a player that connects from a banned country', async () => {
const connectedMessage = {
steamId: '76561198028175941',
playerName: 'Catalysm',
entityId: '171',
ip: '192.168.1.1',
date: '2020-07-15',
time: '22:40:53',
uptime: '569.373',
msg: 'Player connected, entityid=171, name=Catalysm, steamid=76561198028175941, steamOwner=76561198028175941, ip=192.168.1.1',
country: 'BE',
player: {
createdAt: 1591719367715,
updatedAt: 1594845653806,
id: 27,
steamId: '76561198028175941',
entityId: 171,
ip: '192.168.1.1',
country: null,
currency: 0,
avatarUrl: '',
name: 'Catalysm',
positionX: -1279,
positionY: 61,
positionZ: 209,
inventory: {
steamid: '76561198028175941',
entityid: 171,
bag: [Array],
belt: [Array],
equipment: [Object]
},
playtime: 70,
lastOnline: '2020-07-15T20:36:01Z',
banned: false,
deaths: 0,
zombieKills: 0,
playerKills: 0,
score: 0,
level: 1,
lastTeleportTime: '2020-06-09 18:14:59.737',
server: 1,
user: 3,
role: 9
},
server: sails.testServer,
type: 'playerConnected'
};
const handleCountryBan = sails.hooks.countryban.handleCountryBan;
const notifSpy = sandbox.spy(sails.hooks.discordnotifications, 'sendNotification');

await handleCountryBan(connectedMessage);

expect(notifSpy).to.have.been.calledOnce;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍


});

});