Newer
Older
using Newtonsoft.Json;
using REST_JSON_API;
using System.Collections;
using System.Collections.Generic;
public class ScrollDetails : MonoBehaviour
{
public static ScrollDetails Instance
{
get => _Instance;
set
{
}
}
private static ScrollDetails _Instance;
public GameObject parameterDisplayPrefab;
public static Scroll ActiveScroll { get; private set; }
public GameObject mmtAnswerPopUp;
public static List<RenderedScrollFact> ParameterDisplays { get; private set; }
private static List<ScrollAssignment> LatestBackwartsCompletions;
private static List<Fact> LatestRenderedHints;
John Schihada
committed
public string currentMmtAnswer;
public bool DynamicScrollDescriptionsActive = true;
public bool AutomaticHintGenerationActive = true;
MaZiFAU
committed
private bool SendingViewDone = true;
MaZiFAU
committed
private bool MagicInQue = false;
private readonly IReadOnlyList<string> NoDynamicScroll = new List<string>()
{
};
if (cursor == null)
cursor = FindObjectOfType<WorldCursor>();
Popup = mmtAnswerPopUp.GetComponent<PopupBehavior>();
Popup.gameObject.SetActive(true); // force Awake
ScrollFactHintEvent.AddListener(animateHint);
private void OnDisable()
{
ScrollFactHintEvent.RemoveListener(animateHint);
private void OnDestroy()
{
_Instance = null;
}
ActiveScroll = scroll_to_set;
Transform originalScroll = gameObject.transform.GetChild(1);
John Schihada
committed
Transform originalScrollView = originalScroll.GetChild(1);
Transform originalViewport = originalScrollView.GetChild(0);
originalViewport.GetChild(0).gameObject.DestroyAllChildren();
originalScroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = ActiveScroll.description;
//ParameterDisplays.ForEach(gameObj => Destroy(gameObj));
ParameterDisplays = new();
for (int i = 0; i < ActiveScroll.requiredFacts.Count; i++)
GameObject originalObj =
Instantiate(parameterDisplayPrefab, originalViewport.GetChild(0));
RenderedScrollFact originalRSF =
originalObj.GetComponentInChildren<RenderedScrollFact>();
John Schihada
committed
originalRSF.Populate(ActiveScroll, ActiveScroll.requiredFacts[i].@ref.uri);
.TryGetValue(ActiveScroll.ScrollReference, out (string, int, bool)[] population)
&& population.Length > 0)
{
DynamicScrollInQue = true; // block update on population
foreach ((string Id, int index, bool show) in population)
{
ParameterDisplays[index].URI = Id;
ParameterDisplays[index].transform.parent.gameObject.SetActive(show);
}
DynamicScrollInQue = false; // unblock
}
NewAssignmentEvent.Invoke(); // init display
John Schihada
committed
public bool SetNextEmptyTo(FactObjectUI activator)
{
RenderedScrollFact check = ParameterDisplays
.Find(RSF => RSF != null
&& RSF.Payload != null
if (check != null)
{
check.URI = null;
return false;
}
RenderedScrollFact empty = ParameterDisplays
.Find(RSF => !RSF.IsSet);
if (empty == null)
return false;
empty.SetByFactObject(activator);
return true;
}
if (ActiveScroll == null)
return;
MaZiFAU
committed
if (MagicInQue)
yield break; // only need next in que to finish
MagicInQue = true;
while (!SendingViewDone || DynamicScrollInQue)
MaZiFAU
committed
MagicInQue = false;
if (currentMmtAnswer == null)
{
if (VerboseURI)
Debug.Log("Magic answers:\n" + currentMmtAnswer);
System.DateTime serializeTime = System.DateTime.UtcNow;
ScrollApplicationInfo pushout = JsonConvert.DeserializeObject<ScrollApplicationInfo>(currentMmtAnswer);
Debug.LogFormat($"Answerd serialized in : {(System.DateTime.UtcNow - serializeTime).TotalMilliseconds}ms");
if (pushout.acquiredFacts.Count == 0
|| pushout.errors.Length > 0)
{
ScrollApplicationCheckingErrorEvent.Invoke(pushout.errors);
PushoutFactFailEvent.Invoke();
}
else
Popup.HidePopUp(); //close error Window
MaZiFAU
committed
yield return __GeneratePushoutFacts(pushout.acquiredFacts);
MaZiFAU
committed
IEnumerator __GeneratePushoutFacts(List<MMTFact> pushoutFacts)
{
List<Fact> new_facts = new();
Dictionary<string, string> old_to_new = new();
MaZiFAU
committed
System.DateTime parseTime = System.DateTime.UtcNow;
bool samestep = false;
for (int i = 0; i < pushoutFacts.Count; i++)
{
List<Fact> new_list = Fact.MMTFactory(pushoutFacts[i].MapURIs(old_to_new));
MaZiFAU
committed
if (new_list.Count == 0)
{
Debug.LogWarning("Parsing on pushout-fact returned empty List -> One of the dependent facts does not exist or parsing failed");
continue;
}
foreach (Fact new_fact in new_list)
{
Fact added = FactAdder.AddFactIfNotFound(new_fact, out bool exists, samestep, null, ActiveScroll.label);
if (!exists)
{
new_facts.Add(added);
AnimateExistingFactEvent.Invoke(added.Id, FactWrapper.FactMaterials.Solution);
samestep = true;
}
else
{
// AnimateExistingFactEvent.Invoke(_new, FactWrapper.FactMaterials.Hint); // Automaticly done in FactRecorder
old_to_new.Add(new_fact.Id, added.Id);
MaZiFAU
committed
}
}
//yield return null;
MaZiFAU
committed
}
Debug.Log($"Facts parsed within {(System.DateTime.UtcNow - parseTime).TotalMilliseconds}ms");
yield break;
}
//return; //if you read this, delete this line!
if (ActiveScroll?.ScrollReference == null
|| NoDynamicScroll.Contains(ActiveScroll.ScrollReference))
return;
if (AutomaticHintGenerationActive || DynamicScrollDescriptionsActive)
StartCoroutine(_NewAssignment());
yield break; // only need next in que to finish
DynamicScrollInQue = true;
MaZiFAU
committed
while (!SendingViewDone)
yield return null; // if we dont wait => server will crash
if (currentMmtAnswer == null)
{
ScrollDynamicInfo scrollDynamicInfo;
try
{
scrollDynamicInfo = JsonConvert.DeserializeObject<ScrollDynamicInfo>(currentMmtAnswer);
//scrollDynamicInfo = IJSONsavable<ScrollDynamicInfo>.postprocess(scrollDynamicInfo); // DON'T! will remove hints
}
catch (JsonSerializationException ex)
{
Debug.LogException(ex);
Debug.LogError("Could not Deserialize MMT aswer for /scroll/dynamic\n" + currentMmtAnswer);
yield break;
}
ScrollApplicationCheckingError[] errors = scrollDynamicInfo.errors
.Where(err => err.kind != "nonTotal") // expected
.ToArray();
if (errors.Length > 0)
ScrollApplicationCheckingErrorEvent.Invoke(errors);
processScrollDynamicInfo(scrollDynamicInfo);
}
MaZiFAU
committed
SendingViewDone = false;
while (ParameterDisplays == null) // Wait for server
string body = prepareScrollAssignments();
Marco Zimmer
committed
using UnityWebRequest www = UnityWebRequest.Put(ServerAdress + endpoint, body);
www.SetRequestHeader("Content-Type", "application/json");
System.DateTime sendTime = System.DateTime.UtcNow;
yield return www.SendWebRequest();
MaZiFAU
committed
//if (VerboseURI)
Debug.LogFormat("Server answerd in : {0}ms"
, (System.DateTime.UtcNow - sendTime).TotalMilliseconds);
if (www.result == UnityWebRequest.Result.ConnectionError
|| www.result == UnityWebRequest.Result.ProtocolError)
John Schihada
committed
currentMmtAnswer = null;
yield return null;
currentMmtAnswer = www.downloadHandler.text
.Replace("\"float\":null", "\"float\":0.0"); // cannot convert null to value type
MaZiFAU
committed
SendingViewDone = true;
yield break;
for (int i = 0; i < ParameterDisplays.Count; i++)
{
Fact tempFact = ParameterDisplays[i].Fact;
if (tempFact != null)
assignmentList.Add(new ScrollAssignment(ActiveScroll.requiredFacts[i].@ref.uri, tempFact.Id));
return JsonConvert.SerializeObject(new ScrollApplication(ActiveScroll.ScrollReference, assignmentList));
}
}
private void processScrollDynamicInfo(ScrollDynamicInfo scrollDynamicInfo)
MaZiFAU
committed
//TODO: more hints available in scrollDynamicInfo.rendered.requiredFacts
LatestBackwartsCompletions = scrollDynamicInfo.backward_completions.Count > 0
? scrollDynamicInfo.backward_completions[0]
List<string> hintUris = LatestBackwartsCompletions
.Select(completion => completion.fact.uri)
.ToList();
//Update Scroll, process data for later hints and update Uri-List for which hints are available
_processRenderedScroll(scrollDynamicInfo.rendered, hintUris);
//Show that Hint is available for ScrollParameter
HintAvailableEvent.Invoke(hintUris);
void _processRenderedScroll(Scroll rendered, List<string> hintUris)
if (DynamicScrollDescriptionsActive)
{ // Update scroll-description
Transform scroll = gameObject.transform.GetChild(1).transform;
scroll.GetChild(0).GetComponent<TextMeshProUGUI>().text = rendered.description;
LatestRenderedHints = new();
for (int i = 0; i < rendered.requiredFacts.Count; i++)
RenderedScrollFact RenderedScrollFact = ParameterDisplays
.Find(RSF => RSF.ScrollFactURI == rendered.requiredFacts[i].@ref.uri);
{ // e.g. FUNCs
//if(CommunicationEvents.VerboseScroll)
// Debug.Log($"Descrapancy between requiredFacts and displayed facts:" +
// $"Could not find display with ref: {rendered.requiredFacts[i].@ref.uri}");
if (DynamicScrollDescriptionsActive)
//Update ScrollParameter label
RenderedScrollFact.Scroll = rendered;
List<Fact> HintFactList = Fact.MMTFactory(rendered.requiredFacts[i]);
foreach (Fact HintFact in HintFactList)
hintUris.Add(HintFact.Id); // == rendered.requiredFacts[i].@ref.uri
public void animateHint(string scrollParameterUri)
AnimateExistingFactEvent.Invoke(
scrollParameterUri,
FactWrapper.FactMaterials.Hint
);
Fact hintFact = LatestRenderedHints.Find(x => x.Id == scrollParameterUri); // "Dictionary"
LatestBackwartsCompletions.Find((ScrollAssignment x) => x.fact.uri == scrollParameterUri); // "Dictionary"
if (suitableCompletion != null && suitableCompletion.assignment is OMS assignment)
if (FactRecorder.FindEquivalent(StageStatic.stage.factState.MyFactSpace, hintFact, out string found_key, out Fact _, out bool _, false))
// existing fact -> Animate that
AnimateExistingFactEvent.Invoke(
{ // Generate new FactRepresentation and animate it
AnimateNonExistingFactEvent.Invoke(hintFact);
AnimateExistingFactEvent.Invoke(
scrollParameterUri,
FactWrapper.FactMaterials.Hint
);