Newer
Older
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
public class ScrollDetails : MonoBehaviour
{
public static ScrollDetails Instance
{
get => _Instance;
set
{
if (_Instance == null)
_Instance = value;
else
Destroy(value);
}
}
private static ScrollDetails _Instance;
public GameObject parameterDisplayPrefab;
public GameObject mmtAnswerPopUp;
private static List<ScrollAssignment> LatestCompletions;
private static List<Fact> LatestRenderedHints;
John Schihada
committed
public string currentMmtAnswer;
public bool DynamicScrollDescriptionsActive = true;
public bool AutomaticHintGenerationActive = true;
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
ParameterDisplays.Add(originalRSF);
originalRSF.Populate(ActiveScroll, i);
foreach (int i in PrePopulateActiveScroll())
ParameterDisplays[i].gameObject.SetActive(false);
//set active scroll for ErrorMessagePopup
Popup.ActiveScroll = ActiveScroll;
Popup.ParameterDisplays = ParameterDisplays;
John Schihada
committed
public bool SetNextEmptyTo(FactObjectUI activator)
{
RenderedScrollFact check = ParameterDisplays
.Find(RSF => RSF != null
&& RSF.Payload != null
&& RSF.Payload.Equals(activator)
);
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;
}
/// <summary>
/// Secretly populates <see cref="Scroll"/>s
/// </summary>
/// <returns><c>Array</c> containing indicis of <see cref="Scroll.requiredFacts"/> to be hidden.</returns>
private int[] PrePopulateActiveScroll()
{
switch (ActiveScroll.@ref)
{
case MMT_OMS_URI.ScrollCannonBall:
return new int[] { 2 };
default:
return new int[0];
}
}
while (!DynamicScrollDone)
yield return null; // Wait for last assignment
DynamicScrollDone = false;
if (currentMmtAnswer == null)
{
if (VerboseURI)
Debug.Log("Magic answers:\n" + currentMmtAnswer);
ScrollApplicationInfo pushout = JsonConvert.DeserializeObject<ScrollApplicationInfo>(currentMmtAnswer);
if (pushout.acquiredFacts.Count == 0
|| pushout.errors.Length > 0)
{
ScrollApplicationCheckingErrorEvent.Invoke(pushout.errors);
PushoutFactFailEvent.Invoke();
}
if (AutomaticHintGenerationActive || DynamicScrollDescriptionsActive)
StartCoroutine(_NewAssignment());
while (!DynamicScrollDone)
yield return null; // if we dont wait => server will crash
DynamicScrollDone = false;
if (currentMmtAnswer == null)
{
ScrollDynamicInfo scrollDynamicInfo;
try
{
scrollDynamicInfo = JsonConvert.DeserializeObject<ScrollDynamicInfo>(currentMmtAnswer);
}
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);
}
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();
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;
John Schihada
committed
currentMmtAnswer = answer;
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 FilledScroll(ActiveScroll.@ref, assignmentList));
}
}
private void ReadPushout(List<MMTDeclaration> pushoutFacts)
bool samestep = false;
{
Fact newFact = ParsingDictionary.parseFactDictionary[pushoutFacts[i].getType()].Invoke(pushoutFacts[i]);
AnimateExistingFactEvent.Invoke
(FactManager.AddFactIfNotFound(newFact, out _, samestep, null, ActiveScroll.label).Id
, FactWrapper.FactMaterials.Solution);
Debug.Log("Parsing on pushout-fact returned null -> One of the dependent facts does not exist");
private void processScrollDynamicInfo(ScrollDynamicInfo scrollDynamicInfo)
LatestCompletions = scrollDynamicInfo.completions.Count > 0
? scrollDynamicInfo.completions[0]
List<string> hintUris = LatestCompletions
.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);
if (DynamicScrollDescriptionsActive)
//Update ScrollParameter label
RenderedScrollFact.Scroll = rendered;
Fact HintFact =
ParsingDictionary.parseFactDictionary[rendered.requiredFacts[i].getType()]
.Invoke(rendered.requiredFacts[i]);
//If the fact could not be parsed -> Therefore not all dependent Facts exist -> No Hint
hintUris.Add(HintFact.Id);
LatestRenderedHints.Add(HintFact);
}
public void animateHint(string scrollParameterUri)
if (FactOrganizer.AllFacts.ContainsKey(scrollParameterUri))
AnimateExistingFactEvent.Invoke(
scrollParameterUri,
FactWrapper.FactMaterials.Hint
);
Fact hintFact = LatestRenderedHints.Find(x => x.Id == scrollParameterUri);
ScrollAssignment suitableCompletion =
LatestCompletions.Find(x => x.fact.uri == scrollParameterUri);
if (suitableCompletion != null)
{
if (FactOrganizer.AllFacts.ContainsKey(suitableCompletion.assignment.uri))
AnimateExistingFactEvent.Invoke(
suitableCompletion.assignment.uri,
FactWrapper.FactMaterials.Hint
);
if (FactOrganizer.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
);