JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/Online/DancerProfileService.cs

66 lines
2.3 KiB
C#

using System.Linq;
using LightJSON;
using uPromise;
using WebSocketSharp;
namespace Online
{
public class DancerProfileService
{
private readonly IHttpEngine httpEngine;
public class PlayerProfile
{
public JSON Data;
}
public DancerProfileService(IHttpEngine httpEngine)
{
this.httpEngine = httpEngine;
}
public Promise<PlayerProfile> RequestProfile()
{
string profileId = null;
return OnlineEngine.Session.Refresh()
.Then(session =>
{
profileId = session.ProfileId;
return httpEngine.Execute(session.JmcsFacade.BuildRequest("profile", 2, "/profiles?profileIds=" + profileId));
})
.Then(response =>
{
var profiles = response.Body.AsArray();
// best case; we have a dancer card associated with the Uplay account
JSON matchingProfile = profiles.FirstOrDefault(profile => (profile.GetFieldAsString("profileId") == profileId) && profile.GetFieldAsBool("isExisting", true));
// second best case; we find another dancer card in a linked account
if (matchingProfile == null)
matchingProfile = profiles.FirstOrDefault(profile => profile.GetFieldAsBool("isExisting", true));
// just keep an empty profile
if (matchingProfile == null)
matchingProfile = profiles.FirstOrDefault(profile => profile.GetFieldAsString("profileId") == profileId);
if (matchingProfile == null)
{
// profile not found in the result
return PromiseFactory.StartNewDeferred<PlayerProfile>(dfd => dfd.Reject());
}
// override the profile ID, to transfer the profile from another platform to the Uplay pid
matchingProfile.SetField("profileId", profileId);
PlayerProfile result = new PlayerProfile();
result.Data = matchingProfile;
return PromiseFactory.StartNewDeferred<PlayerProfile>(dfd => dfd.Resolve(result));
});
}
}
}