« get me outta code hell

listArtistsByLatestContribution.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/listArtistsByLatestContribution.js
blob: 0f7095778c1310150c89b9c6f3907788f2af5f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import {chunkMultipleArrays, empty, sortMultipleArrays, stitchArrays}
  from '#sugar';
import T from '#things';

import {
  sortAlphabetically,
  sortAlbumsTracksChronologically,
  sortFlashesChronologically,
} from '#sort';

const {Album, Flash} = T;

export default {
  contentDependencies: [
    'generateListingPage',
    'linkAlbum',
    'linkArtist',
    'linkFlash',
  ],

  extraDependencies: ['html', 'language', 'wikiData'],

  sprawl: ({albumData, artistData, flashData, trackData, wikiInfo}) =>
    ({albumData, artistData, flashData, trackData,
      enableFlashesAndGames: wikiInfo.enableFlashesAndGames}),

  query(sprawl, spec) {
    //
    // First main step is to get the latest thing each artist has contributed
    // to, and the date associated with that contribution! Some notes:
    //
    // * Album and track contributions are considered before flashes, so
    //   they'll take priority if an artist happens to have multiple contribs
    //   landing on the same date to both an album and a flash.
    //
    // * The final (album) contribution list is chunked by album, but also by
    //   date, because an individual album can cover a variety of dates.
    //
    // * If an artist has contributed both artworks and tracks to the album
    //   containing their latest contribution, then that will be indicated
    //   in an annotation, but *only if* those contributions were also on
    //   the same date.
    //
    // * If an artist made contributions to multiple albums on the same date,
    //   then the first of the *albums* sorted chronologically (latest first)
    //   is the one that will count.
    //
    // * Same for artists who've contributed to multiple flashes which were
    //   released on the same date.
    //
    // * The map may exclude artists none of whose contributions were dated.
    //

    const artistLatestContribMap = new Map();

    const considerDate = (artist, date, thing, contribution) => {
      if (!date) {
        return;
      }

      if (artistLatestContribMap.has(artist)) {
        const latest = artistLatestContribMap.get(artist);
        if (latest.date > date) {
          return;
        }

        if (latest.date === date) {
          if (latest.thing === thing) {
            // May combine differnt contributions to the same thing and date.
            latest.contribution.add(contribution);
          }

          // Earlier-processed things of same date take priority.
          return;
        }
      }

      // First entry for artist or more recent contribution than latest date.
      artistLatestContribMap.set(artist, {
        date,
        thing,
        contribution: new Set([contribution]),
      });
    };

    const getArtists = (thing, key) => thing[key].map(({who}) => who);

    const albumsLatestFirst = sortAlbumsTracksChronologically(sprawl.albumData.slice());
    const tracksLatestFirst = sortAlbumsTracksChronologically(sprawl.trackData.slice());
    const flashesLatestFirst = sortFlashesChronologically(sprawl.flashData.slice());

    for (const album of albumsLatestFirst) {
      for (const artist of new Set([
        ...getArtists(album, 'coverArtistContribs'),
        ...getArtists(album, 'wallpaperArtistContribs'),
        ...getArtists(album, 'bannerArtistContribs'),
      ])) {
        // Might combine later with 'track' of the same album and date.
        considerDate(artist, album.coverArtDate ?? album.date, album, 'artwork');
      }
    }

    for (const track of tracksLatestFirst) {
      for (const artist of getArtists(track, 'coverArtistContribs')) {
        // No special effect if artist already has 'artwork' for the same album and date.
        considerDate(artist, track.coverArtDate ?? track.date, track.album, 'artwork');
      }

      for (const artist of new Set([
        ...getArtists(track, 'artistContribs'),
        ...getArtists(track, 'contributorContribs'),
      ])) {
        // Might be combining with 'artwork' of the same album and date.
        considerDate(artist, track.date, track.album, 'track');
      }
    }

    for (const flash of flashesLatestFirst) {
      for (const artist of getArtists(flash, 'contributorContribs')) {
        // Won't take priority above album contributions of the same date.
        considerDate(artist, flash.date, flash, 'flash');
      }
    }

    //
    // Next up is to sort all the processed artist information!
    //
    // Entries with the same album/flash and the same date go together first,
    // with the following rules for sorting artists therein:
    //
    // * If the contributions are different, which can only happen for albums,
    //   then it's tracks-only first, tracks + artworks next, and artworks-only
    //   last.
    //
    // * If the contributions are the same, then sort alphabetically.
    //
    // Entries with different albums/flashes follow another set of rules:
    //
    // * Later dates come before earlier dates.
    //
    // * On the same date, albums come before flashes.
    //
    // * Things of the same type *and* date are sorted alphabetically.
    //

    const artistsAlphabetically =
      sortAlphabetically(
        sprawl.artistData.filter(artist => !artist.isAlias));

    const artists =
      Array.from(artistLatestContribMap.keys());

    const artistContribEntries =
      Array.from(artistLatestContribMap.values());

    const artistThings =
      artistContribEntries.map(({thing}) => thing);

    const artistDates =
      artistContribEntries.map(({date}) => date);

    const artistContributions =
      artistContribEntries.map(({contribution}) => contribution);

    sortMultipleArrays(artistThings, artistDates, artistContributions, artists,
      (thing1, thing2, date1, date2, contrib1, contrib2, artist1, artist2) => {
        if (date1 === date2 && thing1 === thing2) {
          // Move artwork-only contribs after contribs with tracks.
          if (!contrib1.has('track') && contrib2.has('track')) return 1;
          if (!contrib2.has('track') && contrib1.has('track')) return -1;

          // Move track-only contribs before tracks with tracks and artwork.
          if (!contrib1.has('artwork') && contrib2.has('artwork')) return -1;
          if (!contrib2.has('artwork') && contrib1.has('artwork')) return 1;

          // Sort artists of the same type of contribution alphabetically,
          // referring to a previous sort.
          const index1 = artistsAlphabetically.indexOf(artist1);
          const index2 = artistsAlphabetically.indexOf(artist2);
          return index1 - index2;
        } else {
          // Move later dates before earlier ones.
          if (date1 !== date2) return date2 - date1;

          // Move albums before flashes.
          if (thing1 instanceof Album && thing2 instanceof Flash) return -1;
          if (thing1 instanceof Flash && thing2 instanceof Album) return 1;

          // Sort two albums or two flashes alphabetically, referring to a
          // previous sort (which was chronological but includes the correct
          // ordering for things released on the same date).
          const thingsLatestFirst =
            (thing1 instanceof Album
              ? albumsLatestFirst
              : flashesLatestFirst);
          const index1 = thingsLatestFirst.indexOf(thing1);
          const index2 = thingsLatestFirst.indexOf(thing2);
          return index2 - index1;
        }
      });

    const chunks =
      chunkMultipleArrays(artistThings, artistDates, artistContributions, artists,
        (thing, lastThing, date, lastDate) =>
          thing !== lastThing ||
          +date !== +lastDate);

    const chunkThings =
      chunks.map(([artistThings, , , ]) => artistThings[0]);

    const chunkDates =
      chunks.map(([, artistDates, , ]) => artistDates[0]);

    const chunkArtistContributions =
      chunks.map(([, , artistContributions, ]) => artistContributions);

    const chunkArtists =
      chunks.map(([, , , artists]) => artists);

    // And one bonus step - keep track of all the artists whose contributions
    // were all without date.

    const datelessArtists =
      artistsAlphabetically
        .filter(artist => !artists.includes(artist));

    return {
      spec,
      chunkThings,
      chunkDates,
      chunkArtistContributions,
      chunkArtists,
      datelessArtists,
    };
  },

  relations: (relation, query) => ({
    page:
      relation('generateListingPage', query.spec),

    chunkAlbumLinks:
      query.chunkThings
        .map(thing =>
          (thing instanceof Album
            ? relation('linkAlbum', thing)
            : null)),

    chunkFlashLinks:
      query.chunkThings
        .map(thing =>
          (thing instanceof Flash
            ? relation('linkFlash', thing)
            : null)),

    chunkArtistLinks:
      query.chunkArtists
        .map(artists => artists
          .map(artist => relation('linkArtist', artist))),

    datelessArtistLinks:
      query.datelessArtists
        .map(artist => relation('linkArtist', artist)),
  }),

  data: (query) => ({
    chunkDates: query.chunkDates,
    chunkArtistContributions: query.chunkArtistContributions,
  }),

  generate(data, relations, {language}) {
    return relations.page.slots({
      type: 'chunks',

      chunkTitles:
        stitchArrays({
          albumLink: relations.chunkAlbumLinks,
          flashLink: relations.chunkFlashLinks,
          date: data.chunkDates,
        }).map(({albumLink, flashLink, date}) => ({
            date: language.formatDate(date),
            ...(albumLink
              ? {stringsKey: 'album', album: albumLink}
              : {stringsKey: 'flash', flash: flashLink}),
          }))
          .concat(
            (empty(relations.datelessArtistLinks)
              ? []
              : [{stringsKey: 'dateless'}])),

      chunkRows:
        stitchArrays({
          artistLinks: relations.chunkArtistLinks,
          contributions: data.chunkArtistContributions,
        }).map(({artistLinks, contributions}) =>
            stitchArrays({
              artistLink: artistLinks,
              contribution: contributions,
            }).map(({artistLink, contribution}) => ({
                artist: artistLink,
                stringsKey:
                  (contribution.has('track') && contribution.has('artwork')
                    ? 'tracksAndArt'
                 : contribution.has('track')
                    ? 'tracks'
                 : contribution.has('artwork')
                    ? 'art'
                    : null),
              })))
          .concat(
            (empty(relations.datelessArtistLinks)
              ? []
              : [
                  relations.datelessArtistLinks.map(artistLink => ({
                    artist: artistLink,
                  })),
                ])),
    });
  },
};