Skip to content

Commit

Permalink
Merge pull request #86 from ninovanhooff/scoreboards-empty-result
Browse files Browse the repository at this point in the history
ADD PDResultSuccessEmpty
  • Loading branch information
ninovanhooff authored Nov 9, 2024
2 parents 2b7d58d + 50cec6c commit 5ef3109
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/playdate/scoreboards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ type
lastUpdated*: uint32
boards*: seq[PDBoard]

PDResultKind* = enum PDResultSuccess, PDResultError
PDResultKind* = enum
PDResultSuccess,
PDResultUnavailable,
## The operation completed successfully, but the response had no data
PDResultError,

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

PersonalBestCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
Expand All @@ -56,8 +61,10 @@ template invokeCallback(callbackSeqs, value, errorMessage, freeValue, builder: u
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))
if errorMessage == nil:
callback(PDResult[ResultType](kind: PDResultUnavailable))
else:
callback(PDResult[ResultType](kind: PDResultError, message: $errorMessage))
else:
try:
let built = builder
Expand Down Expand Up @@ -88,11 +95,13 @@ proc invokeBoardsListCallback(boardsList: PDBoardsListPtr, errorMessage: ConstCh
PDBoardsList(lastUpdated: boardsList.lastUpdated, boards: boardsSeq)

proc getPersonalBest*(this: ptr PlaydateScoreboards, boardID: string, callback: PersonalBestCallback): int32 {.discardable.} =
## Responds with PDResultUnavailable if no score exists for the current player.
privateAccess(PlaydateScoreboards)
privatePersonalBestCallbacks.insert(callback) # by inserting the callback at the start, it will be popped last: first in, first out
return this.getPersonalBestBinding(boardID.cstring, invokePersonalBestCallback)

proc addScore*(this: ptr PlaydateScoreboards, boardID: string, value: uint32, callback: AddScoreCallback): int32 {.discardable.} =
## Responds with PDResultUnavailable if the score was queued for later submission. Probably, Wi-Fi is not available.
privateAccess(PlaydateScoreboards)
privateAddScoreCallbacks.insert(callback) # by inserting the callback at the start, it will be popped last: first in, first out
return this.addScoreBinding(boardID.cstring, value.cuint, invokeAddScoreCallback)
Expand Down
1 change: 1 addition & 0 deletions tests/t_scoreboards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ proc execScoreboardTests*() =
case score.kind
of PDResultSuccess: echo $score.result
of PDResultError: echo $score.message
of PDResultUnavailable: echo "PDResultUnavailable"

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

0 comments on commit 5ef3109

Please sign in to comment.