feat(ludic.telemetry): an event queue batched to a PostHog-shaped endpoint

Events are encoded once into lines held in memory; a batch is the first
lines joined, with the player id put in where a mark stood, POSTed on
Http's own thread every flush_ms or at flush_at events; a failure keeps
its lines and doubles the wait up to backoff_max; the queue is on disk
every save_ms and read back at the next start; off (the enabled port)
drops the request in flight, empties the queue and deletes the file; and
a run that may not send (can_send) keeps nothing. The player id is the
machine's own id (MachineGuid, IOPlatformUUID, /etc/machine-id) hashed
with the game's salt, else random; it lives in the game's id file beside
whatever else the game keeps there.

Ports: TelemetryWorld (can_send, enabled, now_ms, stamp) and
TelemetryTransport (send, poll, drop; unbound: Http). Config is a record
(host, key, path, lib, queue_file, id_file, id_salt, and the timings).
Facts: TELEMETRY_SENT, _FAILED, _ID. Not a System: it runs from a
launcher's first frame, before any world exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 05:24:55 +03:00
parent cac8c740fa
commit e8c1d54a96
10 changed files with 711 additions and 0 deletions

View file

@ -0,0 +1,51 @@
# ports.ludic - the config, and what the ports answer when the game leaves them unbound
export function telemetry_config(c: TelemetryConfig) -> void { telemetry__cfg = c }
function telemetry__conf() -> TelemetryConfig {
if telemetry__cfg == null { telemetry__cfg = new TelemetryConfig }
return telemetry__cfg
}
function telemetry__yes() -> bool { return true }
function telemetry__wall_ms() -> int { return (Time.now() - telemetry__t0) * 1000 }
function telemetry__wall_stamp() -> string { return DateTime.format(Time.now(), "YYYY-MM-DDTHH:mm:ssZ") }
function telemetry__can_send() -> bool {
let c = telemetry__conf()
if len(c.host) == 0 or len(c.key) == 0 { return false }
return TelemetryWorld.can_send()
}
function telemetry__enabled() -> bool { return TelemetryWorld.enabled() }
function telemetry__now() -> int { return TelemetryWorld.now_ms() }
function telemetry__stamp() -> string { return TelemetryWorld.stamp() }
function telemetry__http_send(url: string, body: string) -> int {
let h = Http.open("POST", url)
if h < 0 { return h }
Http.set(h, "Content-Type", "application/json")
Http.body(h, body)
Http.send(h)
return h
}
function telemetry__http_poll(h: int) -> int {
let st = Http.poll(h)
if st < 0 { return -1 }
var ok = 0
if st > 0 and Http.ok(h) { ok = 1 }
Http.free(h)
return ok
}
function telemetry__http_drop(h: int) -> void { Http.free(h) }
function telemetry__send(body: string) -> int { return TelemetryTransport.send(telemetry__conf().host + telemetry__conf().path, body) }
function telemetry__poll(h: int) -> int { return TelemetryTransport.poll(h) }
# a request abandoned (the player said no)
function telemetry__drop_request() -> void {
if telemetry__h < 0 { return }
TelemetryTransport.drop(telemetry__h)
telemetry__h = -1
}