Computer Science & Software Engineering
Game API
- Building Leaderboards with CRUD
- Two Types of Leaderboards
- Setup Steps
- HTTP Methods (CRUD Operations)
- How to Add Leaderboard to Your Game
- Architecture Flow
- Integration Steps
- Turn Score & Leaderboard On By Default
- Option 1: Auto-Initialize Score Counter (Recommended)
- Option 2: Show Leaderboard on Load
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
// Import javaURI and fetchOptions from config.js
import { javaURI, fetchOptions } from '/assets/js/api/config.js';
// GET request - fetchOptions includes authentication automatically
const res = await fetch(
`${javaURI}/api/events/SCORE_COUNTER`,
fetchOptions
);
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
// Import javaURI and fetchOptions from config.js
import { javaURI, fetchOptions } from '/assets/js/api/config.js';
const endpoint = '/api/events/ELEMENTARY_LEADERBOARD';
const url = `${javaURI}${endpoint}`;
// Create payload matching Java backend AlgorithmicEvent structure
const requestBody = {
payload: {
user: name,
score: score,
gameName: this.gameName
}
};
// POST - only specify method and body, fetchOptions handles headers
const res = await fetch(
url,
{
...fetchOptions,
method: 'POST',
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
// Import javaURI and fetchOptions from config.js
import { javaURI, fetchOptions } from '/assets/js/api/config.js';
const url = `${javaURI}/api/events/ELEMENTARY_LEADERBOARD/${id}`;
// DELETE - only specify method, fetchOptions handles authentication
const res = await fetch(
url,
{
...fetchOptions,
method: 'DELETE'
}
);
How to Add Leaderboard to Your Game
Architecture Flow
1. Game Objects (Coin.js, NPC.js) → write to GameEnv.stats
2. GameEnv.stats → read by GameEnvScore (Score Manager)
3. GameEnvScore → displays in Score Counter UI
4. GameEnvScore → saves via POST to Backend API → stores in Database
5. Leaderboard.js → fetches via GET from Backend API → displays in Leaderboard UI
Integration Steps
// In GameEnv constructor
this.stats = { coinsCollected: 0, levelsCompleted: 0 };
this.scoreConfig = {
counterVar: 'coinsCollected',
counterLabel: 'Coins Collected',
scoreVar: 'coinsCollected'
};
this.scoreManager = null;
// In Coin.js collect() method
this.gameEnv.stats.coinsCollected++;
// In Game.js or your main game file
import Leaderboard from '../Leaderboard.js';
// Create leaderboard instance (inside your game setup)
this.leaderboardInstance = new Leaderboard(this.gameControl, {
gameName: 'AdventureGame',
parentId: 'gameContainer',
initiallyHidden: false // Set to true to hide on load
});
Press ESC during gameplay to open the pause menu. This gives you access to:
- Toggle Score - Show/hide the score counter
- Save Score - Submit your score to the backend
- Toggle Leaderboard - Show/hide the leaderboard
Turn Score & Leaderboard On By Default
Option 1: Auto-Initialize Score Counter (Recommended)
Add this to your Game.js constructor or initialization method:
// In Game.js constructor, after gameControl is created
if (this.gameControl?.gameEnv) {
this.gameControl.gameEnv.initScoreManager().then(() => {
// Auto-show score counter on game start
this.gameControl.gameEnv.scoreManager.toggleScoreDisplay();
});
}
Option 2: Show Leaderboard on Load
Set initiallyHidden: false when creating the leaderboard:
// In your game initialization
this.leaderboardInstance = new Leaderboard(this.gameControl, {
gameName: 'AdventureGame',
parentId: 'gameContainer',
initiallyHidden: false // ← Change to false to show on load
});
// Optional: Force positioning for consistent display
setTimeout(() => {
const container = document.getElementById('leaderboard-container');
if (container) {
container.style.position = 'fixed';
container.style.top = '80px';
container.style.right = '20px';
container.style.zIndex = '1000';
}
}, 100);