Skip to content

Commit 5bbe32d

Browse files
committed
add talo continuity support
1 parent 3ae47eb commit 5bbe32d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1374
-176
lines changed

Assets/Samples/Playground/Scenes/Playground.unity

Lines changed: 604 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Samples/Playground/Scripts/Continuity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEngine;
2+
using TaloGameServices;
3+
using UnityEngine.UI;
4+
5+
public class ToggleContinuity : MonoBehaviour
6+
{
7+
private void Start()
8+
{
9+
SetText();
10+
}
11+
12+
private bool GetValue()
13+
{
14+
return Talo.Settings.continuityEnabled;
15+
}
16+
17+
private void SetText()
18+
{
19+
GetComponentInChildren<Text>().text = GetValue() ? "Toggle off" : "Toggle on";
20+
}
21+
22+
public void OnButtonClick()
23+
{
24+
Talo.Settings.continuityEnabled = !Talo.Settings.continuityEnabled;
25+
SetText();
26+
27+
ResponseMessage.SetText($"Continuity is now {(Talo.Settings.continuityEnabled ? "enabled" : "disabled")}");
28+
}
29+
}

Assets/Samples/Playground/Scripts/Continuity/ToggleContinuity.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEngine;
2+
using TaloGameServices;
3+
using UnityEngine.UI;
4+
5+
public class ToggleOfflineMode : MonoBehaviour
6+
{
7+
private void Start()
8+
{
9+
SetText();
10+
}
11+
12+
private bool GetValue()
13+
{
14+
return Talo.Settings.offlineMode;
15+
}
16+
17+
private void SetText()
18+
{
19+
GetComponentInChildren<Text>().text = GetValue() ? "Go online" : "Go offline";
20+
}
21+
22+
public void OnButtonClick()
23+
{
24+
Talo.Settings.offlineMode = !Talo.Settings.offlineMode;
25+
SetText();
26+
27+
ResponseMessage.SetText($"You are now now {(Talo.Settings.offlineMode ? "offline" : "online")}");
28+
}
29+
}

Assets/Samples/Playground/Scripts/Continuity/ToggleOfflineMode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Samples/Playground/Scripts/Players/IdentifyPlayer.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class IdentifyPlayer : MonoBehaviour
88
{
99
public string service, identifier;
1010

11+
private void Start()
12+
{
13+
Talo.Players.OnIdentified += OnIdentified;
14+
}
15+
1116
public async void OnButtonClick()
1217
{
1318
await Identify();
@@ -18,18 +23,21 @@ private async Task Identify()
1823
try
1924
{
2025
await Talo.Players.Identify(service, identifier);
21-
22-
var panel = GameObject.Find("Panel");
23-
if (panel != null)
24-
{
25-
ResponseMessage.SetText("Identified!");
26-
panel.GetComponent<Image>().color = new Color(135 / 255f, 1f, 135 / 255f);
27-
}
2826
}
2927
catch (Exception err)
3028
{
3129
ResponseMessage.SetText(err.Message);
3230
throw err;
3331
}
3432
}
33+
34+
private void OnIdentified(Player player)
35+
{
36+
var panel = GameObject.Find("Panel");
37+
if (panel != null)
38+
{
39+
ResponseMessage.SetText("Identified!");
40+
panel.GetComponent<Image>().color = new Color(135 / 255f, 1f, 135 / 255f);
41+
}
42+
}
3543
}

Assets/Samples/Playground/Scripts/Stats/TrackStat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public void OnButtonClick()
1212
Track();
1313
}
1414

15-
private void Track()
15+
private async void Track()
1616
{
1717
try
1818
{
19-
Talo.Stats.Track(statInternalName, change);
19+
await Talo.Stats.Track(statInternalName, change);
2020

2121
ResponseMessage.SetText($"{statInternalName} changed by {change}");
2222
}

Packages/com.trytalo.talo/Runtime/BaseAPI.cs

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,82 @@
22
using UnityEngine;
33
using System.Threading.Tasks;
44
using UnityEngine.Networking;
5+
using System.Collections.Generic;
56

67
namespace TaloGameServices
78
{
89
public class BaseAPI
910
{
10-
protected TaloManager manager;
1111
protected string baseUrl;
1212

13-
public BaseAPI(TaloManager manager, string service)
13+
public BaseAPI(string service)
1414
{
15-
this.manager = manager;
16-
baseUrl = $"{manager.settings.apiUrl}/{service}";
15+
baseUrl = $"{Talo.Settings.apiUrl}/{service}";
1716
}
1817

1918
public Uri GetUri()
2019
{
2120
return new Uri(baseUrl);
2221
}
2322

24-
protected async Task<string> Call(Uri uri, string method, string content = "")
23+
private List<HttpHeader> BuildHeaders()
24+
{
25+
var headers = new List<HttpHeader>
26+
{
27+
new HttpHeader("Authorization", $"Bearer {Talo.Settings.accessKey}"),
28+
new HttpHeader("Content-Type", "application/json"),
29+
new HttpHeader("Accept", "application/json"),
30+
new HttpHeader("X-Talo-Dev-Build", Debug.isDebugBuild ? "1" : "0"),
31+
new HttpHeader("X-Talo-Include-Dev-Data", Debug.isDebugBuild ? "1" : "0")
32+
};
33+
34+
if (Talo.CurrentAlias != null)
35+
{
36+
headers.Add(new HttpHeader("X-Talo-Player", Talo.CurrentPlayer.id));
37+
headers.Add(new HttpHeader("X-Talo-Alias", Talo.CurrentAlias.id.ToString()));
38+
}
39+
40+
var sessionToken = Talo.PlayerAuth.SessionManager.GetSessionToken();
41+
if (!string.IsNullOrEmpty(sessionToken))
42+
{
43+
headers.Add(new HttpHeader("X-Talo-Session", sessionToken));
44+
}
45+
46+
return headers;
47+
}
48+
49+
protected async Task<string> Call(
50+
Uri uri,
51+
string method,
52+
string content = "",
53+
List<HttpHeader> headers = null,
54+
bool continuity = false
55+
)
2556
{
2657
if (Talo.TestMode)
2758
{
2859
return RequestMock.HandleCall(uri, method);
2960
}
3061

62+
var continuityTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
63+
64+
var allHeaders = continuity ? headers : BuildHeaders();
65+
66+
if (Talo.Settings.offlineMode)
67+
{
68+
return HandleOfflineRequest(uri, method, content, allHeaders);
69+
}
70+
3171
byte[] json = new System.Text.UTF8Encoding().GetBytes(content);
3272

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

38-
www.SetRequestHeader("Authorization", $"Bearer {manager.settings.accessKey}");
39-
www.SetRequestHeader("Content-Type", "application/json");
40-
www.SetRequestHeader("Accept", "application/json");
41-
www.SetRequestHeader("X-Talo-Dev-Build", Debug.isDebugBuild ? "1" : "0");
42-
www.SetRequestHeader("X-Talo-Include-Dev-Data", Debug.isDebugBuild ? "1" : "0");
43-
44-
if (Talo.CurrentAlias != null)
45-
{
46-
www.SetRequestHeader("X-Talo-Player", Talo.CurrentPlayer.id);
47-
www.SetRequestHeader("X-Talo-Alias", Talo.CurrentAlias.id.ToString());
48-
}
49-
50-
var sessionToken = Talo.PlayerAuth.SessionManager.GetSessionToken();
51-
if (!string.IsNullOrEmpty(sessionToken))
78+
foreach (var header in allHeaders)
5279
{
53-
www.SetRequestHeader("X-Talo-Session", sessionToken);
80+
www.SetRequestHeader(header.key, header.value);
5481
}
5582

5683
var op = www.SendWebRequest();
@@ -66,6 +93,11 @@ protected async Task<string> Call(Uri uri, string method, string content = "")
6693
}
6794
else
6895
{
96+
if (www.responseCode >= 500 || www.result != UnityWebRequest.Result.ProtocolError)
97+
{
98+
Talo.Continuity.PushRequest(uri, method, content, headers, continuityTimestamp);
99+
}
100+
69101
string message = "";
70102
string errorCode = "";
71103

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

83115
if (string.IsNullOrEmpty(errorCode))
84116
{
85-
throw new Exception($"Request failed: {message}");
117+
throw new RequestException(www.responseCode, new Exception(message));
86118
}
87119
else
88120
{
@@ -91,5 +123,16 @@ protected async Task<string> Call(Uri uri, string method, string content = "")
91123
}
92124
}
93125
}
126+
127+
private string HandleOfflineRequest(
128+
Uri uri,
129+
string method,
130+
string content = "",
131+
List<HttpHeader> headers = null
132+
)
133+
{
134+
Talo.Continuity.PushRequest(uri, method, content, headers, DateTimeOffset.Now.ToUnixTimeMilliseconds());
135+
throw new RequestException(0, new Exception("Offline mode enabled"));
136+
}
94137
}
95138
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace TaloGameServices
6+
{
7+
public class ContinuityAPI : BaseAPI
8+
{
9+
public ContinuityAPI() : base("") { }
10+
11+
public async Task Replay(
12+
Uri uri,
13+
string method,
14+
string content,
15+
List<HttpHeader> headers
16+
)
17+
{
18+
await Call(uri, method, content, headers, true);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)