SteamworksStats.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.SteamIntegration
  7. {
  8. [System.Serializable]
  9. public class SteamworksStats
  10. {
  11. public bool broadcastStatsOnStart = true;
  12. public string statisticsBroadcastEvent = "SetStatisticData";
  13. public bool listenForStatisticsEvents = true;
  14. public string statisticsEvent = "StatisticDataChanged";
  15. [InlineEditor(InlineEditorObjectFieldModes.Boxed), PropertySpace(4,0)]
  16. public SteamworksStatLibrary steamStats;
  17. public void Start(SteamworksStatLibrary steamStatsLibrary)
  18. {
  19. Timer.ExecuteRealTimeNotDisposable(2000, () =>
  20. {
  21. if(broadcastStatsOnStart) BroadcastStats();
  22. if(steamStats == null) steamStats = steamStatsLibrary;
  23. if(listenForStatisticsEvents)
  24. {
  25. for (int i = 0; i < steamStats.library.Count; i++)
  26. {
  27. var stat = steamStats.library[i];
  28. if(stat.statType == SteamStatType.Integer) GenericEvents.StartListening($"{statisticsEvent}", SetStatInt);
  29. else if(stat.statType == SteamStatType.Float) GenericEvents.StartListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
  30. }
  31. }
  32. });
  33. }
  34. public void Stop()
  35. {
  36. Steamworks.SteamUserStats.StoreStats();
  37. if(listenForStatisticsEvents)
  38. {
  39. for (int i = 0; i < steamStats.library.Count; i++)
  40. {
  41. var stat = steamStats.library[i];
  42. if(stat.statType == SteamStatType.Integer) GenericEvents.StopListening($"{statisticsEvent}", SetStatInt);
  43. else if(stat.statType == SteamStatType.Float) GenericEvents.StopListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
  44. }
  45. }
  46. }
  47. public SteamworksStatData GetSteamStatData(string statName)
  48. {
  49. for (int i = 0; i < steamStats.library.Count; i++)
  50. {
  51. if(steamStats.library[i].name == statName || steamStats.library[i].statName == statName) return steamStats.library[i];
  52. }
  53. //Debug.LogWarning($"Could not find stat with name \"{statName}\" in SteamStatLibrary");
  54. return null;
  55. }
  56. [Button("Print Stat")]
  57. public void PrintStat(string statName)
  58. {
  59. SteamworksStatData statData = GetSteamStatData(statName);
  60. if(statData == null) return;
  61. string value = "";
  62. if(statData.statType == SteamStatType.Integer) value += Steamworks.SteamUserStats.GetStatInt(statData.statName);
  63. else if(statData.statType == SteamStatType.Float) value += Steamworks.SteamUserStats.GetStatFloat(statData.statName);
  64. Debug.Log($"{statData.statName}: {value}");
  65. }
  66. [Button("Set Stat Integer")]
  67. public void SetStatInt(string statName, int newValue)
  68. {
  69. SteamworksStatData statData = GetSteamStatData(statName);
  70. if (statData == null) return;
  71. if (statData.statType != SteamStatType.Integer) return;
  72. Steamworks.SteamUserStats.SetStat(statData.statName, newValue);
  73. if(Steamworks.SteamUserStats.GetStatInt(statData.statName) != newValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks");
  74. else Steamworks.SteamUserStats.StoreStats();
  75. }
  76. [Button("Set Stat Float")]
  77. public void SetStatFloat(string statName, float newFloatValue)
  78. {
  79. SteamworksStatData statData = GetSteamStatData(statName);
  80. if (statData == null) return;
  81. if (statData.statType != SteamStatType.Float) return;
  82. Steamworks.SteamUserStats.SetStat(statData.statName, newFloatValue);
  83. if(Steamworks.SteamUserStats.GetStatFloat(statData.statName) != newFloatValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks");
  84. else Steamworks.SteamUserStats.StoreStats();
  85. }
  86. public void BroadcastStats()
  87. {
  88. for (int i = 0; i < steamStats.library.Count; i++)
  89. {
  90. var stat = steamStats.library[i];
  91. if(stat.statType == SteamStatType.Integer)
  92. {
  93. int value = Steamworks.SteamUserStats.GetStatInt(stat.name);
  94. if(value != 0) GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
  95. }
  96. else if(stat.statType == SteamStatType.Float)
  97. {
  98. float value = Steamworks.SteamUserStats.GetStatFloat(stat.name);
  99. if(value != 0) GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
  100. }
  101. }
  102. }
  103. }
  104. }