2014-06-30 04:17:04 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-06-30 04:17:04 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-05-17 03:25:19 +02:00
|
|
|
package itdelatrisu.opsu.beatmap;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-05-17 03:25:19 +02:00
|
|
|
import itdelatrisu.opsu.ErrorHandler;
|
2015-08-31 06:21:58 +02:00
|
|
|
import itdelatrisu.opsu.Options;
|
2015-05-17 03:25:19 +02:00
|
|
|
import itdelatrisu.opsu.Utils;
|
2015-02-12 08:27:33 +01:00
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
2015-05-17 03:25:19 +02:00
|
|
|
import itdelatrisu.opsu.db.BeatmapDB;
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.util.ArrayList;
|
2014-07-17 18:49:12 +02:00
|
|
|
import java.util.Arrays;
|
2014-06-30 04:17:04 +02:00
|
|
|
import java.util.Collections;
|
2015-06-14 03:16:27 +02:00
|
|
|
import java.util.HashMap;
|
2015-02-10 05:22:58 +01:00
|
|
|
import java.util.HashSet;
|
2014-07-17 07:00:28 +02:00
|
|
|
import java.util.Iterator;
|
2014-07-17 18:49:12 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indexed, expanding, doubly-linked list data type for song groups.
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public class BeatmapSetList {
|
|
|
|
/** Song group structure (each group contains a list of beatmaps). */
|
|
|
|
private static BeatmapSetList list;
|
2015-01-11 20:17:33 +01:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Search pattern for conditional expressions. */
|
2014-07-17 18:49:12 +02:00
|
|
|
private static final Pattern SEARCH_CONDITION_PATTERN = Pattern.compile(
|
2015-09-08 18:26:28 +02:00
|
|
|
"(ar|cs|od|hp|bpm|length|stars?)(==?|>=?|<=?)((\\d*\\.)?\\d+)"
|
2014-07-17 18:49:12 +02:00
|
|
|
);
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** List containing all parsed nodes. */
|
2015-05-17 03:25:19 +02:00
|
|
|
private ArrayList<BeatmapSetNode> parsedNodes;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-05-17 03:25:19 +02:00
|
|
|
/** Total number of beatmaps (i.e. Beatmap objects). */
|
2014-06-30 04:17:04 +02:00
|
|
|
private int mapCount = 0;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Current list of nodes (subset of parsedNodes, used for searches). */
|
2015-05-17 03:25:19 +02:00
|
|
|
private ArrayList<BeatmapSetNode> nodes;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-02-10 05:22:58 +01:00
|
|
|
/** Set of all beatmap set IDs for the parsed beatmaps. */
|
|
|
|
private HashSet<Integer> MSIDdb;
|
2015-06-14 03:16:27 +02:00
|
|
|
|
2015-06-30 02:22:38 +02:00
|
|
|
/** Map of all MD5 hashes to beatmaps. */
|
|
|
|
private HashMap<String, Beatmap> beatmapHashDB;
|
2015-02-10 05:22:58 +01:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Index of current expanded node (-1 if no node is expanded). */
|
2015-01-29 02:57:43 +01:00
|
|
|
private int expandedIndex;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-02-12 08:27:33 +01:00
|
|
|
/** Start and end nodes of expanded group. */
|
2015-05-17 03:25:19 +02:00
|
|
|
private BeatmapSetNode expandedStartNode, expandedEndNode;
|
2015-02-12 08:27:33 +01:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The last search query. */
|
2015-01-29 02:57:43 +01:00
|
|
|
private String lastQuery;
|
2015-01-11 20:17:33 +01:00
|
|
|
|
2015-01-21 01:24:59 +01:00
|
|
|
/**
|
|
|
|
* Creates a new instance of this class (overwriting any previous instance).
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public static void create() { list = new BeatmapSetList(); }
|
2015-01-21 01:24:59 +01:00
|
|
|
|
2015-01-11 20:17:33 +01:00
|
|
|
/**
|
|
|
|
* Returns the single instance of this class.
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public static BeatmapSetList get() { return list; }
|
2015-01-16 21:44:13 +01:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private BeatmapSetList() {
|
|
|
|
parsedNodes = new ArrayList<BeatmapSetNode>();
|
2015-02-10 05:22:58 +01:00
|
|
|
MSIDdb = new HashSet<Integer>();
|
2015-06-30 02:22:38 +02:00
|
|
|
beatmapHashDB = new HashMap<String, Beatmap>();
|
2015-01-29 02:57:43 +01:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the list's fields.
|
|
|
|
* This does not erase any parsed nodes.
|
|
|
|
*/
|
|
|
|
public void reset() {
|
2014-06-30 04:17:04 +02:00
|
|
|
nodes = parsedNodes;
|
2015-01-29 02:57:43 +01:00
|
|
|
expandedIndex = -1;
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedStartNode = expandedEndNode = null;
|
2015-01-29 02:57:43 +01:00
|
|
|
lastQuery = "";
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of elements.
|
|
|
|
*/
|
|
|
|
public int size() { return nodes.size(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a song group.
|
2015-05-17 03:25:19 +02:00
|
|
|
* @param beatmaps the list of beatmaps in the group
|
|
|
|
* @return the new BeatmapSetNode
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public BeatmapSetNode addSongGroup(ArrayList<Beatmap> beatmaps) {
|
2015-05-17 04:49:18 +02:00
|
|
|
BeatmapSet beatmapSet = new BeatmapSet(beatmaps);
|
|
|
|
BeatmapSetNode node = new BeatmapSetNode(beatmapSet);
|
2015-02-02 06:15:16 +01:00
|
|
|
parsedNodes.add(node);
|
2015-05-17 03:25:19 +02:00
|
|
|
mapCount += beatmaps.size();
|
2015-02-10 05:22:58 +01:00
|
|
|
|
|
|
|
// add beatmap set ID to set
|
2015-05-17 03:25:19 +02:00
|
|
|
int msid = beatmaps.get(0).beatmapSetID;
|
2015-02-10 05:22:58 +01:00
|
|
|
if (msid > 0)
|
|
|
|
MSIDdb.add(msid);
|
2015-06-14 03:16:27 +02:00
|
|
|
|
2015-06-30 02:22:38 +02:00
|
|
|
// add MD5 hashes to table
|
|
|
|
for (Beatmap beatmap : beatmaps) {
|
|
|
|
if (beatmap.md5Hash != null)
|
|
|
|
beatmapHashDB.put(beatmap.md5Hash, beatmap);
|
|
|
|
}
|
2015-02-10 05:22:58 +01:00
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
return node;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:27:33 +01:00
|
|
|
/**
|
|
|
|
* Deletes a song group from the list, and also deletes the beatmap
|
|
|
|
* directory associated with the node.
|
|
|
|
* @param node the node containing the song group to delete
|
|
|
|
* @return true if the song group was deleted, false otherwise
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public boolean deleteSongGroup(BeatmapSetNode node) {
|
2015-02-12 08:27:33 +01:00
|
|
|
if (node == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// re-link base nodes
|
|
|
|
int index = node.index;
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode ePrev = getBaseNode(index - 1), eCur = getBaseNode(index), eNext = getBaseNode(index + 1);
|
2015-02-12 08:27:33 +01:00
|
|
|
if (ePrev != null) {
|
|
|
|
if (ePrev.index == expandedIndex)
|
|
|
|
expandedEndNode.next = eNext;
|
|
|
|
else if (eNext != null && eNext.index == expandedIndex)
|
|
|
|
ePrev.next = expandedStartNode;
|
|
|
|
else
|
|
|
|
ePrev.next = eNext;
|
|
|
|
}
|
|
|
|
if (eNext != null) {
|
|
|
|
if (eNext.index == expandedIndex)
|
|
|
|
expandedStartNode.prev = ePrev;
|
|
|
|
else if (ePrev != null && ePrev.index == expandedIndex)
|
|
|
|
eNext.prev = expandedEndNode;
|
|
|
|
else
|
|
|
|
eNext.prev = ePrev;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all node references
|
2015-09-02 21:37:16 +02:00
|
|
|
BeatmapSet beatmapSet = node.getBeatmapSet();
|
|
|
|
Beatmap beatmap = beatmapSet.get(0);
|
2015-02-12 08:27:33 +01:00
|
|
|
nodes.remove(index);
|
|
|
|
parsedNodes.remove(eCur);
|
2015-09-02 21:37:16 +02:00
|
|
|
mapCount -= beatmapSet.size();
|
2015-05-17 03:25:19 +02:00
|
|
|
if (beatmap.beatmapSetID > 0)
|
|
|
|
MSIDdb.remove(beatmap.beatmapSetID);
|
2015-09-04 05:16:52 +02:00
|
|
|
for (Beatmap bm : beatmapSet) {
|
2015-09-02 21:37:16 +02:00
|
|
|
if (bm.md5Hash != null)
|
|
|
|
this.beatmapHashDB.remove(bm.md5Hash);
|
|
|
|
}
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
// reset indices
|
|
|
|
for (int i = index, size = size(); i < size; i++)
|
|
|
|
nodes.get(i).index = i;
|
|
|
|
if (index == expandedIndex) {
|
|
|
|
expandedIndex = -1;
|
|
|
|
expandedStartNode = expandedEndNode = null;
|
|
|
|
} else if (expandedIndex > index) {
|
|
|
|
expandedIndex--;
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode expandedNode = expandedStartNode;
|
2015-05-17 04:49:18 +02:00
|
|
|
for (int i = 0, size = expandedNode.getBeatmapSet().size();
|
2015-02-12 08:27:33 +01:00
|
|
|
i < size && expandedNode != null;
|
|
|
|
i++, expandedNode = expandedNode.next)
|
|
|
|
expandedNode.index = expandedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing the track
|
2015-05-17 03:25:19 +02:00
|
|
|
File dir = beatmap.getFile().getParentFile();
|
2015-02-12 08:27:33 +01:00
|
|
|
if (MusicController.trackExists() || MusicController.isTrackLoading()) {
|
2015-05-17 03:25:19 +02:00
|
|
|
File audioFile = MusicController.getBeatmap().audioFilename;
|
|
|
|
if (audioFile != null && audioFile.equals(beatmap.audioFilename)) {
|
2015-02-12 08:27:33 +01:00
|
|
|
MusicController.reset();
|
|
|
|
System.gc(); // TODO: why can't files be deleted without calling this?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 03:45:44 +01:00
|
|
|
// remove entry from cache
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapDB.delete(dir.getName());
|
2015-03-05 03:45:44 +01:00
|
|
|
|
2015-02-12 08:27:33 +01:00
|
|
|
// delete the associated directory
|
2015-08-31 06:21:58 +02:00
|
|
|
BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null;
|
|
|
|
if (ws != null)
|
|
|
|
ws.pause();
|
2015-02-12 08:27:33 +01:00
|
|
|
try {
|
|
|
|
Utils.deleteToTrash(dir);
|
|
|
|
} catch (IOException e) {
|
|
|
|
ErrorHandler.error("Could not delete song group.", e, true);
|
|
|
|
}
|
2015-08-31 06:21:58 +02:00
|
|
|
if (ws != null)
|
|
|
|
ws.resume();
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a song from a song group, and also deletes the beatmap file.
|
|
|
|
* If this causes the song group to be empty, then the song group and
|
|
|
|
* beatmap directory will be deleted altogether.
|
|
|
|
* @param node the node containing the song group to delete (expanded only)
|
|
|
|
* @return true if the song or song group was deleted, false otherwise
|
2015-05-17 03:25:19 +02:00
|
|
|
* @see #deleteSongGroup(BeatmapSetNode)
|
2015-02-12 08:27:33 +01:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public boolean deleteSong(BeatmapSetNode node) {
|
|
|
|
if (node == null || node.beatmapIndex == -1 || node.index != expandedIndex)
|
2015-02-12 08:27:33 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// last song in group?
|
2015-05-17 04:49:18 +02:00
|
|
|
int size = node.getBeatmapSet().size();
|
|
|
|
if (size == 1)
|
2015-02-12 08:27:33 +01:00
|
|
|
return deleteSongGroup(node);
|
|
|
|
|
|
|
|
// reset indices
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode expandedNode = node.next;
|
|
|
|
for (int i = node.beatmapIndex + 1;
|
2015-02-12 08:27:33 +01:00
|
|
|
i < size && expandedNode != null && expandedNode.index == node.index;
|
|
|
|
i++, expandedNode = expandedNode.next)
|
2015-05-17 03:25:19 +02:00
|
|
|
expandedNode.beatmapIndex--;
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
// remove song reference
|
2015-05-17 04:49:18 +02:00
|
|
|
Beatmap beatmap = node.getBeatmapSet().remove(node.beatmapIndex);
|
2015-02-12 08:27:33 +01:00
|
|
|
mapCount--;
|
2015-09-02 21:37:16 +02:00
|
|
|
if (beatmap.md5Hash != null)
|
|
|
|
beatmapHashDB.remove(beatmap.md5Hash);
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
// re-link nodes
|
|
|
|
if (node.prev != null)
|
|
|
|
node.prev.next = node.next;
|
|
|
|
if (node.next != null)
|
|
|
|
node.next.prev = node.prev;
|
|
|
|
|
2015-03-05 03:45:44 +01:00
|
|
|
// remove entry from cache
|
2015-05-17 03:25:19 +02:00
|
|
|
File file = beatmap.getFile();
|
|
|
|
BeatmapDB.delete(file.getParentFile().getName(), file.getName());
|
2015-03-05 03:45:44 +01:00
|
|
|
|
2015-02-12 08:27:33 +01:00
|
|
|
// delete the associated file
|
2015-08-31 06:21:58 +02:00
|
|
|
BeatmapWatchService ws = (Options.isWatchServiceEnabled()) ? BeatmapWatchService.get() : null;
|
|
|
|
if (ws != null)
|
|
|
|
ws.pause();
|
2015-02-12 08:27:33 +01:00
|
|
|
try {
|
2015-03-05 03:45:44 +01:00
|
|
|
Utils.deleteToTrash(file);
|
2015-02-12 08:27:33 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
ErrorHandler.error("Could not delete song.", e, true);
|
|
|
|
}
|
2015-08-31 06:21:58 +02:00
|
|
|
if (ws != null)
|
|
|
|
ws.resume();
|
2015-02-12 08:27:33 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Returns the total number of parsed maps (i.e. Beatmap objects).
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
public int getMapCount() { return mapCount; }
|
|
|
|
|
2015-02-19 06:33:32 +01:00
|
|
|
/**
|
|
|
|
* Returns the total number of parsed maps sets.
|
|
|
|
*/
|
|
|
|
public int getMapSetCount() { return parsedNodes.size(); }
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Returns the BeatmapSetNode at an index, disregarding expansions.
|
2015-03-08 21:34:28 +01:00
|
|
|
* @param index the node index
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public BeatmapSetNode getBaseNode(int index) {
|
2014-06-30 04:17:04 +02:00
|
|
|
if (index < 0 || index >= size())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return nodes.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a random base node.
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public BeatmapSetNode getRandomNode() {
|
|
|
|
BeatmapSetNode node = getBaseNode((int) (Math.random() * size()));
|
2014-07-15 06:20:36 +02:00
|
|
|
if (node != null && node.index == expandedIndex) // don't choose an expanded group node
|
2014-07-11 01:13:53 +02:00
|
|
|
node = node.next;
|
|
|
|
return node;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Returns the BeatmapSetNode a given number of positions forward or backwards.
|
2014-06-30 04:17:04 +02:00
|
|
|
* @param node the starting node
|
|
|
|
* @param shift the number of nodes to shift forward (+) or backward (-).
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public BeatmapSetNode getNode(BeatmapSetNode node, int shift) {
|
|
|
|
BeatmapSetNode startNode = node;
|
2014-06-30 04:17:04 +02:00
|
|
|
if (shift > 0) {
|
|
|
|
for (int i = 0; i < shift && startNode != null; i++)
|
|
|
|
startNode = startNode.next;
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < shift && startNode != null; i++)
|
|
|
|
startNode = startNode.prev;
|
|
|
|
}
|
|
|
|
return startNode;
|
|
|
|
}
|
2014-07-11 01:13:53 +02:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
|
|
|
* Returns the index of the expanded node (or -1 if nothing is expanded).
|
|
|
|
*/
|
|
|
|
public int getExpandedIndex() { return expandedIndex; }
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Expands the node at an index by inserting a new node for each Beatmap
|
2014-07-11 01:13:53 +02:00
|
|
|
* in that node and hiding the group node.
|
2015-09-10 05:51:16 +02:00
|
|
|
* @param index the node index
|
2014-07-11 01:13:53 +02:00
|
|
|
* @return the first of the newly-inserted nodes
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public BeatmapSetNode expand(int index) {
|
2014-06-30 04:17:04 +02:00
|
|
|
// undo the previous expansion
|
2014-07-11 01:13:53 +02:00
|
|
|
unexpand();
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode node = getBaseNode(index);
|
2014-06-30 04:17:04 +02:00
|
|
|
if (node == null)
|
2014-07-11 01:13:53 +02:00
|
|
|
return null;
|
|
|
|
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedStartNode = expandedEndNode = null;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-11 01:13:53 +02:00
|
|
|
// create new nodes
|
2015-05-17 04:49:18 +02:00
|
|
|
BeatmapSet beatmapSet = node.getBeatmapSet();
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode prevNode = node.prev;
|
|
|
|
BeatmapSetNode nextNode = node.next;
|
2015-05-17 04:49:18 +02:00
|
|
|
for (int i = 0, size = beatmapSet.size(); i < size; i++) {
|
|
|
|
BeatmapSetNode newNode = new BeatmapSetNode(beatmapSet);
|
2014-06-30 04:17:04 +02:00
|
|
|
newNode.index = index;
|
2015-05-17 03:25:19 +02:00
|
|
|
newNode.beatmapIndex = i;
|
2014-06-30 04:17:04 +02:00
|
|
|
newNode.prev = node;
|
|
|
|
|
2014-07-11 01:13:53 +02:00
|
|
|
// unlink the group node
|
|
|
|
if (i == 0) {
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedStartNode = newNode;
|
2014-07-11 01:13:53 +02:00
|
|
|
newNode.prev = prevNode;
|
|
|
|
if (prevNode != null)
|
|
|
|
prevNode.next = newNode;
|
|
|
|
}
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
node.next = newNode;
|
|
|
|
node = node.next;
|
|
|
|
}
|
|
|
|
if (nextNode != null) {
|
|
|
|
node.next = nextNode;
|
|
|
|
nextNode.prev = node;
|
|
|
|
}
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedEndNode = node;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
expandedIndex = index;
|
2015-02-12 08:27:33 +01:00
|
|
|
return expandedStartNode;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Undoes the current expansion, if any.
|
|
|
|
*/
|
2014-07-11 01:13:53 +02:00
|
|
|
private void unexpand() {
|
2014-06-30 04:17:04 +02:00
|
|
|
if (expandedIndex < 0 || expandedIndex >= size())
|
2014-07-11 01:13:53 +02:00
|
|
|
return;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-11 01:13:53 +02:00
|
|
|
// recreate surrounding links
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode
|
2014-07-11 01:13:53 +02:00
|
|
|
ePrev = getBaseNode(expandedIndex - 1),
|
|
|
|
eCur = getBaseNode(expandedIndex),
|
|
|
|
eNext = getBaseNode(expandedIndex + 1);
|
|
|
|
if (ePrev != null)
|
|
|
|
ePrev.next = eCur;
|
|
|
|
eCur.prev = ePrev;
|
|
|
|
eCur.index = expandedIndex;
|
2014-06-30 04:17:04 +02:00
|
|
|
eCur.next = eNext;
|
|
|
|
if (eNext != null)
|
|
|
|
eNext.prev = eCur;
|
|
|
|
|
|
|
|
expandedIndex = -1;
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedStartNode = expandedEndNode = null;
|
2014-07-11 01:13:53 +02:00
|
|
|
return;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-19 02:33:41 +02:00
|
|
|
* Initializes the links in the list.
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2014-07-19 02:33:41 +02:00
|
|
|
public void init() {
|
2014-06-30 04:17:04 +02:00
|
|
|
if (size() < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// sort the list
|
2015-05-29 08:34:08 +02:00
|
|
|
Collections.sort(nodes, BeatmapSortOrder.getSort().getComparator());
|
2014-06-30 04:17:04 +02:00
|
|
|
expandedIndex = -1;
|
2015-02-12 08:27:33 +01:00
|
|
|
expandedStartNode = expandedEndNode = null;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// create links
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode lastNode = nodes.get(0);
|
2014-06-30 04:17:04 +02:00
|
|
|
lastNode.index = 0;
|
|
|
|
lastNode.prev = null;
|
|
|
|
for (int i = 1, size = size(); i < size; i++) {
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode node = nodes.get(i);
|
2014-06-30 04:17:04 +02:00
|
|
|
lastNode.next = node;
|
|
|
|
node.index = i;
|
|
|
|
node.prev = lastNode;
|
|
|
|
|
|
|
|
lastNode = node;
|
|
|
|
}
|
|
|
|
lastNode.next = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-16 21:44:13 +01:00
|
|
|
* Creates a new list of song groups in which each group contains a match to a search query.
|
2014-07-17 07:00:28 +02:00
|
|
|
* @param query the search query (terms separated by spaces)
|
2014-06-30 04:17:04 +02:00
|
|
|
* @return false if query is the same as the previous one, true otherwise
|
|
|
|
*/
|
|
|
|
public boolean search(String query) {
|
|
|
|
if (query == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// don't redo the same search
|
|
|
|
query = query.trim().toLowerCase();
|
|
|
|
if (lastQuery != null && query.equals(lastQuery))
|
|
|
|
return false;
|
|
|
|
lastQuery = query;
|
2014-07-17 18:49:12 +02:00
|
|
|
LinkedList<String> terms = new LinkedList<String>(Arrays.asList(query.split("\\s+")));
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
// if empty query, reset to original list
|
2014-07-17 18:49:12 +02:00
|
|
|
if (query.isEmpty() || terms.isEmpty()) {
|
2014-06-30 04:17:04 +02:00
|
|
|
nodes = parsedNodes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:49:12 +02:00
|
|
|
// find and remove any conditional search terms
|
|
|
|
LinkedList<String> condType = new LinkedList<String>();
|
|
|
|
LinkedList<String> condOperator = new LinkedList<String>();
|
|
|
|
LinkedList<Float> condValue = new LinkedList<Float>();
|
|
|
|
|
|
|
|
Iterator<String> termIter = terms.iterator();
|
|
|
|
while (termIter.hasNext()) {
|
|
|
|
String term = termIter.next();
|
|
|
|
Matcher m = SEARCH_CONDITION_PATTERN.matcher(term);
|
|
|
|
if (m.find()) {
|
|
|
|
condType.add(m.group(1));
|
|
|
|
condOperator.add(m.group(2));
|
|
|
|
condValue.add(Float.parseFloat(m.group(3)));
|
|
|
|
termIter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build an initial list from first search term
|
2015-05-17 03:25:19 +02:00
|
|
|
nodes = new ArrayList<BeatmapSetNode>();
|
2014-07-17 18:49:12 +02:00
|
|
|
if (terms.isEmpty()) {
|
|
|
|
// conditional term
|
|
|
|
String type = condType.remove();
|
|
|
|
String operator = condOperator.remove();
|
|
|
|
float value = condValue.remove();
|
2015-05-17 03:25:19 +02:00
|
|
|
for (BeatmapSetNode node : parsedNodes) {
|
2015-05-17 04:49:18 +02:00
|
|
|
if (node.getBeatmapSet().matches(type, operator, value))
|
2014-07-17 18:49:12 +02:00
|
|
|
nodes.add(node);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// normal term
|
|
|
|
String term = terms.remove();
|
2015-05-17 03:25:19 +02:00
|
|
|
for (BeatmapSetNode node : parsedNodes) {
|
2015-05-17 04:49:18 +02:00
|
|
|
if (node.getBeatmapSet().matches(term))
|
2014-07-17 18:49:12 +02:00
|
|
|
nodes.add(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through remaining normal search terms
|
|
|
|
while (!terms.isEmpty()) {
|
|
|
|
if (nodes.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
String term = terms.remove();
|
|
|
|
|
|
|
|
// remove nodes from list if they don't match all terms
|
2015-05-17 03:25:19 +02:00
|
|
|
Iterator<BeatmapSetNode> nodeIter = nodes.iterator();
|
2014-07-17 18:49:12 +02:00
|
|
|
while (nodeIter.hasNext()) {
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode node = nodeIter.next();
|
2015-05-17 04:49:18 +02:00
|
|
|
if (!node.getBeatmapSet().matches(term))
|
2014-07-17 18:49:12 +02:00
|
|
|
nodeIter.remove();
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2014-07-17 07:00:28 +02:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
|
2014-07-17 18:49:12 +02:00
|
|
|
// iterate through remaining conditional terms
|
|
|
|
while (!condType.isEmpty()) {
|
|
|
|
if (nodes.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
String type = condType.remove();
|
|
|
|
String operator = condOperator.remove();
|
|
|
|
float value = condValue.remove();
|
|
|
|
|
|
|
|
// remove nodes from list if they don't match all terms
|
2015-05-17 03:25:19 +02:00
|
|
|
Iterator<BeatmapSetNode> nodeIter = nodes.iterator();
|
2014-07-17 18:49:12 +02:00
|
|
|
while (nodeIter.hasNext()) {
|
2015-05-17 03:25:19 +02:00
|
|
|
BeatmapSetNode node = nodeIter.next();
|
2015-05-17 04:49:18 +02:00
|
|
|
if (!node.getBeatmapSet().matches(type, operator, value))
|
2014-07-17 18:49:12 +02:00
|
|
|
nodeIter.remove();
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-10 05:22:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the list contains the given beatmap set ID.
|
|
|
|
* <p>
|
|
|
|
* Note that IDs for older maps might have been improperly parsed, so
|
|
|
|
* there is no guarantee that this method will return an accurate value.
|
|
|
|
* @param id the beatmap set ID to check
|
|
|
|
* @return true if id is in the list
|
|
|
|
*/
|
|
|
|
public boolean containsBeatmapSetID(int id) { return MSIDdb.contains(id); }
|
2015-06-30 02:22:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the beatmap associated with the given hash.
|
|
|
|
* @param beatmapHash the MD5 hash
|
|
|
|
* @return the associated beatmap, or {@code null} if no match was found
|
|
|
|
*/
|
|
|
|
public Beatmap getBeatmapFromHash(String beatmapHash) {
|
|
|
|
return beatmapHashDB.get(beatmapHash);
|
2015-06-14 03:16:27 +02:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|