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) { if(steamStats == null) steamStats = steamStatsLibrary; if(broadcastStatsOnStart) BroadcastStats(); 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.name); else if(statData.statType == SteamStatType.Float) value += Steamworks.SteamUserStats.GetStatFloat(statData.name); 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(statName, newValue); if(Steamworks.SteamUserStats.GetStatInt(statName) != newValue) Debug.LogWarning($"Stat \"{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(statName, newFloatValue); if(Steamworks.SteamUserStats.GetStatFloat(statName) != newFloatValue) Debug.LogWarning($"Stat \"{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); GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value); } else if(stat.statType == SteamStatType.Float) { float value = Steamworks.SteamUserStats.GetStatFloat(stat.name); GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value); } } } } }