Skip to content

Commit

Permalink
Rtc server change, payment, exp values for dna
Browse files Browse the repository at this point in the history
  • Loading branch information
voropserg committed Aug 9, 2023
1 parent 95ec22f commit c49e983
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 57 deletions.
76 changes: 73 additions & 3 deletions Runtime/TotemCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using TotemServices.DNA;
using TotemEntities;
using TotemEntities.DNA;
using TotemConsts;
using TotemEnums;
using TotemUtils;

Expand All @@ -22,6 +23,7 @@ public class TotemCore
private TotemLegacyService _legacyService;
private TotemAuth _auth;
private TotemSmartContractManager _smartContract;
private TotemPayment _payment;
private TotemAnalytics _analytics;
private TotemDebug _debug;

Expand Down Expand Up @@ -195,6 +197,75 @@ public void AddLegacyRecord(object asset, TotemAssetType assetType, string data,
});
}

/// <summary>
/// Opens external web browser to proceed with asset purchase
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assetType"></param>
/// <param name="onSuccess"></param>
/// <param name="onFailure"></param>
public void PurchaseAsset<T>(TotemAssetType assetType, TotemDNAFilter filter, UnityAction<T> onSuccess, UnityAction onFailure) where T : new()
{
_payment.PurchaseAsset(assetType.ToString(), CurrentUser.PublicKey, (success) =>
{
if (!success)
{
onFailure();
return;
}
switch(assetType)
{
case TotemAssetType.avatar:
_smartContract.GetNewAvatar(CurrentUser, filter, onSuccess);
break;
case TotemAssetType.item:
_smartContract.GetNewItem(CurrentUser, filter, onSuccess);
break;
}
});
}

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="asset"></param>
/// <param name="genIndex"></param>
/// <param name="register"></param>
/// <returns></returns>
public uint GetAssetExponentialValue<T>(T asset, int genIndex, TotemDNARegister register)
{
string assetDna = _smartContract.GetAssetBinaryDNA(asset);

int startBitIndex = genIndex * 32;

Debug.Log("DNA length:" + assetDna.Length);
Debug.Log("Start bit: " + startBitIndex);
string expBin = assetDna.Substring(startBitIndex, 32);

string highExpVal = expBin.Substring(0, 16);
string lowExpVal = expBin.Substring(16);

switch(register)
{
case TotemDNARegister.LOW:
return System.Convert.ToUInt32(lowExpVal, 2);

case TotemDNARegister.HIGH:
return System.Convert.ToUInt32(highExpVal, 2);

default:
return 0;
}
}

public float GetAssetExponentialValueProbability(uint value)
{
return 1f - Mathf.Exp(-value / (float)ServicesEnv.AssetExponentialConstant);
}

/// <summary>
/// Returns Id of the asset
Expand All @@ -217,13 +288,12 @@ private void CreateServicesGameObject()
_analytics = _servicesGameObject.AddComponent<TotemAnalytics>();
_auth = _servicesGameObject.AddComponent<TotemAuth>();
_smartContract = _servicesGameObject.AddComponent<TotemSmartContractManager>();
_payment = _servicesGameObject.AddComponent<TotemPayment>();
_debug = _servicesGameObject.AddComponent<TotemDebug>();

UnityThread.initUnityThread();

MonoBehaviour.DontDestroyOnLoad(_servicesGameObject);
}



}

7 changes: 4 additions & 3 deletions Runtime/consts/services.env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ public static class ServicesEnv

#region SmartContract

public const string SmartContractUrl = "https://matic-mumbai.chainstacklabs.com";
public const string SmartContractUrl = "https://rpc-mumbai.maticvigil.com";
public const string SmartContractAvatars = "0x11dBDbF2e6D262c2fe7e73ace1A60c6862dC14dE";
public const string SmartContractItems = "0xEc9C96eF9b90a950057EDbe40B42385f3b1cE78C";
public const string SmartContractAvatarsFilterName = "totem-common-files/filters/totem-avatar";
public const string SmartContractItemsFilterName = "totem-common-files/filters/totem-item";
public const int AssetExponentialConstant = 7057;

#endregion

#region Payment

public const string PaymentSystem = "withpaper";
public const string PaymentAPIUrl = "https://dev-api.totem.gdn/payments";

public const string PaymentAPIUrl = "https://api.totem.gdn/payments";
public const string PaymentServiceUrl = "https://auth.totem.gdn/app-purchase/";

#endregion

Expand Down
4 changes: 0 additions & 4 deletions Runtime/enums/TotemAssetType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum TotemAssetType
{
avatar,
Expand Down
9 changes: 9 additions & 0 deletions Runtime/enums/TotemDNARegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace TotemEnums
{
public enum TotemDNARegister
{
LOW,
HIGH
}
}
11 changes: 11 additions & 0 deletions Runtime/enums/TotemDNARegister.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/services/TotemAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void Awake()

public void RecordAction(TotemServicesAction action, string gameId, TotemUser user, string userEmail)
{
#if! UNITY_EDITOR && !UNITY_WEBGL
#if !UNITY_EDITOR && !UNITY_WEBGL
var key = new EthECKey(TotemUtils.Convert.HexToByteArray(user.PublicKey), false);
string address = key.GetPublicAddress();

Expand Down
40 changes: 18 additions & 22 deletions Runtime/services/TotemAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,26 @@

namespace TotemServices
{
[Serializable]
public class SocketMessage
{
[JsonProperty("event")]
public string Event { get; set; }

public SocketRoomData data;
}

[Serializable]
public class SocketRoomData
{
public string room;
}

public class TotemAuth : MonoBehaviour
{

#region Models

private class IdToken
{
public int iat;
Expand All @@ -33,20 +48,6 @@ private class IdToken
public string profileImage;
}

[Serializable]
private class SocketMessage
{
[JsonProperty("event")]
public string Event { get; set; }

public SocketRoomData data;
}

[Serializable]
private class SocketRoomData
{
public string room;
}
#endregion

private const string redirectUrlQueryName = "success_url";
Expand Down Expand Up @@ -99,16 +100,16 @@ public async void LoginUser(UnityAction<TotemUser> onComplete, string gameId)
loginComplete = false;


string socketRoomId = GenerateSocketRoomId();
string socketRoomId = WebUtils.GenerateSocketRoomId();
string query = $"?{socketEnabledQueryName}=true&{socketRoomIdQueryName}={socketRoomId}";
#if !UNITY_EDITOR
if (!string.IsNullOrEmpty(gameId))
{
query += $"&{gameIdQueryName}={gameId}";
}
#endif
#if UNITY_ANDROID || UNITY_IOS
query += $"&{redirectUrlQueryName}={LoadRedirectUrl()}";
#if UNITY_ANDROID || UNITY_IOS && !UNITY_EDITOR
query += $"&{redirectUrlQueryName}={LoadRedirectUrl()}";
#elif UNITY_WEBGL && !UNITY_EDITOR
query += $"&{autoCloseQueryName}=true";
#endif
Expand Down Expand Up @@ -300,10 +301,5 @@ private void SetupWebSocket(string roomId, string authQuery)
};

}

private string GenerateSocketRoomId()
{
return Guid.NewGuid().ToString();
}
}
}
Loading

0 comments on commit c49e983

Please sign in to comment.