Skip to content

Commit

Permalink
Use PDResult for scoreboard callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Oct 25, 2024
1 parent a28d5cb commit 7e91b32
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
40 changes: 22 additions & 18 deletions src/playdate/scoreboards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ type
lastUpdated*: uint32
boards*: seq[PDBoard]

PersonalBestCallback* = proc(score: PDScore, errorMessage: string) {.raises: [].}
AddScoreCallback* = proc(score: PDScore, errorMessage: string) {.raises: [].}
BoardsListCallback* = proc(boards: PDBoardsList, errorMessage: string) {.raises: [].}
ScoresCallback* = proc(scores: PDScoresList, errorMessage: string) {.raises: [].}
PDResultKind* = enum PDResultSuccess, PDResultError

PDResult*[T] = object
case kind*: PDResultKind
of PDResultSuccess: result*: T
of PDResultError: message*: string

PersonalBestCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
AddScoreCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
BoardsListCallback* = proc(result: PDResult[PDBoardsList]) {.raises: [].}
ScoresCallback* = proc(result: PDResult[PDScoresList]) {.raises: [].}

var
# The sdk callbacks unfortunately don't provide a userdata field to tag the callback with eg. the boardID
Expand All @@ -46,20 +53,17 @@ var
privateBoardsListCallbacks = newSeq[BoardsListCallback]()

template invokeCallback(callbackSeqs, value, errorMessage, freeValue, builder: untyped) =
let callback = callbackSeqs.pop()
if value == nil and errorMessage == nil:
callback(default(typeof(builder)), "Playdate-nim: No value provided for callback")
return

if value == nil:
callback(default(typeof(builder)), $errorMessage)
return

try:
let nimObj = builder
callback(nimObj, $errorMessage)
finally:
freeValue(value)
type ResultType = typeof(builder)
let callback = callbackSeqs.pop()
if value == nil:
let message = if errorMessage == nil: "Playdate-nim: No value provided for callback" else: $errorMessage
callback(PDResult[ResultType](kind: PDResultError, message: message))
else:
try:
let built = builder
callback(PDResult[ResultType](kind: PDResultSuccess, result: built))
finally:
freeValue(value)

proc scoreBuilder(score: PDScoreRaw | PDScorePtr): PDScore =
PDSCore(value: score.value.uint32, rank: score.rank.uint32, player: $score.player)
Expand Down
12 changes: 7 additions & 5 deletions tests/t_scoreboards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ proc execScoreboardTests*() =
suite "Scoreboards API verification":

test "getPersonalBest":
playdate.scoreboards.getPersonalBest("some_board") do (score: PDScore, errorMessage: string) -> void:
discard
playdate.scoreboards.getPersonalBest("some_board") do (score: PDResult[PDScore]) -> void:
case score.kind
of PDResultSuccess: echo $score.result
of PDResultError: echo $score.message

test "addScore":
playdate.scoreboards.addScore("some_board", 123) do (score: PDScore, errorMessage: string) -> void:
playdate.scoreboards.addScore("some_board", 123) do (score: PDResult[PDScore]) -> void:
discard

test "getScoreboards":
playdate.scoreboards.getScoreboards() do (boards: PDBoardsList, errorMessage: string) -> void:
playdate.scoreboards.getScoreboards() do (boards: PDResult[PDBoardsList]) -> void:
discard

test "getScores":
playdate.scoreboards.getScores("some_board") do (boards: PDScoresList, errorMessage: string) -> void:
playdate.scoreboards.getScores("some_board") do (scores: PDResult[PDScoresList]) -> void:
discard

0 comments on commit 7e91b32

Please sign in to comment.