Select Git revision
packages.config
Fact.cs 6.78 KiB
using UnityEngine;
using UnityEngine.Networking;
public abstract class Fact
{
public int Id;
public GameObject Representation;
public string backendURI;
public string backendValueURI; // supposed to be null, for Facts without values eg. Points, OpenLines, OnLineFacts...
public string format(float t) {
return t.ToString("0.0000").Replace(',', '.');
}
}
public class AddFactResponse
{
//class to Read AddFact Responses.
public string factUri;
public string factValUri;
public static AddFactResponse sendAdd(string path, string body) {
Debug.Log(body);
//Put constructor parses stringbody to byteArray internally (goofy workaround)
UnityWebRequest www = UnityWebRequest.Put(path, body);
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader("Content-Type", "application/json");
AsyncOperation op = www.Send();
while (!op.isDone) { }
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
return null;
}
else
{
string answer = www.downloadHandler.text;
return JsonUtility.FromJson<AddFactResponse>(answer);
}
}
}
//I am not sure if we ever need to attach these to an object, so one script for all for now...
public class PointFact : Fact
{
public Vector3 Point;
public Vector3 Normal;
public PointFact(int i,Vector3 P, Vector3 N) {
this.Id = i;
this.Point = P;
this.Normal = N;
string body = @"{ ""a"":" +format(P.x) + @"," + @"""b"":" + format(P.y) + @","+@"""c"":" + format(P.y) + "}";
Debug.Log(body);
AddFactResponse res = AddFactResponse.sendAdd("localhost:8081/fact/add/vector", body);
this.backendURI = res.factUri;
}
public PointFact(int i, float a, float b, float c, string uri)
{
this.Id = i;
this.Point = new Vector3(a,b,c);
this.Normal = new Vector3(0,1,0);
this.backendURI = uri;
}