@@ -15,23 +15,23 @@ eventLib.listener({
1515 storeLib . refresh ( ) ;
1616 var game = storeLib . getGameById ( event . data . gameId ) ;
1717 if ( game ) {
18- var baseUrl = app . config [ 'officeleague.baseUrl' ] || 'http:// localhost:8080/portal/draft/office-league/ app' ;
19- var gameJson = createGameJson ( game , baseUrl ) ;
18+ var baseUrl = app . config [ 'officeleague.baseUrl' ] || 'localhost:8080/webapp/com.enonic. app.officeleague ' ;
19+ var gameData = createGameData ( game , baseUrl ) ;
2020 eventLib . send ( {
2121 type : OFFICE_LEAGUE_GAME_REPORT_EVENT_ID ,
2222 distributed : false ,
2323 data : {
24- game : JSON . stringify ( gameJson ) //TODO Bug in eventLib
24+ game : JSON . stringify ( gameData ) //TODO Bug in eventLib
2525 }
2626 } ) ;
2727 }
2828 }
2929} ) ;
3030
31- var createGameJson = function ( game , baseUrl ) {
32- var gameJson = {
31+ const createGameData = function ( game , baseUrl ) {
32+ const gameData = {
3333 gameId : game . _id ,
34- gameUrl : url ( baseUrl , '/games/' + game . _id ) ,
34+ url : url ( baseUrl , '/games/' + game . _id ) ,
3535 finished : game . finished ,
3636 startTime : game . time ,
3737 modifiedTime : game . _timestamp ,
@@ -54,7 +54,7 @@ var createGameJson = function (game, baseUrl) {
5454 } ;
5555
5656 var league = storeLib . getLeagueById ( game . leagueId ) ;
57- gameJson . league = createLeagueJson ( league , baseUrl ) ;
57+ gameData . league = createLeagueJson ( league , baseUrl ) ;
5858
5959 var p , gp , playerJson , player , leaguePlayer ;
6060 for ( p = 0 ; p < game . gamePlayers . length ; p ++ ) {
@@ -64,14 +64,14 @@ var createGameJson = function (game, baseUrl) {
6464
6565 playerJson = createPlayerJson ( player , gp , leaguePlayer , baseUrl ) ;
6666 playerJson . ranking = storeLib . getRankingForPlayerLeague ( gp . playerId , game . leagueId ) ;
67- gameJson . players [ playerJson . playerId ] = playerJson ;
67+ gameData . players [ p ] = playerJson ;
6868
6969 if ( gp . side === 'red' ) {
70- gameJson . sides . red . totalScore += gp . score ;
71- gameJson . sides . blue . totalScore += gp . scoreAgainst ;
70+ gameData . sides . red . totalScore += gp . score ;
71+ gameData . sides . blue . totalScore += gp . scoreAgainst ;
7272 } else if ( gp . side === 'blue' ) {
73- gameJson . sides . blue . totalScore += gp . score ;
74- gameJson . sides . red . totalScore += gp . scoreAgainst ;
73+ gameData . sides . blue . totalScore += gp . score ;
74+ gameData . sides . red . totalScore += gp . scoreAgainst ;
7575 }
7676 }
7777
@@ -83,12 +83,12 @@ var createGameJson = function (game, baseUrl) {
8383
8484 teamJson = createTeamJson ( team , gt , leagueTeam , baseUrl ) ;
8585 teamJson . ranking = storeLib . getRankingForTeamLeague ( gt . teamId , game . leagueId ) ;
86- gameJson . teams [ teamJson . teamId ] = teamJson ;
86+ gameData . teams [ t ] = teamJson ;
8787 }
8888 var winPoints = ( league . rules || { } ) . pointsToWin || 10 ;
89- setExpectedScore ( gameJson , winPoints ) ;
89+ setExpectedScore ( gameData , winPoints ) ;
9090
91- return gameJson ;
91+ return createNewMessage ( gameData ) ;
9292} ;
9393
9494var createTeamJson = function ( team , gameTeam , leagueTeam , baseUrl ) {
@@ -159,4 +159,154 @@ var avg = function (numberArray) {
159159
160160var url = function ( baseUrl , relUrl ) {
161161 return baseUrl + relUrl ;
162- } ;
162+ } ;
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