Files
junisaber/src/components/search.js

176 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-07-18 20:48:45 +02:00
var algoliasearch = require('algoliasearch/lite');
2018-07-20 09:34:52 +02:00
var bindEvent = require('aframe-event-decorators').bindEvent;
var _ = require('lodash');
const BeatSaverAPI = require('beatsaver-api');
const api = new BeatSaverAPI({
AppName: 'Application Name',
Version: '1.0.0'
});
const zip = require("@zip.js/zip.js/dist/zip");
2018-07-18 20:48:45 +02:00
var client = algoliasearch('QULTOY3ZWU', 'be07164192471df7e97e6fa70c1d041d');
2018-07-22 11:54:26 +02:00
var algolia = client.initIndex('supersaber');
2018-07-18 20:48:45 +02:00
/**
* Search (including the initial list of popular searches).
2018-07-20 15:42:47 +02:00
* Attached to super-keyboard.
2018-07-18 20:48:45 +02:00
*/
AFRAME.registerComponent('search', {
2018-10-13 17:19:36 -07:00
init: function () {
2018-10-29 23:22:47 -07:00
this.eventDetail = {query: '', results: []};
2018-09-19 05:54:05 -07:00
this.popularHits = null;
this.queryObject = {hitsPerPage: 100, query: ''};
2018-07-18 20:48:45 +02:00
// Populate popular.
this.search('');
2018-09-19 05:54:05 -07:00
// Less hits on normal searches.
this.queryObject.hitsPerPage = 30;
2018-10-28 22:40:49 -07:00
2018-10-29 23:22:47 -07:00
this.el.sceneEl.addEventListener('searchclear', () => { this.search(''); });
2018-07-18 20:48:45 +02:00
},
2018-07-20 15:42:47 +02:00
superkeyboardchange: bindEvent(function (evt) {
2018-11-18 02:01:47 -08:00
if (evt.target !== this.el) { return; }
2018-07-20 15:42:47 +02:00
this.search(evt.detail.value);
}),
2018-07-18 20:48:45 +02:00
search: function (query) {
2018-09-19 05:54:05 -07:00
// Use cached for popular hits.
console.log(query);
2018-09-19 05:54:05 -07:00
if (!query && this.popularHits) {
2018-10-04 01:01:31 -07:00
this.eventDetail.results = this.popularHits;
2018-10-29 23:22:47 -07:00
this.eventDetail.query = '';
2018-09-19 05:54:05 -07:00
this.el.sceneEl.emit('searchresults', this.eventDetail);
return;
}
2018-10-29 23:22:47 -07:00
this.eventDetail.query = query;
2018-07-18 20:48:45 +02:00
this.queryObject.query = query;
api.searchMaps({q:query}).then((results)=>{
console.log(results);
Promise.all(_.map(results.docs, async (result) => {
let zipBlob = await fetch(result.versions[0].downloadURL).then(r => r.blob());
const zipFileReader = new zip.BlobReader(zipBlob);
const zipReader = new zip.ZipReader(zipFileReader);
const files = await zipReader.getEntries();
const info = _.find(files, {filename: "Info.dat"});
//const infoStream = new TransformStream();
//const infoPromise = new Response(infoStream.readable).text();
const infoBlob = await info.getData(new zip.BlobWriter());
const infoJson = JSON.parse(await infoBlob.text());
console.log(infoJson);
const songFile = _.find(files, {filename: infoJson._songFilename});
const songBlob = await songFile.getData(new zip.BlobWriter());
const songUrl = URL.createObjectURL(songBlob);
console.log(songUrl);
return {
songName: result.metadata.songName,
songSubName: result.metadata.songAuthorName,
imageUrl: result.versions[0].coverURL,
songUrl: songUrl,
id: result.id,
data: result,
difficulties: _.map(infoJson._difficultyBeatmapSets[0]._difficultyBeatmaps, '_difficulty'),
numBeats: result.metadata.duration,
}
})).then((tmpResults) => {
this.eventDetail.results = tmpResults;
console.log(tmpResults);
this.el.sceneEl.emit('searchresults', this.eventDetail);
});
})
// algolia.search(this.queryObject, (err, content) => {
// // Cache popular hits.
// if (err) {
// this.el.sceneEl.emit('searcherror', null, false);
// console.error(err);
// return;
// }
// if (!query) { this.popularHits = content.hits; }
// this.eventDetail.results = content.hits;
// this.el.sceneEl.emit('searchresults', this.eventDetail);
// });
2018-07-18 20:48:45 +02:00
}
});
2018-10-28 22:40:49 -07:00
/**
* Select genre filter.
*/
AFRAME.registerComponent('search-genre', {
init: function () {
this.eventDetail = {isGenreSearch: true, genre: '', results: []};
this.queryObject = {
filters: '',
hitsPerPage: 100
};
this.el.addEventListener('click', evt => {
2018-10-30 02:20:14 +01:00
this.search(evt.target.closest('.genre').dataset.bindForKey);
2018-10-28 22:40:49 -07:00
});
},
search: function (genre) {
2018-10-29 23:04:50 -07:00
if (genre === 'Video Games') {
2018-10-28 22:40:49 -07:00
this.queryObject.filters = `genre:"Video Game" OR genre:"Video Games"`;
} else {
this.queryObject.filters = `genre:"${genre}"`;
}
algolia.search(this.queryObject, (err, content) => {
if (err) {
this.el.sceneEl.emit('searcherror', null, false);
console.error(err);
return;
}
this.eventDetail.genre = genre;
this.eventDetail.results = content.hits;
this.el.sceneEl.emit('searchresults', this.eventDetail);
});
}
});
2018-07-18 20:48:45 +02:00
/**
* Click listener for search result.
*/
2018-07-20 19:58:09 +02:00
AFRAME.registerComponent('search-result-list', {
init: function () {
const obv = new MutationObserver(mutations => {
for (let i = 0; i < mutations.length; i++) {
if (mutations[i].attributeName === 'data-index') {
this.refreshLayout();
}
}
});
obv.observe(this.el, {attributes: true, childList: false, subtree: true});
},
refreshLayout: function () {
this.el.emit('layoutrefresh', null, false);
},
2018-07-20 19:58:09 +02:00
click: bindEvent(function (evt) {
2018-07-20 20:47:42 +02:00
this.el.sceneEl.emit('menuchallengeselect',
evt.target.closest('.searchResult').dataset.id,
2018-07-20 19:58:09 +02:00
false);
2018-10-13 17:19:36 -07:00
})
2018-07-18 20:48:45 +02:00
});
2018-09-19 05:54:05 -07:00
AFRAME.registerComponent('search-song-name-selected', {
schema: {
anchor: {default: 0},
index: {default: 0},
offset: {default: 0},
selectedChallengeId: {default: ''}
},
update: function () {
const data = this.data;
const el = this.el;
el.object3D.visible = !!data.selectedChallengeId && data.index !== -1;
el.object3D.position.y = data.index * data.offset + data.anchor;
}
});