Skip to content

Commit

Permalink
Sql stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Nov 30, 2023
1 parent a3732b5 commit 102849d
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugin/src/main/bash/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="954"
BUILD_NUM="961"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand;
import com.craftmend.openaudiomc.generic.commands.objects.Argument;
import com.craftmend.openaudiomc.generic.commands.objects.CommandError;
import com.craftmend.openaudiomc.generic.platform.OaColor;
import com.craftmend.openaudiomc.generic.user.User;
import com.craftmend.openaudiomc.spigot.modules.commands.subcommands.playlist.delegates.*;

Expand All @@ -11,7 +13,7 @@ public PlaylistSubCommand() {
super("playlist", "list");

registerArguments(
new Argument("create <playlistName<", "create a new playlist"),
new Argument("create <playlistName>", "create a new playlist"),
new Argument("delete <playlistName>", "delete a playlist"),
new Argument("list", "list all playlists"),
new Argument("remove <playlistName> <index>", "remove a track from a playlist"),
Expand Down Expand Up @@ -61,5 +63,12 @@ public void onExecute(User sender, String[] args) {
delegateTo("add", sender, args);
return;
}

// no valid arguments
message(sender, "Invalid arguments. Use /openaudio playlist for help");
message(sender, "Valid arguments are:");
for (Argument argument : getArguments()) {
message(sender, " - " + OaColor.RED + argument.getSyntax() + OaColor.DARK_AQUA + " - " + OaColor.AQUA + argument.getDescription());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void onExecute(User sender, String[] args) {

playlist.addEntry(new PlaylistEntry(source));

getService(PlaylistService.class).saveAll();
message(sender, "Added " + source + " to playlist " + playlist.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void onExecute(User sender, String[] args) {
}

playlistService.createPlaylist(name, sender.getName());
getService(PlaylistService.class).saveAll();
message(sender, "Created a new playlist with the name " + name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void onExecute(User sender, String[] args) {
}

playlistService.deletePlaylist(name);
getService(PlaylistService.class).saveAll();
message(sender, "Deleted the playlist with the name " + name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void onExecute(User sender, String[] args) {
if (!removed) {
throw new CommandError("There is no track at that index");
}
getService(PlaylistService.class).saveAll();
message(sender, "Removed track at index " + index + " from playlist " + playlist.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public void onExecute(User sender, String[] args) {
throw new CommandError("A playlist with that name does not exist");
}

if (playlist.getEntries().isEmpty()) {
message(sender, "Playlist " + playlist.getName() + " by " + playlist.getCreatedBy() + " is empty");
return;
}

message(sender, "Playlist " + playlist.getName() + " by " + playlist.getCreatedBy() + " has " + playlist.getEntries().size() + " tracks");
for (PlaylistEntry orderedEntry : playlist.getOrderedEntries()) {
clickable(sender, OaColor.GREEN + " - " + OaColor.WHITE + orderedEntry.getIndex() + OaColor.AQUA + " " + orderedEntry.getMedia(), "/openaudio playlist remove " + playlist.getName() + " " + orderedEntry.getIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.craftmend.openaudiomc.generic.database.DatabaseService;
import com.craftmend.openaudiomc.generic.database.internal.Repository;
import com.craftmend.openaudiomc.generic.logging.OpenAudioLogger;
import com.craftmend.openaudiomc.generic.media.MediaService;
import com.craftmend.openaudiomc.generic.media.interfaces.UrlMutation;
import com.craftmend.openaudiomc.generic.service.Inject;
Expand Down Expand Up @@ -49,6 +50,7 @@ public void saveAll() {
// delete entries
for (PlaylistEntry deletedEntry : value.getDeletedEntries()) {
playlistEntryRepository.delete(deletedEntry);
OpenAudioLogger.toConsole("Deleted entry " + deletedEntry.getId() + " from playlist " + value.getName());
}
value.getDeletedEntries().clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import com.craftmend.openaudiomc.generic.database.internal.DataStore;
import com.craftmend.storm.api.enums.ColumnType;
import com.craftmend.storm.api.markers.Column;
import com.craftmend.storm.api.markers.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Getter
@NoArgsConstructor
@Table(name = "playlists")
public class Playlist extends DataStore {

private boolean cacheDirty = true;
Expand All @@ -20,7 +24,7 @@ public class Playlist extends DataStore {
@Column(
type = ColumnType.ONE_TO_MANY,
references = {PlaylistEntry.class},
matchTo = "playlistId"
matchTo = "playlist_id"
)
private List<PlaylistEntry> entries;

Expand All @@ -39,6 +43,7 @@ public Playlist(String playlistName, String createdBy) {
public void addEntry(PlaylistEntry entry) {
// set the index to the last index + 1
updateOrderedEntries(); // ensure the cache is up to date
entry.setPlaylistId(getId());
entry.setIndex(orderedEntries.size());
entries.add(entry);
deletedEntries.removeIf((a) -> a.getId() == entry.getId());
Expand Down Expand Up @@ -76,6 +81,8 @@ public void removeEntry(PlaylistEntry entry) {
private void updateOrderedEntries() {
if (!cacheDirty) return;
orderedEntries.clear();
// add all entries to the ordered list
orderedEntries.addAll(entries);
orderedEntries.sort((o1, o2) -> {
if (o1.getIndex() > o2.getIndex()) return 1;
if (o1.getIndex() < o2.getIndex()) return -1;
Expand All @@ -91,11 +98,17 @@ public String toJsonArray() {
for (PlaylistEntry entry : orderedEntries) {
builder.append('"');
// deprecated since java 10, but some servers still use java 8
builder.append(URLEncoder.encode(entry.getMedia()));



builder.append(encodeString(entry.getMedia()));
builder.append('"');
builder.append(",");
}
builder.deleteCharAt(builder.length() - 1);
// is the buffer larger than 1? remove the last comma
if (builder.length() > 1) {
builder.deleteCharAt(builder.length() - 1);
}
builder.append("]");
return builder.toString();
}
Expand All @@ -105,4 +118,9 @@ public LinkedList<PlaylistEntry> getOrderedEntries() {
return orderedEntries;
}

private String encodeString(String s) {
// only url encode spaces and quotes
return s.replace(" ", "%20").replace("\"", "%22");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@
import com.craftmend.openaudiomc.generic.database.internal.DataStore;
import com.craftmend.storm.api.enums.KeyType;
import com.craftmend.storm.api.markers.Column;
import com.craftmend.storm.api.markers.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@Table(name = "playlist_entries")
public class PlaylistEntry extends DataStore {

@Column(
keyType = KeyType.FOREIGN,
references = {Playlist.class}
references = {Playlist.class},
name = "playlist_id"
)
private Integer playlistId;

@Column
private String media;

@Column
private int index;
@Column(name = "playlist_index")
private Integer index;

public PlaylistEntry(String source) {
super();
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/resources/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="954"
BUILD_NUM="961"
4 changes: 2 additions & 2 deletions plugin/src/main/resources/openaudiomc-build.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BUILD_VERSION="954"
BUILD_COMMIT="3d3532ec7a08ca2c10dbe22ce2e0ed142a46fb0f"
BUILD_VERSION="961"
BUILD_COMMIT="a3732b54d979770a1c7f631b9054a2c24037143e"
BUILD_AUTHOR="Mats"

0 comments on commit 102849d

Please sign in to comment.