Skip to content

Commit

Permalink
Merge branch 'Develop' into MathMagic
Browse files Browse the repository at this point in the history
  • Loading branch information
Astrovicis committed Jun 28, 2019
2 parents cec902b + 7ceec42 commit a575184
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 29 deletions.
Binary file modified .vs/calcflow2018.2/v15/Server/sqlite3/storage.ide
Binary file not shown.
Binary file modified .vs/calcflow2018.2/v15/Server/sqlite3/storage.ide-shm
Binary file not shown.
21 changes: 21 additions & 0 deletions Assets/_Scripts/Core/Extensions/Collection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;

using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -81,6 +82,26 @@ public static int IndexOfElementWithValue<T,V>(this List<T> data, V fieldValue)
return index;
}

public static int IndexOfElementMatchingRegex<T,V>(this List<T> data, string reg)
{
int index = -1;
for (int i = 0; i < data.Count; i++)
{
Dictionary<string, object> entry = data[i] as Dictionary<string, object>;
Regex rgx = new Regex(reg);
foreach (string val in entry.Values)
{
if(rgx.IsMatch(val))
{
index = i;
break;
}
}
}

return index;
}

public static List<T> SubList<T>(this List<T> data, int index, int length = 0)
{
if (length == 0)
Expand Down
44 changes: 31 additions & 13 deletions Assets/_Scripts/GraphScripts/OutputMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,7 @@ protected void HandleInput(FlexActionableComponent sender)
{
button.SetState(ClaimCommitButton.CommitButtonState.Processing);

StartCoroutine(ClaimCommitButton.Instance.commit.claim(
(obj) =>
{
button.SetState(ClaimCommitButton.CommitButtonState.YetToBeCommitted);
},
(nada) =>
{
button.SetState(ClaimCommitButton.CommitButtonState.CantCommit);
})
);
StartCoroutine(claimAndCommit(button));
}
else if (state == ClaimCommitButton.CommitButtonState.YetToBeCommitted)
{
Expand All @@ -181,20 +172,47 @@ protected void HandleInput(FlexActionableComponent sender)
calcManager.manageText();
}

public IEnumerator claimAndCommit(ClaimCommitButton button)
{
var claim = new Utils.CoroutineWithData<bool>(MatryxCortex.Instance, ClaimCommitButton.Instance.commit.claim());
yield return claim;
yield return claim.result;

if (claim.result)
{
var commit = new Utils.CoroutineWithData<bool>(MatryxCortex.Instance, ClaimCommitButton.Instance.commit.create());
yield return commit;
yield return commit.result;

if (commit.result)
{
button.SetState(ClaimCommitButton.CommitButtonState.Committed);
}
else
{
button.SetState(ClaimCommitButton.CommitButtonState.CantCommit);
}
}
else
{
button.SetState(ClaimCommitButton.CommitButtonState.CantCommit);
}
}

public static string lastSurface = "";
private void LookupCommitStateByExpressionSet(object sender, EventArgs args)
{
ClaimCommitButton.Instance.SetState(ClaimCommitButton.CommitButtonState.Disabled);

var surface = SerializeSurface();
Debug.Log("surface: " + surface);
if (lastSurface.Equals(surface)) return;
if (!MatryxCommit.storageClaimsLoaded) return;

List<string> fileNames = new List<string> { "jsonContent.json" };
List<byte[]> contents = new List<byte[]> { MatryxCortex.serializer.Serialize(surface) };
List<string> fileTypes = new List<string> { "application/json" };
List<string> contents = new List<string> { surface };
// get ipfs hash
var ipfsHashRequest = new Utils.CoroutineWithData<string>(MatryxCortex.Instance, MatryxCortex.uploadFiles(fileNames, contents, fileTypes, "&only-hash=true",
var ipfsHashRequest = new Utils.CoroutineWithData<string>(MatryxCortex.Instance, MatryxCortex.uploadFiles(fileNames, contents, "&only-hash=true",
(object multiHash) =>
{
// This correct? You should look at commits that've already been created and see if there is one with this hash
Expand Down
5 changes: 3 additions & 2 deletions Assets/_Scripts/Matryx/ContractWrappers/MatryxCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public MatryxCommit() {}
public MatryxCommit(string commitHash)
{
hash = commitHash;
value = Nethereum.Util.UnitConversion.Convert.ToWei(1);
}

public MatryxCommit(string commitHash, string cont) : this(commitHash)
Expand Down Expand Up @@ -392,9 +393,9 @@ public IEnumerator uploadContent()
if (!content.Equals(""))
{
List<string> fileNames = new List<string>() { "jsonContent.json"};
List<byte[]> contents = new List<byte[]>() { MatryxCortex.serializer.Serialize(content)};
List<string> contents = new List<string> { content };
List<string> fileTypes = new List<string>() { "application/json" };
var uploadToIPFS = new Utils.CoroutineWithData<string>(MatryxCortex.Instance, MatryxCortex.uploadFiles(fileNames, contents, fileTypes));
var uploadToIPFS = new Utils.CoroutineWithData<string>(MatryxCortex.Instance, MatryxCortex.uploadFiles(fileNames, contents));
yield return uploadToIPFS;
ipfsContentHash = uploadToIPFS.result;
yield return true;
Expand Down
32 changes: 22 additions & 10 deletions Assets/_Scripts/Matryx/MatryxCortex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Nethereum.ABI.Decoders;
using Nanome.Core.Extension;
using System.Reflection;
using System.Linq;

namespace Matryx
{
Expand All @@ -28,7 +29,7 @@ public class MatryxCortex : MonoBehaviour

public static MatryxCortex Instance { get; private set; }
public static Serializer serializer = new Serializer();
public static string cortexURL = "https://cortex.matryx.ai";
public static string cortexURL = "https://cortex-staging.matryx.ai";
public static string platformInfoURL = cortexURL + "/platform/getInfo";
public static string userInfoURL = cortexURL + "/user/getInfo";
public static string tokenInfoURL = cortexURL + "/token/getInfo";
Expand All @@ -49,7 +50,7 @@ public class MatryxCortex : MonoBehaviour
public static string ipfsAddURL = ipfsURL + "/add?pin=false";

public static string jsonUploadURL = cortexURL + "/upload/json";
public static string filesUploadURL = ipfsURL + "/add?recursive=true&quieter=true"; //&wrap-with-directory=true
public static string filesUploadURL = ipfsURL + "/add?quieter=true&wrap-with-directory=true";

public static List<string> supportedCalcflowCategories = new List<string>();

Expand Down Expand Up @@ -506,7 +507,15 @@ private static IEnumerator GetCommit(string commitHash, bool getContent, Async.E
(cont) =>
{
commit.content = Utils.Substring(cont as string, '{', '}');
onSuccess?.Invoke(commit);
if (commit.content == "")
{
onError(cont);
}
else
{
onSuccess?.Invoke(commit);
}

}, onError);
}
else
Expand Down Expand Up @@ -577,15 +586,16 @@ private static IEnumerator GetMyCommits(Async.EventDelegate onSuccess = null, As
}
}

public static IEnumerator uploadFiles(List<string> fileNames, List<byte[]> contents, List<string> fileTypes, string urlModifier = "", Async.EventDelegate onSuccess = null, Async.EventDelegate onError = null)
public static IEnumerator uploadFiles(List<string> fileNames, List<string> contents, string urlModifier = "", Async.EventDelegate onSuccess = null, Async.EventDelegate onError = null)
{
WWWForm form = new WWWForm();
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
for (var i = 0; i < fileNames.Count; i++)
{
form.AddBinaryData("files", contents[i], fileNames[i], fileTypes[i]);
formData.Add(new MultipartFormFileSection("path", contents[i], Encoding.UTF8, fileNames[i]));
}

UnityWebRequest request = UnityWebRequest.Post(filesUploadURL+urlModifier, form);
UnityWebRequest request = UnityWebRequest.Post(filesUploadURL+urlModifier, formData);

yield return request.SendWebRequest();
Debug.Log("request completed with code: " + request.responseCode);
if (request.isNetworkError || request.responseCode != 200)
Expand All @@ -595,7 +605,9 @@ public static IEnumerator uploadFiles(List<string> fileNames, List<byte[]> conte
}
else
{
var res = serializer.Deserialize<object>(request.downloadHandler.data) as Dictionary<string, object>;
var lastLine = Encoding.UTF8.GetBytes(request.downloadHandler.text.Trim().Split('\n').Last());
var res = serializer.Deserialize<object>(lastLine) as Dictionary<string, object>;

string multiHash = res["Hash"] as string;

onSuccess?.Invoke(multiHash);
Expand Down Expand Up @@ -645,7 +657,7 @@ public static IEnumerator uploadJson(string title, string description, string ip

public static void RunGetJSONContent(string ipfsHash, Async.EventDelegate onSuccess = null, Async.EventDelegate onError = null)
{
queueMetered(GetIPFSFilesContent(ipfsHash, "jsonContent.json", onSuccess, onError));
queueMetered(GetIPFSFilesContent(ipfsHash, "Qm", onSuccess, onError));
}

//public static IEnumerator GetIPFSJSONContent(string ipfsHash, string elementName = "", Async.EventDelegate onSuccess = null, Async.EventDelegate onError = null)
Expand Down Expand Up @@ -690,7 +702,7 @@ public static IEnumerator GetIPFSFilesContent(string ipfsHash, string elementNam
{
res = serializer.Deserialize<object>(contwww.bytes) as Dictionary<string, object>;
links = res["Links"] as List<object>;
index = links.IndexOfElementWithValue(elementName);
index = links.IndexOfElementMatchingRegex<object, string>(@"^Qm[0-9a-zA-Z]{44}$");
if(index != -1)
{
link = true;
Expand Down
4 changes: 2 additions & 2 deletions Assets/_Scripts/Matryx/MatryxGlobals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public static DateTime FromUnixTime(BigInteger unixTime)
{
var timestring = unixTime.ToString();
double timeAsDouble = Convert.ToDouble(timestring);
return epoch.AddSeconds(timeAsDouble);
return TimeZoneInfo.ConvertTimeFromUtc(epoch.AddSeconds(timeAsDouble), TimeZoneInfo.Local);
}

public static DateTime FromUnixTime(string unixTime)
{
return epoch.AddSeconds(double.Parse(unixTime));
return TimeZoneInfo.ConvertTimeFromUtc(epoch.AddSeconds(double.Parse(unixTime)), TimeZoneInfo.Local);
}

public static double ToUnixTime(DateTime time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override string textToDisplay()
case CommitButtonState.YetToBeClaimed:
return "Timestamp\nto\nMatryx";
case CommitButtonState.Processing:
return "Sending\nEthereum\nTransation";
return "Sending\nEthereum\nTransaction";
case CommitButtonState.YetToBeCommitted:
return "Commit\nto\nMatryx";
case CommitButtonState.Committed:
Expand Down
9 changes: 8 additions & 1 deletion Assets/_Scripts/Matryx/UI/Menus/CommitMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public void UpdateDisplay(object commit)

public void ErrorGettingCommit(object nothing)
{
description.text = "Commit content formatting error.";
if (nothing == "")
{
description.text = "Could not load commit contents. \n";
}
else
{
description.text = "Contents: (not Calcflow compatible)\n" + nothing.ToString();
}
}
}

0 comments on commit a575184

Please sign in to comment.