138 lines
4.2 KiB
GDScript
138 lines
4.2 KiB
GDScript
extends Node
|
|
|
|
### SISETMARE SU RAILS IL FATTORE DI CONVERSIONE DA UNITA' A KM!!!
|
|
### ORA HO LASCIATO 1:1 PER TEST
|
|
|
|
#Achievements and stats must be published on Steam even if game is not yet released
|
|
#if you have an error setting achievements or stats in debug mode remember to open your steam client
|
|
#with jmp games account or another account who bought this game. If error persist try to clos and reopen steam client.
|
|
const KNOWN := [
|
|
"stat_time_played",
|
|
"stat_distance_km",
|
|
"stat_stop_number",
|
|
"stat_photo_number",
|
|
"stat_toottoot_number",
|
|
]
|
|
const TIME_PLAYED_STAT: String = "stat_time_played"
|
|
const DISTANCE_KM_STAT: String = "stat_distance_km"
|
|
const STATS_STORE_INTERVAL_SECONDS: int = 60
|
|
|
|
var _pending_time_played_seconds: float = 0.0
|
|
var _pending_distance_km: float = 0.0
|
|
var _stats_store_timer: float = 0.0
|
|
|
|
#how it works:
|
|
#func _on_match_finished(score: int) -> void:
|
|
# StatsManager.add_int("stat_time_played", 1)
|
|
# StatsManager.set_int("stat_total_score", StatsManager.get_int("stat_total_score") + score)
|
|
# StatsManager.store() #only one push at the end of the game
|
|
# if StatsManager.get_int("stat_time_played") >= 10:
|
|
# AchievementManager.unlock("ACH_PLAY_10_HOURS") #it call storeStats()
|
|
|
|
|
|
func _ready() -> void:
|
|
if not SteamManager.is_on_steam:
|
|
set_process(false)
|
|
return
|
|
|
|
Steam.user_stats_stored.connect(_on_stats_stored)
|
|
|
|
func _process(delta: float) -> void:
|
|
_pending_time_played_seconds += delta
|
|
_stats_store_timer += delta
|
|
if _stats_store_timer >= STATS_STORE_INTERVAL_SECONDS:
|
|
_flush_periodic_stats(false)
|
|
|
|
func _exit_tree() -> void:
|
|
_flush_periodic_stats(true)
|
|
|
|
func get_int(stat: String) -> int:
|
|
if not SteamManager.is_on_steam:
|
|
return 0
|
|
return Steam.getStatInt(stat)
|
|
|
|
func get_float(stat: String) -> float:
|
|
if not SteamManager.is_on_steam:
|
|
return 0.0
|
|
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 SteamManager.is_on_steam:
|
|
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:
|
|
if not SteamManager.is_on_steam:
|
|
return
|
|
set_int(stat, get_int(stat) + amount)
|
|
|
|
func add_distance_km(distance_km: float) -> void:
|
|
if not SteamManager.is_on_steam:
|
|
return
|
|
if distance_km <= 0.0:
|
|
return
|
|
|
|
_pending_distance_km += distance_km
|
|
|
|
func update_avg_rate(stat: String, count_this_session: float, session_length: float) -> void:
|
|
if not SteamManager.is_on_steam:
|
|
return
|
|
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 SteamManager.is_on_steam:
|
|
return
|
|
if not Steam.storeStats():
|
|
push_error("storeStats failed; data saved locally")
|
|
|
|
#periodic stats incrementation; store data every 60 secs
|
|
func _flush_periodic_stats(force_store: bool) -> void:
|
|
if not SteamManager.is_on_steam:
|
|
_pending_time_played_seconds = 0.0
|
|
_pending_distance_km = 0.0
|
|
_stats_store_timer = 0.0
|
|
return
|
|
|
|
var has_changes: bool = false
|
|
|
|
var whole_seconds: int = int(floor(_pending_time_played_seconds))
|
|
if whole_seconds > 0:
|
|
_pending_time_played_seconds -= float(whole_seconds)
|
|
add_int(TIME_PLAYED_STAT, whole_seconds)
|
|
has_changes = true
|
|
|
|
var whole_km: int = int(floor(_pending_distance_km))
|
|
if whole_km > 0:
|
|
_pending_distance_km -= float(whole_km)
|
|
add_int(DISTANCE_KM_STAT, whole_km)
|
|
has_changes = true
|
|
|
|
_stats_store_timer = 0.0
|
|
if has_changes and (force_store or whole_seconds > 0 or whole_km > 0):
|
|
store()
|
|
|
|
#Development only: reset stats
|
|
func reset_all(include_achievements: bool = false) -> void:
|
|
if not SteamManager.is_on_steam:
|
|
return
|
|
if !Steam.resetAllStats(include_achievements):
|
|
push_error("resetAllStats failed")
|
|
|
|
func _on_stats_stored(_game: int, result: int) -> void:
|
|
if !result:
|
|
print("stats not ready: (result %d)" % [result])
|