Reduce loading time after commands on music page
This commit is contained in:
parent
b01f8721da
commit
1a368f8f38
11
src/main/java/net/Broken/RestApi/Data/AllMusicInfoData.java
Normal file
11
src/main/java/net/Broken/RestApi/Data/AllMusicInfoData.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package net.Broken.RestApi.Data;
|
||||||
|
|
||||||
|
public class AllMusicInfoData {
|
||||||
|
public CurrentMusicData currentMusic;
|
||||||
|
public PlaylistData playlist;
|
||||||
|
|
||||||
|
public AllMusicInfoData(CurrentMusicData currentMusic, PlaylistData playlist) {
|
||||||
|
this.currentMusic = currentMusic;
|
||||||
|
this.playlist = playlist;
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import net.Broken.audio.Youtube.SearchResult;
|
|||||||
import net.Broken.audio.Youtube.YoutubeTools;
|
import net.Broken.audio.Youtube.YoutubeTools;
|
||||||
import net.dv8tion.jda.core.entities.Guild;
|
import net.dv8tion.jda.core.entities.Guild;
|
||||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -59,7 +60,6 @@ public class MusicWebAPIController {
|
|||||||
logger.trace("currentMusicInfo for " + guild.getName());
|
logger.trace("currentMusicInfo for " + guild.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
Music musicCommande = (Music) MainBot.commandes.get("music");
|
|
||||||
|
|
||||||
if(guild.getAudioManager().isConnected()){
|
if(guild.getAudioManager().isConnected()){
|
||||||
AudioPlayer player = AudioM.getInstance(guild).getGuildMusicManager().player;
|
AudioPlayer player = AudioM.getInstance(guild).getGuildMusicManager().player;
|
||||||
@ -92,7 +92,58 @@ public class MusicWebAPIController {
|
|||||||
return new ResponseEntity<>(new PlaylistData(list), HttpStatus.OK);
|
return new ResponseEntity<>(new PlaylistData(list), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO change token to cookie
|
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("/getAllInfo")
|
||||||
|
public ResponseEntity<AllMusicInfoData> getAllInfo(@RequestParam(value = "guild") String guildId, @CookieValue("token") String token){
|
||||||
|
if(token != null) {
|
||||||
|
Guild guild = MainBot.jda.getGuildById(guildId);
|
||||||
|
if(guild == null ){
|
||||||
|
|
||||||
|
logger.warn("All info without guild!");
|
||||||
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
|
||||||
|
logger.trace("All info from USER: " + user.getName() + " GUILD: " + guild.getName());
|
||||||
|
|
||||||
|
PlaylistData list = new PlaylistData(AudioM.getInstance(guild).getGuildMusicManager().scheduler.getList());
|
||||||
|
CurrentMusicData musicData;
|
||||||
|
if(guild.getAudioManager().isConnected()){
|
||||||
|
AudioPlayer player = AudioM.getInstance(guild).getGuildMusicManager().player;
|
||||||
|
AudioTrack currentTrack = player.getPlayingTrack();
|
||||||
|
if(currentTrack == null)
|
||||||
|
{
|
||||||
|
musicData = new CurrentMusicData(null,0, "STOP",false, AudioM.getInstance(guild).getGuildMusicManager().scheduler.isAutoFlow());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
UserAudioTrackData uat = new UserAudioTrackData(AudioM.getInstance(guild).getGuildMusicManager().scheduler.getCurrentPlayingTrack());
|
||||||
|
musicData = new CurrentMusicData(uat, currentTrack.getPosition(), currentTrack.getState().toString(), player.isPaused(), AudioM.getInstance(guild).getGuildMusicManager().scheduler.isAutoFlow());
|
||||||
|
}
|
||||||
|
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
musicData = new CurrentMusicData(null,0, "DISCONNECTED",false, false);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(new AllMusicInfoData(musicData, list),HttpStatus.OK);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (UnknownTokenException e) {
|
||||||
|
logger.warn("All info with unknown token");
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
logger.warn("All Info without token!");
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/command", method = RequestMethod.POST)
|
@RequestMapping(value = "/command", method = RequestMethod.POST)
|
||||||
public ResponseEntity<CommandResponseData> command(@RequestBody CommandPostData data, HttpServletRequest request, @RequestParam(value = "guild") String guildId, @CookieValue("token") String token){
|
public ResponseEntity<CommandResponseData> command(@RequestBody CommandPostData data, HttpServletRequest request, @RequestParam(value = "guild") String guildId, @CookieValue("token") String token){
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
//import * as M from "./materialize";
|
|
||||||
|
|
||||||
let savedPlaylist;
|
let savedPlaylist;
|
||||||
let error = false;
|
let error = false;
|
||||||
let state;
|
let state;
|
||||||
@ -14,6 +12,7 @@ let btn_flush;
|
|||||||
let btn_add;
|
let btn_add;
|
||||||
let switchAutoFlow;
|
let switchAutoFlow;
|
||||||
let loadingFlag = true;
|
let loadingFlag = true;
|
||||||
|
let musicLoadFlag = false;
|
||||||
let guild;
|
let guild;
|
||||||
let interval;
|
let interval;
|
||||||
|
|
||||||
@ -31,7 +30,6 @@ $(document).ready(function () {
|
|||||||
switchAutoFlow = $("#autoflow");
|
switchAutoFlow = $("#autoflow");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
M.Modal.init($('#modalAdd').get(0));
|
M.Modal.init($('#modalAdd').get(0));
|
||||||
|
|
||||||
$('#modal_current_info').modal();
|
$('#modal_current_info').modal();
|
||||||
@ -45,7 +43,6 @@ $(document).ready(function () {
|
|||||||
modal_loading.open();
|
modal_loading.open();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('.dropdown-button').dropdown({
|
$('.dropdown-button').dropdown({
|
||||||
inDuration: 300,
|
inDuration: 300,
|
||||||
outDuration: 225,
|
outDuration: 225,
|
||||||
@ -58,27 +55,29 @@ $(document).ready(function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
listeners();
|
listeners();
|
||||||
|
getCurentMusic();
|
||||||
interval = setInterval("getCurentMusic()", 1000);
|
interval = setInterval("getCurentMusic()", 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function getCurentMusic() {
|
function getCurentMusic() {
|
||||||
$.get("api/music/currentMusicInfo?guild=" + guild, function (data) {
|
$.get("api/music/getAllInfo?guild=" + guild, function (data) {
|
||||||
}).done(function (data) {
|
}).done(function (data) {
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
error = false;
|
error = false;
|
||||||
M.Toast.dismissAll();
|
M.Toast.dismissAll();
|
||||||
}
|
}
|
||||||
state = data.state;
|
state = data.currentMusic.state;
|
||||||
switch (data.state) {
|
switch (data.currentMusic.state) {
|
||||||
case "STOP":
|
case "STOP":
|
||||||
disconected = false;
|
disconected = false;
|
||||||
$('#modalChanels').modal('close');
|
$('#modalChanels').modal('close');
|
||||||
$('#music_text').text("Connected on Vocal Channel");
|
$('#music_text').text("Connected on Vocal Channel");
|
||||||
|
|
||||||
if (!$('#btn_info').hasClass("indeterminate")) {
|
if (!$('#music_progress').hasClass("determinate")) {
|
||||||
$('#btn_info').addClass("determinate").removeClass("indeterminate");
|
$('#music_progress').addClass("determinate").removeClass("indeterminate");
|
||||||
}
|
}
|
||||||
$('#music_progress').width("0%");
|
$('#music_progress').width("0%");
|
||||||
if (Cookies.get('token') !== undefined) {
|
if (Cookies.get('token') !== undefined) {
|
||||||
@ -118,7 +117,7 @@ function getCurentMusic() {
|
|||||||
btn_play.children().text("pause");
|
btn_play.children().text("pause");
|
||||||
btn_play.removeClass("green");
|
btn_play.removeClass("green");
|
||||||
btn_play.addClass("amber");
|
btn_play.addClass("amber");
|
||||||
updateControl(data);
|
updateControl(data.currentMusic);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -129,22 +128,28 @@ function getCurentMusic() {
|
|||||||
btn_play.removeClass("amber");
|
btn_play.removeClass("amber");
|
||||||
btn_play.addClass("green");
|
btn_play.addClass("green");
|
||||||
btn_play.addClass("green");
|
btn_play.addClass("green");
|
||||||
updateControl(data);
|
updateControl(data.currentMusic);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "LOADING":
|
case "LOADING":
|
||||||
disconected = false;
|
disconected = false;
|
||||||
$('#modalChanels').modal('close');
|
$('#modalChanels').modal('close');
|
||||||
if (!$('#btn_info').hasClass("determinate")) {
|
updateControl(data.currentMusic);
|
||||||
$('#btn_info').addClass("indeterminate").removeClass("determinate");
|
if ($('#music_progress').hasClass("determinate")) {
|
||||||
|
$('#music_progress').addClass("indeterminate").removeClass("determinate");
|
||||||
|
}
|
||||||
|
if(!musicLoadFlag){
|
||||||
|
clearInterval(interval);
|
||||||
|
interval = setInterval("getCurentMusic()", 200);
|
||||||
|
musicLoadFlag = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "DISCONNECTED":
|
case "DISCONNECTED":
|
||||||
$('#music_text').text("Disconnected from Vocal");
|
$('#music_text').text("Disconnected from Vocal");
|
||||||
|
|
||||||
if (!$('#btn_info').hasClass("indeterminate")) {
|
if (!$('#music_progress').hasClass("determinate")) {
|
||||||
$('#btn_info').addClass("determinate").removeClass("indeterminate");
|
$('#music_progress').addClass("determinate").removeClass("indeterminate");
|
||||||
}
|
}
|
||||||
$('#music_progress').width("0%");
|
$('#music_progress').width("0%");
|
||||||
|
|
||||||
@ -173,19 +178,34 @@ function getCurentMusic() {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (switchAutoFlow.is(':checked') != data.autoflow)
|
if (switchAutoFlow.is(':checked') !== data.currentMusic.autoflow)
|
||||||
switchAutoFlow.prop('checked', data.autoflow);
|
switchAutoFlow.prop('checked', data.currentMusic.autoflow);
|
||||||
if(data.state !== "DISCONNECTED" && data.state !== "STOP")
|
if (data.currentMusic.state !== "DISCONNECTED" && data.currentMusic.state !== "STOP"){
|
||||||
getPlayList();
|
getPlayList(data.playlist.list);
|
||||||
|
if(data.currentMusic.state !== "LOADING"){
|
||||||
|
$(".ctl-btn").removeClass("disabled");
|
||||||
|
if(musicLoadFlag){
|
||||||
|
clearInterval(interval);
|
||||||
|
interval = setInterval("getCurentMusic()", 1000);
|
||||||
|
musicLoadFlag = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
if (loadingFlag) {
|
if (loadingFlag) {
|
||||||
modal_loading.close();
|
modal_loading.close();
|
||||||
loadingFlag = false;
|
loadingFlag = false;
|
||||||
}
|
}
|
||||||
|
$(".ctl-btn").removeClass("disabled");
|
||||||
|
if(musicLoadFlag){
|
||||||
|
clearInterval(interval);
|
||||||
|
interval = setInterval("getCurentMusic()", 1000);
|
||||||
|
musicLoadFlag = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
}).fail(function (data) {
|
||||||
.fail(function (data) {
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
error = true;
|
error = true;
|
||||||
console.error("Connection lost, I keep trying to refresh!");
|
console.error("Connection lost, I keep trying to refresh!");
|
||||||
@ -200,10 +220,8 @@ function getCurentMusic() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPlayList() {
|
function getPlayList(data) {
|
||||||
$.get("api/music/getPlaylist?guild=" + guild, function (data) {
|
|
||||||
}).done(function (data) {
|
|
||||||
data = data.list;
|
|
||||||
if (data != null && data.length != 0) {
|
if (data != null && data.length != 0) {
|
||||||
var noUpdate = comparePlaylist(data, savedPlaylist);
|
var noUpdate = comparePlaylist(data, savedPlaylist);
|
||||||
if (!noUpdate) {
|
if (!noUpdate) {
|
||||||
@ -232,8 +250,6 @@ function getPlayList() {
|
|||||||
url: $(this).attr("data_url")
|
url: $(this).attr("data_url")
|
||||||
};
|
};
|
||||||
sendCommand(command, true);
|
sendCommand(command, true);
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -245,24 +261,9 @@ function getPlayList() {
|
|||||||
modal_loading.close();
|
modal_loading.close();
|
||||||
loadingFlag = false;
|
loadingFlag = false;
|
||||||
}
|
}
|
||||||
$(".ctl-btn").removeClass("disabled");
|
|
||||||
|
|
||||||
|
|
||||||
}).fail(function (data) {
|
|
||||||
if (!error) {
|
|
||||||
M.toast({
|
|
||||||
html: " <i class=\"material-icons\" style='margin-right: 10px'>warning</i> Connection Lost!",
|
|
||||||
classes: 'red',
|
|
||||||
displayLength: 99999999
|
|
||||||
});
|
|
||||||
error = true;
|
|
||||||
}
|
|
||||||
if (loadingFlag) {
|
|
||||||
modal_loading.close();
|
|
||||||
loadingFlag = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,13 +312,15 @@ function updateModal(data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateControl(data) {
|
function updateControl(data) {
|
||||||
|
|
||||||
|
if($('#music_text').text() !== data.info.audioTrackInfo.title){
|
||||||
$('#music_text').text(data.info.audioTrackInfo.title);
|
$('#music_text').text(data.info.audioTrackInfo.title);
|
||||||
var percent = (data.currentPos / data.info.audioTrackInfo.length) * 100;
|
|
||||||
if (!$('#music_progress').hasClass("indeterminate")) {
|
|
||||||
$('#music_progress').addClass("determinate").removeClass("indeterminate");
|
|
||||||
}
|
}
|
||||||
|
var percent = (data.currentPos / data.info.audioTrackInfo.length) * 100;
|
||||||
|
|
||||||
$('#music_progress').width(percent + "%");
|
$('#music_progress').width(percent + "%");
|
||||||
|
|
||||||
|
if(data.state !== "LOADING"){
|
||||||
if (Cookies.get('token') !== undefined) {
|
if (Cookies.get('token') !== undefined) {
|
||||||
enableBtn(btn_play);
|
enableBtn(btn_play);
|
||||||
enableBtn(btn_stop);
|
enableBtn(btn_stop);
|
||||||
@ -336,6 +339,11 @@ function updateControl(data) {
|
|||||||
disableBtn(btn_next);
|
disableBtn(btn_next);
|
||||||
disableBtn(btn_disconnect_music);
|
disableBtn(btn_disconnect_music);
|
||||||
}
|
}
|
||||||
|
if (!$('#music_progress').hasClass("determinate")) {
|
||||||
|
$('#music_progress').addClass("determinate").removeClass("indeterminate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('#music_img').attr("src", "https://img.youtube.com/vi/" + data.info.audioTrackInfo.identifier + "/hqdefault.jpg");
|
$('#music_img').attr("src", "https://img.youtube.com/vi/" + data.info.audioTrackInfo.identifier + "/hqdefault.jpg");
|
||||||
@ -346,6 +354,7 @@ function updateControl(data) {
|
|||||||
|
|
||||||
function sendCommand(command, modal) {
|
function sendCommand(command, modal) {
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
|
interval = null;
|
||||||
if (modal) {
|
if (modal) {
|
||||||
modal_loading.open();
|
modal_loading.open();
|
||||||
|
|
||||||
@ -360,6 +369,9 @@ function sendCommand(command, modal) {
|
|||||||
data: JSON.stringify(command),
|
data: JSON.stringify(command),
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
loadingFlag = true;
|
loadingFlag = true;
|
||||||
|
getCurentMusic();
|
||||||
|
if(interval != null)
|
||||||
|
clearInterval(interval);
|
||||||
interval = setInterval("getCurentMusic()", 1000);
|
interval = setInterval("getCurentMusic()", 1000);
|
||||||
|
|
||||||
if (command.command === "ADD") {
|
if (command.command === "ADD") {
|
||||||
@ -409,7 +421,6 @@ function comparePlaylist(list1, list2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function search() {
|
function search() {
|
||||||
let input_search = $('#input_search');
|
let input_search = $('#input_search');
|
||||||
let list = $("#search_result");
|
let list = $("#search_result");
|
||||||
@ -435,7 +446,6 @@ function search() {
|
|||||||
"</li>";
|
"</li>";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
list.append(html)
|
list.append(html)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -522,8 +532,6 @@ function ytTimeToTime(duration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function msToTime(duration) {
|
function msToTime(duration) {
|
||||||
var milliseconds = parseInt((duration % 1000) / 100)
|
var milliseconds = parseInt((duration % 1000) / 100)
|
||||||
, seconds = parseInt((duration / 1000) % 60)
|
, seconds = parseInt((duration / 1000) % 60)
|
||||||
@ -545,14 +553,14 @@ function listeners() {
|
|||||||
$('#btn_play').click(function () {
|
$('#btn_play').click(function () {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case "PLAYING":
|
case "PLAYING":
|
||||||
sendCommand({command: "PAUSE"}, false);
|
sendCommand({command: "PAUSE"}, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "PAUSE":
|
case "PAUSE":
|
||||||
sendCommand({command: "PLAY"}, false);
|
sendCommand({command: "PLAY"}, true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
sendCommand({command: "PLAY"}, false);
|
sendCommand({command: "PLAY"}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -582,8 +590,6 @@ function listeners() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('#modalChanels').change(function () {
|
$('#modalChanels').change(function () {
|
||||||
if ($('#btn_ok_channel').hasClass("disabled")) {
|
if ($('#btn_ok_channel').hasClass("disabled")) {
|
||||||
$('#btn_ok_channel').removeClass("disabled");
|
$('#btn_ok_channel').removeClass("disabled");
|
||||||
|
Loading…
Reference in New Issue
Block a user