MoraGaming SDK
Add leaderboards to your HTML5 game with one script tag and one line of code. Scores appear next to your game on MoraGaming, ranked today, this week and all time.
1. Add the SDK
Load it before your game's code. It's about 6 KB (2 KB compressed) and has no dependencies.
<script src="https://moragaming.com/sdk/v1.js"></script>
Then connect once when your game loads:
MoraGaming.init().then(function (info) {
// info.user → { username } or null for guests
// info.lang → "en", "es", "pt", "fr", "de" or "tr"
// info.standalone → true when the game isn't running on MoraGaming
});
2. Submit scores
Call this whenever a round ends. Only the player's best score counts, so you can send every result.
MoraGaming.submitScore(score);
If your game has more than one leaderboard, pass its key:
MoraGaming.submitScore(42, "fewest-moves");
Guests are asked to log in by MoraGaming, and their best score from the session is saved as soon as they do. Your game keeps running while they log in.
Your game never breaks without us. Outside MoraGaming (while you develop locally, or on another site) every SDK call still resolves. submitScore answers { ok: false, reason: "standalone" } and leaderboards come back empty.
Reference
MoraGaming.init()
Connects to the page around your game. Safe to call more than once. Resolves with { standalone, user, lang, game, leaderboards }, where leaderboards lists each board's key, name, order ("desc" means higher is better) and format.
MoraGaming.submitScore(score, boardKey?)
Resolves with one of:
{ ok: true, isNewBest, score, all, week, day } | Saved. all, week and day each hold { best, rank }. |
{ ok: false, reason: "login_required" } | The player is a guest. MoraGaming has shown them a log-in prompt. |
{ ok: false, reason: "rejected", message } | The score was refused, for example because it's outside the board's allowed range. |
{ ok: false, reason: "standalone" } | Not running on MoraGaming. |
MoraGaming.getLeaderboard({ board?, period?, limit? })
period is "day", "week" or "all" (default). limit is 1–100 (default 10). Resolves with { board, period, entries, me, total }. Each entry is { rank, username, score, isMe }; me is the player's own entry, even when they're outside the top list. Days and weeks are in UTC, and weeks start on Monday.
MoraGaming.getLeaderboard({ period: "week", limit: 5 }).then(function (lb) {
lb.entries.forEach(function (e) { console.log(e.rank, e.username, e.score); });
});
MoraGaming.showLeaderboard(boardKey?)
Scrolls to the leaderboard shown next to your game, leaving full screen first if needed. Handy for a "Leaderboard" button on your game-over screen.
MoraGaming.promptLogin()
Opens the MoraGaming log-in window, for example from a "Save my score" button.
MoraGaming.getUser(), getLanguage(), isStandalone()
Return the current values straight away, without waiting. Call init() first.
MoraGaming.on("userChanged", callback)
Runs when the player logs in or out while your game is open. The callback receives { username } or null.
Scores and fairness
- Each leaderboard can have a lowest and highest allowed score. Anything outside is rejected.
- Scores are only accepted from a player who started your game on its MoraGaming page, within the last 12 hours.
- Players can send at most 30 scores a minute.
- MoraGaming staff can remove suspicious scores.
Like on any web game portal, a determined player can still send a fake score, because the game runs in their browser. Keep realistic limits on each board, and tell us if you see something odd.
Languages
Show your game in the player's language with MoraGaming.getLanguage(). Games hosted on MoraGaming also receive it as ?lang= in their address.