← Back to the blog

· 5 min read

An online leaderboard for my web game with Cloudflare Workers and D1

How I added a ranking to Cutastrophe with a Worker of about a hundred lines and a D1 database, which alternatives I looked at (Supabase, Talo) and what the server can't guarantee.

GodotCloudflare WorkersD1Game development

Cutastrophe is a 20-second game I made in Godot for a game jam. It had a local high score, but a number only the player can see falls short: I wanted people to be able to compare themselves. This is the online leaderboard I added, why I built it on Cloudflare and what it still doesn’t solve.

What I needed

Very little, and it’s worth writing it down before choosing a tool:

  • Two boards, one for normal mode and one for hard mode. Zen mode doesn’t submit scores: with no clock there’s no time bonus, so the numbers wouldn’t be comparable.
  • A free-form name, with no accounts or passwords. It’s a 20-second game; nobody is going to sign up to play it.
  • Only each player’s best score, so the ranking doesn’t fill up with one person.

The options I looked at

Before building anything, I looked at what already existed. Two options are worth knowing if you’re in the same situation:

  • Supabase. A platform built on Postgres. Its quickstart uses exactly a leaderboard table: you create it and Supabase exposes it on its own as a REST API, with row-level access rules to decide who can read and write. I’d pick it if I already wanted Postgres, user accounts or a dashboard to look at the data.
  • Talo. An open-source backend designed specifically for games, with a Godot plugin and a Unity one. It includes leaderboards, cloud saves, stats and player authentication, and you can host it yourself. I’d pick it if I wanted several of those services already integrated and not just a ranking.

Neither struck me as a bad choice; both solve the problem. They just brought more than I needed.

Why I ended up on Cloudflare

Because I already had it. This site and its chat already live on Cloudflare, so one more piece doesn’t add an account or a new service. On top of that, D1 (Cloudflare’s SQLite database) is available on the Workers free plan, with 5 million rows read and 100,000 written per day (as of today, November 22, 2026, according to their documentation; Cloudflare may change these limits). For the ranking of a small game, that’s more than enough.

The cost is that I write the server logic myself. But the logic is short.

How it turned out

Three pieces: the game in Godot, a Worker that exposes two routes and a table in D1.

Godot (HTTPRequest) ──►  Worker /scores  ──►  D1 (scores table)

The table keeps one record per player and mode:

CREATE TABLE IF NOT EXISTS scores (
  player_id  TEXT    NOT NULL,
  mode       TEXT    NOT NULL CHECK (mode IN ('normal', 'hard')),
  name       TEXT    NOT NULL,
  score      INTEGER NOT NULL,
  updated_at INTEGER NOT NULL,
  PRIMARY KEY (player_id, mode)
);

That primary key does most of the work. When a score arrives, the Worker does an upsert: if the player already has a record, it keeps the higher of the two.

await env.DB.prepare(
  `INSERT INTO scores (player_id, mode, name, score, updated_at) VALUES (?, ?, ?, ?, ?)
   ON CONFLICT (player_id, mode) DO UPDATE SET
     name = excluded.name,
     score = MAX(scores.score, excluded.score),
     updated_at = excluded.updated_at`
).bind(playerId, mode, name, score, now).run();

The player’s rank comes from counting how many scores are higher, plus one. And the top list is a query ordered by score.

One id per player, no accounts

With no accounts, how does the server know two submissions come from the same person? The game generates a random id the first time it starts and saves it, along with the name, in a local file:

func _ready() -> void:
	var cfg := ConfigFile.new()
	cfg.load(PATH)
	player_id = cfg.get_value("player", "id", "")
	player_name = cfg.get_value("player", "name", "")
	if player_id.is_empty():
		var crypto := Crypto.new()
		player_id = crypto.generate_random_bytes(16).hex_encode()
		_save()

The CORS detail

The web game runs on itch.io (and embedded in this site), so its requests go to a different origin than the Worker. The browser blocks them unless the Worker answers with CORS headers and handles the preflight request (OPTIONS). It’s one of those things that never shows up when you test locally and breaks everything once you publish.

The local high score

The local record is much simpler: a ConfigFile at user://records.cfg, with one key per mode. In a web export, Godot stores that file in the browser’s storage, so it survives between sessions without me doing anything.

func save_record(score: int) -> bool:
	if autoplay or score <= best_score():
		return false
	var cfg := ConfigFile.new()
	cfg.load(RECORD_PATH)
	cfg.set_value("records", _record_key(), score)
	cfg.save(RECORD_PATH)
	return true

What the server can’t guarantee

This is the most important part of the article: this leaderboard is not cheat-proof, and it’s worth knowing why.

  • The score is computed by the game on the player’s machine and sent to the server. The server has no way to verify it.
  • The player_id is a random number, not an identity. Anyone can make up a new one.
  • The score cap only stops absurd values: anyone who can see the game’s requests can replay them with other values within that range.
  • The limit of one submission every 10 seconds per player is dodged with another id.

For a jam game where the only thing at stake is glory, I think that’s a reasonable deal. If there were prizes, I’d have to move validation to the server (for example, have it receive the whole run and recompute the score) or require real authentication. What the Worker does do is the basics: validate types and ranges, trim and clean the name, and trust nothing that comes from the client.

A note on how I built it

I had no experience with Workers or D1. I used Claude to understand how a leaderboard works at its core (what the server should store and what it shouldn’t) and to explore ideas for integrating it with Cloudflare. I wrote the final code myself.

What I take away

  • Write the requirements before choosing a tool. With the list above, ruling out bigger services was easy.
  • A good primary key saves you code. With (player_id, mode) and a MAX() in the upsert, there’s no need to query before writing.
  • Decide what you won’t protect, and say so. A ranking without anti-cheat is fine if you know it; what isn’t fine is pretending it has it.

If you want to do the same in your own game, I wrote a step-by-step tutorial, with an example repo and a section on how to harden the ranking.