SteamworksStats.cs 4.7 KB

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