using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Core; using Sirenix.OdinInspector; namespace KairoEngine.SteamIntegration { [System.Serializable] public class SteamworksStats { public bool broadcastStatsOnStart = true; public string statisticsBroadcastEvent = "SetStatisticData"; public bool listenForStatisticsEvents = true; public string statisticsEvent = "StatisticDataChanged"; [InlineEditor(InlineEditorObjectFieldModes.Boxed), PropertySpace(4,0)] public SteamworksStatLibrary steamStats; public void Start(SteamworksStatLibrary steamStatsLibrary) { Timer.ExecuteRealTimeNotDisposable(2000, () => { if(broadcastStatsOnStart) BroadcastStats(); if(steamStats == null) steamStats = steamStatsLibrary; if(listenForStatisticsEvents) { for (int i = 0; i < steamStats.library.Count; i++) { var stat = steamStats.library[i]; if(stat.statType == SteamStatType.Integer) GenericEvents.StartListening($"{statisticsEvent}", SetStatInt); else if(stat.statType == SteamStatType.Float) GenericEvents.StartListeningForStringFloat($"{statisticsEvent}", SetStatFloat); } } }); } public void Stop() { Steamworks.SteamUserStats.StoreStats(); if(listenForStatisticsEvents) { for (int i = 0; i < steamStats.library.Count; i++) { var stat = steamStats.library[i]; if(stat.statType == SteamStatType.Integer) GenericEvents.StopListening($"{statisticsEvent}", SetStatInt); else if(stat.statType == SteamStatType.Float) GenericEvents.StopListeningForStringFloat($"{statisticsEvent}", SetStatFloat); } } } public SteamworksStatData GetSteamStatData(string statName) { for (int i = 0; i < steamStats.library.Count; i++) { if(steamStats.library[i].name == statName || steamStats.library[i].statName == statName) return steamStats.library[i]; } //Debug.LogWarning($"Could not find stat with name \"{statName}\" in SteamStatLibrary"); return null; } [Button("Print Stat")] public void PrintStat(string statName) { SteamworksStatData statData = GetSteamStatData(statName); if(statData == null) return; string value = ""; if(statData.statType == SteamStatType.Integer) value += Steamworks.SteamUserStats.GetStatInt(statData.statName); else if(statData.statType == SteamStatType.Float) value += Steamworks.SteamUserStats.GetStatFloat(statData.statName); Debug.Log($"{statData.statName}: {value}"); } [Button("Set Stat Integer")] public void SetStatInt(string statName, int newValue) { SteamworksStatData statData = GetSteamStatData(statName); if (statData == null) return; if (statData.statType != SteamStatType.Integer) return; Steamworks.SteamUserStats.SetStat(statData.statName, newValue); if(Steamworks.SteamUserStats.GetStatInt(statData.statName) != newValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks"); else Steamworks.SteamUserStats.StoreStats(); } [Button("Set Stat Float")] public void SetStatFloat(string statName, float newFloatValue) { SteamworksStatData statData = GetSteamStatData(statName); if (statData == null) return; if (statData.statType != SteamStatType.Float) return; Steamworks.SteamUserStats.SetStat(statData.statName, newFloatValue); if(Steamworks.SteamUserStats.GetStatFloat(statData.statName) != newFloatValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks"); else Steamworks.SteamUserStats.StoreStats(); } public void BroadcastStats() { for (int i = 0; i < steamStats.library.Count; i++) { var stat = steamStats.library[i]; if(stat.statType == SteamStatType.Integer) { int value = Steamworks.SteamUserStats.GetStatInt(stat.name); if(value != 0) GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value); } else if(stat.statType == SteamStatType.Float) { float value = Steamworks.SteamUserStats.GetStatFloat(stat.name); if(value != 0) GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value); } } } } }