74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
|
|
using Online;
|
|
|
|
using uPromise;
|
|
|
|
using UnityEngine;
|
|
|
|
namespace Databases
|
|
{
|
|
public class SongDatabase
|
|
{
|
|
public class SongEntry
|
|
{
|
|
public string Title;
|
|
public string Artist;
|
|
public string CoverImageUrl;
|
|
}
|
|
|
|
public static readonly SongDatabase Instance = new SongDatabase();
|
|
|
|
private Dictionary<string, SongEntry> songEntries = new Dictionary<string, SongEntry>();
|
|
private bool databaseLoaded = false;
|
|
|
|
public Promise Initialize()
|
|
{
|
|
if (databaseLoaded)
|
|
return PromiseFactory.StartNewDeferred(dfd => dfd.Resolve());
|
|
|
|
var deferred = new Deferred();
|
|
|
|
OnlineEngine.SongDatabase.QuerySongDatabase()
|
|
.Then(database =>
|
|
{
|
|
databaseLoaded = true;
|
|
|
|
foreach (var child in database.Children)
|
|
{
|
|
var entry = new SongEntry();
|
|
entry.Title = child.Value.GetFieldAsString("title");
|
|
entry.Artist = child.Value.GetFieldAsString("artist");
|
|
|
|
if (child.Value.HasField("assets"))
|
|
{
|
|
entry.CoverImageUrl = child.Value.GetFieldAsObject("assets").GetFieldAsString("phoneCoverImageUrl");
|
|
}
|
|
else if (child.Value.HasField("parentMapName"))
|
|
{
|
|
var parentName = child.Value.GetFieldAsString("parentMapName");
|
|
if (database.Children.ContainsKey(parentName))
|
|
entry.CoverImageUrl = database.Children[parentName].GetFieldAsObject("assets").GetFieldAsString("phoneCoverImageUrl");
|
|
else
|
|
Debug.LogError("Missing parent map: " + parentName);
|
|
}
|
|
|
|
songEntries.Add(child.Key, entry);
|
|
}
|
|
|
|
deferred.Resolve();
|
|
})
|
|
.Fail(() => deferred.Reject());
|
|
|
|
return deferred.Promise;
|
|
}
|
|
|
|
public SongEntry FindSong(string mapName)
|
|
{
|
|
if (songEntries.ContainsKey(mapName))
|
|
return songEntries[mapName];
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|