Skip to content

Commit

Permalink
Merge pull request #73 from TaloDev/continuity
Browse files Browse the repository at this point in the history
Add continuity
  • Loading branch information
tudddorrr authored Sep 2, 2024
2 parents 3ae47eb + 01387c3 commit bd7db3d
Show file tree
Hide file tree
Showing 66 changed files with 1,522 additions and 261 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
8 changes: 8 additions & 0 deletions Packages/com.trytalo.talo/Runtime/APIs.meta

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

138 changes: 138 additions & 0 deletions Packages/com.trytalo.talo/Runtime/APIs/BaseAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using UnityEngine;
using System.Threading.Tasks;
using UnityEngine.Networking;
using System.Collections.Generic;

namespace TaloGameServices
{
public class BaseAPI
{
protected string baseUrl;

public BaseAPI(string service)
{
baseUrl = $"{Talo.Settings.apiUrl}/{service}";
}

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

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();

foreach (var header in allHeaders)
{
www.SetRequestHeader(header.key, header.value);
}

var op = www.SendWebRequest();

while (!op.isDone)
{
await Task.Yield();
}

if (www.result == UnityWebRequest.Result.Success)
{
return www.downloadHandler.text;
}
else
{
if (www.responseCode >= 500 || www.result != UnityWebRequest.Result.ProtocolError)
{
Talo.Continuity.PushRequest(uri, method, content, headers, continuityTimestamp);
}

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

try
{
var jsonError = JsonUtility.FromJson<ErrorResponse>(www.downloadHandler.text);
message = string.IsNullOrEmpty(jsonError.message) ? www.downloadHandler.text : jsonError.message;
errorCode = jsonError.errorCode;
}
catch (Exception)
{
message = www.error;
}

if (string.IsNullOrEmpty(errorCode))
{
throw new RequestException(www.responseCode, new Exception(message));
}
else
{
throw new PlayerAuthException(errorCode, new Exception(message));
}
}
}
}

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/APIs/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/APIs/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
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
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 bd7db3d

Please sign in to comment.