Skip to content

Commit

Permalink
add talo continuity support
Browse files Browse the repository at this point in the history
  • Loading branch information
tudddorrr committed Sep 1, 2024
1 parent 3ae47eb commit e794b1c
Show file tree
Hide file tree
Showing 43 changed files with 1,374 additions and 176 deletions.
604 changes: 604 additions & 0 deletions Assets/Samples/Playground/Scenes/Playground.unity

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Assets/Samples/Playground/Scripts/Continuity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Assets/Samples/Playground/Scripts/Continuity/ToggleContinuity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using UnityEngine;
using TaloGameServices;
using UnityEngine.UI;

public class ToggleContinuity : MonoBehaviour
{
private void Start()
{
SetText();
}

private bool GetValue()
{
return Talo.Settings.continuityEnabled;
}

private void SetText()
{
GetComponentInChildren<Text>().text = GetValue() ? "Toggle off" : "Toggle on";
}

public void OnButtonClick()
{
Talo.Settings.continuityEnabled = !Talo.Settings.continuityEnabled;
SetText();

ResponseMessage.SetText($"Continuity is now {(Talo.Settings.continuityEnabled ? "enabled" : "disabled")}");
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Assets/Samples/Playground/Scripts/Continuity/ToggleOfflineMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using UnityEngine;
using TaloGameServices;
using UnityEngine.UI;

public class ToggleOfflineMode : MonoBehaviour
{
private void Start()
{
SetText();
}

private bool GetValue()
{
return Talo.Settings.offlineMode;
}

private void SetText()
{
GetComponentInChildren<Text>().text = GetValue() ? "Go online" : "Go offline";
}

public void OnButtonClick()
{
Talo.Settings.offlineMode = !Talo.Settings.offlineMode;
SetText();

ResponseMessage.SetText($"You are now now {(Talo.Settings.offlineMode ? "offline" : "online")}");
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions Assets/Samples/Playground/Scripts/Players/IdentifyPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class IdentifyPlayer : MonoBehaviour
{
public string service, identifier;

private void Start()
{
Talo.Players.OnIdentified += OnIdentified;
}

public async void OnButtonClick()
{
await Identify();
Expand All @@ -18,18 +23,21 @@ private async Task Identify()
try
{
await Talo.Players.Identify(service, identifier);

var panel = GameObject.Find("Panel");
if (panel != null)
{
ResponseMessage.SetText("Identified!");
panel.GetComponent<Image>().color = new Color(135 / 255f, 1f, 135 / 255f);
}
}
catch (Exception err)
{
ResponseMessage.SetText(err.Message);
throw err;
}
}

private void OnIdentified(Player player)
{
var panel = GameObject.Find("Panel");
if (panel != null)
{
ResponseMessage.SetText("Identified!");
panel.GetComponent<Image>().color = new Color(135 / 255f, 1f, 135 / 255f);
}
}
}
4 changes: 2 additions & 2 deletions Assets/Samples/Playground/Scripts/Stats/TrackStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public void OnButtonClick()
Track();
}

private void Track()
private async void Track()
{
try
{
Talo.Stats.Track(statInternalName, change);
await Talo.Stats.Track(statInternalName, change);

ResponseMessage.SetText($"{statInternalName} changed by {change}");
}
Expand Down
85 changes: 64 additions & 21 deletions Packages/com.trytalo.talo/Runtime/BaseAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,82 @@
using UnityEngine;
using System.Threading.Tasks;
using UnityEngine.Networking;
using System.Collections.Generic;

namespace TaloGameServices
{
public class BaseAPI
{
protected TaloManager manager;
protected string baseUrl;

public BaseAPI(TaloManager manager, string service)
public BaseAPI(string service)
{
this.manager = manager;
baseUrl = $"{manager.settings.apiUrl}/{service}";
baseUrl = $"{Talo.Settings.apiUrl}/{service}";
}

public Uri GetUri()
{
return new Uri(baseUrl);
}

protected async Task<string> Call(Uri uri, string method, string content = "")
private List<HttpHeader> BuildHeaders()
{
var headers = new List<HttpHeader>
{
new HttpHeader("Authorization", $"Bearer {Talo.Settings.accessKey}"),
new HttpHeader("Content-Type", "application/json"),
new HttpHeader("Accept", "application/json"),
new HttpHeader("X-Talo-Dev-Build", Debug.isDebugBuild ? "1" : "0"),
new HttpHeader("X-Talo-Include-Dev-Data", Debug.isDebugBuild ? "1" : "0")
};

if (Talo.CurrentAlias != null)
{
headers.Add(new HttpHeader("X-Talo-Player", Talo.CurrentPlayer.id));
headers.Add(new HttpHeader("X-Talo-Alias", Talo.CurrentAlias.id.ToString()));
}

var sessionToken = Talo.PlayerAuth.SessionManager.GetSessionToken();
if (!string.IsNullOrEmpty(sessionToken))
{
headers.Add(new HttpHeader("X-Talo-Session", sessionToken));
}

return headers;
}

protected async Task<string> Call(
Uri uri,
string method,
string content = "",
List<HttpHeader> headers = null,
bool continuity = false
)
{
if (Talo.TestMode)
{
return RequestMock.HandleCall(uri, method);
}

var continuityTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

var allHeaders = continuity ? headers : BuildHeaders();

if (Talo.Settings.offlineMode)
{
return HandleOfflineRequest(uri, method, content, allHeaders);
}

byte[] json = new System.Text.UTF8Encoding().GetBytes(content);

using (UnityWebRequest www = new(uri, method))
{
if (json.Length > 0) www.uploadHandler = new UploadHandlerRaw(json);
www.downloadHandler = new DownloadHandlerBuffer();

www.SetRequestHeader("Authorization", $"Bearer {manager.settings.accessKey}");
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Accept", "application/json");
www.SetRequestHeader("X-Talo-Dev-Build", Debug.isDebugBuild ? "1" : "0");
www.SetRequestHeader("X-Talo-Include-Dev-Data", Debug.isDebugBuild ? "1" : "0");

if (Talo.CurrentAlias != null)
{
www.SetRequestHeader("X-Talo-Player", Talo.CurrentPlayer.id);
www.SetRequestHeader("X-Talo-Alias", Talo.CurrentAlias.id.ToString());
}

var sessionToken = Talo.PlayerAuth.SessionManager.GetSessionToken();
if (!string.IsNullOrEmpty(sessionToken))
foreach (var header in allHeaders)
{
www.SetRequestHeader("X-Talo-Session", sessionToken);
www.SetRequestHeader(header.key, header.value);
}

var op = www.SendWebRequest();
Expand All @@ -66,6 +93,11 @@ protected async Task<string> Call(Uri uri, string method, string content = "")
}
else
{
if (www.responseCode >= 500 || www.result != UnityWebRequest.Result.ProtocolError)
{
Talo.Continuity.PushRequest(uri, method, content, headers, continuityTimestamp);
}

string message = "";
string errorCode = "";

Expand All @@ -82,7 +114,7 @@ protected async Task<string> Call(Uri uri, string method, string content = "")

if (string.IsNullOrEmpty(errorCode))
{
throw new Exception($"Request failed: {message}");
throw new RequestException(www.responseCode, new Exception(message));
}
else
{
Expand All @@ -91,5 +123,16 @@ protected async Task<string> Call(Uri uri, string method, string content = "")
}
}
}

private string HandleOfflineRequest(
Uri uri,
string method,
string content = "",
List<HttpHeader> headers = null
)
{
Talo.Continuity.PushRequest(uri, method, content, headers, DateTimeOffset.Now.ToUnixTimeMilliseconds());
throw new RequestException(0, new Exception("Offline mode enabled"));
}
}
}
21 changes: 21 additions & 0 deletions Packages/com.trytalo.talo/Runtime/ContinuityAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace TaloGameServices
{
public class ContinuityAPI : BaseAPI
{
public ContinuityAPI() : base("") { }

public async Task Replay(
Uri uri,
string method,
string content,
List<HttpHeader> headers
)
{
await Call(uri, method, content, headers, true);
}
}
}
11 changes: 11 additions & 0 deletions Packages/com.trytalo.talo/Runtime/ContinuityAPI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using UnityEngine;
using System.Collections.Generic;
using System.Linq;

namespace TaloGameServices
namespace TaloGameServices
{
[System.Serializable]
public class OfflineSavesContent
Expand Down
7 changes: 4 additions & 3 deletions Packages/com.trytalo.talo/Runtime/EventsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace TaloGameServices
{
public class EventsAPI : BaseAPI
{
public event Action OnFlushed;

private List<Event> queue = new List<Event>();
private readonly int minQueueSize = 10;

public EventsAPI(TaloManager manager) : base(manager, "v1/events") { }
public EventsAPI() : base("v1/events") { }

private string GetWindowMode()
{
Expand Down Expand Up @@ -89,12 +91,11 @@ public async Task Flush()
try
{
await Call(uri, "POST", content);
manager.ResetFlushTimer();
OnFlushed.Invoke();
}
catch (Exception err)
{
Debug.LogError(err.Message);
queue.AddRange(eventsToSend);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Packages/com.trytalo.talo/Runtime/FeedbackAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace TaloGameServices
{
public class FeedbackAPI : BaseAPI
{
public FeedbackAPI(TaloManager manager) : base(manager, "v1/game-feedback") { }
public FeedbackAPI() : base("v1/game-feedback") { }

public async Task<FeedbackCategory[]> GetCategories()
{
var uri = new Uri(baseUrl + "/categories");

var uri = new Uri($"{baseUrl}/categories");
var json = await Call(uri, "GET");

var res = JsonUtility.FromJson<FeedbackCategoriesResponse>(json);
return res.feedbackCategories;
}
Expand All @@ -21,7 +21,7 @@ public async Task Send(string internalName, string comment)
{
Talo.IdentityCheck();

var uri = new Uri(baseUrl + $"/categories/{internalName}");
var uri = new Uri($"{baseUrl}/categories/{internalName}");
var content = JsonUtility.ToJson(new FeedbackPostRequest { comment = comment });

await Call(uri, "POST", content);
Expand Down
Loading

0 comments on commit e794b1c

Please sign in to comment.