-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from TaloDev/continuity
Add continuity
- Loading branch information
Showing
66 changed files
with
1,522 additions
and
261 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
Assets/Samples/Playground/Scripts/Continuity/ToggleContinuity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Samples/Playground/Scripts/Continuity/ToggleContinuity.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
Assets/Samples/Playground/Scripts/Continuity/ToggleOfflineMode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Samples/Playground/Scripts/Continuity/ToggleOfflineMode.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.