Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.2 kB
2
Indexable
Never
using UnityEngine;
using System;
using System.Linq;

using axr.sdk;
using axr.sdk.Services;
using axr.sdk.Models;
using System.Collections.Generic;

public class AXRManager : MonoBehaviour{ //TODO: Add timeout
    [Header ("online configuration")]
    public bool online = true;

    [Header("AXR SDK")]
    private CoreSDK CoreSDK;
    private ServiceFactory ServiceFactory;
    [Space(10)]
    [Header("StatUid")]
    public Guid buildingDataStatUid = new Guid("fa322f61-5d75-45ec-b69d-b4ce262a930d");

    //[Header("AXR Events")]
    public delegate void OnRetrivePersonaEvent(Persona persona);
    public static event OnRetrivePersonaEvent OnRetrivePersona;
    public delegate void OnRetriveBuildingDataEvent(PersonaStat personaStat);
    public static event OnRetriveBuildingDataEvent OnRetriveBuildingData;

    public static AXRManager Instance {
        get{
            if(_instance == null){
                _instance = new AXRManager();
            }
            return _instance;
        }
    }
    private static AXRManager _instance;

    public AXRManager(){
        _instance = this;
    }

#region PlayerData

    public async void GetPlayerPersona(){
        try{
            AXRCoreSDK SDK = AXRCoreSDK.GetInstance();
            if (SDK != null){
                CoreSDK = SDK.Instance;
                ServiceFactory = SDK.ServiceFactory;
            }
            var personaService = ServiceFactory.GetService<PersonaService>();
            Dictionary<string, string> query = new Dictionary<string, string>();
            query.Add("userUid", CoreSDK.LoggedInUser.Uid.ToString());
            var result = await personaService.FindAll(query);
            if (result != null){
                Debug.Log("success!");
                OnRetrivePersona.Invoke(result[0]);
                Debug.Log(result[0]);
            }
            else{
                Debug.Log("fail!");
            }
        }
        catch (Exception error){
            Debug.LogError("Failed device login. Error=" + error.Message);
        }
    }
    public async void GetPlayerBuildingData(){
        try{
            AXRCoreSDK SDK = AXRCoreSDK.GetInstance();
            if (SDK != null){
                CoreSDK = SDK.Instance;
                ServiceFactory = SDK.ServiceFactory;
            }
            var personaStatService = ServiceFactory.GetService<PersonaStatService>();
            var result = await personaStatService.FindAllByPersonaId(PlayerDataManager.playerPersona.Uid);

            if (result != null){
                Debug.Log("success!");
                var buildingData = result.FirstOrDefault(stat => stat.StatUid == buildingDataStatUid);
                OnRetriveBuildingData.Invoke(buildingData);
            }
            else{
                Debug.Log("fail!");
            }
        }
        catch (Exception error){
            Debug.LogError("Failed device login. Error=" + error.Message);
        }
    }
    
    public void UpdateBuildingData(string compresedBuildingData){ 
        if(!online){
            Debug.Log("updated!");
            PlayerPrefs.SetString("buildingData",compresedBuildingData);
            PlayerDataManager._playerData._BaseBuildingData = compresedBuildingData;
            return;
        }
    }

    public void CollectedResources(){
        if(!online){
            Debug.Log("updated!");
            string compresedBuildingData = BuildingManager.Instance.GetActiveBuildingData();
            PlayerPrefs.SetString("buildingData",compresedBuildingData);
            PlayerDataManager._playerData._BaseBuildingData = compresedBuildingData;
            return;
        }
    }
    
#endregion

#region Shop
    /// <summary>
    /// Used to verify shop purchase from backend
    /// </summary>
    /// <param name="shopData">ShopObject to be purhcased</param>
    public void PurchaseShopObject(ShopObjectData shopData){
        Debug.Log($"AXRManager : attempting purchase");
        //TODO: AXR Purchase
        if(!online){
            return;
        }
    }
#endregion

}
Leave a Comment