Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class EventsAPI : BaseAPI
{
public event Action OnFlushed;

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

private List<Event> eventsToFlush = new ();
internal List<Event> eventsToFlush = new ();
private bool lockFlushes;
private bool flushAttemptedDuringLock;

Expand Down Expand Up @@ -118,5 +118,13 @@ public async Task Flush()
await Flush();
}
}

public void ClearQueue()
{
queue.Clear();
eventsToFlush.Clear();
lockFlushes = false;
flushAttemptedDuringLock = false;
}
}
}
47 changes: 47 additions & 0 deletions Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PlayersAPI : BaseAPI
public event Action<Player> OnIdentified;
public event Action OnIdentificationStarted;
public event Action OnIdentificationFailed;
public event Action OnIdentityCleared;

private readonly string _offlineDataPath = Application.persistentDataPath + "/ta.bin";

Expand Down Expand Up @@ -82,6 +83,8 @@ public async Task<Player> IdentifySteam(string ticket, string identity = "")

public async Task<Player> Update()
{
Talo.IdentityCheck();

var uri = new Uri($"{baseUrl}/{Talo.CurrentPlayer.id}");
var content = JsonUtility.ToJson(Talo.CurrentPlayer);
var json = await Call(uri, "PATCH", Prop.SanitiseJson(content));
Expand Down Expand Up @@ -146,6 +149,50 @@ private PlayerAlias GetOfflineAlias()
return JsonUtility.FromJson<PlayerAlias>(Talo.Crypto.ReadFileContent(_offlineDataPath));
}

private void DeleteOfflineAlias()
{
if (File.Exists(_offlineDataPath))
{
try
{
File.Delete(_offlineDataPath);
}
catch (Exception e)
{
Debug.LogWarning($"Failed to delete offline player data: {e.Message}");
}
}
}

public async Task ClearIdentity()
{
Talo.IdentityCheck();

try
{
DeleteOfflineAlias();
}
catch (Exception e)
{
Debug.LogWarning($"Error deleting offline alias: {e.Message}");
}

try
{
// clears the alias and resets the socket (doesn't require auth)
await Talo.PlayerAuth.SessionManager.ClearSession();
}
catch (Exception ex)
{
Debug.LogWarning($"Error clearing session: {ex.Message}");
}

Talo.Events.ClearQueue();
Talo.Continuity.ClearRequests();

OnIdentityCleared?.Invoke();
}

public async Task<PlayersSearchResponse> Search(string query)
{
var encodedQuery = Uri.EscapeDataString(query.Trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,11 @@ public async void ProcessRequests()
throw new ContinuityReplayException(exceptions);
}
}

public void ClearRequests()
{
_requests.Clear();
WriteRequests();
}
}
}
8 changes: 8 additions & 0 deletions Assets/Talo Game Services/Talo/Tests/PlayersAPI.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
@@ -0,0 +1,54 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine.TestTools;
using UnityEngine;
using System.Threading.Tasks;

namespace TaloGameServices.Test
{
internal class IdentityClearedEventMock
{
public bool identityCleared = false;

public void Invoke()
{
identityCleared = true;
}
}

internal class ClearIdentityTest
{
[OneTimeSetUp]
public void Setup()
{
var tm = new GameObject().AddComponent<TaloManager>();
tm.settings = ScriptableObject.CreateInstance<TaloSettings>();
tm.settings.autoConnectSocket = false;

Talo.CurrentAlias = new PlayerAlias() {
player = new Player() {
id = "uuid"
}
};
}

[UnityTest]
public IEnumerator ClearIdentity_ShouldClearAliasData()
{
var eventMock = new IdentityClearedEventMock();
Talo.Players.OnIdentityCleared += eventMock.Invoke;

yield return Talo.Events.Track("test-event");
Assert.IsNotEmpty(Talo.Events.queue);

Talo.Players.ClearIdentity();
Assert.IsNull(Talo.CurrentAlias);
Assert.IsTrue(eventMock.identityCleared);
Assert.IsEmpty(Talo.Events.queue);
Assert.IsEmpty(Talo.Events.eventsToFlush);
Assert.IsFalse(Talo.Continuity.HasRequests());

Talo.Players.OnIdentityCleared -= eventMock.Invoke;
}
}
}

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