Skip to content

Commit b3d4708

Browse files
Persijn Kwekkeboompoi33
authored andcommitted
Webhooks Slack message #691
Added proper formatting for finished games
1 parent 5c38804 commit b3d4708

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

src/main/resources/lib/office-league-report.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ eventLib.listener({
1818
var baseUrl = app.config['officeleague.baseUrl'] || 'localhost:8080/webapp/com.enonic.app.officeleague';
1919
var gameData = createGameData(game, baseUrl);
2020

21-
log.info(JSON.stringify(gameData, null, 4));
22-
2321
eventLib.send({
2422
type: OFFICE_LEAGUE_GAME_REPORT_EVENT_ID,
2523
distributed: false,

src/main/resources/lib/office-league-slack.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ function createFinishedGameMessage(data) {
1414
"blocks": []
1515
};
1616

17-
let matchResult = "";
18-
let ratingResult = "";
17+
let matchResult;
18+
let ratingResult;
1919

2020
if (data.playerCount > 2) {
2121
matchResult = formatWinning(data.teams);
22-
ratingResult = formatRating(data.teams) + "\n" + formatRatingPlayers(data.players);
22+
ratingResult = [].concat(
23+
ratingContext(data.teams),
24+
ratingContextPlayers(data.players)
25+
);
2326
}
2427
else {
2528
matchResult = formatWinning(data.players);
26-
ratingResult = formatRating(data.players);
29+
ratingResult = ratingContext(data.players);
2730
}
2831

2932
message.blocks.push(
@@ -40,21 +43,13 @@ function createFinishedGameMessage(data) {
4043
{
4144
"type": "mrkdwn",
4245
"text": matchResult
43-
},
44-
45-
]
46-
},
47-
{
48-
"type": "context",
49-
"elements": [
50-
{
51-
"type": "mrkdwn",
52-
"text": ratingResult
53-
},
46+
}
5447
]
5548
}
5649
);
5750

51+
message.blocks = message.blocks.concat(ratingResult);
52+
5853
return message;
5954
}
6055

@@ -93,7 +88,7 @@ function createNewGameMessage(data) {
9388
}
9489
);
9590

96-
message.blocks.concat(createTeamSection(data));
91+
message.blocks = message.blocks.concat(createTeamSection(data));
9792
} else {
9893
const sides = getSides(data.players);
9994

@@ -125,7 +120,7 @@ function createNewGameMessage(data) {
125120
}
126121

127122
/**
128-
* Method for formating the expted message
123+
* Method for formating the expected message
129124
* @param {Array<Object>} group - player or team
130125
* @param {string|number} expectedScoreWinner - The winners expected score
131126
* @param {string|number} expectedscoreLoser - The losing teams expected score
@@ -162,23 +157,53 @@ function formatWinning(opponents) {
162157
* Formats a message of diff in rating
163158
* @param {Array<object>} opponents - Team or players
164159
*/
165-
function formatRating(opponents) {
160+
function ratingContext(opponents) {
166161
const winner = opponents[0].score > opponents[1].score ? opponents[0] : opponents[1];
167162
const loser = opponents[1].score > opponents[0].score ? opponents[0] : opponents[1];
168163

169-
return `${winner.name} wins ${winner.ratingDelta} points. New rating ${winner.rating}\n` +
170-
`${loser.name} loses ${loser.ratingDelta} points. New rating ${loser.rating}`;
164+
const context = [
165+
imageAndTextContext(winner),
166+
imageAndTextContext(loser)
167+
];
168+
169+
return context;
171170
}
172171

173-
function formatRatingPlayers(players) {
174-
let playerRatingText = "";
172+
/**
173+
*
174+
* @param {object} profile Team or player profile
175+
* @param {boolean} context If the context should be inlcuded or elements/fields only
176+
* @returns {object|Array} Based on the context boolean
177+
*/
178+
function imageAndTextContext(profile) {
179+
const winLose = profile.ratingDelta < 0 ? '⬇️' : '⬆️';
180+
const elements = [
181+
{
182+
"type": "image",
183+
"image_url": `${profile.imageUrl}`,
184+
"alt_text": `Profile image of ${profile.name}`
185+
},
186+
{
187+
"type": "mrkdwn",
188+
"text": `${profile.name} ${winLose} ${profile.ratingDelta} point${profile.ratingDelta == 1 || profile.ratingDelta == -1 ? '' : 's'}. New rating ${profile.rating}\n`
189+
},
190+
];
175191

176-
for (const player in players) {
177-
const winLose = player.ratingDelta < 0 ? 'lose' : 'wins';
178-
playerRatingText += `${player.name} ${winLose} ${player.ratingDelta} points. New rating ${player.rating}\n`
179-
}
180192

181-
return playerRatingText;
193+
return {
194+
"type": "context",
195+
"elements": elements
196+
};
197+
}
198+
199+
function ratingContextPlayers(players) {
200+
let blocks = [];
201+
202+
for (const player of players) {
203+
blocks = blocks.concat(imageAndTextContext(player));
204+
};
205+
206+
return blocks;
182207
}
183208

184209
function createVsTeamSection(players) {

src/main/resources/lib/webhooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ const processGameWebhook = function (gameData, url) {
8080
});
8181

8282
if (response.status >= 300 || response.status < 200) {
83-
log.info('Error posting webhook to [' + url + ']: ' + response.status + ' ' + response.message);
84-
log.info(JSON.stringify(response, null, 4));
83+
log.warn('Error posting webhook to [' + url + ']: ' + response.status + ' ' + response.message);
84+
log.warn(JSON.stringify(response, null, 4));
8585
}
8686

8787
}

0 commit comments

Comments
 (0)