using Cysharp.Threading.Tasks;
using Ubisoft.Hotel.Rpc;
using UnityEngine;
namespace Ubisoft.Hotel.Cache.Test
{
public class TestConsumerCacheDrivers : MonoBehaviour
{
#region Consumer data types
///
/// Class representing some consumer data type that needs to be stored in cache
///
private class Data1
{
string StringData { get; set; }
}
///
/// Class representing some consumer data type that needs to be stored in cache
///
private class Data2
{
string StringData { get; set; }
int IntData { get; set; }
}
///
/// Class representing some consumer data type that needs to be stored in cache
///
private class Data3
{
string StringData { get; set; }
int IntData { get; set; }
bool BoolData { get; set; }
}
#endregion
// Cache drivers: One for each consumer data type that needs to be stored in cache
private GenericCacheDriver m_data1Driver;
private GenericCacheDriver m_data2Driver;
private GenericCacheDriver m_data3Driver;
private GenericProtobufCacheDriver m_echoDriver;
private GenericProtobufCacheDriver m_creaturesDriver;
async void Start()
{
await InitCacheAsync();
await Test1();
}
private async UniTask InitCacheAsync()
{
// This cache will be set to work in online mode
m_data1Driver = await GenericCacheDriver.CreateAsync("Data1", "data_1", new CacheSettings(true, false, false, false));
// This cache will be set to work in online mode and be sensitive to the game version
m_data2Driver = await GenericCacheDriver.CreateAsync("Data2", "data_2", new CacheSettings(true, false, true, false));
// This cache will be set to work in offline mode
m_data3Driver = await GenericCacheDriver.CreateAsync("Data3", "data_3", new CacheSettings(true, true, false, false));
// These drivers work with Protobuf messages and store filtered data
// TODO Check what happens when sharing the same RootFolder
m_echoDriver = await GenericProtobufCacheDriver.CreateAsync("Actions", "echo", new CacheSettings(true, false, false, false));
m_creaturesDriver = await GenericProtobufCacheDriver.CreateAsync("Actions", "creatures", new CacheSettings(true, false, false, false));
}
// TODO Find a way to provide a basic cache flow to the consumer (same for GenericProtobufCacheDriver)
// GetDataAsync should check the cache parameters (online/offline, etc) to know if we can send requests to server
// E.g. GetProfile (1):
// - The game action could send to server the current cached crc, and return or not the profile data depending on whether the crc is up-to-date
// - In this case we need to have a GetCrcAsync method in the driver, and the consumer should call SaveDataAsync whenever the action returns new data
// E.g. GetProfile (2):
// - We could have separate game actions to perform the sync and to get the data
// - In this case the consumer could provide the Sync method to call via SyncMethod delegate, and the driver could extract the crc from metadata and perform the Sync internally
// - The callback for the sync method should be set to a method in GenericCacheDriver (OnSync), so that depending on the result (Ok, Update, etc) we can update the cache and return the right data
private async UniTask Test1()
{
string crc = await m_data1Driver.GetCrcAsync();
Debug.Log($"Loaded crc for {m_data1Driver.RootFolder}: {crc}");
crc = await m_data2Driver.GetCrcAsync();
Debug.Log($"Loaded crc for {m_data2Driver.RootFolder}: {crc}");
crc = await m_data3Driver.GetCrcAsync();
Debug.Log($"Loaded crc for {m_data3Driver.RootFolder}: {crc}");
crc = await m_echoDriver.GetCrcAsync();
Debug.Log($"Loaded crc for {m_echoDriver.RootFolder}/{m_echoDriver.Filepath}: {crc}");
crc = await m_creaturesDriver.GetCrcAsync();
Debug.Log($"Loaded crc for {m_creaturesDriver.RootFolder}/{m_creaturesDriver.Filepath}: {crc}");
}
}
}