Reduce loading time after commands on music page

This commit is contained in:
Sebastien 2018-11-29 15:58:41 +02:00
parent b01f8721da
commit 1a368f8f38
3 changed files with 212 additions and 144 deletions

View 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;
}
}

View File

@ -17,6 +17,7 @@ import net.Broken.audio.Youtube.SearchResult;
import net.Broken.audio.Youtube.YoutubeTools;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.VoiceChannel;
import org.apache.http.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -59,7 +60,6 @@ public class MusicWebAPIController {
logger.trace("currentMusicInfo for " + guild.getName());
}
Music musicCommande = (Music) MainBot.commandes.get("music");
if(guild.getAudioManager().isConnected()){
AudioPlayer player = AudioM.getInstance(guild).getGuildMusicManager().player;
@ -92,7 +92,58 @@ public class MusicWebAPIController {
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)
public ResponseEntity<CommandResponseData> command(@RequestBody CommandPostData data, HttpServletRequest request, @RequestParam(value = "guild") String guildId, @CookieValue("token") String token){

View File

@ -1,5 +1,3 @@
//import * as M from "./materialize";
let savedPlaylist;
let error = false;
let state;
@ -14,6 +12,7 @@ let btn_flush;
let btn_add;
let switchAutoFlow;
let loadingFlag = true;
let musicLoadFlag = false;
let guild;
let interval;
@ -31,7 +30,6 @@ $(document).ready(function () {
switchAutoFlow = $("#autoflow");
M.Modal.init($('#modalAdd').get(0));
$('#modal_current_info').modal();
@ -45,7 +43,6 @@ $(document).ready(function () {
modal_loading.open();
$('.dropdown-button').dropdown({
inDuration: 300,
outDuration: 225,
@ -58,27 +55,29 @@ $(document).ready(function () {
});
listeners();
getCurentMusic();
interval = setInterval("getCurentMusic()", 1000);
}
});
function getCurentMusic() {
$.get("api/music/currentMusicInfo?guild=" + guild, function (data) {
$.get("api/music/getAllInfo?guild=" + guild, function (data) {
}).done(function (data) {
if (error) {
error = false;
M.Toast.dismissAll();
}
state = data.state;
switch (data.state) {
state = data.currentMusic.state;
switch (data.currentMusic.state) {
case "STOP":
disconected = false;
$('#modalChanels').modal('close');
$('#music_text').text("Connected on Vocal Channel");
if (!$('#btn_info').hasClass("indeterminate")) {
$('#btn_info').addClass("determinate").removeClass("indeterminate");
if (!$('#music_progress').hasClass("determinate")) {
$('#music_progress').addClass("determinate").removeClass("indeterminate");
}
$('#music_progress').width("0%");
if (Cookies.get('token') !== undefined) {
@ -118,7 +117,7 @@ function getCurentMusic() {
btn_play.children().text("pause");
btn_play.removeClass("green");
btn_play.addClass("amber");
updateControl(data);
updateControl(data.currentMusic);
break;
@ -129,22 +128,28 @@ function getCurentMusic() {
btn_play.removeClass("amber");
btn_play.addClass("green");
btn_play.addClass("green");
updateControl(data);
updateControl(data.currentMusic);
break;
case "LOADING":
disconected = false;
$('#modalChanels').modal('close');
if (!$('#btn_info').hasClass("determinate")) {
$('#btn_info').addClass("indeterminate").removeClass("determinate");
updateControl(data.currentMusic);
if ($('#music_progress').hasClass("determinate")) {
$('#music_progress').addClass("indeterminate").removeClass("determinate");
}
if(!musicLoadFlag){
clearInterval(interval);
interval = setInterval("getCurentMusic()", 200);
musicLoadFlag = true;
}
break;
case "DISCONNECTED":
$('#music_text').text("Disconnected from Vocal");
if (!$('#btn_info').hasClass("indeterminate")) {
$('#btn_info').addClass("determinate").removeClass("indeterminate");
if (!$('#music_progress').hasClass("determinate")) {
$('#music_progress').addClass("determinate").removeClass("indeterminate");
}
$('#music_progress').width("0%");
@ -173,19 +178,34 @@ function getCurentMusic() {
}
break;
}
if (switchAutoFlow.is(':checked') != data.autoflow)
switchAutoFlow.prop('checked', data.autoflow);
if(data.state !== "DISCONNECTED" && data.state !== "STOP")
getPlayList();
if (switchAutoFlow.is(':checked') !== data.currentMusic.autoflow)
switchAutoFlow.prop('checked', data.currentMusic.autoflow);
if (data.currentMusic.state !== "DISCONNECTED" && data.currentMusic.state !== "STOP"){
getPlayList(data.playlist.list);
if(data.currentMusic.state !== "LOADING"){
$(".ctl-btn").removeClass("disabled");
if(musicLoadFlag){
clearInterval(interval);
interval = setInterval("getCurentMusic()", 1000);
musicLoadFlag = false;
}
}
}
else {
if (loadingFlag) {
modal_loading.close();
loadingFlag = false;
}
$(".ctl-btn").removeClass("disabled");
if(musicLoadFlag){
clearInterval(interval);
interval = setInterval("getCurentMusic()", 1000);
musicLoadFlag = false;
}
}
})
.fail(function (data) {
}).fail(function (data) {
if (!error) {
error = true;
console.error("Connection lost, I keep trying to refresh!");
@ -200,10 +220,8 @@ function getCurentMusic() {
})
}
function getPlayList() {
$.get("api/music/getPlaylist?guild=" + guild, function (data) {
}).done(function (data) {
data = data.list;
function getPlayList(data) {
if (data != null && data.length != 0) {
var noUpdate = comparePlaylist(data, savedPlaylist);
if (!noUpdate) {
@ -232,8 +250,6 @@ function getPlayList() {
url: $(this).attr("data_url")
};
sendCommand(command, true);
});
}
}
@ -245,24 +261,9 @@ function getPlayList() {
modal_loading.close();
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) {
if($('#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 + "%");
if(data.state !== "LOADING"){
if (Cookies.get('token') !== undefined) {
enableBtn(btn_play);
enableBtn(btn_stop);
@ -336,6 +339,11 @@ function updateControl(data) {
disableBtn(btn_next);
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");
@ -346,6 +354,7 @@ function updateControl(data) {
function sendCommand(command, modal) {
clearInterval(interval);
interval = null;
if (modal) {
modal_loading.open();
@ -360,6 +369,9 @@ function sendCommand(command, modal) {
data: JSON.stringify(command),
success: function (data) {
loadingFlag = true;
getCurentMusic();
if(interval != null)
clearInterval(interval);
interval = setInterval("getCurentMusic()", 1000);
if (command.command === "ADD") {
@ -409,7 +421,6 @@ function comparePlaylist(list1, list2) {
}
function search() {
let input_search = $('#input_search');
let list = $("#search_result");
@ -435,7 +446,6 @@ function search() {
"</li>";
list.append(html)
});
@ -522,8 +532,6 @@ function ytTimeToTime(duration) {
}
function msToTime(duration) {
var milliseconds = parseInt((duration % 1000) / 100)
, seconds = parseInt((duration / 1000) % 60)
@ -545,14 +553,14 @@ function listeners() {
$('#btn_play').click(function () {
switch (state) {
case "PLAYING":
sendCommand({command: "PAUSE"}, false);
sendCommand({command: "PAUSE"}, true);
break;
case "PAUSE":
sendCommand({command: "PLAY"}, false);
sendCommand({command: "PLAY"}, true);
break;
default:
sendCommand({command: "PLAY"}, false);
sendCommand({command: "PLAY"}, true);
}
});
@ -582,8 +590,6 @@ function listeners() {
});
$('#modalChanels').change(function () {
if ($('#btn_ok_channel').hasClass("disabled")) {
$('#btn_ok_channel').removeClass("disabled");