Skip to content

Commit

Permalink
Updated to support newest kcapp and general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Sep 4, 2021
1 parent 14c3bd6 commit 3419bce
Show file tree
Hide file tree
Showing 5 changed files with 910 additions and 387 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# announcer
Simple `node` script listening for certain events on `socket.io` and announce them in a [Slack](https://slack.com/) channel



## Example
#### Match Started
Once a official match starts, a message will be posted showing the two players and a link to spectate the match
Expand Down
115 changes: 41 additions & 74 deletions kcapp-announcer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
var debug = require('debug')('kcapp-announcer');
const debug = require('debug')('kcapp-announcer');

var axios = require('axios');
var moment = require('moment');
var schedule = require("node-schedule");
var _ = require("underscore");
const axios = require('axios');
const moment = require('moment');
const schedule = require("node-schedule");
const _ = require("underscore");

var officeId = process.argv[2] || 1;
const officeId = parseInt(process.argv[2]) || 1;

var API_URL = "http://localhost:8001";
var GUI_URL = process.env.GUI_URL || "http://localhost:3000";
const API_URL = "http://localhost:8001";
const GUI_URL = process.env.GUI_URL || "http://localhost:3000";

var token = process.env.SLACK_KEY || "<slack_key_goes_here>";
var channel = process.env.SLACK_CHANNEL || "<channel_id_goes_here>";
var doAnnounce = process.env.ANNOUNCE || false;
const token = process.env.SLACK_KEY || "<slack_key_goes_here>";
const channel = process.env.SLACK_CHANNEL || "<channel_id_goes_here>";
const doAnnounce = (process.env.ANNOUNCE || false) === "true";
if (doAnnounce) {
debug(`Posting messages to Slack is enabled`);
}

const { WebClient } = require('@slack/web-api');
const web = new WebClient(token);

var message = require('./slack-message')(GUI_URL, channel);

var threads = {};
const message = require('./slack-message')(GUI_URL, channel);
const threads = {};

function postToSlack(matchId, msg) {
debug(`Posting message ${JSON.stringify(msg)}`);
Expand All @@ -45,7 +47,7 @@ function editMessage(matchId, msg) {
if (doAnnounce) {
(async () => {
try {
const response = await web.chat.update(msg);
await web.chat.update(msg);
} catch (error) {
debug(`Error when posting to slack: ${JSON.stringify(error.data)}`);
}
Expand All @@ -59,53 +61,18 @@ function editMessage(matchId, msg) {
// Post schedule of overdue matches every weekday at 09:00 CEST
schedule.scheduleJob('0 8 * * 1-5', () => {
axios.all([
axios.get(API_URL + "/player"),
axios.get(API_URL + "/tournament/groups"),
axios.get(API_URL + "/tournament/current/" + officeId)
axios.get(`${API_URL}/player`),
axios.get(`${API_URL}/tournament/groups`),
axios.get(`${API_URL}/tournament/current/${officeId}`)
]).then(axios.spread((playersResponse, groupResponse, tournamentResponse) => {
var players = playersResponse.data;
var groups = groupResponse.data;
var tournament = tournamentResponse.data;
axios.get(API_URL + "/tournament/" + tournament.id + "/matches")
const players = playersResponse.data;
const groups = groupResponse.data;
const tournament = tournamentResponse.data;
axios.get(`${API_URL}/tournament/${tournament.id}/matches`)
.then(response => {
var matches = response.data;

var msg = {
text: "Pending Official Matches :trophy:",
channel: channel,
attachments: []
}
const matches = response.data;

for (var groupId in matches) {
var group = matches[groupId];

var newMatches = _.filter(group, (match) => {
return !match.is_finished && moment(match.created_at).isSameOrBefore(moment(), 'day');
});
if (newMatches.length === 0) {
continue;
}

var groupMatches = "";
for (var i = newMatches.length - 1; i >= 0; i--) {
var match = newMatches[i];

var home = players[match.players[0]];
var homePlayerName = home.slack_handle ? `<${home.slack_handle}>` : home.name;
var away = players[match.players[1]];
var awayPlayerName = away.slack_handle ? `<${away.slack_handle}>` : away.name;
var week = moment(match.created_at).diff(moment(tournament.start_time), "weeks") + 1;
groupMatches += `Week ${week}: ${homePlayerName} - ${awayPlayerName}\n`;
}

msg.attachments.push({
"fallback": `${groups[groupId].name} Pending Matches`,
"author_name": "",
"title": `${groups[groupId].name}`,
"text": `${groupMatches}`,
"mrkdwn_in": [ "text" ]
});
}
const msg = message.tournamentMatches(tournament, matches, players, groups)
if (msg.attachments.length > 0) {
postToSlack(undefined, msg);
}
Expand All @@ -118,17 +85,17 @@ schedule.scheduleJob('0 8 * * 1-5', () => {
});
});

var kcapp = require('kcapp-sio-client/kcapp')("localhost", 3000);
const kcapp = require('kcapp-sio-client/kcapp')("localhost", 3000, "kcapp-announcer", "http");
kcapp.connect(() => {
kcapp.on('order_changed', function (data) {
var legId = data.leg_id;
axios.get(API_URL + "/leg/" + legId).then(response => {
var leg = response.data;
axios.get(API_URL + "/leg/" + legId + "/players").then(response => {
var players = response.data;
axios.get(API_URL + "/match/" + leg.match_id).then(response => {
var match = response.data;
if (match.tournament_id !== null && match.tournament.office_id == officeId) {
const legId = data.leg_id;
axios.get(`${API_URL}/leg/${legId}`).then(response => {
const leg = response.data;
axios.get(`${API_URL}/leg/${legId}/players`).then(response => {
const players = response.data;
axios.get(`${API_URL}/match/${leg.match_id}`).then(response => {
const match = response.data;
if (match.tournament_id !== null && match.tournament.office_id === officeId) {
postToSlack(leg.match_id, message.matchStarted(match, players));
} else {
debug("Skipping announcement of unofficial match...");
Expand All @@ -145,12 +112,12 @@ kcapp.connect(() => {
});

kcapp.on('leg_finished', function (data) {
var match = data.match;
var leg = data.leg;
const match = data.match;
const leg = data.leg;

if (match.tournament_id !== null && match.tournament.office_id == officeId) {
axios.get(API_URL + "/leg/" + match.legs[0].id + "/players").then(response => {
var players = response.data;
if (match.tournament_id !== null && match.tournament.office_id === officeId) {
axios.get(`${API_URL}/leg/${match.legs[0].id}/players`).then(response => {
const players = response.data;
postToSlack(match.id, message.legFinished(threads[match.id], players, match, leg, data.throw));
if (match.is_finished) {
editMessage(match.id, message.matchEnded(match, players));
Expand All @@ -163,4 +130,4 @@ kcapp.connect(() => {
});
});

debug(`Waiting for events to announce for office id ${officeId}...`);
debug(`Waiting for events to announce for office id ${officeId}...`);
Loading

0 comments on commit 3419bce

Please sign in to comment.