Add POST /me/join and a join card: accounts survive a world reset
Found while resetting the live test world: joinWorld's comment promised POST /worlds/:id/join but no route existed, so after reset-world (accounts kept) nobody could found a POP again. Added POST /me/join (default: first running world; 409 if already joined), a join card on the POP screen, en/nb strings, API.md + openapi. Client no longer polls /movements while it has no operator (that 404ed). Tested: npm test 209/209 (new test/me-join.test.js incl. response contract), openapi check, npm run e2e (new check: account without a POP sees the join card and founds one). NOT tested: joining a second/other world (only the default-world path in the browser), nb text of the card visually. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
parent
bf7af0679c
commit
9104480463
9 changed files with 170 additions and 6 deletions
|
|
@ -25,6 +25,7 @@ REST JSON under `/api/v1/`. The machine-readable contract is `docs/openapi.json`
|
||||||
| `POST /auth/login` | token for another device (`invalid_credentials`, `forbidden` when banned) |
|
| `POST /auth/login` | token for another device (`invalid_credentials`, `forbidden` when banned) |
|
||||||
| `GET /auth/tokens`, `DELETE /auth/tokens/:tokenId`, `POST /auth/logout` | list this account's active device tokens, revoke one, revoke the current one |
|
| `GET /auth/tokens`, `DELETE /auth/tokens/:tokenId`, `POST /auth/logout` | list this account's active device tokens, revoke one, revoke the current one |
|
||||||
| `GET /me` | user, operators (per world), POPs |
|
| `GET /me` | user, operators (per world), POPs |
|
||||||
|
| `POST /me/join {worldId?}` | an account with no operator (world reset, new world) founds its capital POP (201, returns `/me`); `409 conflict` if already joined |
|
||||||
| `GET /towns/:id` | full POP state: resources `{amount, rate, cap}` at `serverTime`, power, buildings with the server's build plan, queue |
|
| `GET /towns/:id` | full POP state: resources `{amount, rate, cap}` at `serverTime`, power, buildings with the server's build plan, queue |
|
||||||
| `POST /towns/:id/build {key}` | build/upgrade one level of a field (`f1..f18`) or an infrastructure building |
|
| `POST /towns/:id/build {key}` | build/upgrade one level of a field (`f1..f18`) or an infrastructure building |
|
||||||
| `DELETE /towns/:id/queue/:eventId` | cancel a running build, 80% refund |
|
| `DELETE /towns/:id/queue/:eventId` | cancel a running build, 80% refund |
|
||||||
|
|
|
||||||
|
|
@ -3533,6 +3533,99 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/me/join": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Join a world with this account: creates the operator and its capital POP (default: the first running world)",
|
||||||
|
"tags": [
|
||||||
|
"me"
|
||||||
|
],
|
||||||
|
"operationId": "postMeJoin",
|
||||||
|
"parameters": [],
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"worldId": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"bearer": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "OK",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Me"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"401": {
|
||||||
|
"description": "Error codes: unauthorized",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Error codes: not_found",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"409": {
|
||||||
|
"description": "Error codes: conflict, world_full",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Error codes: idempotency_key_reuse, validation_failed",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Error codes: internal_error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/messages": {
|
"/messages": {
|
||||||
"post": {
|
"post": {
|
||||||
"summary": "Send a message to an operator (toOperatorId or toName) or to your alliance (toAlliance). Plain text, max 2000 characters, rate-limited",
|
"summary": "Send a message to an operator (toOperatorId or toName) or to your alliance (toAlliance). Plain text, max 2000 characters, rate-limited",
|
||||||
|
|
|
||||||
16
e2e/run.js
16
e2e/run.js
|
|
@ -155,6 +155,22 @@ async function suiteDesktop(browser, S) {
|
||||||
P.base = S.base;
|
P.base = S.base;
|
||||||
await coreFlow(P, 'desktop', false, true);
|
await coreFlow(P, 'desktop', false, true);
|
||||||
await P.ctx.close();
|
await P.ctx.close();
|
||||||
|
|
||||||
|
// An account that lost its operator (world reset) sees a join card and gets a POP with one click.
|
||||||
|
const u = await S.signup('rejoin' + Date.now() % 1e5);
|
||||||
|
S.db.run('DELETE FROM buildings WHERE town_id = ?', u.townId);
|
||||||
|
S.db.run('DELETE FROM towns WHERE owner_id = ?', u.operatorId);
|
||||||
|
S.db.run('DELETE FROM operators WHERE id = ?', u.operatorId);
|
||||||
|
const J = await L.newPage(browser, PROFILES.desktop);
|
||||||
|
await J.page.addInitScript((tok) => { try { localStorage.setItem('upstream.token', tok); } catch (e) { /* ignore */ } }, u.token);
|
||||||
|
await J.page.goto(S.base + '/#/pop');
|
||||||
|
await check('desktop: account without a POP sees the join card and founds one', async () => {
|
||||||
|
await J.page.waitForSelector('[data-screen="pop"] .btn.primary', { timeout: 15000 });
|
||||||
|
await J.page.click('[data-screen="pop"] .btn.primary');
|
||||||
|
await J.page.waitForSelector('.town-name', { timeout: 15000 });
|
||||||
|
return noErrors(J);
|
||||||
|
});
|
||||||
|
await J.ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function suitePhone(browser, S) {
|
async function suitePhone(browser, S) {
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@
|
||||||
'pop.level': 'Lv {n}', 'pop.notBuilt': 'Not built', 'pop.max': 'Max', 'pop.inQueue': 'Building',
|
'pop.level': 'Lv {n}', 'pop.notBuilt': 'Not built', 'pop.max': 'Max', 'pop.inQueue': 'Building',
|
||||||
'pop.upgradeReady': 'Can upgrade', 'pop.locked': 'Requirements missing',
|
'pop.upgradeReady': 'Can upgrade', 'pop.locked': 'Requirements missing',
|
||||||
'pop.load.error': 'Could not load this POP.',
|
'pop.load.error': 'Could not load this POP.',
|
||||||
'pop.noTown': 'No POP on this account yet.',
|
'pop.noTown': 'No POP on this account yet.', 'pop.join': 'Found my POP', 'pop.join.hint': 'The world was reset or is new. Join it to get a capital POP and start building.',
|
||||||
'pop.bonus': 'Rural bonus', 'pop.bonus.none': 'No rural areas in reach',
|
'pop.bonus': 'Rural bonus', 'pop.bonus.none': 'No rural areas in reach',
|
||||||
|
|
||||||
'power.title': 'Power', 'power.out': 'Output', 'power.draw': 'Draw', 'power.net': 'Net', 'power.buffer': 'Buffer',
|
'power.title': 'Power', 'power.out': 'Output', 'power.draw': 'Draw', 'power.net': 'Net', 'power.buffer': 'Buffer',
|
||||||
|
|
@ -212,7 +212,7 @@
|
||||||
'pop.level': 'Nv {n}', 'pop.notBuilt': 'Ikke bygget', 'pop.max': 'Maks', 'pop.inQueue': 'Under bygging',
|
'pop.level': 'Nv {n}', 'pop.notBuilt': 'Ikke bygget', 'pop.max': 'Maks', 'pop.inQueue': 'Under bygging',
|
||||||
'pop.upgradeReady': 'Kan oppgraderes', 'pop.locked': 'Mangler krav',
|
'pop.upgradeReady': 'Kan oppgraderes', 'pop.locked': 'Mangler krav',
|
||||||
'pop.load.error': 'Kunne ikke laste denne POP-en.',
|
'pop.load.error': 'Kunne ikke laste denne POP-en.',
|
||||||
'pop.noTown': 'Ingen POP på denne kontoen ennå.',
|
'pop.noTown': 'Ingen POP på denne kontoen ennå.', 'pop.join': 'Etabler min POP', 'pop.join.hint': 'Verdenen er nullstilt eller ny. Bli med for å få en hovedstads-POP og begynne å bygge.',
|
||||||
'pop.bonus': 'Landsbygdbonus', 'pop.bonus.none': 'Ingen landsbygd innen rekkevidde',
|
'pop.bonus': 'Landsbygdbonus', 'pop.bonus.none': 'Ingen landsbygd innen rekkevidde',
|
||||||
|
|
||||||
'power.title': 'Strøm', 'power.out': 'Produksjon', 'power.draw': 'Forbruk', 'power.net': 'Netto', 'power.buffer': 'Buffer',
|
'power.title': 'Strøm', 'power.out': 'Produksjon', 'power.draw': 'Forbruk', 'power.net': 'Netto', 'power.buffer': 'Buffer',
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@
|
||||||
// ------------------------------------------------------------------ refetchers
|
// ------------------------------------------------------------------ refetchers
|
||||||
let movesFlight = null;
|
let movesFlight = null;
|
||||||
App.refreshMovements = () => {
|
App.refreshMovements = () => {
|
||||||
if (!App.state.user) return Promise.resolve(null);
|
if (!App.state.user || !App.state.operator) return Promise.resolve(null);
|
||||||
if (movesFlight) return movesFlight;
|
if (movesFlight) return movesFlight;
|
||||||
lastMovesFetch = Date.now();
|
lastMovesFetch = Date.now();
|
||||||
movesFlight = api.get('/movements').then((r) => {
|
movesFlight = api.get('/movements').then((r) => {
|
||||||
|
|
|
||||||
|
|
@ -366,12 +366,26 @@
|
||||||
return h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('pop.infra.title') })), h('div', { class: 'rows' }, list.map(infraRow)));
|
return h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', { text: App.t('pop.infra.title') })), h('div', { class: 'rows' }, list.map(infraRow)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An account with no operator (the world was reset or a new one opened): one tap founds the capital POP.
|
||||||
|
function joinCard() {
|
||||||
|
const btn = h('button', { class: 'btn primary', type: 'button', 'data-keep': '' }, App.t('pop.join'));
|
||||||
|
btn.addEventListener('click', async () => {
|
||||||
|
btn.disabled = true;
|
||||||
|
try {
|
||||||
|
const me = await App.api.post('/me/join', {});
|
||||||
|
App.setSession(me.user, me.operators);
|
||||||
|
await App.enter('pop');
|
||||||
|
} catch (e) { toast(App.errMsg(e), 'error'); btn.disabled = false; }
|
||||||
|
});
|
||||||
|
return h('div', { class: 'card pad' }, h('h2', { text: App.t('pop.noTown') }), h('p', { text: App.t('pop.join.hint') }), btn);
|
||||||
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
dirty = false;
|
dirty = false;
|
||||||
refs = { tiles: new Map(), rows: new Map(), queue: [] };
|
refs = { tiles: new Map(), rows: new Map(), queue: [] };
|
||||||
clear(box);
|
clear(box);
|
||||||
const t = td();
|
const t = td();
|
||||||
if (!App.state.townId) { box.appendChild(h('p', { class: 'empty pad', text: App.t('pop.noTown') })); return; }
|
if (!App.state.townId) { box.appendChild(joinCard()); return; }
|
||||||
if (!t) { box.appendChild(h('div', { class: 'boot' }, h('div', { class: 'spinner', 'aria-hidden': 'true' }), h('p', { text: App.t('common.loading') }))); return; }
|
if (!t) { box.appendChild(h('div', { class: 'boot' }, h('div', { class: 'spinner', 'aria-hidden': 'true' }), h('p', { text: App.t('common.loading') }))); return; }
|
||||||
// other screens add cards here through App.popSections (military: incoming banner on top, expansion below the power card)
|
// other screens add cards here through App.popSections (military: incoming banner on top, expansion below the power card)
|
||||||
const extra = (slot) => (App.popSections || []).filter((s) => s.slot === slot).map((s) => { try { return s.render(t); } catch (e) { console.error(e); return null; } });
|
const extra = (slot) => (App.popSections || []).filter((s) => s.slot === slot).map((s) => { try { return s.render(t); } catch (e) { console.error(e); return null; } });
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
/* Create an operator for a user in a world, with a capital POP placed by the spawn ring. Not a player-facing route
|
/* Create an operator for a user in a world, with a capital POP placed by the spawn ring. Not a player-facing route
|
||||||
* on its own: signup and `POST /worlds/:id/join` call it. */
|
* on its own: signup and `POST /me/join` call it. */
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const R = require('../../public/js/rules.js');
|
const R = require('../../public/js/rules.js');
|
||||||
const D = require('../../public/js/data.js');
|
const D = require('../../public/js/data.js');
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,23 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
function register(router, { queries }) {
|
const { GameError } = require('../errors');
|
||||||
|
const { reply } = require('../http');
|
||||||
|
function register(router, { queries, commands, db }) {
|
||||||
router.get('/me', { summary: 'The current user, their operators and POPs', tag: 'me', response: { $ref: 'Me' }, errors: ['unauthorized'] },
|
router.get('/me', { summary: 'The current user, their operators and POPs', tag: 'me', response: { $ref: 'Me' }, errors: ['unauthorized'] },
|
||||||
({ user }) => queries.me(user.id));
|
({ user }) => queries.me(user.id));
|
||||||
|
|
||||||
|
// An account without an operator (the world was reset, or a new one opened) founds its POP here. Signup does the same inline.
|
||||||
|
router.post('/me/join', {
|
||||||
|
summary: 'Join a world with this account: creates the operator and its capital POP (default: the first running world)', tag: 'me', status: 201,
|
||||||
|
body: { type: 'object', properties: { worldId: { type: 'integer', minimum: 1 } } },
|
||||||
|
response: { $ref: 'Me' }, errors: ['unauthorized', 'validation_failed', 'not_found', 'conflict', 'world_full'],
|
||||||
|
}, ({ user, body }) => {
|
||||||
|
const w = body.worldId !== undefined ? db.get("SELECT id FROM worlds WHERE id = ? AND status = 'running'", body.worldId)
|
||||||
|
: db.get("SELECT id FROM worlds WHERE status = 'running' ORDER BY id LIMIT 1");
|
||||||
|
if (!w) throw new GameError('not_found', { what: 'world' });
|
||||||
|
commands.run('joinWorld', { userId: user.id, worldId: w.id });
|
||||||
|
return reply(201, queries.me(user.id));
|
||||||
|
});
|
||||||
|
|
||||||
router.get('/notifications', {
|
router.get('/notifications', {
|
||||||
summary: 'Notifications for this user (cursor: after = last seen id)', tag: 'me',
|
summary: 'Notifications for this user (cursor: after = last seen id)', tag: 'me',
|
||||||
query: { type: 'object', properties: { after: { type: 'integer', minimum: 0 }, limit: { type: 'integer', minimum: 1, maximum: 100 } } },
|
query: { type: 'object', properties: { after: { type: 'integer', minimum: 0 }, limit: { type: 'integer', minimum: 1, maximum: 100 } } },
|
||||||
|
|
|
||||||
25
test/me-join.test.js
Normal file
25
test/me-join.test.js
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
'use strict';
|
||||||
|
/* An account with no operator (world reset) joins again over HTTP; a second join conflicts; unknown world is 404. */
|
||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert');
|
||||||
|
const { createTestServer } = require('./helpers');
|
||||||
|
|
||||||
|
test('POST /me/join: founds a POP for an account without an operator', async () => {
|
||||||
|
const t = await createTestServer({ contract: true });
|
||||||
|
try {
|
||||||
|
const a = await t.signup('alice');
|
||||||
|
assert.equal((await t.api('/me/join', { method: 'POST', body: {} }, a.token)).body.error.code, 'conflict', 'already joined');
|
||||||
|
assert.equal((await t.api('/me/join', { method: 'POST', body: {} })).status, 401);
|
||||||
|
t.srv.db.run('DELETE FROM buildings WHERE town_id = ?', a.townId);
|
||||||
|
t.srv.db.run('DELETE FROM towns WHERE owner_id = ?', a.operatorId);
|
||||||
|
t.srv.db.run('DELETE FROM operators WHERE id = ?', a.operatorId);
|
||||||
|
assert.deepEqual((await t.api('/me', {}, a.token)).body.operators, []);
|
||||||
|
assert.equal((await t.api('/me/join', { method: 'POST', body: { worldId: 99 } }, a.token)).status, 404);
|
||||||
|
const r = await t.api('/me/join', { method: 'POST', body: {} }, a.token);
|
||||||
|
assert.equal(r.status, 201);
|
||||||
|
assert.equal(r.body.operators.length, 1);
|
||||||
|
assert.equal(r.body.operators[0].towns[0].isCapital, true);
|
||||||
|
const town = await t.api('/towns/' + r.body.operators[0].towns[0].id, {}, a.token);
|
||||||
|
assert.equal(town.status, 200);
|
||||||
|
} finally { await t.close(); }
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue