/api
(after your host address)
http://
but whe connecting to WS endpoints you'll use ws://
. The secure version of these protocols are https://
and wss://
, respectively.
Refer to this in addition to the Swagger docs to have a better idea on how to utilize the endpoints (and what to expect).
- Users will register via
/users/register
, they will receive a token sent via email - Verify the account via
/users/verify-email/{token}
, otherwise they won't be able to log in. - Login via
/users/login
to obtain an access token and a refresh token- Access Token:
- This will be your authorization tool, in every authenticated endpoint include a header with the following format
"Authorization": "Bearer <JWT_ACCESS_TOKEN>"
- Access tokens have a really short lifespan, when it expires you must acquire a new one (see Refresh Token)
- ATTENTION: For WebSocket endpoints, since you cannot attach
"Authorization"
headers to your WebSocket connection, we will "smuggle" the token inside"Sec-WebSocket-Protocol"
header instead. In Javascript and JS-based frameworks this means adding a["access_token|YOUR_TOKEN_HERE"]
to the second argument of thenew WebSocket()
call (first argument is the WebSocket URL).
- This will be your authorization tool, in every authenticated endpoint include a header with the following format
- Refresh Token:
- This token has a much longer life span, and is used to obtain new access tokens via
/users/refresh/
- Make sure to store this token securely (
HttpOnly
cookies) to prevent exposure via JavaScript injection or XSS.
- This token has a much longer life span, and is used to obtain new access tokens via
- Access Token:
ALTERNATIVELY: Send a POST request to /api/users/guest
to get access_token
and refresh_token
of a guest account. Guest accounts cannot change display name, password. The rest behaves as normal.
- If forgotten password, send the user's email to
/users/forgot-password
and they'll receive an email with a token - Supply the token and a new password to
/users/reset-password
to change the account's password. Changing the password will immediatey invalidate ALL access tokens and refresh tokens.
- Users can send a GET, PATCH, DELETE to
/users/me/
to read, update (change display name only), and delete their account, respectively. - Access tokens are stateless and can't be disabled individually, so log out via
/users/logout/
to invalidate the given refresh token and prevent it from generating any more access tokens.
- To start queueing for a match, join the WebSocket endpoint
/game/queue
(unranked) or/game/ranked-queue
(ranked).- These two are pretty much the same, except ranked games are matched based on rating proximity and give rating changes.
- The problems distribution in unranked matches are based on appearance chances, but in ranked the distribution of problemsare predetermined (check .env settings)
- Once a match is found, a WebSocket JSON object of
type: "match_found"
will be sent to you. Insidedata
includes information like your"match_id"
and opponent details. - Use the mentioned
"match_id"
to connect to the WebSocket of the game e.g./game/play/{match_id}
- While inside the game websocket, here are the messages you'll receive:
type: "game_state"
: sent on join/query; contains information about the current state of the game (you and your opponent)type: "problem"
: sent on join/when your current problem is solved; contains description of the current problem you must solvetype: "submission_result"
: sent after you submit your code; contains the result of the execution of your code on hidden test cases (and errors if there are any)type: "match_end"
: sent when match ends; contains the final information about the match like winner, rating changes, etc.type: "error"
: sent when your messages causes an error; contains error messagetype: "chat"
: sent when you or your opponent sends a message.
- And here are the messages you can send, note that after the game finishes, the server won't accept any more messages (even though WS is still open):
type: "submit"
: used to submit your code for execution. Inside your"data"
property:code: string
: your code string (includes boilerplate)
type: "forfeit"
: used to forfeit the matchtype: "query"
: used to fetch current match data.type: "ability"
: used to signal a buy/use of abilities. Inside your"data"
property:action: string
: eitherbuy
oruse
ability_id: string
: the ID string of the ability
type: "chat"
: used to send a message to your opponent in the game room. Inside your"data"
property:message: string
: the message you want to send
- The backend introduces the
GET
endpoint/game/current-game
to get the authorized user's current match (if Any). - Hence, I suggest the frontend performs this fetch every visit and "throws" the player back into the game in progress if a game is found.
- Check the RoomSettings object to determine how your room creation settings UI will look like.
- Include those settings as a JSON body dict inside your
POST
request to/rooms/create
for a public room or/rooms/create?is_public=false
for a private room. If successful, your response will contain a"room_code"
. User is automatically set as host of this room. - To update the current room's settings, send the same JSON dict to your
PATCH
request to/rooms/{room_code}/settings
. Only the host can change the room settings. - To fetch information about the room (for any reason), perform a
GET
request to/rooms/{room_code}
as any authorized user.
- Connect to the lobby via the
/rooms/lobby
WebSocket endpoint - You'll periodically receive a
type: "room_list"
JSON message with the arrayrooms
containing all the available public rooms (even full ones so you can customize how you want to render full rooms). Data contained in each array object contains room code, host username/display_name, room settings and current player count.
- Connect to a room WebSocket at
/rooms/{room_code}
. - When inside, you may receive the following JSON objects:
type: "room_state"
: sent on join; contains information about the current room (and room settings)type: "game_started"
: sent on game start; inside"data"
you'll find thegame_id
, join and participate like in section 5.3type: "chat"
: chat between room members; inside"data"
you'll find themessage
type: "settings_updated"
: sent on room updates: contain information about the current room settingstype: "error"
: sent when your messages causes an error; contains error message
- JSON objects you can send:
type: "toggle_ready"
: toggles your ready state.type: "start_game"
: start the game as the host; both players must be ready before the host starts the game.type: "chat"
: used to send a message to your opponent in the room. Inside your"data"
property:message: string
: the message you want to send
Hence it is advised the frontend keeps the room WebSocket alive during game as well and have the players return to the room screen after finishing the match e.g.
"match_end"
event