Skip to content

Latest commit

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Simple Leaderboard Function

This is a basic online leaderboard function made with Supabase. This code is mainly meant to be used with our project, Meltdown.

Usage

Use any API clients, or curl like the following:

curl -X POST <PUBLIC_URL>/functions/v1/leaderboard \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <LOCAL_ANON_KEY>" \
-d '{"playerScore": 4200}'# payload body in JSON

Leaderboard API

Base URL: /functions/v1/leaderboard

All responses are JSON. All error responses follow the shape { "error": string }.


Endpoints

GET / — Get top N players

Returns the top players on the leaderboard, ranked by their best (highest) score.

Query Parameters

ParameterTypeRequiredDefaultConstraintsDescription
countintegerNo5Min: 1, Max: 50Number of top players to return

Responses

StatusDescription
200Success
500Internal server error

200 Response

{
"count": 5,
"top_players": [
{
"player_name": "Alice",
"best_score": "98000"
}
]
}

top_players entry shape

FieldTypeDescription
player_namestringPlayer's name
best_scorestringPlayer's all-time highest score (numeric string)

GET /?playerName= — Get scores for a player

Returns a list of score entries for a specific player, ordered newest first. Each entry includes the score's global rank across all leaderboard entries.

Query Parameters

ParameterTypeRequiredDefaultConstraintsDescription
playerNamestringYesMax: 64 charsThe player's name to look up
countintegerNo5Min: 1, Max: 50Number of score entries to return

Responses

StatusDescription
200Success
404Player not found
500Internal server error

200 Response

{
"player_name": "Alice",
"count": 5,
"total_entries": 3,
"scores": [
{
"id": 12,
"score": "98000",
"rank": 1,
"created_at": "2026-03-07T09:00:00Z"
}
]
}

scores entry shape

FieldTypeDescription
idnumberUnique score entry ID
scorestringScore value as a numeric string
ranknumberGlobal rank of this score across all players
created_atstringISO 8601 timestamp of when the score was recorded

POST /?playerName= — Submit a score

Inserts a new score entry for a player.

Query Parameters

ParameterTypeRequiredConstraintsDescription
playerNamestringYesMax: 64 charsThe player's name

Request Body (application/json)

FieldTypeRequiredDescription
playerScorenumber | stringYesThe score to record. Use a number for values ≤ 9007199254740991. Use a numeric string for larger values to avoid precision loss.
{ "playerScore": 15000 }
{ "playerScore": "99999999999999999999" }

Responses

StatusDescription
201Score created successfully
400Validation error
500Internal server error

201 Response

{
"data": {
"id": 42,
"player_name": "Alice",
"score": "15000",
"created_at": "2026-03-07T11:00:00Z"
}
}

400 Error cases

CauseError message
playerName missing or empty`playerName` is required and must be a non-empty string.
playerName too long`playerName` must not exceed 64 characters.
playerScore missing or wrong type`playerScore` is required and must be a number or numeric string.
playerScore is a float`playerScore` must be an integer, not a float.
playerScore is a number above MAX_SAFE_INTEGER`playerScore` exceeds Number.MAX_SAFE_INTEGER. Pass it as a numeric string instead.
playerScore is outside PostgreSQL bigint range`playerScore` is outside the PostgreSQL bigint range.

PATCH /?playerName= — Rename a player

Renames a player across all their score entries.

Query Parameters

ParameterTypeRequiredConstraintsDescription
playerNamestringYesMax: 64 charsThe player's current name

Request Body (application/json)

FieldTypeRequiredConstraintsDescription
newPlayerNamestringYesMax: 64 charsThe new name for the player
{ "newPlayerName": "Bob" }

Responses

StatusDescription
200Player successfully renamed
400Validation error
404Player not found
409New name is already taken
500Internal server error

200 Response

{
"message": "Player \"Alice\" successfully renamed to \"Bob\".",
"updated_entries": 3
}

400 Error cases

CauseError message
playerName missing or empty`playerName` is required and must be a non-empty string.
Either name exceeds 64 characters`playerName` must not exceed 64 characters.
Body is missing or not valid JSONRequest body is missing or is not valid JSON
newPlayerName missing or empty`newPlayerName` is required and must be a non-empty string.
newPlayerName is the same as playerName`newPlayerName` must be different from the current player name.

DELETE /?playerName= — Remove a player

Permanently deletes a player and all their score entries.

Query Parameters

ParameterTypeRequiredConstraintsDescription
playerNamestringYesMax: 64 charsThe name of the player to remove

Responses

StatusDescription
200Player successfully removed
400Validation error
404Player not found
500Internal server error

200 Response

{
"message": "Player \"Alice\" and all their scores have been removed.",
"deleted_entries": 3
}

400 Error cases

CauseError message
playerName missing or empty`playerName` query param is required.
playerName too long`playerName` must not exceed 64 characters.

Common error shapes

404 Not Found

{ "error": "Player \"Alice\" not found." }

409 Conflict(PATCH only)

{ "error": "Player name \"Bob\" is already taken." }

500 Internal Server Error

{ "error": "Unexpected error message" }

Supabase Project Commands

These are some commonly used commands for working with Supabase.

# login
supabase login
# db schema update
supabase db diff -f name_of_the_change
# start supabase project locally
supabase start
# list access keys
supabase secrets list
# clear all local db
supabase db reset
# apply the db migration locally
supabase migration up
# serve the edge functions locally with logs
supabase functions serve
# deploy the database to production
supabase db push

About

simple leaderboard function

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages