Skip to content

Commit

Permalink
Sync reaching 100% and complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sbanca committed Dec 20, 2024
1 parent a8f606d commit fe7e9ed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
101 changes: 51 additions & 50 deletions Assets/Scripts/Multiplayer/MultiplayerSceneSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,6 +49,11 @@ void Start()
onLargeDataReceived += OnLargeDataReceived;
}

private void Update()
{
ProcessQueue();
}

void OnDestroy()
{
onLargeDataReceived -= OnLargeDataReceived;
Expand Down Expand Up @@ -206,24 +212,12 @@ public async void StartSyncProgressDisplayForSrokes(int TargetPlayerId, LinkedLi

foreach (var stroke in strokes)
{
int retryCount = 0;
const int maxRetries = 3;
bool acknowledged = false;

while (retryCount < maxRetries)
while (await MultiplayerManager.m_Instance.CheckStrokeReception(stroke, TargetPlayerId))
{
if (await MultiplayerManager.m_Instance.CheckStrokeReception(stroke, TargetPlayerId))
{
acknowledged = true;
break;
}
retryCount++;
await Task.Delay(200);
}

if (!acknowledged)
ControllerConsoleScript.m_Instance.AddNewLine($"Stroke {stroke.m_Guid} failed to synchronize after {maxRetries} retries.");

sentStrokes++;
SynchHistoryPercentage(TargetPlayerId, strokes.Count, sentStrokes);
}
Expand Down Expand Up @@ -269,27 +263,54 @@ private void SynchHistoryComplete(int id)

#region Local infoCard commands

private InfoCardAnimation infoCard;
private readonly object infoCardLock = new object();
private ConcurrentQueue<string> messageQueue = new ConcurrentQueue<string>();
private InfoCardAnimation infoCard;

private void EnqueueMessage(string message)
{
messageQueue.Enqueue(message);
}

public void DisplaySynchInfo(string text = "Sync Started!")
private void ProcessQueue() //once per frame
{
lock (infoCardLock)
if (messageQueue.TryDequeue(out string message))
{
if (infoCard == null)
{
OutputWindowScript.m_Instance.CreateInfoCardAtController(
InputManager.ControllerName.Brush,
text,
fPopScalar: 1.0f
);
infoCard = RetrieveInfoCard();
DisplaySynchInfo(message);
}
else
{
UpdateInfoCard(message);
}
}
}

public InfoCardAnimation RetrieveInfoCard()
private void DisplaySynchInfo(string text)
{
if (infoCard == null)
{
OutputWindowScript.m_Instance.CreateInfoCardAtController(
InputManager.ControllerName.Brush,
text,
fPopScalar: 1.0f
);
infoCard = RetrieveInfoCard();
}
else
{
UpdateInfoCard(text);
}
}

private void UpdateInfoCard(string text)
{
infoCard.GetComponentInChildren<TextMeshPro>().text = text;
infoCard.UpdateHoldingDuration(5f);
}

private InfoCardAnimation RetrieveInfoCard()
{
InfoCardAnimation[] allInfoCards = FindObjectsOfType<InfoCardAnimation>();
foreach (var card in allInfoCards)
Expand All @@ -303,41 +324,21 @@ public InfoCardAnimation RetrieveInfoCard()
return null;
}

public void StartSynchInfo()
{
EnqueueMessage("Sync Started!");
}
public void SynchInfoPercentageUpdate()
{
lock (infoCardLock)
{
int percentage = (int)((float)SketchMemoryScript.AllStrokesCount() / numberOfCommandsExpected * 100);
string text = $"Sync {percentage}%";

if (infoCard == null)
{
DisplaySynchInfo(text);
return;
}

infoCard.GetComponentInChildren<TextMeshPro>().text = text;
infoCard.UpdateHoldingDuration(5f);
}
int percentage = (int)((float)SketchMemoryScript.AllStrokesCount() / numberOfCommandsExpected * 100);
EnqueueMessage($"Sync {percentage}%");
}

public void HideSynchInfo()
{
lock (infoCardLock)
{
if (infoCard == null)
{
DisplaySynchInfo("Sync Ended!");
return;
}

infoCard.GetComponentInChildren<TextMeshPro>().text = "Sync Ended!";
infoCard.UpdateHoldingDuration(3.0f);
ControllerConsoleScript.m_Instance.AddNewLine("Sync Ended!");
}
EnqueueMessage("Sync Ended!");
}


#endregion

}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/SketchControlsScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4862,7 +4862,7 @@ public void IssueGlobalCommand(GlobalCommands rEnum, int iParam1 = -1,
SketchSurfacePanel.m_Instance.EatToolsInput();
break;
case GlobalCommands.DisplaySynchInfo:
MultiplayerSceneSync.m_Instance.DisplaySynchInfo();
MultiplayerSceneSync.m_Instance.StartSynchInfo();
break;
case GlobalCommands.SynchInfoPercentageUpdate:
MultiplayerSceneSync.m_Instance.SynchInfoPercentageUpdate();
Expand Down

0 comments on commit fe7e9ed

Please sign in to comment.