mirror of
https://github.com/Dangoware/dmp-core.git
synced 2025-04-19 09:42:53 -05:00
test commit
This commit is contained in:
parent
588a9cbd94
commit
96cfbe9a50
1 changed files with 86 additions and 78 deletions
|
@ -9,7 +9,7 @@ pub enum QueueState {
|
|||
Played,
|
||||
Current,
|
||||
AddHere,
|
||||
None,
|
||||
NoState,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
|
@ -56,8 +56,6 @@ impl QueueItemType<'_> {
|
|||
pub enum QueueSource {
|
||||
Library,
|
||||
Playlist(Uuid),
|
||||
Search,
|
||||
Queue,
|
||||
File,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -72,7 +70,7 @@ impl QueueItem<'_> {
|
|||
fn new() -> Self {
|
||||
QueueItem {
|
||||
item: QueueItemType::None,
|
||||
state: QueueState::None,
|
||||
state: QueueState::NoState,
|
||||
source: QueueSource::Library,
|
||||
by_human: false
|
||||
}
|
||||
|
@ -88,6 +86,9 @@ pub struct Queue<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Queue<'a> {
|
||||
fn dbg_items(&self) {
|
||||
dbg!(self.items.iter().map(|item| item.item.clone() ).collect::<Vec<QueueItemType>>(), self.items.len());
|
||||
}
|
||||
pub fn new() -> Result<Self, Box<dyn Error>> {
|
||||
Ok(
|
||||
Queue {
|
||||
|
@ -98,16 +99,18 @@ impl<'a> Queue<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn current_index(&mut self) -> i16 {
|
||||
let mut i = 1;
|
||||
pub fn current_index(&mut self/* , max: usize */) -> Option<usize> {
|
||||
let mut e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len();
|
||||
// TODO: make the max number of past songs modular
|
||||
while e > 50 {
|
||||
self.items.remove(0);
|
||||
e = self.items.iter().filter(|song| song.state == QueueState::Played ).collect::<Vec<&QueueItem>>().len();
|
||||
i+=1;
|
||||
e -=1;
|
||||
}
|
||||
if e == 0 {
|
||||
None
|
||||
}else {
|
||||
Some(e - 1)
|
||||
}
|
||||
e as i16 - 1
|
||||
}
|
||||
|
||||
fn contains_state(&self, state: QueueState) -> bool {
|
||||
|
@ -126,27 +129,28 @@ impl<'a> Queue<'a> {
|
|||
|
||||
pub fn add_item(&mut self, item: QueueItemType<'a>, source: QueueSource, by_human: bool) -> Result<(), Box<dyn Error>> {
|
||||
use QueueState::*;
|
||||
let mut i: usize = 0;
|
||||
let ind = self.current_index();
|
||||
let mut i: i16 = 1;
|
||||
|
||||
self.items = self.items.iter().enumerate().map(|(j, item_)| {
|
||||
let mut item_ = item_.to_owned();
|
||||
// get the index of the current AddHere item and give it to i
|
||||
if item_.state == AddHere {
|
||||
i = j as i16 + 1 - ind;
|
||||
item_.state = None;
|
||||
}
|
||||
if item_.state == Current {
|
||||
i = j as i16 + 1 - ind;
|
||||
i = j - ind.unwrap_or(0);
|
||||
item_.state = NoState;
|
||||
} else if item_.state == Current {
|
||||
i = j - ind.unwrap_or(0);
|
||||
}
|
||||
item_
|
||||
}).collect::<Vec<QueueItem>>();
|
||||
let pos = (ind + i) as usize;
|
||||
|
||||
let pos = ind.unwrap_or(0) + i + if !self.is_empty() || (self.is_empty() && ind == None) { 0 } else { 1 };
|
||||
// dbg!(&pos, &i, &ind);
|
||||
self.items.insert(
|
||||
pos,
|
||||
QueueItem {
|
||||
item: item.clone(),
|
||||
state: if pos == self.items.len() && i == 1 {
|
||||
state: if pos == self.items.len() && i == 0 {
|
||||
Current
|
||||
}else {
|
||||
AddHere
|
||||
|
@ -160,24 +164,20 @@ impl<'a> Queue<'a> {
|
|||
|
||||
pub fn add_item_next(&mut self, item: QueueItemType<'a>, source: QueueSource) {
|
||||
use QueueState::*;
|
||||
let ind = self.current_index();
|
||||
let ind_ = self.current_index();
|
||||
let ind = ind_.unwrap_or(0);
|
||||
let empty = self.is_empty();
|
||||
|
||||
self.items.insert(
|
||||
// index would go out of bounds if empty ( current index = -1 )
|
||||
if empty {
|
||||
(ind + 1) as usize
|
||||
}else {
|
||||
(ind + 2) as usize
|
||||
},
|
||||
(ind + if !empty && ind_ == None { 1 } else { 2 }),
|
||||
QueueItem {
|
||||
item,
|
||||
state: if empty {
|
||||
Current
|
||||
}else if self.items.get((ind + 1) as usize).is_none() || (!self.contains_state(AddHere) && self.items.get((ind + 1) as usize).is_some()) {
|
||||
}else if self.items.get(ind + 1).is_none() || (!self.contains_state(AddHere) && self.items.get(ind + 1).is_some()) {
|
||||
AddHere
|
||||
}else {
|
||||
None
|
||||
NoState
|
||||
},
|
||||
source,
|
||||
by_human: true
|
||||
|
@ -187,17 +187,19 @@ impl<'a> Queue<'a> {
|
|||
|
||||
pub fn remove_item(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
|
||||
use QueueState::*;
|
||||
let ind = (self.current_index() + index as i16 + 1) as usize;
|
||||
let remove_index: usize = (if let Some(current_index) = self.current_index() { dbg!(¤t_index); current_index } else { 0 } + index );
|
||||
|
||||
if ind < self.items.len() {
|
||||
// dbg!(/*&remove_index, self.current_index(), &index,*/ &self.items[remove_index]);
|
||||
|
||||
if remove_index < self.items.len() {
|
||||
// update the state of the next item to replace the item being removed
|
||||
if self.items.get(ind + 1).is_some() {
|
||||
self.items[ind + 1].state = self.items[ind].state;
|
||||
}else if self.items[ind].state != Current {
|
||||
self.items[ind - 1].state = self.items[ind].state;
|
||||
if self.items.get(remove_index + 1).is_some() {
|
||||
self.items[remove_index + 1].state = self.items[remove_index].state;
|
||||
}else if self.items[remove_index].state != Current {
|
||||
self.items[remove_index - 1].state = self.items[remove_index].state;
|
||||
}
|
||||
self.items[ind].state = None;
|
||||
self.items.remove(ind);
|
||||
self.items[remove_index].state = NoState;
|
||||
self.items.remove(remove_index);
|
||||
Ok(())
|
||||
}else {
|
||||
Err("No Songs to remove!".into())
|
||||
|
@ -210,11 +212,14 @@ impl<'a> Queue<'a> {
|
|||
|
||||
pub fn clear_except(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
|
||||
let mut index = index;
|
||||
let ind = self.current_index();
|
||||
let ind = match self.current_index() {
|
||||
Some(e) => e,
|
||||
None => return Err("nothing to clear!".into())
|
||||
};
|
||||
let empty = self.is_empty();
|
||||
|
||||
if !empty {
|
||||
index += ind as usize;
|
||||
index += ind;
|
||||
}else {
|
||||
index -=1
|
||||
}
|
||||
|
@ -222,7 +227,7 @@ impl<'a> Queue<'a> {
|
|||
if !empty && index < self.items.len() {
|
||||
let i = self.items[index].clone();
|
||||
self.items.retain(|item| item.state == QueueState::Played || *item == i );
|
||||
self.items[(ind+1) as usize].state = QueueState::Current
|
||||
self.items[ind+1].state = QueueState::Current
|
||||
}else {
|
||||
return Err("index out of bounds!".into());
|
||||
}
|
||||
|
@ -238,37 +243,26 @@ impl<'a> Queue<'a> {
|
|||
}
|
||||
|
||||
fn move_to(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
|
||||
let mut index = index;
|
||||
let empty = self.is_empty();
|
||||
let ind = self.current_index();
|
||||
let nothing_error = Err("Nothing in the queue to move to!".into());
|
||||
let ind = self.current_index().unwrap_or(0);
|
||||
let index = if !empty { index + ind } else { return nothing_error; };
|
||||
|
||||
if !empty {
|
||||
index += ind as usize;
|
||||
}else {
|
||||
return Err("Nothing in the queue to move to!".into());
|
||||
}
|
||||
|
||||
dbg!(1);
|
||||
if !empty && index < self.items.len() -1 {
|
||||
// TODO: make this check for player position
|
||||
let pos = self.player.position();
|
||||
if pos.is_some_and(|dur| !dur.is_zero() ) {
|
||||
self.items[ind as usize].state = QueueState::Played
|
||||
let position = self.player.position();
|
||||
if position.is_some_and(|dur| !dur.is_zero() ) {
|
||||
self.items[ind].state = QueueState::Played;
|
||||
}
|
||||
dbg!(2);
|
||||
|
||||
let to_item = self.items[index].clone();
|
||||
let new_ind = self.current_index() as usize;
|
||||
dbg!(3);
|
||||
let ind = self.current_index().unwrap_or(0);
|
||||
|
||||
// dbg!(&self.items, &new_ind, &to_item.item, &self.items[new_ind + 1].item, &self.items.len());
|
||||
loop {
|
||||
dbg!(4);
|
||||
|
||||
if self.items[new_ind + 1].item != to_item.item {
|
||||
self.remove_item(0);
|
||||
dbg!(&self.items, &new_ind, &to_item.item, &self.items[new_ind + 1].item, &self.items.len());
|
||||
sleep(Duration::from_millis(1000));
|
||||
if self.items[ind].item != to_item.item {
|
||||
if let Err(e) = self.remove_item(0) {
|
||||
dbg!(&e); self.dbg_items(); return Err(e);
|
||||
}
|
||||
// dbg!(&to_item.item, &self.items[ind].item);
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
|
@ -303,21 +297,34 @@ impl<'a> Queue<'a> {
|
|||
#[test]
|
||||
fn item_add_test() {
|
||||
let mut q = Queue::new().unwrap();
|
||||
for _ in 0..5 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::Queue, by_human: false });
|
||||
}
|
||||
q.clear();
|
||||
dbg!(1);
|
||||
for _ in 0..1 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::File, by_human: false });
|
||||
}
|
||||
dbg!(2);
|
||||
|
||||
// q.clear();
|
||||
dbg!(3);
|
||||
|
||||
for _ in 0..5 {
|
||||
// dbg!("tick!");
|
||||
q.add_item(QueueItemType::Song(Uuid::new_v4()), QueueSource::Library, true).unwrap();
|
||||
// dbg!(&q.items, &q.items.len());
|
||||
}
|
||||
dbg!(4);
|
||||
dbg!(&q.items, &q.items.len());
|
||||
|
||||
// q.clear_played();
|
||||
// for _ in 0..3 {
|
||||
// q.remove_item(0).inspect_err(|e| println!("{e:?}"));
|
||||
// }
|
||||
for _ in 0..2 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Test, state: QueueState::None, source: QueueSource::Queue, by_human: false });
|
||||
for _ in 0..1 {
|
||||
q.remove_item(0).inspect_err(|e| println!("{e:?}"));
|
||||
}
|
||||
q.add_item_next(QueueItemType::Test, QueueSource::File);
|
||||
// for _ in 0..2 {
|
||||
// q.items.push(QueueItem { item: QueueItemType::Test, state: QueueState::NoState, source: QueueSource::Library, by_human: false });
|
||||
// }
|
||||
// dbg!(5);
|
||||
|
||||
// q.add_item_next(QueueItemType::Test, QueueSource::File);
|
||||
// dbg!(6);
|
||||
|
||||
dbg!(&q.items, &q.items.len());
|
||||
}
|
||||
|
@ -325,13 +332,13 @@ fn item_add_test() {
|
|||
#[test]
|
||||
fn test_() {
|
||||
let mut q = Queue::new().unwrap();
|
||||
for _ in 0..100 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::Queue, by_human: false });
|
||||
for _ in 0..1 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::File, by_human: false });
|
||||
}
|
||||
for _ in 0..2 {
|
||||
q.add_item(QueueItemType::Song(Uuid::new_v4()), QueueSource::Library, true).unwrap();
|
||||
}
|
||||
q.add_item_next(QueueItemType::Test, QueueSource::Queue);
|
||||
q.add_item_next(QueueItemType::Test, QueueSource::File);
|
||||
|
||||
dbg!(&q.items, &q.items.len());
|
||||
|
||||
|
@ -340,15 +347,16 @@ fn test_() {
|
|||
#[test]
|
||||
fn move_test() {
|
||||
let mut q = Queue::new().unwrap();
|
||||
// for _ in 0..1 {
|
||||
// q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::Queue, by_human: false });
|
||||
// }
|
||||
for _ in 0..1 {
|
||||
q.items.push(QueueItem { item: QueueItemType::Song(Uuid::new_v4()), state: QueueState::Played, source: QueueSource::File, by_human: false });
|
||||
}
|
||||
for _ in 0..5 {
|
||||
q.add_item(QueueItemType::Song(Uuid::new_v4()), QueueSource::Library, true).unwrap();
|
||||
}
|
||||
q.add_item(QueueItemType::Test, QueueSource::Library, true).unwrap();
|
||||
// q.add_item(QueueItemType::Test, QueueSource::Library, true).unwrap();
|
||||
dbg!(&q.items, &q.items.len());
|
||||
|
||||
q.move_to(3).inspect_err(|e| {dbg!(e);});
|
||||
dbg!(&q.items, &q.items.len());
|
||||
// q.dbg_items();
|
||||
}
|
Loading…
Reference in a new issue