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
Changes from 1 commit
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
12 changes: 9 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,
PDResultSuccessEmpty
ninovanhooff marked this conversation as resolved.
Show resolved Hide resolved
## The operation completed successfully, but the response had no data
PDResultError,

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

PersonalBestCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
Expand All @@ -56,8 +61,7 @@ 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))
callback(PDResult[ResultType](kind: PDResultSuccessEmpty))
else:
try:
let built = builder
Expand Down Expand Up @@ -88,11 +92,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 PDResultSuccessEmpty 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 PDResultSuccessEmpty if the score was qeued for later submission. Probably, Wi-Fi is not available.
ninovanhooff marked this conversation as resolved.
Show resolved Hide resolved
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
Loading