-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseSongRetriever.cs
More file actions
116 lines (96 loc) · 4 KB
/
Copy pathBaseSongRetriever.cs
File metadata and controls
116 lines (96 loc) · 4 KB
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
using Newtonsoft.Json;
using SpotifyAPI.Web;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
namespace GOJ.Game
{
public abstract class BaseSongRetriever : ISongRetriever
{
/// <summary>
/// Edit this value to tune how often to get a new set of tracks from the user
/// </summary>
protected static TimeSpan TimeBetweenFetchingNewSetFromSpotify = new TimeSpan(14, 0, 0, 0);
protected TrackRetrievalManager.UserTrackOrigins m_trackOrigin;
protected readonly string m_folderPath = Path.Combine(Application.persistentDataPath, "RetrievedTracks");
protected virtual string FilePath => Path.Combine(m_folderPath, $"{m_trackOrigin}.json");
protected virtual string PlayerPrefsPath => m_trackOrigin.ToString();
public BaseSongRetriever(TrackRetrievalManager.UserTrackOrigins trackOrigin)
{
m_trackOrigin = trackOrigin;
}
public async Task<(IEnumerable<FullTrack> tracks, TrackRetrievalManager.UserTrackOrigins origin)> RetrieveSongs(bool includeExplicit)
{
IEnumerable<FullTrack> tracks = null;
if (ShouldGetTracksFromSpotify())
{
tracks = await GetTracksFromSpotify();
SaveTracksLocally(tracks);
}
else
{
tracks = await LoadTracksLocally();
}
if (!includeExplicit)
tracks = tracks.Where(x => x != null && !x.Explicit);
tracks = tracks.Where(x => !string.IsNullOrEmpty(x.Name));
return (tracks, m_trackOrigin);
}
protected abstract Task<IEnumerable<FullTrack>> GetTracksFromSpotify();
protected virtual void SaveTracksLocally(IEnumerable<FullTrack> tracks)
{
if(!Directory.Exists(m_folderPath))
Directory.CreateDirectory(m_folderPath);
string data = JsonConvert.SerializeObject(tracks);
File.WriteAllText(FilePath, data);
PlayerPrefs.SetString($"{PlayerPrefsPath}LastFetchedDateTime", DateTime.UtcNow.ToString());
}
protected virtual async Task<List<FullTrack>> LoadTracksLocally()
{
List<FullTrack> tracks = new List<FullTrack>();
if (!Directory.Exists(m_folderPath))
{
Logger.LogError($"Directory {m_folderPath} doesn't exist, not loading any tracks for {m_trackOrigin}");
return tracks;
}
if (File.Exists(FilePath))
{
string data = File.ReadAllText(FilePath);
//Fallback incase the created file is empty
if (string.IsNullOrEmpty(data))
{
tracks = await GetTracksFromSpotify() as List<FullTrack>;
SaveTracksLocally(tracks);
}
else
{
tracks = JsonConvert.DeserializeObject<List<FullTrack>>(data);
}
}
else
{
Logger.LogError($"File {FilePath} does not exist.");
}
return tracks;
}
protected virtual bool ShouldGetTracksFromSpotify()
{
if (GameManager.Instance.DebugAlwaysRetrieveFromSpotify)
return true;
//Need to fetch from Spotify and make the local save data file
if (!File.Exists(FilePath))
return true;
//Need to save the last saved timestamp to PlayerPrefs
if (!PlayerPrefs.HasKey($"{PlayerPrefsPath}LastFetchedDateTime"))
return true;
if (DateTime.TryParse(PlayerPrefs.GetString($"{PlayerPrefsPath}LastFetchedDateTime"), out var lastSavedTimestamp))
return DateTime.UtcNow - lastSavedTimestamp >= TimeBetweenFetchingNewSetFromSpotify;
else
return true;
}
}
}