Files
playdate-kit/Sources/PlaydateKit/Scoreboards/Scoreboards.swift
T

92 lines
4.3 KiB
Swift
Raw Normal View History

2026-07-24 10:33:45 +02:00
internal import CPlaydate
/// The cached `playdate->scoreboards` C API table.
var scoreboardsAPI: UnsafePointer<playdate_scoreboards> { Playdate.scoreboardsAPI.unsafelyUnwrapped }
2026-07-24 10:33:45 +02:00
2026-09-18 13:15:08 +00:00
/// Online leaderboards. Requests return `false` if they could not start; completions
/// fail with `PlaydateError` (the C error message). One pending completion per
/// operation: a repeat request replaces it. C results are copied and freed.
public enum Scoreboards {}
2026-07-24 10:33:45 +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
2026-09-18 13:15:08 +00:00
/// Submits `value` to `boardID`; `completion` gets the resulting score.
2026-07-24 10:33:45 +02:00
@discardableResult
public static func addScore(boardID: String, value: UInt32,
completion: @escaping (Result<Score, PlaydateError>) -> Void) -> Bool {
2026-07-24 10:33:45 +02:00
addScoreCompletion = completion
2026-09-18 13:15:08 +00:00
return boardID.withCString { cBoardID in
scoreboardsAPI.pointee.addScore.unsafelyUnwrapped(cBoardID, value, { score, errorMessage in
let completion = Scoreboards.addScoreCompletion
Scoreboards.addScoreCompletion = nil
completion?(Scoreboards.result(score, errorMessage))
2026-07-24 10:33:45 +02:00
}) != 0
}
}
2026-09-18 13:15:08 +00:00
/// Fetches the current player's best score on `boardID`.
2026-07-24 10:33:45 +02:00
@discardableResult
public static func getPersonalBest(boardID: String,
completion: @escaping (Result<Score, PlaydateError>) -> Void) -> Bool {
2026-07-24 10:33:45 +02:00
personalBestCompletion = completion
2026-09-18 13:15:08 +00:00
return boardID.withCString { cBoardID in
scoreboardsAPI.pointee.getPersonalBest.unsafelyUnwrapped(cBoardID, { score, errorMessage in
let completion = Scoreboards.personalBestCompletion
Scoreboards.personalBestCompletion = nil
completion?(Scoreboards.result(score, errorMessage))
2026-07-24 10:33:45 +02:00
}) != 0
}
}
2026-09-18 13:15:08 +00:00
/// Fetches the game's boards.
2026-07-24 10:33:45 +02:00
@discardableResult
public static func getScoreboards(completion: @escaping (Result<BoardsList, PlaydateError>) -> Void) -> Bool {
2026-07-24 10:33:45 +02:00
boardsCompletion = completion
return scoreboardsAPI.pointee.getScoreboards.unsafelyUnwrapped({ boards, errorMessage in
let completion = Scoreboards.boardsCompletion
Scoreboards.boardsCompletion = nil
2026-07-24 10:33:45 +02:00
guard let boards else {
completion?(.failure(PlaydateError(cString: errorMessage)))
2026-07-24 10:33:45 +02:00
return
}
let list = BoardsList(boards.pointee)
scoreboardsAPI.pointee.freeBoardsList.unsafelyUnwrapped(boards)
2026-07-24 10:33:45 +02:00
completion?(.success(list))
}) != 0
}
2026-09-18 13:15:08 +00:00
/// Fetches the scores on `boardID`.
2026-07-24 10:33:45 +02:00
@discardableResult
public static func getScores(boardID: String,
completion: @escaping (Result<ScoresList, PlaydateError>) -> Void) -> Bool {
2026-07-24 10:33:45 +02:00
scoresCompletion = completion
2026-09-18 13:15:08 +00:00
return boardID.withCString { cBoardID in
scoreboardsAPI.pointee.getScores.unsafelyUnwrapped(cBoardID, { scores, errorMessage in
let completion = Scoreboards.scoresCompletion
Scoreboards.scoresCompletion = nil
2026-07-24 10:33:45 +02:00
guard let scores else {
completion?(.failure(PlaydateError(cString: errorMessage)))
2026-07-24 10:33:45 +02:00
return
}
let list = ScoresList(scores.pointee)
scoreboardsAPI.pointee.freeScoresList.unsafelyUnwrapped(scores)
2026-07-24 10:33:45 +02:00
completion?(.success(list))
}) != 0
}
}
private static func result(_ score: UnsafeMutablePointer<PDScore>?,
_ errorMessage: UnsafePointer<CChar>?) -> Result<Score, PlaydateError> {
2026-07-24 10:33:45 +02:00
guard let score else {
return .failure(PlaydateError(cString: errorMessage))
2026-07-24 10:33:45 +02:00
}
let value = Score(score.pointee)
scoreboardsAPI.pointee.freeScore.unsafelyUnwrapped(score)
2026-07-24 10:33:45 +02:00
return .success(value)
}
}