SteamworksStats.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public bool debug = false;
  16. [InlineEditor(InlineEditorObjectFieldModes.Boxed), PropertySpace(4,0)]
  17. public SteamworksStatLibrary steamStats;
  18. public void Start(SteamworksStatLibrary steamStatsLibrary, bool debug = false)
  19. {
  20. this.debug = debug;
  21. if(steamStats == null) steamStats = steamStatsLibrary;
  22. if(broadcastStatsOnStart) BroadcastStats();
  23. Timer.ExecuteRealTimeNotDisposable(2000, () =>
  24. {
  25. if(listenForStatisticsEvents)
  26. {
  27. for (int i = 0; i < steamStats.library.Count; i++)
  28. {
  29. var stat = steamStats.library[i];
  30. if(stat.statType == SteamStatType.Integer) GenericEvents.StartListening($"{statisticsEvent}", SetStatInt);
  31. else if(stat.statType == SteamStatType.Float) GenericEvents.StartListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
  32. }
  33. }
  34. });
  35. }
  36. public void Stop()
  37. {
  38. Steamworks.SteamUserStats.StoreStats();
  39. if(listenForStatisticsEvents)
  40. {
  41. for (int i = 0; i < steamStats.library.Count; i++)
  42. {
  43. var stat = steamStats.library[i];
  44. if(stat.statType == SteamStatType.Integer) GenericEvents.StopListening($"{statisticsEvent}", SetStatInt);
  45. else if(stat.statType == SteamStatType.Float) GenericEvents.StopListeningForStringFloat($"{statisticsEvent}", SetStatFloat);
  46. }
  47. }
  48. }
  49. public SteamworksStatData GetSteamStatData(string statName)
  50. {
  51. for (int i = 0; i < steamStats.library.Count; i++)
  52. {
  53. if(steamStats.library[i].name == statName || steamStats.library[i].statName == statName) return steamStats.library[i];
  54. }
  55. //Debug.LogWarning($"Could not find stat with name \"{statName}\" in SteamStatLibrary");
  56. return null;
  57. }
  58. [Button("Print Stat")]
  59. public void PrintStat(string statName)
  60. {
  61. SteamworksStatData statData = GetSteamStatData(statName);
  62. if(statData == null) return;
  63. string value = "";
  64. if(statData.statType == SteamStatType.Integer) value += Steamworks.SteamUserStats.GetStatInt(statData.statName);
  65. else if(statData.statType == SteamStatType.Float) value += Steamworks.SteamUserStats.GetStatFloat(statData.statName);
  66. Debug.Log($"{statData.statName}: {value}");
  67. }
  68. [Button("Set Stat Integer")]
  69. public void SetStatInt(string statName, int newValue)
  70. {
  71. SteamworksStatData statData = GetSteamStatData(statName);
  72. if (statData == null) return;
  73. if (statData.statType != SteamStatType.Integer) return;
  74. Steamworks.SteamUserStats.SetStat(statData.statName, newValue);
  75. if(Steamworks.SteamUserStats.GetStatInt(statData.statName) != newValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks");
  76. else Steamworks.SteamUserStats.StoreStats();
  77. }
  78. [Button("Set Stat Float")]
  79. public void SetStatFloat(string statName, float newFloatValue)
  80. {
  81. SteamworksStatData statData = GetSteamStatData(statName);
  82. if (statData == null) return;
  83. if (statData.statType != SteamStatType.Float) return;
  84. Steamworks.SteamUserStats.SetStat(statData.statName, newFloatValue);
  85. if(Steamworks.SteamUserStats.GetStatFloat(statData.statName) != newFloatValue) Debug.LogWarning($"Stat \"{statData.statName}\" does not exist on Steamworks");
  86. else Steamworks.SteamUserStats.StoreStats();
  87. }
  88. public void BroadcastStats()
  89. {
  90. if(debug) Debug.Log($"Broadcasting Steam Stats ({steamStats.library.Count})");
  91. for (int i = 0; i < steamStats.library.Count; i++)
  92. {
  93. var stat = steamStats.library[i];
  94. if(stat.statType == SteamStatType.Integer)
  95. {
  96. int value = Steamworks.SteamUserStats.GetStatInt(stat.statName);
  97. if(value != 0)
  98. {
  99. if(debug) Debug.Log($"Broadcasting Steam stat event \"{stat.name}\" = {value}");
  100. GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
  101. }
  102. }
  103. else if(stat.statType == SteamStatType.Float)
  104. {
  105. float value = Steamworks.SteamUserStats.GetStatFloat(stat.statName);
  106. if(value != 0)
  107. {
  108. if(debug) Debug.Log($"Broadcasting Steam stat event \"{stat.name}\" = {value}");
  109. GenericEvents.Trigger(statisticsBroadcastEvent, stat.name, value);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }