-
Notifications
You must be signed in to change notification settings - Fork 3
/
crossgame.lua
56 lines (51 loc) · 1.85 KB
/
crossgame.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- Cross-game data reading module for the PlaydateSquad Achievements library.
if not (achievements and achievements.flag_is_playdatesquad_api) then
error("Achievements 'crossgame' module must be loaded after the base PlaydateSquad achievement library.")
end
local crossgame = {}
achievements.crossgame = crossgame
-- Returns whether a game has any listed achievement data.
crossgame.gamePlayed = function(game_id)
return playdate.file.isdir(achievements.paths.get_achievement_folder_root_path(game_id))
end
crossgame.listGames = function()
local games = {}
for _, path in ipairs(playdate.file.listFiles(achievements.paths.shared_data_root)) do
if string.sub(path, -1) == "/" then
local gameid = string.sub(path, 1, -2)
table.insert(games, gameid)
end
end
return games
end
---@param game_id string
---@return achievement_root
-- Returns the achievement data for the requested game if it exists. Otherwise returns false and a reason.
crossgame.getData = function(game_id)
if not crossgame.gamePlayed then
return false, "No achievement data for game '" .. game_id .. "' was found."
end
local data = json.decodeFile(achievements.paths.get_achievement_data_file_path(game_id))
-- Quick sanity check...
if not (data.libversion and data.specversion) then
return false, "Achievement file was found but not valid."
end
local completion_total = 0
local completion_obtained = 0
local keys = {}
for _, ach in ipairs(data.achievements) do
keys[ach.id] = ach
completion_total += score_value
if ach.granted_at then
completion_obtained += score_value
end
end
data.keyedAchievements = keys
data.completionPercentage = completion_obtained / completion_total
return data
end
crossgame.loadImage(game_id, filepath)
local image_path = achievements.paths.get_shared_images_path(game_id) .. filepath
local img, err = gfx.image.new(image_path)
return img, err
end