Skip to content

Commit 5c38804

Browse files
Persijn Kwekkeboompoi33
authored andcommitted
Webhook slack message #691
Added a value for slackIntegration Added slack message format Players and teams are array
1 parent dd7effe commit 5c38804

File tree

3 files changed

+348
-189
lines changed

3 files changed

+348
-189
lines changed

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

Lines changed: 13 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ eventLib.listener({
1717
if (game) {
1818
var baseUrl = app.config['officeleague.baseUrl'] || 'localhost:8080/webapp/com.enonic.app.officeleague';
1919
var gameData = createGameData(game, baseUrl);
20+
21+
log.info(JSON.stringify(gameData, null, 4));
22+
2023
eventLib.send({
2124
type: OFFICE_LEAGUE_GAME_REPORT_EVENT_ID,
2225
distributed: false,
2326
data: {
24-
game: JSON.stringify(gameData) //TODO Bug in eventLib
27+
game: JSON.stringify(gameData)
2528
}
2629
});
30+
2731
}
2832
}
2933
});
@@ -36,10 +40,10 @@ const createGameData = function (game, baseUrl) {
3640
startTime: game.time,
3741
modifiedTime: game._timestamp,
3842
points: [].concat(game.points || []),
39-
players: {},
43+
players: [],
4044
playerCount: game.gamePlayers.length,
4145
teamCount: game.gameTeams.length,
42-
teams: {},
46+
teams: [],
4347
league: {},
4448
sides: {
4549
blue: {
@@ -64,7 +68,7 @@ const createGameData = function (game, baseUrl) {
6468

6569
playerJson = createPlayerJson(player, gp, leaguePlayer, baseUrl);
6670
playerJson.ranking = storeLib.getRankingForPlayerLeague(gp.playerId, game.leagueId);
67-
gameData.players[p] = playerJson;
71+
gameData.players.push(playerJson);
6872

6973
if (gp.side === 'red') {
7074
gameData.sides.red.totalScore += gp.score;
@@ -83,12 +87,12 @@ const createGameData = function (game, baseUrl) {
8387

8488
teamJson = createTeamJson(team, gt, leagueTeam, baseUrl);
8589
teamJson.ranking = storeLib.getRankingForTeamLeague(gt.teamId, game.leagueId);
86-
gameData.teams[t] = teamJson;
90+
gameData.teams.push(teamJson);
8791
}
8892
var winPoints = (league.rules || {}).pointsToWin || 10;
8993
setExpectedScore(gameData, winPoints);
9094

91-
return createNewMessage(gameData);
95+
return gameData;
9296
};
9397

9498
var createTeamJson = function (team, gameTeam, leagueTeam, baseUrl) {
@@ -128,14 +132,14 @@ var createLeagueJson = function (league, baseUrl) {
128132
leagueId: league._id,
129133
name: league.name,
130134
description: league.description,
135+
slackIntegration: league.slackIntegration || false,
131136
imageUrl: url(baseUrl, league.imageUrl)
132137
};
133138
};
134139

135140
var setExpectedScore = function (gameJson, winPoints) {
136-
var player, redRating = [], blueRating = [];
137-
for (var id in gameJson.players) {
138-
player = gameJson.players[id];
141+
var redRating = [], blueRating = [];
142+
for (const player of gameJson.players) {
139143
if (player.side === 'red') {
140144
redRating.push(player.rating);
141145
} else {
@@ -160,153 +164,3 @@ var avg = function (numberArray) {
160164
var url = function (baseUrl, relUrl) {
161165
return baseUrl + relUrl;
162166
};
163-
164-
function createNewMessage(data) {
165-
if (data.finished) {
166-
return createFinishedGameMessage(data);
167-
} else {
168-
return createNewGameMessage(data);
169-
}
170-
}
171-
172-
/**
173-
* Formats the finished game to a slack message format
174-
*/
175-
function createFinishedGameMessage(data) {
176-
const message = {
177-
"blocks": []
178-
};
179-
180-
message.blocks.push(
181-
{
182-
"type": "header",
183-
"text": {
184-
"type": "plain_text",
185-
"text": `Game finished in ${data.league.name}`
186-
}
187-
}
188-
);
189-
190-
return message;
191-
}
192-
193-
/**
194-
* Formats the new game to a slack message format
195-
* @returns Object
196-
*/
197-
function createNewGameMessage(data) {
198-
const message = {
199-
"blocks": []
200-
};
201-
202-
message.blocks.push(
203-
{
204-
"type": "header",
205-
"text": {
206-
"type": "plain_text",
207-
"text": `New game in ${data.league.name}`
208-
}
209-
}
210-
);
211-
if (Array.isArray(data.teams) && data.teams.length > 0) {
212-
message.blocks.push(
213-
createTeamSection(data.teams[1], [data.players[0], data.players[2]]),
214-
createVsTeamSection(data.players),
215-
createTeamSection(data.teams[0], [data.players[1], data.players[3]])
216-
);
217-
} else {
218-
message.blocks.push(
219-
createPlayerSection(data.players[0]),
220-
createVsPlayerSection(data.players),
221-
createPlayerSection(data.players[1])
222-
);
223-
}
224-
225-
return message;
226-
}
227-
228-
function createVsTeamSection(data) {
229-
return {
230-
"type": "context",
231-
"elements": [
232-
{
233-
"type": "image",
234-
"image_url": `${players[0].imageUrl}`,
235-
"alt_text": `Profile image of ${players[0].name}`
236-
},
237-
{
238-
"type": "image",
239-
"image_url": `${players[2].imageUrl}`,
240-
"alt_text": `Profile image of ${players[2].name}`
241-
},
242-
{
243-
"type": "mrkdwn",
244-
"text": "*VS*"
245-
},
246-
{
247-
"type": "image",
248-
"image_url": `${players[1].imageUrl}`,
249-
"alt_text": `Profile image of ${players[1].name}`
250-
},
251-
{
252-
"type": "image",
253-
"image_url": `${players[3].imageUrl}`,
254-
"alt_text": `Profile image of ${players[3].name}`
255-
}
256-
]
257-
};
258-
}
259-
260-
function createVsPlayerSection(players) {
261-
return {
262-
"type": "context",
263-
"elements": [
264-
{
265-
"type": "mrkdwn",
266-
"text": "*VS*"
267-
}
268-
]
269-
};
270-
}
271-
272-
function createPlayerSection(player) {
273-
return {
274-
"type": "section",
275-
"fields": [
276-
{
277-
"type": "mrkdwn",
278-
"text": `*${player.name}* ${player.rating}`
279-
},
280-
],
281-
"accessory": {
282-
"type": "image",
283-
"image_url": `${player.imageUrl}`,
284-
"alt_text": `Profile image of ${player.name}`
285-
}
286-
}
287-
}
288-
289-
function createTeamSection(teamData, players) {
290-
return {
291-
"type": "section",
292-
"text": {
293-
"type": "mrkdwn",
294-
"text": `*${teamData.name}*`
295-
},
296-
"fields": [
297-
{
298-
"type": "mrkdwn",
299-
"text": `*${players[0].name}* ${players[0].rating}`
300-
},
301-
{
302-
"type": "mrkdwn",
303-
"text": `*${players[1].name}* ${players[1].rating}`
304-
}
305-
],
306-
"accessory": {
307-
"type": "image",
308-
"image_url": `${teamData.imageUrl}`,
309-
"alt_text": `Profile image for ${teamData.name}`
310-
}
311-
};
312-
}

0 commit comments

Comments
 (0)