add steam overlay, achievements and stats managers
This commit is contained in:
62
core/steam_manager/stats_manager.gd
Normal file
62
core/steam_manager/stats_manager.gd
Normal file
@@ -0,0 +1,62 @@
|
||||
extends Node
|
||||
|
||||
#Achievement e statistiche devono essere pubblicati live nel backend
|
||||
#Steamworks anche se il gioco non è ancora uscito altrimenti le chiamate ritornano errore
|
||||
const KNOWN := [
|
||||
"stat_games_played",
|
||||
"stat_total_score",
|
||||
"stat_distance_km",
|
||||
]
|
||||
|
||||
#how it works:
|
||||
#func _on_match_finished(score: int) -> void:
|
||||
# Stats.add_int("stat_games_played", 1)
|
||||
# Stats.set_int("stat_total_score", Stats.get_int("stat_total_score") + score)
|
||||
# Stats.store() #only one push at the end of the game
|
||||
# if Stats.get_int("stat_games_played") >= 10:
|
||||
# Achievements.unlock("ACH_PLAY_10_GAMES") #it call storeStats()
|
||||
|
||||
func _ready() -> void:
|
||||
if not SteamManager.is_on_steam:
|
||||
return
|
||||
|
||||
Steam.user_stats_stored.connect(_on_stats_stored)
|
||||
|
||||
func get_int(stat: String) -> int:
|
||||
return Steam.getStatInt(stat)
|
||||
|
||||
func get_float(stat: String) -> float:
|
||||
return Steam.getStatFloat(stat)
|
||||
|
||||
func set_int(stat: String, value: int) -> void:
|
||||
if not KNOWN.has(stat):
|
||||
push_warning("Stat not found: %s" % stat)
|
||||
return
|
||||
if not Steam.setStatInt(stat, value):
|
||||
push_error("setStatInt failed (incremental with value less than current value? is published on steam?): %s" % stat)
|
||||
|
||||
func set_float(stat: String, value: float) -> void:
|
||||
if not KNOWN.has(stat):
|
||||
push_warning("Stat not found: %s" % stat)
|
||||
return
|
||||
if not Steam.setStatFloat(stat, value):
|
||||
push_error("setStatFloat failed: %s" % stat)
|
||||
|
||||
func add_int(stat: String, amount: int = 1) -> void:
|
||||
set_int(stat, get_int(stat) + amount)
|
||||
|
||||
func update_avg_rate(stat: String, count_this_session: float, session_length: float) -> void:
|
||||
Steam.updateAvgRateStat(stat, count_this_session, session_length)
|
||||
|
||||
#Send all data to server; call it at the end of the game of when exit
|
||||
func store() -> void:
|
||||
if not Steam.storeStats():
|
||||
push_error("storeStats failed; data saved locally")
|
||||
|
||||
#Development only: reset stats
|
||||
func reset_all(include_achievements: bool = false) -> void:
|
||||
Steam.resetAllStats(include_achievements)
|
||||
Steam.storeStats()
|
||||
|
||||
func _on_stats_stored(_game: int, result: int) -> void:
|
||||
print("Stats save on server, result=%d" % result)
|
||||
Reference in New Issue
Block a user