Computer Science & Software Engineering
Game API
- Building Leaderboards with CRUD
- Two Types of Leaderboards
- Setup Steps
- HTTP Methods (CRUD Operations)
- Interactive: API Restaurant Simulator
- Quick Reference: CRUD Operations
Building Leaderboards with CRUD
Learn to implement dynamic and elementary leaderboard systems
Two Types of Leaderboards
- Real-time updates as players improve
- Tracks progress over time
- Uses UPDATE to modify scores
- Best for: Games with multiple attempts
- One-time submission per user
- Fixed rankings after completion
- Uses POST only to submit
- Best for: Quizzes, competitions, challenges
Setup Steps
- Log in to your account at pages.opencodingsociety.com/login
- Play the game! Then press
escto save your score - Toggle the leaderboard. Choose
Dynamic Leaderboardto see your saved score ranked!
HTTP Methods (CRUD Operations)
Purpose: Retrieve data from the server. Think of this as asking the database for your stats.
Use case: Displaying current leaderboard rankings, checking user scores, loading game state.
View Code Example
const base =
window.javaBackendUrl ||
(location.hostname === 'localhost' ? 'http://localhost:8585' : javaURI);
const res = await fetch(
`${base.replace(/\/$/, '')}/api/events/SCORE_COUNTER`,
{
...fetchOptions,
method: 'GET'
}
);
Purpose: Send data to create a new resource. This adds you to the database initially.
Use case: First-time user registration, initial score submission, creating new game entry.
View Code Example
const endpoint = '/api/events/ELEMENTARY_LEADERBOARD';
console.log('POST endpoint:', endpoint);
const base =
window.javaBackendUrl ||
(location.hostname === 'localhost' ? 'http://localhost:8585' : javaURI);
const url = `${base.replace(/\/$/, '')}${endpoint}`;
console.log('Full URL:', url);
// Create payload matching Java backend AlgorithmicEvent structure
const requestBody = {
payload: {
user: name,
score: score,
gameName: this.gameName
}
};
console.log('Payload:', JSON.stringify(requestBody));
// POST to backend - using fetchOptions for proper authentication
const res = await fetch(
url,
{
...fetchOptions,
method: 'POST',
headers: {
...fetchOptions?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
}
);
Purpose: Remove data from the server. Permanently delete a user's entry or stats.
Use case: User requests account deletion, removing invalid entries, clearing old data, resetting progress.
View Code Example
const base =
window.javaBackendUrl ||
(location.hostname === 'localhost' ? 'http://localhost:8585' : javaURI);
const url = `${base.replace(/\/$/, '')}/api/events/ELEMENTARY_LEADERBOARD/${id}`;
console.log('DELETE URL:', url);
// DELETE from backend - using fetchOptions for proper authentication
const res = await fetch(
url,
{
...fetchOptions,
method: 'DELETE'
}
);
Interactive: API Restaurant Simulator
You're a waiter at API Restaurant! Match each customer request to the correct HTTP method
Quick Reference: CRUD Operations
| Operation | HTTP Method | Purpose | Example |
|---|---|---|---|
| Read | GET | Retrieve data | View leaderboard |
| Create | POST | Add new data | Submit first score |
| Update | PUT | Modify existing | Beat high score |
| Delete | DELETE | Remove data | Clear account |