2026-07-24 10:33:45 +02:00
|
|
|
internal import CPlaydate
|
|
|
|
|
|
2026-07-25 12:55:53 +02:00
|
|
|
/// The cached `playdate->scoreboards` C API table.
|
2026-07-25 12:39:32 +02:00
|
|
|
var scoreboardsAPI: UnsafePointer<playdate_scoreboards> { Playdate.scoreboardsAPI.unsafelyUnwrapped }
|
2026-07-24 10:33:45 +02:00
|
|
|
|
2026-07-24 11:10:25 +02:00
|
|
|
/// The scoreboards API for games with online leaderboards.
|
2026-07-25 12:55:53 +02:00
|
|
|
///
|
|
|
|
|
/// The C callbacks carry no userdata, so one completion per operation kind
|
|
|
|
|
/// is tracked at a time; starting a second request of the same kind before
|
|
|
|
|
/// the first completes replaces the stored completion.
|
2026-07-24 11:10:25 +02:00
|
|
|
public enum Scoreboards {}
|
2026-07-24 10:33:45 +02:00
|
|
|
|
2026-07-24 11:10:25 +02:00
|
|
|
extension Scoreboards {
|
|
|
|
|
nonisolated(unsafe) private static var addScoreCompletion: ((Result<Score, PlaydateError>) -> Void)?
|
|
|
|
|
nonisolated(unsafe) private static var personalBestCompletion: ((Result<Score, PlaydateError>) -> Void)?
|
|
|
|
|
nonisolated(unsafe) private static var boardsCompletion: ((Result<BoardsList, PlaydateError>) -> Void)?
|
|
|
|
|
nonisolated(unsafe) private static var scoresCompletion: ((Result<ScoresList, PlaydateError>) -> Void)?
|
2026-07-24 10:33:45 +02:00
|
|
|
|
|
|
|
|
/// Submits a score to the board. Returns `false` if the request could
|
|
|
|
|
/// not be started.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public static func addScore(boardID: String, value: UInt32,
|
2026-07-24 11:10:25 +02:00
|
|
|
completion: @escaping (Result<Score, PlaydateError>) -> Void) -> Bool {
|
2026-07-24 10:33:45 +02:00
|
|
|
addScoreCompletion = completion
|
|
|
|
|
return boardID.withPlaydateCString { cBoardID in
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.addScore.unsafelyUnwrapped(cBoardID, value, { score, errorMessage in
|
2026-07-24 11:10:25 +02:00
|
|
|
let completion = Scoreboards.addScoreCompletion
|
|
|
|
|
Scoreboards.addScoreCompletion = nil
|
|
|
|
|
completion?(Scoreboards.result(score, errorMessage))
|
2026-07-24 10:33:45 +02:00
|
|
|
}) != 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetches the current player's best score on the board.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public static func getPersonalBest(boardID: String,
|
2026-07-24 11:10:25 +02:00
|
|
|
completion: @escaping (Result<Score, PlaydateError>) -> Void) -> Bool {
|
2026-07-24 10:33:45 +02:00
|
|
|
personalBestCompletion = completion
|
|
|
|
|
return boardID.withPlaydateCString { cBoardID in
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.getPersonalBest.unsafelyUnwrapped(cBoardID, { score, errorMessage in
|
2026-07-24 11:10:25 +02:00
|
|
|
let completion = Scoreboards.personalBestCompletion
|
|
|
|
|
Scoreboards.personalBestCompletion = nil
|
|
|
|
|
completion?(Scoreboards.result(score, errorMessage))
|
2026-07-24 10:33:45 +02:00
|
|
|
}) != 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetches the list of the game's boards.
|
|
|
|
|
@discardableResult
|
2026-07-24 11:10:25 +02:00
|
|
|
public static func getScoreboards(completion: @escaping (Result<BoardsList, PlaydateError>) -> Void) -> Bool {
|
2026-07-24 10:33:45 +02:00
|
|
|
boardsCompletion = completion
|
2026-07-25 07:23:19 +02:00
|
|
|
return scoreboardsAPI.pointee.getScoreboards.unsafelyUnwrapped({ boards, errorMessage in
|
2026-07-24 11:10:25 +02:00
|
|
|
let completion = Scoreboards.boardsCompletion
|
|
|
|
|
Scoreboards.boardsCompletion = nil
|
2026-07-24 10:33:45 +02:00
|
|
|
guard let boards else {
|
2026-07-24 11:10:25 +02:00
|
|
|
completion?(.failure(PlaydateError(cString: errorMessage)))
|
2026-07-24 10:33:45 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let list = BoardsList(boards.pointee)
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.freeBoardsList.unsafelyUnwrapped(boards)
|
2026-07-24 10:33:45 +02:00
|
|
|
completion?(.success(list))
|
|
|
|
|
}) != 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fetches the scores on the board.
|
|
|
|
|
@discardableResult
|
|
|
|
|
public static func getScores(boardID: String,
|
2026-07-24 11:10:25 +02:00
|
|
|
completion: @escaping (Result<ScoresList, PlaydateError>) -> Void) -> Bool {
|
2026-07-24 10:33:45 +02:00
|
|
|
scoresCompletion = completion
|
|
|
|
|
return boardID.withPlaydateCString { cBoardID in
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.getScores.unsafelyUnwrapped(cBoardID, { scores, errorMessage in
|
2026-07-24 11:10:25 +02:00
|
|
|
let completion = Scoreboards.scoresCompletion
|
|
|
|
|
Scoreboards.scoresCompletion = nil
|
2026-07-24 10:33:45 +02:00
|
|
|
guard let scores else {
|
2026-07-24 11:10:25 +02:00
|
|
|
completion?(.failure(PlaydateError(cString: errorMessage)))
|
2026-07-24 10:33:45 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let list = ScoresList(scores.pointee)
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.freeScoresList.unsafelyUnwrapped(scores)
|
2026-07-24 10:33:45 +02:00
|
|
|
completion?(.success(list))
|
|
|
|
|
}) != 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static func result(_ score: UnsafeMutablePointer<PDScore>?,
|
2026-07-24 11:10:25 +02:00
|
|
|
_ errorMessage: UnsafePointer<CChar>?) -> Result<Score, PlaydateError> {
|
2026-07-24 10:33:45 +02:00
|
|
|
guard let score else {
|
2026-07-24 11:10:25 +02:00
|
|
|
return .failure(PlaydateError(cString: errorMessage))
|
2026-07-24 10:33:45 +02:00
|
|
|
}
|
|
|
|
|
let value = Score(score.pointee)
|
2026-07-25 07:23:19 +02:00
|
|
|
scoreboardsAPI.pointee.freeScore.unsafelyUnwrapped(score)
|
2026-07-24 10:33:45 +02:00
|
|
|
return .success(value)
|
|
|
|
|
}
|
|
|
|
|
}
|