Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD PDResultSuccessEmpty #86

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading