Skip to content

Commit

Permalink
Change the way of generate random heard list
Browse files Browse the repository at this point in the history
  • Loading branch information
YuziO2 committed Jul 11, 2022
1 parent 5a66fe2 commit 2ec9170
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 51 deletions.
11 changes: 9 additions & 2 deletions src/renderer/components/PlayerBar/VirtualCurrentPlaylist.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import { mapActions, mapGetters, mapState } from 'vuex';
import { workerExecute } from '@/worker/message';
import CenteredTip from '@/components/CenteredTip.vue';
import { LOOP_MODE } from '@/store/modules/playlist';
const SourceName = {
list: '歌单',
Expand Down Expand Up @@ -132,7 +133,9 @@ export default {
if (this.ui.radioMode) return '私人 FM';
if (this.showFindInput && this.findInput) return `找到 ${this.filteredList.length}`;
return `${this.queue.list.length}`;
}
},
/** @returns {import('@/store/modules/playlist').State} */
playlist() { return this.$store.state.playlist; },
},
methods: {
...mapActions([
Expand All @@ -141,7 +144,8 @@ export default {
'clearPlaylist',
'playTrackIndex',
'toggleCollectPopup',
'removeTrackFromPlaylist'
'removeTrackFromPlaylist',
'insertTrackIntoRandomPlaylist',
]),
toggleFindInput() {
this.showFindInput = !this.showFindInput;
Expand Down Expand Up @@ -176,6 +180,9 @@ export default {
if (this.indexMap.size > 0 && this.indexMap.has(index)) {
i = this.indexMap.get(index);
}
if (this.playlist.loopMode == LOOP_MODE.RANDOM) {
this.insertTrackIntoRandomPlaylist({ index: i, offset: 0 });
}
this.playTrackIndex(i);
},
sourceTipText(track) {
Expand Down
29 changes: 27 additions & 2 deletions src/renderer/components/TrackList/TrackList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default {
'toggleCollectPopup',
'insertTrackIntoPlaylist',
'insertTrackIntoRandomPlaylist',
'generateRandomHeardList',
'playPlaylist'
]),
handleCollect(id) {
Expand All @@ -106,7 +107,7 @@ export default {
if (this.findTrackInPlaylist(index) > -1) {
// track exists in playlist
if (this.playlist.loopMode == LOOP_MODE.RANDOM) {
this.insertTrackIntoRandomPlaylist(index);
this.insertTrackIntoRandomPlaylist({index,offset: 1});
this.$toast.message('已添加下一首播放 _(:з」∠)_');
return;
}
Expand All @@ -119,7 +120,11 @@ export default {
index: this.playlist.index + 1
});
if (this.playlist.loopMode == LOOP_MODE.RANDOM) {
this.insertTrackIntoRandomPlaylist(this.playlist.index + 1);
if (this.playlist.randomHeardList.length == 0) {//若列表为空,直接生成
this.generateRandomHeardList(this.playlist.list.length);
}
else //否则插入
this.insertTrackIntoRandomPlaylist({ index:this.playlist.index + 1 ,offset:1});
}
this.$toast.message('已添加下一首播放 _(:з」∠)_');
},
Expand All @@ -132,6 +137,26 @@ export default {
},
playTrack(index) {
const i = this.findTrackInPlaylist(index);
if (this.playlist.loopMode == LOOP_MODE.RANDOM) {
if (i > -1) {
// track exists in playlist
this.insertTrackIntoRandomPlaylist({index: i,offset: 0});
this.playTrackIndex(i);
return;
}
if (this.playlist.randomHeardList.length == 0) {
this.generateRandomHeardList(this.playlist.list.length);
}
this.insertTrackIntoPlaylist({
tracks: [this.trackDetails[index]],
source: this.source,
index: this.playlist.index,
});
const newIndex = this.findTrackInPlaylist(index);
this.insertTrackIntoRandomPlaylist({ index:newIndex, offset: 0 });
this.playTrackIndex(newIndex);
return;
}
if (i > -1) {
// track exists in playlist
this.playTrackIndex(i);
Expand Down
59 changes: 25 additions & 34 deletions src/renderer/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,24 +415,19 @@ export async function playTrackIndex({ state, commit, dispatch }, index) {
* @param {ActionContext} param0
*/
export function playNextTrack({ commit, dispatch, getters }) {
const { index, list, loopMode, randomHeardList, randomHeardListPointer } = getters.queue;
let { index, list, loopMode, randomHeardList, randomHeardListPointer } = getters.queue;
let nextIndex;
switch (loopMode) {
case LOOP_MODE.RANDOM:
if (!randomHeardList.length) {//第一次切歌,将当次歌曲也放入
//randomHeardList.push(index);
commit(types.PUSH_RANDOMLIST_HEARD_LIST, index);
nextIndex = Math.floor(Math.random() * list.length);
//randomHeardList.push(nextIndex);
commit(types.PUSH_RANDOMLIST_HEARD_LIST, nextIndex);
commit(types.SET_RANDOMLIST_POINTER, 1);
if (randomHeardList.length == 0) {
commit(types.GENERATE_RANDOM_HEARD_LIST, list.length);
randomHeardList = getters.queue.randomHeardList;
}
else if (randomHeardListPointer === randomHeardList.length - 1) {//若已到队列尾部,随机生成新歌
nextIndex = Math.floor(Math.random() * list.length);
commit(types.PUSH_RANDOMLIST_HEARD_LIST, nextIndex);
commit(types.SET_RANDOMLIST_POINTER, randomHeardListPointer + 1);
if (randomHeardListPointer == randomHeardList.length - 1) {
commit(types.SET_RANDOMLIST_POINTER, 0);
nextIndex = randomHeardList[0];
}
else {//否则,使用队列里的歌曲
else {
nextIndex = randomHeardList[randomHeardListPointer + 1];
commit(types.SET_RANDOMLIST_POINTER, randomHeardListPointer + 1);
}
Expand All @@ -448,22 +443,17 @@ export function playNextTrack({ commit, dispatch, getters }) {
* @param {ActionContext} param0
*/
export function playPreviousTrack({ commit, dispatch, getters }) {
const { index, list, loopMode, randomHeardList, randomHeardListPointer } = getters.queue;
let { index, list, loopMode, randomHeardList, randomHeardListPointer } = getters.queue;
let nextIndex;
switch (loopMode) {
case LOOP_MODE.RANDOM:
if (!randomHeardList.length) {
nextIndex = Math.floor(Math.random() * list.length);
commit(types.UNSHIFT_RANDOMLIST_HEARD_LIST, index);
commit(types.UNSHIFT_RANDOMLIST_HEARD_LIST, nextIndex);
// randomHeardList.unshift(index);
// randomHeardList.unshift(nextIndex);
commit(types.SET_RANDOMLIST_POINTER, 0);
if (randomHeardList.length == 0) {
commit(types.GENERATE_RANDOM_HEARD_LIST, list.length);
randomHeardList = getters.queue.randomHeardList;
}
else if (randomHeardListPointer === 0) {//若在队列头部,则在队列头插入随机新歌,此时指针不用变!
nextIndex = Math.floor(Math.random() * list.length);
//randomHeardList.unshift(nextIndex);
commit(types.UNSHIFT_RANDOMLIST_HEARD_LIST, nextIndex);
if (randomHeardListPointer == 0) {
commit(types.SET_RANDOMLIST_POINTER, randomHeardList.length - 1);
nextIndex = randomHeardList[randomHeardList.length - 1];
}
else {
nextIndex = randomHeardList[randomHeardListPointer - 1];
Expand Down Expand Up @@ -493,9 +483,9 @@ export async function playPlaylist({ commit, dispatch, state }, { tracks, source
commit(types.ACTIVATE_RADIO, false);
}
if (firstIndex === -1 && state.playlist.loopMode === LOOP_MODE.RANDOM) {
firstIndex = Math.floor(Math.random() * list.length);
commit(types.CLEAR_RANDOMLIST);
commit(types.SET_RANDOMLIST_POINTER, 0);//换歌单了,重置随机队列
//firstIndex = Math.floor(Math.random() * list.length);
commit(types.GENERATE_RANDOM_HEARD_LIST, list.length);//换歌单了,重置随机队列
firstIndex = state.playlist.randomHeardList[0];
}
if (firstIndex === -1) {
firstIndex = 0;
Expand All @@ -509,6 +499,7 @@ export async function playPlaylist({ commit, dispatch, state }, { tracks, source
export function clearPlaylist({ commit, dispatch }) {
commit(types.SET_PLAY_LIST, []);
commit(types.SET_CURRENT_INDEX, 0);
commit(types.GENERATE_RANDOM_HEARD_LIST, 0);
dispatch('updateUiTrack');
}

Expand Down Expand Up @@ -621,8 +612,7 @@ export function nextLoopMode({ commit, state }) {
commit(types.SET_LOOP_MODE_SINGLE);
break;
case LOOP_MODE.SINGLE:
commit(types.CLEAR_RANDOMLIST);//换播放方式了,重置随机队列
commit(types.SET_RANDOMLIST_POINTER, 0);
commit(types.GENERATE_RANDOM_HEARD_LIST, state.playlist.list.length);//换播放方式了,重置随机队列
commit(types.SET_LOOP_MODE_RANDOM);
break;
case LOOP_MODE.RANDOM:
Expand Down Expand Up @@ -650,19 +640,20 @@ export function insertTrackIntoPlaylist({ commit, state }, payload) {
* @param {number} payload
*/
export function insertTrackIntoRandomPlaylist({ commit, state }, payload) {
if (state.playlist.randomHeardList.length == 0)
commit(types.PUSH_RANDOMLIST_HEARD_LIST, 0);
commit(types.INSERT_TRACK_INTO_RANDOM_PLAYLIST, payload);
}

export function generateRandomHeardList({ commit, state }, payload) {
commit(types.GENERATE_RANDOM_HEARD_LIST, payload);
}

/**
* @param {ActionContext} param0
*/
export function removeTrackFromPlaylist({ getters, commit, dispatch }, payload) {
commit(types.CLEAR_RANDOMLIST);//删歌了,重置随机队列
commit(types.SET_RANDOMLIST_POINTER, 0);
const playingId = getters.playing.id;
commit(types.REMOVE_TRACK_FROM_PLAYLIST, payload);
commit(types.GENERATE_RANDOM_HEARD_LIST, getters.queue.list.length);//删歌了,重置随机队列
if (playingId !== getters.playing.id) {
dispatch('updateUiTrack');
}
Expand Down
23 changes: 13 additions & 10 deletions src/renderer/store/modules/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ const mutations = {
[types.SET_LOOP_MODE_RANDOM](state) {
state.loopMode = LOOP_MODE.RANDOM;
},
[types.PUSH_RANDOMLIST_HEARD_LIST](state, payload) {
state.randomHeardList.push(payload);
},
[types.UNSHIFT_RANDOMLIST_HEARD_LIST](state, payload) {
state.randomHeardList.unshift(payload);
[types.GENERATE_RANDOM_HEARD_LIST](state, payload) {
state.randomHeardList = Array(payload).fill(1).map((v, i) => i);
for (let i = state.randomHeardList.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[state.randomHeardList[i], state.randomHeardList[j]] = [state.randomHeardList[j], state.randomHeardList[i]];
}//生成一个0-(length-1)的乱序不重复随机数组
state.randomHeardListPointer = 0;//初始化指针
},
[types.SET_RANDOMLIST_POINTER](state, payload) {
state.randomHeardListPointer = payload;
},
[types.CLEAR_RANDOMLIST](state) {
state.randomHeardList = [];
},
[types.INSERT_TRACK_INTO_RANDOM_PLAYLIST](state, payload) {
state.randomHeardList.splice(state.randomHeardListPointer + 1, 0, payload);
[types.INSERT_TRACK_INTO_RANDOM_PLAYLIST](state, { index, offset }) {
state.randomHeardList.splice(state.randomHeardListPointer + offset, 0, index)
for (let i = 0; i < state.randomHeardList.length; i++)
if (state.randomHeardList[i] >= index)
if (i != state.randomHeardListPointer + offset)
state.randomHeardList[i]++;
},
[types.RESTORE_PLAYLIST](state, { index, loopMode, list }) {
state.index = index || 0;
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/store/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX';
export const SET_LOOP_MODE_LIST = 'SET_LOOP_MODE_LOOP';
export const SET_LOOP_MODE_SINGLE = 'SET_LOOP_MODE_SINGLE';
export const SET_LOOP_MODE_RANDOM = 'SET_LOOP_MODE_RANDOM';
export const PUSH_RANDOMLIST_HEARD_LIST = 'PUSH_RANDOMLIST_HEARD_LIST';
export const UNSHIFT_RANDOMLIST_HEARD_LIST = 'UNSHIFT_RANDOMLIST_HEARD_LIST';
export const GENERATE_RANDOM_HEARD_LIST = 'GENERATE_RANDOM_HEARD_LIST';
export const SET_RANDOMLIST_POINTER = 'SET_RANDOMLIST_POINTER'
export const CLEAR_RANDOMLIST = 'CLEAR_RANDOMLIST';
export const INSERT_TRACK_INTO_RANDOM_PLAYLIST = 'INSERT_TRACK_INTO_RANDOM_PLAYLIST';
export const RESTORE_PLAYLIST = 'RESTORE_PLAYLIST';
export const INSERT_TRACK_INTO_PLAYLIST = 'INSERT_TRACK_INTO_PLAYLIST';
Expand Down

0 comments on commit 2ec9170

Please sign in to comment.