Developers
The timer app
All developer pages · From docs/timer.md in the release that is running.
The club timer started as a phone web app (index.html, config.js, routes.js) in front of a Google Apps Script web app that read and wrote the club's Google Sheet. This page starts from that version and goes through what needs to change to make it use the pub-runners.com API instead.
The result lives in public/timer and is served from https://pub-runners.com/timer/, so each change below has a working example there.
What goes away
- The Apps Script web app and its
script.google.com/macros/s/.../execURL inconfig.js. The API on the same site replaces it. - The Google Sheet as the record: names, the "New runners" tab, results written back, and the CSV copy in Drive. pub-runners.com is the record now.
routes.jsand the copied GPX files. Each run carries its own route.- The shared club code, the device ID and the timer role. Sign-ins and a join code replace them.
What needs to change
1. Where it is served
- Before: static files hosted anywhere, calling the script URL.
- Change: the files go in
public/timerin the club's repo and are served from/timer/. Deleteconfig.js, keep<base href="/timer/">, and call the API with relative paths on the same site. - The site's security policy applies: scripts only from
.jsfiles inpublic/timer, no inline<script>oronclick="...", nothing from CDNs or other sites, andfetchonly to the same site. Map tiles from OpenStreetMap are allowed.
2. Who is who
- Before: one club code typed once and sent on every request, a device ID, and a timer role claimed by the first phone. A person was the slug of their typed name.
- Change for timers: timers are hosts signed in on pub-runners.com (
/timer/sign-in), using the site's cookie session and CSRF token. Dropclaimtimerandreleasetimer: callGET /api/v1/timing/runsto find out who you are (200 is a host, 401 needs signing in, 403 is not allowed). Any host can time, several at once, so a dead phone no longer strands the run. - Change for participants: a phone joins tonight's run once with the club's join code (
1901unless a host changes it) throughPOST /api/v1/participant/join, and gets a token. Send the token asAuthorization: Bearer pt_...instead of the code on every request. The join code only ever opens the run dated today. - Change for people: identify runners by
runner_idfrom the API, not by name slug. Someone new is anew_runnerwith akeymade on the phone, and becomes a runner when the results are built.
3. How requests are made
- Before: every action was a
GETto the script URL withaction,code,devandidin the query string, including a new runner's email. - Change: use
GETonly to read. Timer actions become events sent withPOST /api/v1/timing/runs/{id}/events. Participant actions have their own endpoints. Bodies are JSON, so nothing personal goes in a URL. - Errors: they come back as JSON with a
messageand the HTTP status, instead of codes likebad_codeornot_timer. Show themessage.
4. Taps must survive bad signal
- Before: a finish showed on screen at once. If its request failed, the next poll wiped it, and the time was lost.
- Change: make every tap an event with a UUID and a time made on the phone. Put it in a queue in browser storage and resend until the server accepts it. Resending is safe, because the server ignores a UUID it already has. Update the screen straight away from the queue.
5. Polling and the clock
- Before: every phone fetched the whole run with
action=stateevery 4 seconds, and estimated its clock offset from the script'snow. - Change: timers poll
GET /timing/runs/{id}/statewithIf-None-Match, so an unchanged run costs a 304. Participants pollGET /participant/state, which holds only their own check-in and counts. Both responses carryserver_timefor the clock offset. A participant's late start uses the server's clock (POST /participant/start), so it no longer mixes two phones' clocks.
6. The name list
- Before:
namessent the full list to every phone with the code. - Change: timers get
GET /timing/runs/{id}/roster: everyone, people seen recently first, with a predicted time. Participants search withGET /participant/roster?q=(three letters or more, ten matches at most), so they can find themselves but never download the list.
7. Routes and venues
- Before: the timer picked a route and venue (
setroute) fromroutes.js. - Change: a host opens tonight's run with
POST /timing/runs/tonight. The run comes with its season, location,route_gpx_urlandroute_image_url. Turn the GPX URL into a path before fetching it.
8. Results
- Before: "Confirm and write to the sheet" (
finalize) wrote the results, and downloaded and saved a CSV. - Change: "Build the results" calls
POST /timing/runs/{id}/finalise. It builds draft results on the site, which a host checks and publishes in the admin. There is no CSV and no sheet. Building closes the run to new events, so later fixes happen in the admin.
Each Apps Script action, and what it becomes
Event names are type values sent to POST /timing/runs/{id}/events. Other paths are under /api/v1.
| Apps Script action | What it becomes |
|---|---|
state |
Timer: GET /timing/runs/{id}/state. Participant: GET /participant/state |
names |
Timer: GET /timing/runs/{id}/roster. Participant: GET /participant/roster?q= |
checkin |
Participant: POST /participant/join, then POST /participant/checkin. Timer: checkin event with runner_id |
register |
checkin with new_runner (key, first_name, last_name, optional email) in the body |
claimtimer, releasetimer |
Nothing: sign in as a host |
setroute |
Nothing: POST /timing/runs/tonight opens the run with its route |
start |
gun event |
startme |
Participant: POST /participant/start. Timer: start event for that runner |
finish |
finish event. Pairs and groups are one finish with up to 10 runners |
finishanon |
finish with no runners, then assign to name it |
back |
back event |
undo |
void of the event in question |
fixtime |
self_reported with the seconds |
swap |
void both finishes, then two finish events at the same moments, swapped |
setkind |
attendance (walked, pubbed, timed), or void it to make them a runner again |
rename |
Not available yet: assign only names an unchecked runner. Fix names in the admin |
leave |
Participant: DELETE /participant/checkin. Timer: void of their check-in |
endnow |
end event |
resume |
void of the end event |
finalize |
POST /timing/runs/{id}/finalise (force for "ran, no time") |
discard |
Nothing: void the events you do not want. An unfinalised run publishes nothing |
The join code has two new timer actions of its own: GET /timing/join-code shows it, and PATCH /timing/join-code changes it.
Still to do
- Renaming someone needs a server change first (see
renameabove). - Offline use: neither version has a service worker, so the app needs signal to open. Taps made while it is open are kept and resent.
- PB alerts for the timer are held in memory, so a reload before "Got it" loses them.
Developing
See Contributing (CONTRIBUTING.md): change the files in public/timer, open a pull request, and try it on the pull request's preview site. The full API is in the Partner API guide and the API reference.