Merge games from the profile's itch.io collections into the library (deduped against owned games) so bookmarked free games show up

This commit is contained in:
Jose Falanga 2026-08-06 18:52:55 -03:00
parent 9cd4cb32e3
commit e9f25d104b
3 changed files with 106 additions and 23 deletions

View file

@ -387,6 +387,68 @@ func _get_owned_games() -> Array:
return res.get("items", [])
## Returns every game present in any of the profile's collections, as full
## Game dicts (CollectionGame embeds the whole game object). These aren't
## necessarily owned: the user may have bookmarked free games in a collection.
func get_collection_games() -> Array:
return await thread_group.exec(_get_collection_games)
func _get_collection_games() -> Array:
if not is_logged_in:
return []
var params := {"profileId": profile.get("user", {}).get("id", profile.get("id", 0))}
var res := await _rpc_call("Fetch.ProfileCollections", params)
if "error" in res:
logger.warn("Fetch.ProfileCollections failed: " + str(res["error"]))
return []
if res.get("stale", false):
logger.info("Collections are stale. Refreshing from itch.io.")
params["fresh"] = true
res = await _rpc_call("Fetch.ProfileCollections", params)
if "error" in res:
logger.warn("Fetch.ProfileCollections (fresh) failed: " + str(res["error"]))
return []
return await _fetch_collection_games(
profile.get("user", {}).get("id", profile.get("id", 0)),
res.get("items", [])
)
## Paginates through Fetch.Collection.Games for every collection, collecting the
## embedded game objects. Each collection can span multiple pages (cursor), and
## a page served from butlerd's local cache is re-issued fresh.
func _fetch_collection_games(profile_id: int, collections: Array) -> Array:
var games := []
for c in collections:
var collection: Dictionary = c
var collection_id: int = collection.get("id", 0)
var cursor: Variant = null
while true:
var params := {"profileId": profile_id, "collectionId": collection_id}
if cursor != null:
params["cursor"] = cursor
var res := await _rpc_call("Fetch.Collection.Games", params)
if "error" in res:
logger.warn("Fetch.Collection.Games failed for collection " + str(collection_id) + ": " + str(res["error"]))
break
if res.get("stale", false):
params["fresh"] = true
res = await _rpc_call("Fetch.Collection.Games", params)
if "error" in res:
logger.warn("Fetch.Collection.Games (fresh) failed for collection " + str(collection_id) + ": " + str(res["error"]))
break
for cg in res.get("items", []):
var collection_game: Dictionary = cg
var game: Dictionary = collection_game.get("game", {})
if not game.is_empty():
games.append(game)
cursor = res.get("nextCursor", null)
if cursor == null:
break
return games
## Returns every installed game (a "cave" in butlerd terminology).
func get_caves() -> Array:
return await thread_group.exec(_get_caves)