-
Notifications
You must be signed in to change notification settings - Fork 0
/
Playlist.cs
216 lines (180 loc) · 6.82 KB
/
Playlist.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.IO;
namespace milonga_generator
{
public class Playlist
{
// all the acts added to the program
public List<Act> acts { get; set; }
// the number of acts that the playlist will have
public int actsQuantity { get; set; }
// the number of songs that each act will have
public int actsLength { get; set; }
// if the playlist will have interludes between acts
public bool interludesUsage { get; set; }
// the path of the folder that contains the interludes
public string interludesPath { get; set; }
// if the playlist will use the same interlude
public bool interludeUsageSame { get; set; }
public Playlist()
{
// defaults values
acts = new List<Act>();
actsQuantity = -1;
actsLength = -1;
interludesUsage = true;
interludesPath = "";
interludeUsageSame = false;
}
public List<string> getErrorMessages()
{
List<string> messages = new List<string>();
// playlistActs
if (acts.Count == 0)
{
messages.Add("Debe agregar al menos una tanda");
}
// actsQuantity
if (actsQuantity < 1)
{
messages.Add("El número de tandas es inválido");
}
else if (actsQuantity > acts.Count)
{
messages.Add("No pueden haber más tandas que las agregadas");
}
// actsLength
if (actsLength < 1)
{
messages.Add("El número de piezas es inválido");
}
// interludesPath;
if (interludesUsage && interludesPath == "")
{
string aux = interludeUsageSame ? "la cortina" : "las cortinas";
messages.Add($"Debe ingresar la ubicación de {aux}");
}
return messages;
}
public bool validate()
{
return getErrorMessages().Count == 0;
}
public void writeSettingsFile(string filePath)
{
// open a stream of the file and create the writer
FileStream stream = File.OpenWrite(filePath);
BinaryWriter writer = new BinaryWriter(stream);
// write the settings
writer.Write(Act.parseToString(acts));
writer.Write(actsQuantity);
writer.Write(actsLength);
writer.Write(interludesUsage);
writer.Write(interludesPath);
writer.Write(interludeUsageSame);
writer.Close();
stream.Close();
}
public void readSettingsFile(string filePath)
{
// open a stream of the file and create the reader
FileStream stream = File.OpenRead(filePath);
BinaryReader reader = new BinaryReader(stream);
// read the settings
acts = Act.parseToList(reader.ReadString());
actsQuantity = reader.ReadInt32();
actsLength = reader.ReadInt32();
interludesUsage = reader.ReadBoolean();
interludesPath = reader.ReadString();
interludeUsageSame = reader.ReadBoolean();
reader.Close();
stream.Close();
}
public void generate(string path)
{
string playlistGenerated = generate();
writePlaylistFile(path, playlistGenerated);
}
private string generate()
{
string fileString = "";
List<string> interludesFiles = MusicFolder.getMusicFiles(interludesPath);
// interlude exceptions
if (interludesUsage && (
(!interludeUsageSame && interludesFiles.Count < actsQuantity - 1)
|| (interludeUsageSame && interludesFiles.Count == 0 && actsQuantity != 1)))
{
int interludesNeeded = interludeUsageSame ? 1 : actsQuantity - 1;
string aux = interludesNeeded == 1 ? "pieza" : "piezas";
throw new Exception($"La carpeta de cortinas {interludesPath} necesita tener al menos {interludesNeeded} {aux}");
}
// act exceptions
foreach (Act act in acts)
{
// check if all the acts have enough songs
if (MusicFolder.getMusicFiles(act.path).Count < actsLength)
{
string aux = actsLength == 1 ? "pieza" : "piezas";
throw new Exception($"La carpeta {act.path} necesita tener al menos {actsLength} {aux}");
}
}
// sort the acts and interludes
List<Act> actsSorted = sortRandom(acts);
List<string> interludesSorted = interludesUsage ? sortRandom(interludesFiles) : new List<string>();
for (int i = 0; i < actsQuantity; i++)
{
Act current = actsSorted[i];
List<string> filesSorted = sortRandom(MusicFolder.getMusicFiles(current.path));
// header
fileString += $"{(i == 0 ? "" : "\n\n")}# Tanda: {current.name}";
// songs
for (int j = 0; j < actsLength; j++)
{
fileString += $"\n{filesSorted[j]}";
}
if (interludesUsage && i != actsQuantity - 1)
{
fileString += $"\n\n# Cortina {i + 1}";
int interludeIndex = interludeUsageSame ? 0 : i;
fileString += $"\n{interludesSorted[interludeIndex]}";
}
}
return fileString;
}
private void writePlaylistFile(string path, string content)
{
File.WriteAllText(path, content, System.Text.Encoding.UTF8);
}
private List<Act> sortRandom(List<Act> unsorted)
{
List<Act> copy = new List<Act>(unsorted);
Random random = new Random();
int n = copy.Count;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
Act aux = copy[k];
copy[k] = copy[n];
copy[n] = aux;
}
return copy;
}
private List<string> sortRandom(List<string> unsorted)
{
List<string> copy = new List<string>(unsorted);
Random random = new Random();
int n = copy.Count;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
string aux = copy[k];
copy[k] = copy[n];
copy[n] = aux;
}
return copy;
}
}
}